From d9a08dfeebe0eca7738b939d74b059187b24ca3d Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 26 Aug 2014 14:23:09 +0200 Subject: [PATCH] Updates --- src/org/hwo/pulscounter/CounterChannel.java | 15 +- .../hwo/pulscounter/PulsCounterDevice.java | 69 ++++-- .../hwo/pulscounter/PulsCounterInterface.java | 2 + .../hwo/pulscounter/PulsCounterWindow.java | 217 +++++++++++++++--- 4 files changed, 253 insertions(+), 50 deletions(-) diff --git a/src/org/hwo/pulscounter/CounterChannel.java b/src/org/hwo/pulscounter/CounterChannel.java index 0b11e01..55eec98 100644 --- a/src/org/hwo/pulscounter/CounterChannel.java +++ b/src/org/hwo/pulscounter/CounterChannel.java @@ -6,7 +6,11 @@ public class CounterChannel { @TableColumn(label="Kanal",firstColumn=true) private Integer channel; - @TableColumn(label="ZŠhlerstand",after="Kanal") + + @TableColumn(label="Bezeichnung",after="Kanal") + private String bezeichnung; + + @TableColumn(label="ZŠhlerstand",after="Bezeichnung") private Integer value; @TableColumn(label="Korrekturwert",after="ZŠhlerstand") private Integer correct; @@ -22,6 +26,7 @@ public class CounterChannel { channel = ch; value = 0; correct = 0; + bezeichnung = ""; } public Integer getChannel() { @@ -44,6 +49,14 @@ public class CounterChannel { this.correct = correct; } + public String getBezeichnung() { + return bezeichnung; + } + + public void setBezeichnung(String bezeichnung) { + this.bezeichnung = bezeichnung; + } + } diff --git a/src/org/hwo/pulscounter/PulsCounterDevice.java b/src/org/hwo/pulscounter/PulsCounterDevice.java index 9e8cd58..d56413a 100644 --- a/src/org/hwo/pulscounter/PulsCounterDevice.java +++ b/src/org/hwo/pulscounter/PulsCounterDevice.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Random; import org.hwo.io.SerialPort; +import org.hwo.io.servicelink.ServiceLink; public class PulsCounterDevice { @@ -38,39 +39,75 @@ public class PulsCounterDevice { } - public void update() + synchronized public void reset(int ch) { if (serialPort == null) return; - - if (!serialPort.open()) - { - System.err.println("Fehler: Port konnte nicht gešffnet werden!"); - } - + try { - DeviceControlChannel channel = new DeviceControlChannel(serialPort.getInputStream(), serialPort.getOutputStream()); + ServiceLink sl = new ServiceLink(serialPort); - channel.noop(); + sl.writeInt((byte)0, (byte)0, (short)(0x1000 + ch), 0); + + } catch (Exception e) + { + System.err.println("Exception: " + e); + e.printStackTrace(); + } + + if (serialPort.isOpen()) + serialPort.close(); + } + + synchronized public void reset() + { + if (serialPort == null) + return; + + try + { + ServiceLink sl = new ServiceLink(serialPort); + + for (int i=0;i<32;i++) + sl.writeInt((byte)0, (byte)0, (short)(0x1000 + i), 0); + + } catch (Exception e) + { + System.err.println("Exception: " + e); + e.printStackTrace(); + } + + if (serialPort.isOpen()) + serialPort.close(); + } + + + synchronized public void update() + { + if (serialPort == null) + return; + + try + { + ServiceLink sl = new ServiceLink(serialPort); + + System.err.println(String.format("BRKVAL: 0x%04x", sl.readInt((byte)0, (byte)0, (short)0x200))); + System.err.println(String.format("SPLIM : 0x%04x", sl.readInt((byte)0, (byte)0, (short)0x201))); for (int i=0;i<32;i++) { - int inc = random.nextInt(15); - if (inc > 4) - inc = 0; - - channels.get(i).setValue(channels.get(i).getValue() + inc); + channels.get(i).setValue( sl.readInt((byte)0x0D, (byte)0, (short)(0x1000 + i)) ); } } catch (Exception e) { System.err.println("Exception: " + e); e.printStackTrace(); } + if (serialPort.isOpen()) serialPort.close(); - } @@ -101,6 +138,4 @@ public class PulsCounterDevice { public void setSerialPort(SerialPort serialPort) { this.serialPort = serialPort; } - - } diff --git a/src/org/hwo/pulscounter/PulsCounterInterface.java b/src/org/hwo/pulscounter/PulsCounterInterface.java index 96256f7..a11263f 100644 --- a/src/org/hwo/pulscounter/PulsCounterInterface.java +++ b/src/org/hwo/pulscounter/PulsCounterInterface.java @@ -4,12 +4,14 @@ import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.ServiceLoader; public class PulsCounterInterface { public PulsCounterInterface(String portName) { + } public List readCounter() diff --git a/src/org/hwo/pulscounter/PulsCounterWindow.java b/src/org/hwo/pulscounter/PulsCounterWindow.java index 61ceac9..c0e57ac 100644 --- a/src/org/hwo/pulscounter/PulsCounterWindow.java +++ b/src/org/hwo/pulscounter/PulsCounterWindow.java @@ -2,8 +2,11 @@ package org.hwo.pulscounter; import java.awt.EventQueue; +import javax.swing.JFileChooser; import javax.swing.JFrame; +import javax.swing.JMenuItem; import javax.swing.JPanel; +import javax.swing.JPopupMenu; import javax.swing.Timer; import java.awt.BorderLayout; @@ -20,11 +23,13 @@ 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.ui.MousePopupListener; import java.awt.Insets; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Properties; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; @@ -33,6 +38,8 @@ import javax.swing.JScrollPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import javax.swing.border.TitledBorder; import javax.swing.JSpinner; @@ -46,9 +53,16 @@ import javax.swing.BoxLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import org.hwo.datetime.JTimeOfDay; +import javax.swing.JTextField; + public class PulsCounterWindow extends JFrame{ private JComboBox cbInterfaces; private JTable tCounter; @@ -74,6 +88,9 @@ public class PulsCounterWindow extends JFrame{ private JTimeOfDay todTimebarrier; private TableMapper tmTBChannels; + private JTextField tfWorkDir; + + private String workingDirectory; public PulsCounterWindow() { addWindowListener(new WindowAdapter() { @@ -163,15 +180,16 @@ public class PulsCounterWindow extends JFrame{ gbc_panel_2.gridy = 0; panel.add(panel_2, gbc_panel_2); GridBagLayout gbl_panel_2 = new GridBagLayout(); - gbl_panel_2.columnWidths = new int[]{0, 0, 0}; - gbl_panel_2.rowHeights = new int[]{0, 0}; - gbl_panel_2.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; - gbl_panel_2.rowWeights = new double[]{0.0, Double.MIN_VALUE}; + gbl_panel_2.columnWidths = new int[]{0, 0, 0, 0}; + gbl_panel_2.rowHeights = new int[]{0, 0, 0}; + gbl_panel_2.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; + gbl_panel_2.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; panel_2.setLayout(gbl_panel_2); JLabel lblAktualisierungsintervall = new JLabel("Aktualisierungsintervall:"); GridBagConstraints gbc_lblAktualisierungsintervall = new GridBagConstraints(); - gbc_lblAktualisierungsintervall.insets = new Insets(0, 0, 0, 5); + gbc_lblAktualisierungsintervall.anchor = GridBagConstraints.WEST; + gbc_lblAktualisierungsintervall.insets = new Insets(0, 0, 5, 5); gbc_lblAktualisierungsintervall.gridx = 0; gbc_lblAktualisierungsintervall.gridy = 0; panel_2.add(lblAktualisierungsintervall, gbc_lblAktualisierungsintervall); @@ -198,11 +216,58 @@ public class PulsCounterWindow extends JFrame{ }); spUpdateIntervall.setModel(new SpinnerNumberModel(15, 5, 3600, 1)); GridBagConstraints gbc_spUpdateIntervall = new GridBagConstraints(); + gbc_spUpdateIntervall.gridwidth = 2; + gbc_spUpdateIntervall.insets = new Insets(0, 0, 5, 0); gbc_spUpdateIntervall.anchor = GridBagConstraints.EAST; gbc_spUpdateIntervall.gridx = 1; gbc_spUpdateIntervall.gridy = 0; panel_2.add(spUpdateIntervall, gbc_spUpdateIntervall); + JLabel lblArbeitsverzeichnis = new JLabel("Arbeitsverzeichnis:"); + GridBagConstraints gbc_lblArbeitsverzeichnis = new GridBagConstraints(); + gbc_lblArbeitsverzeichnis.anchor = GridBagConstraints.EAST; + gbc_lblArbeitsverzeichnis.insets = new Insets(0, 0, 0, 5); + gbc_lblArbeitsverzeichnis.gridx = 0; + gbc_lblArbeitsverzeichnis.gridy = 1; + panel_2.add(lblArbeitsverzeichnis, gbc_lblArbeitsverzeichnis); + + tfWorkDir = new JTextField(); + tfWorkDir.setEditable(false); + GridBagConstraints gbc_tfWorkDir = new GridBagConstraints(); + gbc_tfWorkDir.insets = new Insets(0, 0, 0, 5); + gbc_tfWorkDir.anchor = GridBagConstraints.NORTH; + gbc_tfWorkDir.fill = GridBagConstraints.HORIZONTAL; + gbc_tfWorkDir.gridx = 1; + gbc_tfWorkDir.gridy = 1; + panel_2.add(tfWorkDir, gbc_tfWorkDir); + tfWorkDir.setColumns(10); + + JButton btnDurchsuchen = new JButton("durchsuchen..."); + btnDurchsuchen.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + + JFileChooser jfc = new JFileChooser(); + jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + + if (jfc.showOpenDialog(PulsCounterWindow.this)==JFileChooser.APPROVE_OPTION) + { + workingDirectory = jfc.getSelectedFile().getAbsolutePath(); + tfWorkDir.setText(workingDirectory); + prefs.put("file.directory",workingDirectory); + try { + prefs.flush(); + } catch (BackingStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }); + GridBagConstraints gbc_btnDurchsuchen = new GridBagConstraints(); + gbc_btnDurchsuchen.gridx = 2; + gbc_btnDurchsuchen.gridy = 1; + panel_2.add(btnDurchsuchen, gbc_btnDurchsuchen); + JPanel panel_3 = new JPanel(); panel_3.setBorder(new TitledBorder(null, "Aktionen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_3 = new GridBagConstraints(); @@ -226,15 +291,30 @@ public class PulsCounterWindow extends JFrame{ gbc_btnNewButton.gridy = 0; panel_3.add(btnNewButton, gbc_btnNewButton); - JButton btnTagesdateiSchreiben = new JButton("Tagesdatei schreiben..."); + 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(); + + synchronized (threadUpdater) { + threadUpdater.notifyAll(); + } + } + }); + GridBagConstraints gbc_btnAlle = new GridBagConstraints(); + gbc_btnAlle.gridx = 3; + gbc_btnAlle.gridy = 0; + panel_3.add(btnAlle, gbc_btnAlle); btnTagesdateiSchreiben.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - writeDayFile(); + saveChannelNames(); } }); btnNewButton.addActionListener(new ActionListener() { @@ -429,6 +509,9 @@ public class PulsCounterWindow extends JFrame{ 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); @@ -445,8 +528,8 @@ public class PulsCounterWindow extends JFrame{ tmCounter = new TableMapper(CounterChannel.class, tCounter); tmCounter.setRows(pulsCounterDevice.getChannels()); tmCounter.getColumnInfo().get(0).setReadOnly(true); - tmCounter.getColumnInfo().get(1).setReadOnly(true); - tmCounter.getColumnInfo().get(3).setReadOnly(true); + tmCounter.getColumnInfo().get(2).setReadOnly(true); + tmCounter.getColumnInfo().get(4).setReadOnly(true); tmCounter.addTableMapperListener(new AbstractTableMapperListener() { @Override @@ -456,8 +539,37 @@ public class PulsCounterWindow extends JFrame{ saveCorrection(row); } } + }); + tCounter.addMouseListener(new MousePopupListener() { + + @Override + public void popupTriggered(int x, int y) { + JPopupMenu popup = new JPopupMenu(); + JMenuItem mi = new JMenuItem(); + mi.setText("RESET"); + mi.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + CounterChannel cc = (CounterChannel)tmCounter.getSelectedRow(); + if (cc != null) + { + pulsCounterDevice.reset(cc.getChannel()-1); + synchronized (threadUpdater) { + threadUpdater.notifyAll(); + } + } + + } + }); + popup.add(mi); + + popup.show(tCounter, x, y); + + } + }); tmTBChannels = new TableMapper(CounterChannel.class, tTBChannels); tmTBChannels.getColumnInfo().get(0).setReadOnly(true); @@ -479,6 +591,7 @@ public class PulsCounterWindow extends JFrame{ threadUpdater.wait(); } updateCounter(); + updateLiveFile(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -502,11 +615,13 @@ public class PulsCounterWindow extends JFrame{ updateTimer.setRepeats(true); updateTimer.start(); + loadChannelNames(); + } private void updateCounter() { - lActivity.setText("Werte aktuelisieren..."); + lActivity.setText("Werte aktualisieren..."); pulsCounterDevice.update(); @@ -538,31 +653,48 @@ public class PulsCounterWindow extends JFrame{ pulsCounterDevice.setSerialPort(sport); } - private void writeDayFile() + private void saveChannelNames() { - Date date = new Date(); + Properties chprop = new Properties(); + + for (CounterChannel ch:pulsCounterDevice.getChannels()) + chprop.put(String.format("channel.%d", ch.getChannel()), ch.getBezeichnung()); + + File pf = new File("chnames.prop"); + + try { + chprop.store(new FileOutputStream(pf), ""); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + private void loadChannelNames() + { + Properties chprop = new Properties(); + + File pf = new File("chnames.prop"); + + try { + chprop.load(new FileInputStream(pf)); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + - /* CSV csv = new CSV(); - String[] row = new String[this.timeBarriers.size() + 1]; + for (CounterChannel ch:pulsCounterDevice.getChannels()) + ch.setBezeichnung( chprop.getProperty(String.format("channel.%d", ch.getChannel()))); - row[0] = ""; - for (int n=1;n