package org.hwo.pulscounter.ui; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import org.hwo.datetime.DateTime; import org.hwo.logging.Logging; import org.hwo.logging.LoggingListener; import org.hwo.pulscounter.DeviceTester; import org.hwo.pulscounter.PulsCounterApplication; import org.hwo.pulscounter.PulsCounterApplicationListener; import org.hwo.pulscounter.device.ServiceLinkDeviceConnector; import org.hwo.servicelink.ServiceLink; import org.hwo.servicelink.exceptions.*; import java.awt.GridBagLayout; import javax.swing.JLabel; import java.awt.GridBagConstraints; import java.awt.Insets; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.swing.JTextField; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JList; import javax.swing.JScrollPane; import static org.hwo.logging.Logging.*; import static org.hwo.logging.LogLevel.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class DeviceTestFrame extends JFrame implements LoggingListener{ private JPanel contentPane; private JTextField tfCommunication; private JTextField tfEEPROM; private JTextField tfIO; private JTextField tfAD; private JTextField tfSerial; private Timer reconnectTimer; private DeviceChecker deviceChecker; private JList lMessages; private DefaultListModel messageListModel; private PulsCounterApplication pulsCounterApplication; /** * Create the frame. */ public DeviceTestFrame(PulsCounterApplication pulsCounterApplication) { this.pulsCounterApplication = pulsCounterApplication; setTitle("Device Test Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 701, 464); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[]{0, 0, 0}; gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0}; gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE}; gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE}; contentPane.setLayout(gbl_contentPane); JLabel lblKommunikation = new JLabel("Communication:"); GridBagConstraints gbc_lblKommunikation = new GridBagConstraints(); gbc_lblKommunikation.anchor = GridBagConstraints.EAST; gbc_lblKommunikation.insets = new Insets(0, 0, 5, 5); gbc_lblKommunikation.gridx = 0; gbc_lblKommunikation.gridy = 0; contentPane.add(lblKommunikation, gbc_lblKommunikation); tfCommunication = new JTextField(); GridBagConstraints gbc_tfCommunication = new GridBagConstraints(); gbc_tfCommunication.insets = new Insets(0, 0, 5, 0); gbc_tfCommunication.fill = GridBagConstraints.HORIZONTAL; gbc_tfCommunication.gridx = 1; gbc_tfCommunication.gridy = 0; contentPane.add(tfCommunication, gbc_tfCommunication); tfCommunication.setColumns(10); JLabel lblLeds = new JLabel("LEDs:"); GridBagConstraints gbc_lblLeds = new GridBagConstraints(); gbc_lblLeds.insets = new Insets(0, 0, 5, 5); gbc_lblLeds.gridx = 0; gbc_lblLeds.gridy = 1; contentPane.add(lblLeds, gbc_lblLeds); JLabel lblEeprom = new JLabel("EEPROM:"); GridBagConstraints gbc_lblEeprom = new GridBagConstraints(); gbc_lblEeprom.anchor = GridBagConstraints.EAST; gbc_lblEeprom.insets = new Insets(0, 0, 5, 5); gbc_lblEeprom.gridx = 0; gbc_lblEeprom.gridy = 2; contentPane.add(lblEeprom, gbc_lblEeprom); tfEEPROM = new JTextField(); GridBagConstraints gbc_tfEEPROM = new GridBagConstraints(); gbc_tfEEPROM.insets = new Insets(0, 0, 5, 0); gbc_tfEEPROM.fill = GridBagConstraints.HORIZONTAL; gbc_tfEEPROM.gridx = 1; gbc_tfEEPROM.gridy = 2; contentPane.add(tfEEPROM, gbc_tfEEPROM); tfEEPROM.setColumns(10); JLabel lblIoCircuit = new JLabel("I/O Circuit:"); GridBagConstraints gbc_lblIoCircuit = new GridBagConstraints(); gbc_lblIoCircuit.anchor = GridBagConstraints.EAST; gbc_lblIoCircuit.insets = new Insets(0, 0, 5, 5); gbc_lblIoCircuit.gridx = 0; gbc_lblIoCircuit.gridy = 3; contentPane.add(lblIoCircuit, gbc_lblIoCircuit); tfIO = new JTextField(); GridBagConstraints gbc_tfIO = new GridBagConstraints(); gbc_tfIO.insets = new Insets(0, 0, 5, 0); gbc_tfIO.fill = GridBagConstraints.HORIZONTAL; gbc_tfIO.gridx = 1; gbc_tfIO.gridy = 3; contentPane.add(tfIO, gbc_tfIO); tfIO.setColumns(10); JLabel lblAdConverters = new JLabel("A/D Converters:"); GridBagConstraints gbc_lblAdConverters = new GridBagConstraints(); gbc_lblAdConverters.anchor = GridBagConstraints.EAST; gbc_lblAdConverters.insets = new Insets(0, 0, 5, 5); gbc_lblAdConverters.gridx = 0; gbc_lblAdConverters.gridy = 4; contentPane.add(lblAdConverters, gbc_lblAdConverters); tfAD = new JTextField(); GridBagConstraints gbc_tfAD = new GridBagConstraints(); gbc_tfAD.insets = new Insets(0, 0, 5, 0); gbc_tfAD.fill = GridBagConstraints.HORIZONTAL; gbc_tfAD.gridx = 1; gbc_tfAD.gridy = 4; contentPane.add(tfAD, gbc_tfAD); tfAD.setColumns(10); JLabel lblSerial = new JLabel("Serial:"); GridBagConstraints gbc_lblSerial = new GridBagConstraints(); gbc_lblSerial.anchor = GridBagConstraints.EAST; gbc_lblSerial.insets = new Insets(0, 0, 5, 5); gbc_lblSerial.gridx = 0; gbc_lblSerial.gridy = 5; contentPane.add(lblSerial, gbc_lblSerial); tfSerial = new JTextField(); GridBagConstraints gbc_tfSerial = new GridBagConstraints(); gbc_tfSerial.insets = new Insets(0, 0, 5, 0); gbc_tfSerial.fill = GridBagConstraints.HORIZONTAL; gbc_tfSerial.gridx = 1; gbc_tfSerial.gridy = 5; contentPane.add(tfSerial, gbc_tfSerial); tfSerial.setColumns(10); JButton btnStart = new JButton("Start"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startTest(); } }); GridBagConstraints gbc_btnStart = new GridBagConstraints(); gbc_btnStart.insets = new Insets(0, 0, 5, 5); gbc_btnStart.gridx = 0; gbc_btnStart.gridy = 6; contentPane.add(btnStart, gbc_btnStart); JButton btnSetDevice = new JButton("Confirm Device"); GridBagConstraints gbc_btnSetDevice = new GridBagConstraints(); gbc_btnSetDevice.insets = new Insets(0, 0, 5, 0); gbc_btnSetDevice.gridx = 1; gbc_btnSetDevice.gridy = 6; contentPane.add(btnSetDevice, gbc_btnSetDevice); JScrollPane scrollPane = new JScrollPane(); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; gbc_scrollPane.gridwidth = 2; gbc_scrollPane.gridx = 0; gbc_scrollPane.gridy = 7; contentPane.add(scrollPane, gbc_scrollPane); lMessages = new JList(); scrollPane.setViewportView(lMessages); this.initialize(); } private PulsCounterApplication application(){ return this.pulsCounterApplication; } private void initialize(){ this.reconnectTimer = new Timer("Reconnection Timer"); messageListModel = new DefaultListModel(); lMessages.setModel(messageListModel); // Logging.getInstance().addLoggingListener(this); setVisible(true); } ServiceLink getServiceLink(){ return ((ServiceLinkDeviceConnector)(application().getInterfaces().get(0))).getServiceLink(); } /* @Override public void connectionStateChanged(Boolean connected) { if (!connected){ tfCommunication.setText("NO CONNECTION"); if (this.deviceChecker != null){ this.deviceChecker.cancel(); } reconnectTimer.schedule(new TimerTask() { @Override public void run() { if (!getServiceLink().isOpen()){ try { getServiceLink().open(); } catch (ServiceLinkException e) { e.printStackTrace(); } } } }, 3000); } else { } } */ private void startTest(){ /* if (this.deviceChecker != null){ while (this.deviceChecker.isAlive()){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } this.deviceChecker = new DeviceChecker(); this.deviceChecker.start(); */ DeviceTester dt = new DeviceTester( (application().getInterfaces().get(0)) ); dt.start(); while (dt.isAlive()){ try { dt.sleep(100); } catch (InterruptedException e) { log(e); } } } @Override public void logMessageArrived(String message) { if (EventQueue.isDispatchThread()){ int pos = messageListModel.size(); messageListModel.addElement(message); if (messageListModel.getSize() > 32) messageListModel.remove(0); lMessages.ensureIndexIsVisible(messageListModel.size()-1); } else { final String msg = message; EventQueue.invokeLater(new Runnable() { @Override public void run() { logMessageArrived(msg); } }); } } class DeviceChecker extends Thread { private boolean cancel; public DeviceChecker(){ cancel = false; } public void cancel(){ this.cancel = true; } @Override public void run() { try { getServiceLink().open(); } catch (ServiceLinkException e) { log(e); } checkCommunication(); checkEEPROM(); checkIO(); checkADC(); while (!cancel){ checkCommunication(); } } private void checkCommunication(){ if (!this.cancel){ Integer version = getServiceLink().getServiceRegisterCache().getCachedInteger(13, 0, 0x001A); Integer uptime = getServiceLink().getServiceRegisterCache().getCachedInteger(13, 0, 0x0022); if ((version != null)&&(uptime != null)){ tfCommunication.setText(String.format("OK (V: %d.%d.%d) Up: %d:%d:%d",version >> 16, (version >> 8) & 0xff, version & 0xff, uptime / 3600, (uptime / 60)%60, uptime % 60)); } } } private void checkEEPROM(){ if (!this.cancel){ Integer eesize = getServiceLink().getServiceRegisterCache().getCachedInteger(13, 0, 0x9000); if (eesize != null){ tfEEPROM.setText(String.format("EEPROM Size Detected: 0x%08x Bytes (%d kBytes)", eesize, (eesize >> 10))); } else { tfEEPROM.setText("EEPROM failed."); } } } private void checkIO(){ if (!this.cancel){ log(INFO,"I/O test begins..."); log(INFO,"I/O reset..."); try { getServiceLink().writeInt(13, 0, 0x0682, 0); getServiceLink().writeInt(13, 0, 0x0683, 0); getServiceLink().writeInt(13, 0, 0x0684, 0); getServiceLink().writeInt(13, 0, 0x0685, 0); log(INFO,"counter reset."); for (int i=0;i<32;i++){ getServiceLink().writeInt(13, 0, 0x0600 + i, 0); } log(INFO,"PullUP-Counter test."); for (int i=0;i<12;i++){ Thread.sleep(250); getServiceLink().writeInt(13, 0, 0x0683, -1); Thread.sleep(250); getServiceLink().writeInt(13, 0, 0x0683, 0); } for (int i=0;i<32;i++){ Thread.sleep(100); Integer c = getServiceLink().readInt(13, 0, 0x0600 + i); if ((c != null) && (c.equals(12))){ log(INFO,String.format("OK: Channel %d counted %d events.", i, c)); } else { log(INFO,String.format("FAILED: Channel %d counted %d events.", i, c)); } } log(INFO,"I/O test finished!"); } catch (IOException | ServiceLinkException e) { e.printStackTrace(); log(INFO,"I/O test FAILED!"); } catch (InterruptedException iex){ iex.printStackTrace(); log(INFO,"I/O test FAILED!"); } } } private void checkADC(){ if (!this.cancel){ } } } }