Adjusting back into the original lengthy version of IrrigationController; for testing...
[iot2.git] / benchmarks / drivers / Java / BlossomSprinkler / BlossomSprinkler.java
index cf0482846d1e13e014571e56704f84d73a6bffe1..209b2fe581ea2ee864714ef51f5d81790da6b76f 100644 (file)
@@ -14,9 +14,8 @@ import java.util.Calendar;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.concurrent.atomic.AtomicBoolean;
-
-// import java.util.HashSet;
-// import java.util.Set;
+import java.util.Set;
+import java.util.HashSet;
 
 // IoT Packages
 import iotruntime.IoTTCP;
@@ -40,7 +39,8 @@ public class BlossomSprinkler implements Sprinkler {
     /*******************************************************************************************************************************************
     **  Constants
     *******************************************************************************************************************************************/
-    public static final int NUMBER_OF_ZONES = 12;
+    //public static final int NUMBER_OF_ZONES = 12;
+       public static final int NUMBER_OF_ZONES = 1;
 
     @config IoTSet<IoTDeviceAddress> blossomSprAddressSet;
     @config IoTSet<IoTDeviceAddress> localAddressSet;
@@ -61,6 +61,11 @@ public class BlossomSprinkler implements Sprinkler {
     private Thread workerThread = null;
     private Thread httpMonitorThread = null;
 
+    public BlossomSprinkler(String _channelId, IoTSet<IoTDeviceAddress> _blossomSprAddressSet, IoTSet<IoTDeviceAddress> _localAddressSet) {
+        this(_channelId);
+               blossomSprAddressSet = _blossomSprAddressSet;
+               localAddressSet = _localAddressSet;             
+    }
 
     public BlossomSprinkler(String _channelId) {
         channelId = _channelId;
@@ -85,7 +90,6 @@ public class BlossomSprinkler implements Sprinkler {
 
         // create the correct number of zones for this controller
         for (int i = 0; i < NUMBER_OF_ZONES; i++) {
-            //zoneStates.add(new ZoneState(i, false, 0));
                        ZoneState zTmp = new ZoneState();
                        zTmp.zoneNumber = i;
                        zTmp.onOffState = false;
@@ -118,7 +122,6 @@ public class BlossomSprinkler implements Sprinkler {
                 {
                     // We replaced methods with fields
                                //z.zoneNumber, z.onOffState z.duration
-                    //if (z.getZoneNumber() == _zone) {
                     if (z.zoneNumber == _zone) {
 
                         // turn on or off the valve
@@ -195,13 +198,13 @@ public class BlossomSprinkler implements Sprinkler {
     *******************************************************************************************************************************************/
 
     private void workerMethod() {
-        while (didEnd.get() == false) {
-
+               System.out.println("Get into worker!");
+               while (didEnd.get() == false) {
+                       //System.out.println("While not end");
             try {
                 zoneStateMutex.acquire();
                 for (ZoneState z : zoneStates) {
                     if (z.onOffState) {
-
                         // if on and time has expired then turn off
                         if (z.duration == 0) {
 
@@ -252,9 +255,6 @@ public class BlossomSprinkler implements Sprinkler {
                 BufferedReader tcpIn = new BufferedReader(new InputStreamReader(recSock.getInputStream()));
                 PrintWriter tcpOut = new PrintWriter(recSock.getOutputStream());
 
-
-
-
                 System.out.println("Waiting For Data");
                 // wait for data to be ready
                 while (!tcpIn.ready()) {
@@ -270,12 +270,11 @@ public class BlossomSprinkler implements Sprinkler {
                     sList.add(s);
                 }
 
-                // System.out.println("---------------------------------------------------------------------");
-                // System.out.println("---------------------------------------------------------------------");
-                // for (String s : sList) {
-                //     System.out.println(s);
-                // }
-
+                System.out.println("---------------------------------------------------------------------");
+                System.out.println("---------------------------------------------------------------------");
+                for (String s : sList) {
+                    System.out.println(s);
+                }
 
                 // get first line and check that it is a GET request
                 String line = sList.get(0);
@@ -360,6 +359,7 @@ public class BlossomSprinkler implements Sprinkler {
 
             tcpOut.print(postMessage);
             tcpOut.flush();
+                       System.out.println("Sent POST message: " + postMessage);
 
             // wait for data
             while (!tcpIn.ready()) {
@@ -431,6 +431,29 @@ public class BlossomSprinkler implements Sprinkler {
             e.printStackTrace();
         }
     }
+       
+       /* TODO: Uncomment this part to do sprinkler test
+       public static void main(String[] args) throws Exception {
+
+               System.out.println("Executing main function!");
+               //IoTDeviceAddress iotDevAdd1 = new IoTDeviceAddress("192.168.0.129", 10009, 80, false, false);
+               //IoTDeviceAddress iotDevAdd2 = new IoTDeviceAddress("192.168.0.84", 10010, 80, false, false);
+               IoTDeviceAddress iotDevAdd1 = new IoTDeviceAddress(args[0], 10009, 80, false, false);
+               IoTDeviceAddress iotDevAdd2 = new IoTDeviceAddress(args[1], 10010, 80, false, false);           
+               Set<IoTDeviceAddress> setBlossom = new HashSet<IoTDeviceAddress>();
+               Set<IoTDeviceAddress> setLocal = new HashSet<IoTDeviceAddress>();
+               setBlossom.add(iotDevAdd1);
+               setLocal.add(iotDevAdd2);
+               IoTSet<IoTDeviceAddress> iotsetBlossom = new IoTSet<IoTDeviceAddress>(setBlossom);
+               IoTSet<IoTDeviceAddress> iotsetLocal = new IoTSet<IoTDeviceAddress>(setLocal);
+               String channelID = "1bd60b0c-2a99-4c83-8a7d-f97bd3f77a51";
+               BlossomSprinkler bs = new BlossomSprinkler(channelID, iotsetBlossom, iotsetLocal);
+               bs.init();
+               System.out.println("Finished init()");
+               bs.setZone(0, true, 60);
+               System.out.println("Finished setZone!");
+
+       }*/
 }