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