7a28cbe8c6a6df49de5a4d7dfc644e93d1cdf0bf
[iot2.git] / benchmarks / drivers / Java / WaterLeakSensor / WaterLeakSensor.java
1 package iotcode.WaterLeakSensor;
2
3 // Standard Java Packages
4 import java.util.*;
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.concurrent.CopyOnWriteArrayList;
7 import java.util.concurrent.Semaphore;
8
9 // Checker annotations
10 //import iotchecker.qual.*;
11 import iotcode.annotation.*;
12
13 // IoT Packages
14 import iotruntime.slave.*;
15 import iotcode.interfaces.*;
16 import iotruntime.zigbee.*;
17
18 /** Class Smartthings sensor driver for Smartthings sensor devices.
19  *
20  * @author      Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
21  * @version     1.0
22  * @since       2016-12-01
23  */
24 public class WaterLeakSensor implements IoTZigbeeCallback, SmartthingsSensor {
25
26         private final int TIMEOUT_FOR_RESEND_MSEC = 900;
27
28         private IoTZigbee zigConnection = null;
29         private boolean didClose; // make sure that the clean up was done correctly
30         private boolean detectStatus = false;
31
32         private int detectedValue = 0;
33         private Date timestampOfLastDetecting = null;
34
35         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
36         private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
37         private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
38         private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
39         static Semaphore gettingLatestDataMutex = new Semaphore(1);
40
41         private List < SmartthingsSensorSmartCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorSmartCallback > ();
42
43         private int sensorId = 0;
44
45         @config private IoTSet<IoTDeviceAddress> waterleakSensorUdpAddress;
46         @config private IoTSet<IoTZigbeeAddress> waterleakSensorZigbeeAddress;
47
48         //public WaterLeakSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
49                 //waterleakSensorUdpAddress = dSet;
50                 //waterleakSensorZigbeeAddress = zigSet;
51         //}
52         public WaterLeakSensor() {
53         }
54
55         public void init() {
56
57                 if (didAlreadyInit.compareAndSet(false, true) == false) {
58                         return; // already init
59                 }
60
61                 didAlreadyClose.set(false);
62
63                 try {
64                         Iterator itrUdp = waterleakSensorUdpAddress.iterator();
65                         Iterator itrZig = waterleakSensorZigbeeAddress.iterator();
66
67                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
68
69                         // DEBUG
70                         System.out.println("DEBUG: Allocate iterators to print out addresses!");
71                         Iterator itrDebugUdp = waterleakSensorUdpAddress.iterator();
72                         IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
73                         System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
74                         System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
75                         System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
76
77                         Iterator itrDebugZig = waterleakSensorZigbeeAddress.iterator();
78                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
79                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
80
81                         zigConnection.registerCallback(this);
82                         System.out.println("Register callback!");
83                         zigConnection.init();
84                         System.out.println("Initialized!");
85
86                         //made by changwoo
87                         sleep(10);
88                         System.out.println("Sending Management Permit Joining Request");
89                         for(int z=0; z<3; z++){
90                                 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
91                                 sleep(0);
92                         }
93
94                         //made by changwoo
95                         while (!didWriteAttrb.get()) {
96                                 System.out.println("Sending Write Attribute Request");
97                                 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
98                                 sleep(0);
99                         }
100
101                         //made by changwoo
102                         System.out.println("Sending Enrollment Reponse");
103                         zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
104                         sleep(0);
105
106                 } catch (Exception e) {
107                         e.printStackTrace();
108                 }
109         }
110
111         //made by changwoo
112         private void sleep(int multipleTime){
113                 if(multipleTime<=0){
114                         multipleTime=1;
115                 }
116                 try{
117                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
118                 } catch(Exception e){
119                         e.printStackTrace();
120                 }
121         }
122
123         public void close() {
124
125                 if (didAlreadyClose.compareAndSet(false, true) == false) {
126                         return; // already init
127                 }
128
129                 didAlreadyInit.set(false);
130
131
132                 try {
133                         zigConnection.close();
134                 } catch (Exception e) {
135                         e.printStackTrace();
136                 }
137         }
138
139         public void Finalize() {
140                 if (!didClose) {
141                         close();
142                 }
143         }
144
145         public void setId(int id) {
146
147                 sensorId = id;
148
149         }
150
151         public int getId() {
152
153                 return sensorId;
154
155         }
156
157         public int getValue() {
158
159                 int tmp = 0;
160                 try {
161                         gettingLatestDataMutex.acquire();
162                         tmp = detectedValue;
163
164                 } catch (Exception e) {
165                         e.printStackTrace();
166                 }
167                 gettingLatestDataMutex.release();
168
169                 return tmp;
170         }
171
172         // WaterLeakSensor: 
173         // - 24 = no leak = false
174         // - 25 = leak = true
175         public boolean isActiveValue() {
176
177                 int tmp = getValue();
178                 if (tmp == 25)
179                         detectStatus = true;
180                 else // Getting 24 here
181                         detectStatus = false;
182
183                 return detectStatus;
184         }
185
186         public long getTimestampOfLastReading() {
187
188                 Date tmp = null;
189                 try {
190                         gettingLatestDataMutex.acquire();
191                         tmp = (Date)timestampOfLastDetecting.clone();
192
193                 } catch (Exception e) {
194                         e.printStackTrace();
195                 }
196                 gettingLatestDataMutex.release();
197                 long retLong = tmp.getTime();
198
199                 return retLong;
200         }
201
202         public void newMessageAvailable(IoTZigbeeMessage _zm) {
203
204                 //made by changwoo
205                 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
206                         IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
207                         if(message.getSuccessOrFail()){
208                                 //do something!
209
210                                 try {
211                                         gettingLatestDataMutex.acquire();
212                                         detectedValue = message.getStatus();
213                                         timestampOfLastDetecting = new Date();
214                                 } catch (Exception e) {
215                                         e.printStackTrace();
216                                 }
217                                 gettingLatestDataMutex.release();
218                                 try {
219                                         for (SmartthingsSensorSmartCallback cb : callbackList) {
220                                                 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
221                                         }
222                                 } catch (Exception e) {
223                                         e.printStackTrace();
224                                 }
225                         }//if
226                 
227                 //made by changwoo
228                 }//if
229                 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
230                         IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
231                         if (message.getSuccessOrFail()) {
232                                 didWriteAttrb.set(true);
233                         }//if
234                 }//else if
235         }
236
237         public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
238                 callbackList.add(_callbackTo);
239         }
240 }