package org.hwo.pulscounter.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.border.EmptyBorder; import org.hwo.datetime.JTimeOfDay; import org.hwo.io.SerialPortExeption; import org.hwo.servicelink.ServiceLink; import org.hwo.servicelink.ServiceLinkException; import org.hwo.servicelink.ServiceLinkRequestFailedException; import org.hwo.ui.MousePopupListener; import org.hwo.pulscounter.CounterChannel; import org.hwo.pulscounter.PulsCounter2Application; import org.postgresql.util.UnixCrypt; import java.awt.GridBagLayout; import java.io.IOException; import java.io.ObjectOutputStream.PutField; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; import java.awt.GridBagConstraints; import java.awt.Insets; import javax.swing.JToggleButton; import javax.swing.SwingConstants; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.border.TitledBorder; import java.awt.GridLayout; import javax.swing.JTextField; import javax.swing.JList; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.border.LineBorder; public class LiveViewFrame extends JFrame implements AppSettingsListener { private JPanel contentPane; private Timer liveViewTimer; private ServiceLink serviceLink; private JLabel[] inputButtons; private JToggleButton[] pullupButtons; private JToggleButton[] outputButtons; private JLabel[] analogLabels; private JPanel pDisplay; private JPanel pPullups; private JPanel pAnalog; private JPanel pOutputs; private Integer pinputs, inputs, outputs, pullups; private JPanel pDateTime; private JLabel lblUnixZeitstempel; private JTextField tfUnixTime; private JLabel lblAktuellesDatum; private JTextField tfHumanDateTime; private JButton btnSynchronisieren; private JPanel panel; private JScrollPane scrollPane; private JList lMessages; private JPanel panel_1; private JButton btnSnapshot; private JButton btnLeeren; private JButton btnLaufzeitKorrigieren; /** * Create the frame. */ public LiveViewFrame() { setTitle("Live Ansicht"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 1037, 875); 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}; gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0}; gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_contentPane.rowWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE}; contentPane.setLayout(gbl_contentPane); pDisplay = new JPanel(); pDisplay.setBorder(new TitledBorder(null, "Eing\u00E4nge", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_pDisplay = new GridBagConstraints(); gbc_pDisplay.insets = new Insets(0, 0, 5, 0); gbc_pDisplay.fill = GridBagConstraints.BOTH; gbc_pDisplay.gridx = 0; gbc_pDisplay.gridy = 0; contentPane.add(pDisplay, gbc_pDisplay); GridBagLayout gbl_pDisplay = new GridBagLayout(); gbl_pDisplay.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50}; gbl_pDisplay.rowHeights = new int[] {25, 25, 25, 25}; gbl_pDisplay.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0}; gbl_pDisplay.rowWeights = new double[]{1.0, 1.0,1.0, 1.0}; pDisplay.setLayout(gbl_pDisplay); pPullups = new JPanel(); pPullups.setBorder(new TitledBorder(null, "PullUPs", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_pPullups = new GridBagConstraints(); gbc_pPullups.insets = new Insets(0, 0, 5, 0); gbc_pPullups.fill = GridBagConstraints.BOTH; gbc_pPullups.gridx = 0; gbc_pPullups.gridy = 1; contentPane.add(pPullups, gbc_pPullups); GridBagLayout gbl_pPullups = new GridBagLayout(); gbl_pPullups.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50}; gbl_pPullups.rowHeights = new int[] {25, 25, 25, 25}; gbl_pPullups.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0}; gbl_pPullups.rowWeights = new double[]{1.0, 1.0,1.0, 1.0}; pPullups.setLayout(gbl_pPullups); pOutputs = new JPanel(); pOutputs.setBorder(new TitledBorder(null, "Ausg\u00E4nge", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_pOutputs = new GridBagConstraints(); gbc_pOutputs.insets = new Insets(0, 0, 5, 0); gbc_pOutputs.fill = GridBagConstraints.BOTH; gbc_pOutputs.gridx = 0; gbc_pOutputs.gridy = 2; contentPane.add(pOutputs, gbc_pOutputs); GridBagLayout gbl_pOutputs = new GridBagLayout(); gbl_pOutputs.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50}; gbl_pOutputs.rowHeights = new int[] {25, 25, 25, 25}; gbl_pOutputs.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0}; gbl_pOutputs.rowWeights = new double[]{1.0, 1.0,1.0, 1.0}; pOutputs.setLayout(gbl_pOutputs); pAnalog = new JPanel(); pAnalog.setBorder(new TitledBorder(null, "Analogkan\u00E4le", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_pAnalog = new GridBagConstraints(); gbc_pAnalog.insets = new Insets(0, 0, 5, 0); gbc_pAnalog.fill = GridBagConstraints.BOTH; gbc_pAnalog.gridx = 0; gbc_pAnalog.gridy = 3; contentPane.add(pAnalog, gbc_pAnalog); pAnalog.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); pDateTime = new JPanel(); pDateTime.setBorder(new TitledBorder(null, "Datum und Zeit", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_pDateTime = new GridBagConstraints(); gbc_pDateTime.insets = new Insets(0, 0, 5, 0); gbc_pDateTime.fill = GridBagConstraints.BOTH; gbc_pDateTime.gridx = 0; gbc_pDateTime.gridy = 4; contentPane.add(pDateTime, gbc_pDateTime); GridBagLayout gbl_pDateTime = new GridBagLayout(); gbl_pDateTime.columnWidths = new int[]{0, 0, 0, 0, 0}; gbl_pDateTime.rowHeights = new int[]{0, 0, 0}; gbl_pDateTime.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE}; gbl_pDateTime.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; pDateTime.setLayout(gbl_pDateTime); lblUnixZeitstempel = new JLabel("Unix Zeitstempel:"); GridBagConstraints gbc_lblUnixZeitstempel = new GridBagConstraints(); gbc_lblUnixZeitstempel.insets = new Insets(0, 0, 5, 5); gbc_lblUnixZeitstempel.anchor = GridBagConstraints.EAST; gbc_lblUnixZeitstempel.gridx = 0; gbc_lblUnixZeitstempel.gridy = 0; pDateTime.add(lblUnixZeitstempel, gbc_lblUnixZeitstempel); tfUnixTime = new JTextField(); GridBagConstraints gbc_tfUnixTime = new GridBagConstraints(); gbc_tfUnixTime.insets = new Insets(0, 0, 5, 5); gbc_tfUnixTime.fill = GridBagConstraints.HORIZONTAL; gbc_tfUnixTime.gridx = 1; gbc_tfUnixTime.gridy = 0; pDateTime.add(tfUnixTime, gbc_tfUnixTime); tfUnixTime.setColumns(10); lblAktuellesDatum = new JLabel("Aktuelles Datum:"); GridBagConstraints gbc_lblAktuellesDatum = new GridBagConstraints(); gbc_lblAktuellesDatum.insets = new Insets(0, 0, 5, 5); gbc_lblAktuellesDatum.anchor = GridBagConstraints.EAST; gbc_lblAktuellesDatum.gridx = 2; gbc_lblAktuellesDatum.gridy = 0; pDateTime.add(lblAktuellesDatum, gbc_lblAktuellesDatum); tfHumanDateTime = new JTextField(); GridBagConstraints gbc_tfHumanDateTime = new GridBagConstraints(); gbc_tfHumanDateTime.insets = new Insets(0, 0, 5, 0); gbc_tfHumanDateTime.fill = GridBagConstraints.HORIZONTAL; gbc_tfHumanDateTime.gridx = 3; gbc_tfHumanDateTime.gridy = 0; pDateTime.add(tfHumanDateTime, gbc_tfHumanDateTime); tfHumanDateTime.setColumns(10); btnSynchronisieren = new JButton("Synchronisieren"); btnSynchronisieren.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { syncUnixTime(); } }); GridBagConstraints gbc_btnSynchronisieren = new GridBagConstraints(); gbc_btnSynchronisieren.insets = new Insets(0, 0, 0, 5); gbc_btnSynchronisieren.gridx = 1; gbc_btnSynchronisieren.gridy = 1; pDateTime.add(btnSynchronisieren, gbc_btnSynchronisieren); btnLaufzeitKorrigieren = new JButton("Laufzeit korrigieren"); btnLaufzeitKorrigieren.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { quartzCorrect(); } }); GridBagConstraints gbc_btnLaufzeitKorrigieren = new GridBagConstraints(); gbc_btnLaufzeitKorrigieren.gridx = 3; gbc_btnLaufzeitKorrigieren.gridy = 1; pDateTime.add(btnLaufzeitKorrigieren, gbc_btnLaufzeitKorrigieren); panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Kommandos", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 51, 51))); GridBagConstraints gbc_panel_1 = new GridBagConstraints(); gbc_panel_1.fill = GridBagConstraints.BOTH; gbc_panel_1.insets = new Insets(0, 0, 5, 0); gbc_panel_1.gridx = 0; gbc_panel_1.gridy = 5; contentPane.add(panel_1, gbc_panel_1); GridBagLayout gbl_panel_1 = new GridBagLayout(); gbl_panel_1.columnWidths = new int[]{0, 0}; gbl_panel_1.rowHeights = new int[]{0, 0}; gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE}; gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_1.setLayout(gbl_panel_1); btnSnapshot = new JButton("SnapShot"); btnSnapshot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { manualSnapshot(); } }); GridBagConstraints gbc_btnSnapshot = new GridBagConstraints(); gbc_btnSnapshot.gridx = 0; gbc_btnSnapshot.gridy = 0; panel_1.add(btnSnapshot, gbc_btnSnapshot); panel = new JPanel(); panel.setBorder(new TitledBorder(null, "Meldungen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel = new GridBagConstraints(); gbc_panel.fill = GridBagConstraints.BOTH; gbc_panel.gridx = 0; gbc_panel.gridy = 6; contentPane.add(panel, gbc_panel); GridBagLayout gbl_panel = new GridBagLayout(); gbl_panel.columnWidths = new int[]{383, 0}; gbl_panel.rowHeights = new int[]{0, 131, 0, 0}; gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_panel.rowWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE}; panel.setLayout(gbl_panel); scrollPane = new JScrollPane(); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.insets = new Insets(0, 0, 5, 0); gbc_scrollPane.fill = GridBagConstraints.BOTH; gbc_scrollPane.gridx = 0; gbc_scrollPane.gridy = 1; panel.add(scrollPane, gbc_scrollPane); lMessages = new JList(); scrollPane.setViewportView(lMessages); btnLeeren = new JButton("leeren"); btnLeeren.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((DefaultListModel)lMessages.getModel()).clear(); } }); GridBagConstraints gbc_btnLeeren = new GridBagConstraints(); gbc_btnLeeren.gridx = 0; gbc_btnLeeren.gridy = 2; panel.add(btnLeeren, gbc_btnLeeren); initialize(); } @Override public void dispose() { PulsCounter2Application pc2a = PulsCounter2Application.getApplication(); pc2a.removeAppSettingsListener(this); liveViewTimer.cancel(); super.dispose(); } private void initialize(){ PulsCounter2Application pc2a = PulsCounter2Application.getApplication(); lMessages.setModel(new DefaultListModel()); inputButtons = new JLabel[32]; outputButtons = new JToggleButton[32]; pullupButtons = new JToggleButton[32]; for (int i=0;i<32;i++){ final int n = i; inputButtons[i] = new JLabel(String.format("%d", i)); inputButtons[i].setOpaque(true); inputButtons[i].setHorizontalAlignment(SwingConstants.CENTER); inputButtons[i].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) { if (serviceLink != null){ try { serviceLink.writeInt(13, 0, 0x0600 + n, 0); } catch (ServiceLinkRequestFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ServiceLinkException e) { e.printStackTrace(); } } } }); popup.add(mi); popup.show(inputButtons[n], x, y); } }); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(0, 0, 0, 0); gbc.fill = GridBagConstraints.BOTH; gbc.gridx = i % 8; gbc.gridy = (i / 8); pDisplay.add(inputButtons[i], gbc); outputButtons[i] = new JToggleButton(String.format("%d", i)); outputButtons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { setOutput(n, outputButtons[n].isSelected()); } }); pOutputs.add(outputButtons[i], gbc); pullupButtons[i] = new JToggleButton(String.format("%d", i)); pullupButtons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { setPullUp(n, pullupButtons[n].isSelected()); } }); pPullups.add(pullupButtons[i],gbc); } analogLabels = new JLabel[8]; for (int i=0;i<8;i++){ analogLabels[i] = new JLabel(String.format("---")); analogLabels[i].setBorder(new TitledBorder(String.format("AN%d",i))); pAnalog.add(analogLabels[i]); } liveViewTimer = new Timer(); liveViewTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { updateLiveView(); } }, 200, 500); pc2a.addAppSettingsListener(this); serviceLink = pc2a.getServiceLink(); } private synchronized void updateLiveView(){ System.err.println("LiveView Update"); long startTime = System.currentTimeMillis(); if (serviceLink != null){ System.err.println("ServiceLink exists."); try { serviceLink.open(); } catch (ServiceLinkException e) { e.printStackTrace(); } Integer brkval,heapend; brkval = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0020 ); if (brkval == null) brkval = 0; heapend = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0021 ); if (heapend == null) heapend = 0; System.err.println(String.format("PC2-BRKVAL: 0x%04x",brkval)); System.err.println(String.format("PC2-HEAPEND: 0x%04x",heapend)); Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Integer deviceUnixTime = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x001C); if (deviceUnixTime != null){ calendar.setTimeInMillis(deviceUnixTime * 1000L); tfUnixTime.setText(String.format("%d",deviceUnixTime)); tfHumanDateTime.setText(sdf.format(calendar.getTime())); }; pinputs = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0680 ); inputs = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0681 ); outputs = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0682 ); pullups = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0683 ); Integer[] values = new Integer[32]; for (int i=0;i<32;i++){ values[i] = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0600 + i); }; if (pinputs == null) pinputs = 0; if (inputs == null) inputs = 0; if (outputs == null) outputs = 0; if (pullups == null) pullups = 0; System.err.println(String.format("Inputs State (Phys.): 0x%08x",pinputs)); System.err.println(String.format("Inputs State: 0x%08x",inputs)); System.err.println(String.format("Outputs State: 0x%08x",outputs)); System.err.println(String.format("PullUP State: 0x%08x",pullups)); for (int i=0;i<32;i++){ if ((inputs & (1<
%d
[%d]
", i, values[i])); } else { inputButtons[i].setText(String.format("
%d
N.A.
", i)); }; if ((outputs & (1<= 0) break; DefaultListModel lm = (DefaultListModel)lMessages.getModel(); lm.addElement(String.format("Assertion: Error: 0x%08x (%d) Position: 0x%04x Mark: %d", assert_error,assert_error, assert_code & 0xffff, (assert_code >> 16) & 0xffff)); lMessages.ensureIndexIsVisible(lm.size()-1); serviceLink.writeInt(13, 0, 0x0025, -1); }; } catch (Exception ex){ System.err.println("Exception while checking for assertions..."); ex.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.err.println(String.format("updateLiveView(): Time needed: %d", (endTime - startTime))); } @Override public synchronized void ServiceLinkChanged(ServiceLink serviceLink) { this.serviceLink = serviceLink; } public synchronized void setOutput(int ch,boolean set){ if (this.serviceLink != null){ try { if (set){ serviceLink.writeInt((byte)13, (byte)0, 0x8100 + ch, 1); } else { serviceLink.writeInt((byte)13, (byte)0, 0x8100 + ch, 0); } } catch (ServiceLinkRequestFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ServiceLinkException e) { e.printStackTrace(); } } } public synchronized void setPullUp(int ch,boolean set){ if (this.serviceLink != null){ if (set){ pullups |= (1<)lMessages.getModel()).addElement(msg); } private void quartzCorrect(){ long lStart,lEnd; long rStart,rEnd; long lDelta,rDelta; long delta; if (this.serviceLink != null){ try { lStart = System.currentTimeMillis(); rStart = serviceLink.readInt(13, 0, 0x0027); Thread.sleep(30000); lEnd = System.currentTimeMillis(); rEnd = serviceLink.readInt(13, 0, 0x0027); lDelta = (lEnd - lStart)*1000; rDelta = rEnd - rStart; delta = lDelta - rDelta; message(String.format("Local: %d Remote: %d Delta: %d", lDelta,rDelta,delta)); delta /= 30; message(String.format("Korrektur: %d", delta)); serviceLink.writeInt(13, 0, 0x1002, (int)delta); } catch (Exception e) { e.printStackTrace(); } }; } }