]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
antitheft gui
authoridgay <idgay>
Wed, 4 Apr 2007 22:06:22 +0000 (22:06 +0000)
committeridgay <idgay>
Wed, 4 Apr 2007 22:06:22 +0000 (22:06 +0000)
apps/AntiTheft/Nodes/antitheft.h
apps/AntiTheft/Root/AntiTheftRootAppC.nc
apps/AntiTheft/java/.cvsignore [new file with mode: 0644]
apps/AntiTheft/java/AntiTheftGui.java

index 7cff06ba53865b29bc32f2f139f50ba1dffd0723..fefe553e4a6bef78da1268dedee65672baae9a7c 100644 (file)
@@ -26,7 +26,7 @@ enum {
 
   AM_SETTINGS = 54,
   AM_THEFT = 99,
-  AM_ALERTS = 22,
+  AM_ALERT = 22,
   DIS_SETTINGS = 42,
   COL_ALERTS = 11,
 
@@ -35,12 +35,12 @@ enum {
   DEFAULT_CHECK_INTERVAL = 1000
 };
 
-typedef nx_struct {
+typedef nx_struct settings {
   nx_uint8_t alert, detect;
   nx_uint16_t checkInterval;
 } settings_t;
 
-typedef nx_struct {
+typedef nx_struct alert {
   nx_uint16_t stolenId;
 } alert_t;
 
index fa97f7179a48f13d9c297a6122365112b926f21e..f961ed966353d886ee9011bcffbde09586df4d42 100644 (file)
@@ -48,7 +48,7 @@ implementation
 
   /* Finally, instantiate and wire a collector (to receive theft alerts) and
      a serial sender (to send the alerts to the PC) */
-  components CollectionC, new SerialAMSenderC(AM_ALERTS) as AlertsForwarder;
+  components CollectionC, new SerialAMSenderC(AM_ALERT) as AlertsForwarder;
 
   AntiTheftRootC.CollectionControl -> CollectionC;
   AntiTheftRootC.RootControl -> CollectionC;
diff --git a/apps/AntiTheft/java/.cvsignore b/apps/AntiTheft/java/.cvsignore
new file mode 100644 (file)
index 0000000..c6dc062
--- /dev/null
@@ -0,0 +1,4 @@
+AlertMsg.java
+Constants.java
+SettingsMsg.java
+antitheft.jar
index 1fd12a5953762b91c5834dd3da4d29049f3b5a0a..83db2bf7c029c70edf9c2ec61cbf7f0fdd7cb62a 100644 (file)
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
+import java.io.*;
+import net.tinyos.message.*;
 import net.tinyos.packet.*;
+import net.tinyos.util.*;
 
-public class AntiTheftGui {
+public class AntiTheftGui implements MessageListener, Messenger {
+    MoteIF mote;
+    JFrame frame;
     JTextArea mssgArea;
     JTextField fieldInterval;
     JCheckBox detDarkCb, detAccelCb, repLedCb, repSirenCb, repServerCb,
@@ -52,6 +57,8 @@ public class AntiTheftGui {
     public AntiTheftGui() {
        try {
            guiInit();
+           mote = new MoteIF(this);
+           mote.registerListener(new AlertMsg(), this);
        }
        catch(Exception e) {
            e.printStackTrace();
@@ -112,7 +119,7 @@ public class AntiTheftGui {
        mainPanel.add(buttonPanel, BorderLayout.EAST);
 
        // The frame part
-       JFrame frame = new JFrame("AntiTheft");
+       frame = new JFrame("AntiTheft");
        frame.setSize(mainPanel.getPreferredSize());
        frame.getContentPane().add(mainPanel);
        frame.setVisible(true);
@@ -121,14 +128,80 @@ public class AntiTheftGui {
            });
     }
 
-    public synchronized void theft(String mssg) {
-       mssgArea.append(mssg + "\n");
-       mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
+    void error(String msg) {
+       JOptionPane.showMessageDialog(frame, msg, "Error",
+                                     JOptionPane.ERROR_MESSAGE);
     }
 
-    public void updateSettings() { }
+    public void updateSettings() { 
+       SettingsMsg smsg = new SettingsMsg();
+       short alert = 0;
+       short detect = 0;
+       int checkInterval = Constants.DEFAULT_CHECK_INTERVAL;
+
+       /* Extract current interval value, ignoring bad values */
+       String intervalS = fieldInterval.getText().trim();
+       try {
+           int newInterval = Integer.parseInt(intervalS);
+           if (newInterval < 10) throw new NumberFormatException();
+           checkInterval = newInterval;
+       }
+       catch (NumberFormatException e) { 
+           /* Reset field when value is bad */
+           fieldInterval.setText("" + checkInterval);
+       }
+
+       if (repLedCb.isSelected())
+           alert |= Constants.ALERT_LEDS;
+       if (repSirenCb.isSelected())
+           alert |= Constants.ALERT_SOUND;
+       if (repNeighboursCb.isSelected())
+           alert |= Constants.ALERT_RADIO;
+       if (repServerCb.isSelected())
+           alert |= Constants.ALERT_ROOT;
+       if (alert == 0) {
+           /* If nothing select, force-select LEDs */
+           alert = Constants.ALERT_LEDS;
+           repLedCb.setSelected(true);
+       }
+
+       if (detDarkCb.isSelected())
+           detect |= Constants.DETECT_DARK;
+       if (detAccelCb.isSelected())
+           detect |= Constants.DETECT_ACCEL;
+       if (detect == 0) {
+           /* If no detection selected, force-select dark */
+           detect = Constants.DETECT_DARK;
+           detDarkCb.setSelected(true);
+       }
+
+       smsg.set_alert(alert);
+       smsg.set_detect(detect);
+       smsg.set_checkInterval(checkInterval);
+
+       try {
+           mote.send(MoteIF.TOS_BCAST_ADDR, smsg);
+       }
+       catch (IOException e) {
+           error("Cannot send message to mote");
+       }
+    }
 
     public static void main(String[] args) {
        AntiTheftGui me = new AntiTheftGui();
     }
+
+    synchronized public void messageReceived(int dest_addr, Message msg) {
+       if (msg instanceof AlertMsg) {
+           AlertMsg alertMsg = (AlertMsg)msg;
+
+           String warning = "Theft of " + alertMsg.get_stolenId() + "\n";
+           mssgArea.append(warning);
+           mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
+       }
+    }
+
+    public void message(String s) {
+       error(s);
+    }
 }