From 8cf002e6484dc2d26d69b050eedecfd20023a0f4 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Mon, 20 Oct 2014 21:32:52 +0200 Subject: [PATCH] First RPC Version --- src/org/hwo/pulscounter/CounterChannel.java | 2 +- src/org/hwo/pulscounter/IPulsCounter.java | 24 ++ .../hwo/pulscounter/NewPulsCounterDevice.java | 230 ++++++++++++++ src/org/hwo/pulscounter/PulsCounter.java | 66 +++- .../hwo/pulscounter/PulsCounterWindow.java | 284 ++++++++++-------- .../pulscounter/ui/NetworkSelectorDialog.java | 119 ++++++++ 6 files changed, 602 insertions(+), 123 deletions(-) create mode 100644 src/org/hwo/pulscounter/IPulsCounter.java create mode 100644 src/org/hwo/pulscounter/NewPulsCounterDevice.java create mode 100644 src/org/hwo/pulscounter/ui/NetworkSelectorDialog.java diff --git a/src/org/hwo/pulscounter/CounterChannel.java b/src/org/hwo/pulscounter/CounterChannel.java index 55eec98..d75d5fc 100644 --- a/src/org/hwo/pulscounter/CounterChannel.java +++ b/src/org/hwo/pulscounter/CounterChannel.java @@ -3,7 +3,7 @@ package org.hwo.pulscounter; import org.hwo.models.TableMapper.TableColumn; public class CounterChannel { - + @TableColumn(label="Kanal",firstColumn=true) private Integer channel; diff --git a/src/org/hwo/pulscounter/IPulsCounter.java b/src/org/hwo/pulscounter/IPulsCounter.java new file mode 100644 index 0000000..82748d0 --- /dev/null +++ b/src/org/hwo/pulscounter/IPulsCounter.java @@ -0,0 +1,24 @@ +package org.hwo.pulscounter; + +public interface IPulsCounter { + + public String[] getDeviceName(); + + public int getChannels(); + + public String getChannelName(Integer channel); + public void setChannelName(Integer channel,String name); + + public int[] getChannelCounters(); + public void setChannelCounter(Integer channel,Integer count); + + public int[] getChannelOffsets(); + public void setChannelOffset(Integer channel,Integer offset); + + public String getPhysicalInterfaceName(); + public void setPhysicalInterfaceName(String interfaceName); + + public String[] getPhysicalInterfaceNames(); + + +} diff --git a/src/org/hwo/pulscounter/NewPulsCounterDevice.java b/src/org/hwo/pulscounter/NewPulsCounterDevice.java new file mode 100644 index 0000000..bf95897 --- /dev/null +++ b/src/org/hwo/pulscounter/NewPulsCounterDevice.java @@ -0,0 +1,230 @@ +package org.hwo.pulscounter; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; +import java.util.Random; + +import org.hwo.io.SerialPort; +import org.hwo.io.servicelink.ServiceLink; +import org.hwo.io.servicelink.ServiceLinkException; +import org.hwo.io.servicelink.ServiceLinkRequestFailedException; + +public class NewPulsCounterDevice implements IPulsCounter { + private ServiceLink serviceLink; + private Properties chprop; + + public NewPulsCounterDevice() + { + this.chprop = new Properties(); + File pf = new File("chnames.prop"); + + try { + chprop.load(new FileInputStream(pf)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + String defaultPort = chprop.getProperty("io.port", "COM1"); + System.err.println("Default Port: " + defaultPort); + + setPhysicalInterfaceName(defaultPort); + } + + synchronized public void reset(int ch) + { + } + + synchronized public void reset() + { + } + + + synchronized public void update() + { + System.err.println(String.format("BRKVAL: 0x%04x", serviceLink.getServiceRegisterCache().getCachedInteger((byte)0, (byte)0, (short)0x200))); + System.err.println(String.format("SPLIM : 0x%04x", serviceLink.getServiceRegisterCache().getCachedInteger((byte)0, (byte)0, (short)0x201))); + } + + private void saveProps(){ + File pf = new File("chnames.prop"); + try { + chprop.store(new FileOutputStream(pf), ""); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void checkOpen(){ + if (!serviceLink.isOpen()) + try { + serviceLink.open(); + } catch (ServiceLinkException e) { + e.printStackTrace(); + } + } + + public String[] getDeviceName() + { + return new String[]{"PulsCounter Board","0.1","alpha"}; + } + + public Date getDeviceTime() + { + return new Date(); + } + + public SerialPort getSerialPort() { + return serviceLink.getSerialPort(); + } + + public void setSerialPort(SerialPort serialPort) throws ServiceLinkException { + + if (this.serviceLink == null) + this.serviceLink = new ServiceLink(serialPort); + + if (this.serviceLink.isOpen()) + this.serviceLink.close(); + + this.serviceLink.setSerialPort(serialPort);; + this.serviceLink.open(); + + try { + this.serviceLink.readInt((byte)0, (byte)0, 0); + } catch (IOException e) { + e.printStackTrace(); + } + + System.err.println("BRKVAL: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0020)); + System.err.println("SPLIM: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0021)); + + } + + @Override + public String getChannelName(Integer channel) { + return chprop.getProperty(String.format("channel.%d", channel),String.format("Kanal %d",channel+1)); + } + + @Override + public void setChannelName(Integer channel, String name) { + + chprop.setProperty(String.format("channel.%d", channel), name); + + saveProps(); + } + + @Override + public int[] getChannelCounters() { + + checkOpen(); + + int nch = getChannels(); + int[] counters = new int[ nch ]; + + for (int i=0;i options = Arrays.asList(args).iterator(); - PulsCounterWindow window = new PulsCounterWindow(); - window.setVisible(true); + while (options.hasNext()) { + String no = options.next(); + + if (no.equals("-s")) { + serverMode = true; + } + + } + + deviceConnection = null; + beacon = new Beacon(44556); + beacon.getProperties().setProperty("rpc.simple.interfaces", "org.hwo.pulsecounter"); + beacon.getProperties().setProperty("rpc.simple.port", "44352"); + beacon.setServerOnly(serverMode); + beacon.start(); + + try { + NewPulsCounterDevice device = new NewPulsCounterDevice(); + + SimpleRPCServer rpcserver = new SimpleRPCServer(InetAddress.getByName("0.0.0.0"), 44352); + rpcserver.registerObject(IPulsCounter.class, device); + rpcserver.start(); + + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + if (!serverMode) { + PulsCounterWindow window = new PulsCounterWindow(); + window.setVisible(true); + } } diff --git a/src/org/hwo/pulscounter/PulsCounterWindow.java b/src/org/hwo/pulscounter/PulsCounterWindow.java index c0e57ac..018cd7e 100644 --- a/src/org/hwo/pulscounter/PulsCounterWindow.java +++ b/src/org/hwo/pulscounter/PulsCounterWindow.java @@ -2,6 +2,7 @@ package org.hwo.pulscounter; import java.awt.EventQueue; +import javax.swing.DesktopManager; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenuItem; @@ -13,16 +14,22 @@ import java.awt.BorderLayout; import javax.swing.JButton; +import java.awt.Desktop; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import javax.swing.JLabel; import javax.swing.JComboBox; +import org.hwo.beacon.Beacon; +import org.hwo.beacon.Beacon.BeaconSender; import org.hwo.csv.CSV; import org.hwo.io.SerialPort; import org.hwo.models.TableMapper.AbstractTableMapperListener; import org.hwo.models.TableMapper.TableMapper; +import org.hwo.pulscounter.ui.NetworkSelectorDialog; +import org.hwo.rpc.simple.SimpleRPCService; +import org.hwo.ui.JObjectSelector; import org.hwo.ui.MousePopupListener; import java.awt.Insets; @@ -58,13 +65,14 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; import org.hwo.datetime.JTimeOfDay; import javax.swing.JTextField; public class PulsCounterWindow extends JFrame{ - private JComboBox cbInterfaces; private JTable tCounter; private TableMapper tmCounter; @@ -91,6 +99,7 @@ public class PulsCounterWindow extends JFrame{ private JTextField tfWorkDir; private String workingDirectory; + private JTextField tfPhysicalInterface; public PulsCounterWindow() { addWindowListener(new WindowAdapter() { @@ -133,42 +142,87 @@ public class PulsCounterWindow extends JFrame{ gbc_panel_1.gridy = 0; panel.add(panel_1, gbc_panel_1); GridBagLayout gbl_panel_1 = new GridBagLayout(); - gbl_panel_1.columnWidths = new int[]{0, 0, 0}; - gbl_panel_1.rowHeights = new int[]{0, 0}; - gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; - gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE}; + gbl_panel_1.columnWidths = new int[]{0, 0, 0, 0}; + gbl_panel_1.rowHeights = new int[]{0, 0, 0}; + gbl_panel_1.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; + gbl_panel_1.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; panel_1.setLayout(gbl_panel_1); + JButton btnNetzwerk = new JButton("Netzwerk..."); + btnNetzwerk.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + BeaconSender sender = NetworkSelectorDialog.show(PulsCounter.getBeacon()); + if (sender != null){ + + int port = Integer.decode(sender.getProperties().getProperty("rpc.simple.port")); + + SimpleRPCService service = new SimpleRPCService(sender.getInetAddress(), port); + IPulsCounter ipc = service.createProxy(IPulsCounter.class); + + counterChannels.clear(); + + changeDeviceConnection(ipc); + } + } + }); + GridBagConstraints gbc_btnNetzwerk = new GridBagConstraints(); + gbc_btnNetzwerk.insets = new Insets(0, 0, 5, 5); + gbc_btnNetzwerk.gridx = 0; + gbc_btnNetzwerk.gridy = 0; + panel_1.add(btnNetzwerk, gbc_btnNetzwerk); + + JButton btnLokal = new JButton("Lokal"); + btnLokal.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + try { + SimpleRPCService service; + service = new SimpleRPCService(InetAddress.getLocalHost(), 44352); + IPulsCounter ipc = service.createProxy(IPulsCounter.class); + + counterChannels.clear(); + + changeDeviceConnection(ipc); + } catch (UnknownHostException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + }); + GridBagConstraints gbc_btnLokal = new GridBagConstraints(); + gbc_btnLokal.insets = new Insets(0, 0, 5, 0); + gbc_btnLokal.gridx = 2; + gbc_btnLokal.gridy = 0; + panel_1.add(btnLokal, gbc_btnLokal); + JLabel lblNewLabel = new JLabel("Schnittstelle:"); GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.anchor = GridBagConstraints.EAST; gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5); gbc_lblNewLabel.gridx = 0; - gbc_lblNewLabel.gridy = 0; + gbc_lblNewLabel.gridy = 1; panel_1.add(lblNewLabel, gbc_lblNewLabel); - cbInterfaces = new JComboBox(); - GridBagConstraints gbc_cbInterfaces = new GridBagConstraints(); - gbc_cbInterfaces.fill = GridBagConstraints.HORIZONTAL; - gbc_cbInterfaces.gridx = 1; - gbc_cbInterfaces.gridy = 0; - panel_1.add(cbInterfaces, gbc_cbInterfaces); - cbInterfaces.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - - System.err.println("Save Pref PORT"); - - prefs.put("io.port", cbInterfaces.getSelectedItem().toString()); - try - { - prefs.sync(); - } catch (Exception e) - { - System.err.println("Exception: " + e); - e.printStackTrace(); - } - setSerialPort(); + tfPhysicalInterface = new JTextField(); + tfPhysicalInterface.setEditable(false); + GridBagConstraints gbc_tfPhysicalInterface = new GridBagConstraints(); + gbc_tfPhysicalInterface.insets = new Insets(0, 0, 0, 5); + gbc_tfPhysicalInterface.anchor = GridBagConstraints.NORTH; + gbc_tfPhysicalInterface.fill = GridBagConstraints.HORIZONTAL; + gbc_tfPhysicalInterface.gridx = 1; + gbc_tfPhysicalInterface.gridy = 1; + panel_1.add(tfPhysicalInterface, gbc_tfPhysicalInterface); + tfPhysicalInterface.setColumns(10); + + JButton btnWhlen = new JButton("w\u00E4hlen..."); + btnWhlen.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + selectPhysicalInterface(); } }); + GridBagConstraints gbc_btnWhlen = new GridBagConstraints(); + gbc_btnWhlen.gridx = 2; + gbc_btnWhlen.gridy = 1; + panel_1.add(btnWhlen, gbc_btnWhlen); JPanel panel_2 = new JPanel(); panel_2.setBorder(new TitledBorder(null, "Einstellungen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); @@ -291,17 +345,11 @@ public class PulsCounterWindow extends JFrame{ gbc_btnNewButton.gridy = 0; panel_3.add(btnNewButton, gbc_btnNewButton); - JButton btnTagesdateiSchreiben = new JButton("Kanalbezeichnungen speichern..."); - GridBagConstraints gbc_btnTagesdateiSchreiben = new GridBagConstraints(); - gbc_btnTagesdateiSchreiben.insets = new Insets(0, 0, 0, 5); - gbc_btnTagesdateiSchreiben.gridx = 2; - gbc_btnTagesdateiSchreiben.gridy = 0; - panel_3.add(btnTagesdateiSchreiben, gbc_btnTagesdateiSchreiben); - JButton btnAlle = new JButton("Alle 0"); btnAlle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - pulsCounterDevice.reset(); + + resetAllChannels(); synchronized (threadUpdater) { threadUpdater.notifyAll(); @@ -312,11 +360,6 @@ public class PulsCounterWindow extends JFrame{ gbc_btnAlle.gridx = 3; gbc_btnAlle.gridy = 0; panel_3.add(btnAlle, gbc_btnAlle); - btnTagesdateiSchreiben.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - saveChannelNames(); - } - }); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { synchronized (threadUpdater) { @@ -503,30 +546,15 @@ public class PulsCounterWindow extends JFrame{ private void initialize() { applicationExiting = false; - pulsCounterDevice = new PulsCounterDevice(); prefs = Preferences.userRoot(); - String defaultPort = prefs.get("io.port", "COM1"); - workingDirectory = prefs.get("file.directory", (new File(".")).getAbsolutePath()); tfWorkDir.setText(workingDirectory); - - for (String portName:SerialPort.getPortNames()) - cbInterfaces.addItem(portName); - // cbInterfaces.addItem("dummy:"); - - System.err.println("Default Port: " + defaultPort); - - for (int i=0;i < cbInterfaces.getItemCount();i++) - { - if (cbInterfaces.getItemAt(i).equals(defaultPort)) - cbInterfaces.setSelectedIndex(i); - } + counterChannels = new ArrayList(); tmCounter = new TableMapper(CounterChannel.class, tCounter); - tmCounter.setRows(pulsCounterDevice.getChannels()); tmCounter.getColumnInfo().get(0).setReadOnly(true); tmCounter.getColumnInfo().get(2).setReadOnly(true); tmCounter.getColumnInfo().get(4).setReadOnly(true); @@ -534,9 +562,14 @@ public class PulsCounterWindow extends JFrame{ tmCounter.addTableMapperListener(new AbstractTableMapperListener() { @Override public void ValueChanged(int row, int column) { - if (column == 2) + switch (column) { + case 1: + saveChannelName(row); + break; + case 3: saveCorrection(row); + break; } } @@ -556,7 +589,8 @@ public class PulsCounterWindow extends JFrame{ CounterChannel cc = (CounterChannel)tmCounter.getSelectedRow(); if (cc != null) { - pulsCounterDevice.reset(cc.getChannel()-1); + PulsCounter.getDeviceConnection().setChannelCounter(cc.getChannel(), 0); + synchronized (threadUpdater) { threadUpdater.notifyAll(); } @@ -575,8 +609,6 @@ public class PulsCounterWindow extends JFrame{ tmTBChannels.getColumnInfo().get(0).setReadOnly(true); tmTBChannels.getColumnInfo().get(1).setReadOnly(true); tmTBChannels.getColumnInfo().get(3).setReadOnly(true); - - threadUpdater = new Thread(new Runnable() { @Override @@ -591,7 +623,7 @@ public class PulsCounterWindow extends JFrame{ threadUpdater.wait(); } updateCounter(); - updateLiveFile(); +// updateLiveFile(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -613,27 +645,46 @@ public class PulsCounterWindow extends JFrame{ } }); updateTimer.setRepeats(true); - updateTimer.start(); - - loadChannelNames(); - + updateTimer.start(); } private void updateCounter() { lActivity.setText("Werte aktualisieren..."); - - pulsCounterDevice.update(); - - lDeviceType.setText(pulsCounterDevice.getDeviceName()); - lDeviceTime.setText(pulsCounterDevice.getDeviceTime().toLocaleString()); + System.err.println("Werte aktualisieren..."); - if (cbTimeBarriers.getItemCount()==0) + if (counterChannels == null) { - for (TimeBarrier tb: pulsCounterDevice.getTimeBarriers()) - { - cbTimeBarriers.addItem(tb); + tmCounter.setRows(counterChannels); + } + + IPulsCounter ipc = PulsCounter.getDeviceConnection(); + + if (ipc == null) + return; + + int nch = ipc.getChannels(); + + System.err.println(String.format("GUI: updateCounter(): Channel Array Size: %d / %d",nch,counterChannels.size())); + + + if (nch != counterChannels.size()){ + counterChannels.clear(); + + for (int i=0;i