From 52e6847fc2d28e6298a18f75d88008652c00fe09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sat, 3 May 2014 11:46:11 +0200 Subject: [PATCH] changed snmp timing to 100ms timeout added control to fake pulse heartrate disabled snmp check on display mode --- healthdisplay/.idea/uiDesigner.xml | 3 + healthdisplay/pom.xml | 2 +- .../main/java/de/psychose/ActorDisplay.java | 10 ++- .../main/java/de/psychose/ChaOSCclient.java | 19 +++- .../src/main/java/de/psychose/Main.java | 59 ++++++++++++ .../src/main/java/de/psychose/MainForm.form | 27 ++++-- .../src/main/java/de/psychose/MainForm.java | 89 +++++++++---------- .../main/java/de/psychose/PulseControl.form | 42 +++++++++ .../main/java/de/psychose/PulseControl.java | 76 ++++++++++++++++ .../src/main/java/de/psychose/PulseData.java | 55 ++++++++++++ .../main/java/de/psychose/SnmpStatClient.java | 52 ++++++----- .../main/java/de/psychose/StatsDisplay.java | 4 + 12 files changed, 357 insertions(+), 81 deletions(-) create mode 100644 healthdisplay/src/main/java/de/psychose/Main.java create mode 100644 healthdisplay/src/main/java/de/psychose/PulseControl.form create mode 100644 healthdisplay/src/main/java/de/psychose/PulseControl.java create mode 100644 healthdisplay/src/main/java/de/psychose/PulseData.java diff --git a/healthdisplay/.idea/uiDesigner.xml b/healthdisplay/.idea/uiDesigner.xml index c312b7a..c47fa82 100644 --- a/healthdisplay/.idea/uiDesigner.xml +++ b/healthdisplay/.idea/uiDesigner.xml @@ -125,6 +125,9 @@ + + + diff --git a/healthdisplay/pom.xml b/healthdisplay/pom.xml index 53cfc74..89362f5 100644 --- a/healthdisplay/pom.xml +++ b/healthdisplay/pom.xml @@ -63,7 +63,7 @@ - de.psychose.MainForm + de.psychose.Main diff --git a/healthdisplay/src/main/java/de/psychose/ActorDisplay.java b/healthdisplay/src/main/java/de/psychose/ActorDisplay.java index efe1d63..e2f0531 100644 --- a/healthdisplay/src/main/java/de/psychose/ActorDisplay.java +++ b/healthdisplay/src/main/java/de/psychose/ActorDisplay.java @@ -9,6 +9,7 @@ import java.awt.event.ActionEvent; * @date: 14.04.14 21:44 */ public class ActorDisplay { + private final Timer timer; private final static Color onColor = Color.WHITE; private final static Color offColor = Color.RED; private final static String offText = "no data"; @@ -73,7 +74,7 @@ public class ActorDisplay { } public ActorDisplay() { - final Timer timer = new Timer(100, new AbstractAction() { + this.timer = new Timer(100, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { @@ -127,8 +128,11 @@ public class ActorDisplay { } } }); - timer.setRepeats(true); - timer.start(); + this.timer.setRepeats(true); + } + + public void startErrorTimer() { + this.timer.start(); } } diff --git a/healthdisplay/src/main/java/de/psychose/ChaOSCclient.java b/healthdisplay/src/main/java/de/psychose/ChaOSCclient.java index ec4fc9d..9674dfa 100644 --- a/healthdisplay/src/main/java/de/psychose/ChaOSCclient.java +++ b/healthdisplay/src/main/java/de/psychose/ChaOSCclient.java @@ -40,6 +40,22 @@ public class ChaOSCclient { return changeChaoscSubscription(true); } + public void sendPulse(String actor, int heartbeat, int pulse, int oxygen) { + + try { + OSCMessage subscribeMessage = new OSCMessage("/" + actor + "/heartbeat"); + subscribeMessage.addArgument(heartbeat); + subscribeMessage.addArgument(pulse); + subscribeMessage.addArgument(oxygen); + + portOut.send(subscribeMessage); + } catch (IOException e) { + System.out.println("could not send pulse OSC Message"); + e.printStackTrace(); + } + } + + private boolean changeChaoscSubscription(boolean subscribe) { try { OSCMessage subscribeMessage = new OSCMessage("/" + (subscribe ? "subscribe" : "unsubscribe")); @@ -50,8 +66,6 @@ public class ChaOSCclient { portOut.send(subscribeMessage); return true; - } catch (SocketException e) { - e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } @@ -70,6 +84,7 @@ public class ChaOSCclient { InetAddress addr = a.nextElement(); if (addr.isSiteLocalAddress()) { + System.out.println("found address: " + addr.getHostAddress()); return addr; } } diff --git a/healthdisplay/src/main/java/de/psychose/Main.java b/healthdisplay/src/main/java/de/psychose/Main.java new file mode 100644 index 0000000..0a14f32 --- /dev/null +++ b/healthdisplay/src/main/java/de/psychose/Main.java @@ -0,0 +1,59 @@ +package de.psychose; + +import javax.swing.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.net.SocketException; +import java.net.UnknownHostException; + +/** + * @author: lucas + * @date: 25.04.14 00:23 + */ +public class Main { + + + public static void main(String[] args) { + + final boolean showErrors = args.length > 0; + + try + { + //UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); + UIManager.setLookAndFeel( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" ); + } + catch ( Exception e ) + { + e.printStackTrace(); + } + + try { + final ChaOSCclient chaOSCclient = new ChaOSCclient("chaosc", 7110); + final SnmpStatClient snmp = new SnmpStatClient("switch/161"); + final MainForm mainForm = new MainForm(showErrors, chaOSCclient, snmp); + final JFrame frame = new JFrame("MainForm"); + frame.setContentPane(mainForm.getMainPanel()); + frame.setResizable(false); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + frame.pack(); + + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + chaOSCclient.stopReceiver(); + super.windowClosing(e); + } + }); + + frame.setVisible(true); + + new Streamer(8888, mainForm.getMainPanel()).run(); + + chaOSCclient.startReceiver(); + + } catch (UnknownHostException | SocketException e) { + e.printStackTrace(); + } + } + +} diff --git a/healthdisplay/src/main/java/de/psychose/MainForm.form b/healthdisplay/src/main/java/de/psychose/MainForm.form index c3adf46..7a2b026 100644 --- a/healthdisplay/src/main/java/de/psychose/MainForm.form +++ b/healthdisplay/src/main/java/de/psychose/MainForm.form @@ -1,6 +1,6 @@
- + @@ -16,27 +16,27 @@ - + - + - + - + - + @@ -54,6 +54,21 @@ + + + + + + + + + + + + + + + diff --git a/healthdisplay/src/main/java/de/psychose/MainForm.java b/healthdisplay/src/main/java/de/psychose/MainForm.java index 45fe384..24bc772 100644 --- a/healthdisplay/src/main/java/de/psychose/MainForm.java +++ b/healthdisplay/src/main/java/de/psychose/MainForm.java @@ -5,25 +5,24 @@ import com.illposed.osc.OSCMessage; import javax.swing.*; import java.awt.event.ActionEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.net.SocketException; -import java.net.UnknownHostException; import java.util.Date; +import java.util.Observable; +import java.util.Observer; /** * @author: lucas * @date: 14.04.14 21:43 */ public class MainForm { - private ChaOSCclient osCclient; - private SnmpStatClient snmpStatClient; - + private final ChaOSCclient osCclient; private JPanel mainPanel; private ActorDisplay actor1; private ActorDisplay actor2; private ActorDisplay actor3; private StatsDisplay statDisplay; + private PulseControl pulse1; + private PulseControl pulse2; + private PulseControl pulse3; private int totalMessageCount = 0; private int messagesTempCounter = 0; @@ -31,15 +30,17 @@ public class MainForm { private long totalTraffic = 0; private long lastTraffic = 0; - public MainForm(final ChaOSCclient chaOSCclient, final SnmpStatClient snmpStatClient) { + public JPanel getMainPanel() { + return mainPanel; + } + + public MainForm(final boolean showErrors, final ChaOSCclient chaOSCclient, final SnmpStatClient snmpStatClient) { this.osCclient = chaOSCclient; - this.snmpStatClient = snmpStatClient; - addActor("merle", "Proband 1", actor1); - addActor("uwe", "Proband 2", actor2); - addActor("bjoern", "Proband 3", actor3); + addActor("merle", "Körper 1", actor1, pulse1); + addActor("uwe", "Körper 2", actor2, pulse2); + addActor("bjoern", "Körper 3", actor3, pulse3); - osCclient.startReceiver(); final Timer timer = new Timer(1000, new AbstractAction() { @Override @@ -55,18 +56,30 @@ public class MainForm { final Timer snmpTimer = new Timer(5000, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - totalTraffic = snmpStatClient.getTrafficSum(); - statDisplay.setTotalTraffic(String.valueOf(totalTraffic / 1024)); - statDisplay.setBandwidth(String.valueOf((totalTraffic - lastTraffic) / 1024 / 5)); + totalTraffic = snmpStatClient.getTrafficSum(); // in kB + statDisplay.setTotalTraffic(String.valueOf(totalTraffic)); + statDisplay.setBandwidth(String.valueOf((totalTraffic - lastTraffic) / 5)); lastTraffic = totalTraffic; } }); snmpTimer.setRepeats(true); - snmpTimer.start(); + + + if(showErrors) { + actor1.startErrorTimer(); + actor2.startErrorTimer(); + actor3.startErrorTimer(); + snmpTimer.start(); + } else { + pulse1.hide(); + pulse2.hide(); + pulse3.hide(); + statDisplay.hide(); + } } - private void addActor(final String actor, final String label, final ActorDisplay actorDisplay) { + private void addActor(final String actor, final String label, final ActorDisplay actorDisplay, final PulseControl pulse) { actorDisplay.setCaption(label); osCclient.addListener("/" + actor.toLowerCase() + "/heartbeat", new OSCListener() { @Override @@ -119,38 +132,18 @@ public class MainForm { } } }); - } - public static void main(String[] args) { - - final String host = args.length > 0 ? args[0] : "chaosc"; - final int port = args.length > 1 ? Integer.parseInt(args[1]) : 7110; - - try { - final ChaOSCclient chaOSCclient = new ChaOSCclient(host, port); - final SnmpStatClient snmp = new SnmpStatClient("switch/161"); - final MainForm mainForm = new MainForm(chaOSCclient, snmp); - final JFrame frame = new JFrame("MainForm"); - frame.setContentPane(mainForm.mainPanel); - frame.setResizable(false); - frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - frame.pack(); - - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - chaOSCclient.stopReceiver(); - super.windowClosing(e); + pulse.addObserver(new Observer() { + @Override + public void update(Observable o, Object arg) { + if(arg instanceof PulseData) { + final PulseData data = (PulseData)arg; + osCclient.sendPulse(actor, data.getHeartbeat(), data.getPulse(), data.getOxygen()); } - }); - - frame.setVisible(true); - - new Streamer(8888, mainForm.mainPanel).run(); - - } catch (UnknownHostException | SocketException e) { - e.printStackTrace(); - } + } + }); } + + } diff --git a/healthdisplay/src/main/java/de/psychose/PulseControl.form b/healthdisplay/src/main/java/de/psychose/PulseControl.form new file mode 100644 index 0000000..9e77e02 --- /dev/null +++ b/healthdisplay/src/main/java/de/psychose/PulseControl.form @@ -0,0 +1,42 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/healthdisplay/src/main/java/de/psychose/PulseControl.java b/healthdisplay/src/main/java/de/psychose/PulseControl.java new file mode 100644 index 0000000..915518e --- /dev/null +++ b/healthdisplay/src/main/java/de/psychose/PulseControl.java @@ -0,0 +1,76 @@ +package de.psychose; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Observable; +import java.util.Random; + +/** + * @author: lucas + * @date: 03.05.14 10:10 + */ +public class PulseControl extends Observable { + private final int PULSE_WOBBLE_WIDTH = 10; + private JCheckBox enableCheckBox; + private JSpinner spinner1; + private JPanel pulsePanel; + private Timer timer; + private Random random = new Random(); + private int heartbeat = 0; + + + public PulseControl() { + enableCheckBox.setFocusable(false); + spinner1.setFocusable(false); + spinner1.setValue(110); + + timer = new Timer(100, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + heartbeat = (heartbeat+1) % 2; + + final int pulseWobbleCenter = (int)spinner1.getValue(); + int pulse = pulseWobbleCenter - PULSE_WOBBLE_WIDTH / 2 + random.nextInt(PULSE_WOBBLE_WIDTH); + + if(pulse < 60) pulse = 60; + if(pulse > 180) pulse = 180; + + final PulseData data = new PulseData(heartbeat, pulse, 95 + random.nextInt(4)); + setChanged(); + notifyObservers(data); + + final int delay = 60000 / pulse; + timer.setDelay(delay); + } + }); + + timer.setRepeats(true); + + enableCheckBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + System.out.println("item state changed"); + JCheckBox checkBox = (JCheckBox)e.getSource(); + if(checkBox.isSelected()) { + if(!timer.isRunning()) { + System.out.println("starting pulsecontrol " + this); + timer.start(); + } + } else { + if(timer.isRunning()) { + System.out.println("stopping pulsecontrol " + this); + timer.stop(); + } + } + + } + }); + } + + public void hide() { + this.pulsePanel.setVisible(false); + } + +} diff --git a/healthdisplay/src/main/java/de/psychose/PulseData.java b/healthdisplay/src/main/java/de/psychose/PulseData.java new file mode 100644 index 0000000..7d23497 --- /dev/null +++ b/healthdisplay/src/main/java/de/psychose/PulseData.java @@ -0,0 +1,55 @@ +package de.psychose; + +/** + * @author: lucas + * @date: 03.05.14 10:58 + */ +public class PulseData { + + private int heartbeat; + private int pulse; + private int oxygen; + + public PulseData() { + + } + + public PulseData(int heartbeat, int pulse, int oxygen) { + this.heartbeat = heartbeat; + this.pulse = pulse; + this.oxygen = oxygen; + } + + public int getHeartbeat() { + return heartbeat; + } + + public void setHeartbeat(int heartbeat) { + this.heartbeat = heartbeat; + } + + public int getPulse() { + return pulse; + } + + public void setPulse(int pulse) { + this.pulse = pulse; + } + + public int getOxygen() { + return oxygen; + } + + public void setOxygen(int oxygen) { + this.oxygen = oxygen; + } + + @Override + public String toString() { + return "PulseData{" + + "heartbeat=" + heartbeat + + ", pulse=" + pulse + + ", oxygen=" + oxygen + + "} "; + } +} diff --git a/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java index a523318..ee84098 100644 --- a/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java +++ b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java @@ -23,58 +23,68 @@ import java.util.List; */ public class SnmpStatClient { public static final String OID_COUNTER = "1.3.6.1.2.1.2.2.1.10"; - private final String host; private HashMap lastPorts = new HashMap<>(); private HashMap sumPorts = new HashMap<>(); + private Snmp snmp; + private CommunityTarget communityTarget; - private CommunityTarget getCommunityTarget() { + private CommunityTarget getCommunityTarget(String host) { CommunityTarget communityTarget = new CommunityTarget(); communityTarget.setCommunity(new OctetString("public")); communityTarget.setVersion(SnmpConstants.version2c); communityTarget.setAddress(new UdpAddress(host)); - communityTarget.setTimeout(500); + communityTarget.setTimeout(100); return communityTarget; } public SnmpStatClient(String host) { - this.host = host; - } - - public long getTrafficSum() { - - long sum = 0; - try { final TransportMapping transportMapping = new DefaultUdpTransportMapping(); transportMapping.listen(); - final Snmp snmp = new Snmp(transportMapping); + this.communityTarget = getCommunityTarget(host); + this.snmp = new Snmp(transportMapping); + + } catch (IOException e) { + System.out.println("error: cannot get traffic from snmp target"); + } + } + + public long getTrafficSum() { + + if (snmp == null || this.communityTarget == null) { + System.out.println("snmp error"); + return 0; + } + + long sum = 0; + + try { + final TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); - final List treeEventList = treeUtils.getSubtree(getCommunityTarget(), new OID(OID_COUNTER)); + final List treeEventList = treeUtils.getSubtree(this.communityTarget, new OID(OID_COUNTER)); for (TreeEvent treeEvent : treeEventList) { if (treeEvent.getStatus() == TreeEvent.STATUS_OK) { for (VariableBinding binding : treeEvent.getVariableBindings()) { int oid = binding.getOid().last(); - long value = binding.getVariable().toLong(); + long value = binding.getVariable().toLong() / 1024; // convert bytes down to kilobytes long lastValue = 0; - if(lastPorts.containsKey(oid)) lastValue = lastPorts.get(oid); + if (lastPorts.containsKey(oid)) lastValue = lastPorts.get(oid); long diff = value - lastValue; - if(diff > 0) { + if (diff > 0) { sumPorts.put(oid, lastValue + diff); } } } } - - snmp.close(); - } catch (IOException e) { - e.printStackTrace(); + } catch (IllegalArgumentException e) { + System.out.println("error: could not resolve address from snmp target"); } - for(long port: sumPorts.values()) { - sum+=port; + for (long port : sumPorts.values()) { + sum += port; } return sum; diff --git a/healthdisplay/src/main/java/de/psychose/StatsDisplay.java b/healthdisplay/src/main/java/de/psychose/StatsDisplay.java index 7b3c76c..d0a002d 100644 --- a/healthdisplay/src/main/java/de/psychose/StatsDisplay.java +++ b/healthdisplay/src/main/java/de/psychose/StatsDisplay.java @@ -30,4 +30,8 @@ public class StatsDisplay { lblBandwidth.setText(bandwidth); } + public void hide() { + this.statPanel.setVisible(false); + } + }