Adding changes and files for doorlock driver
[iot2.git] / benchmarks / other / DoorlockAndOutlet / DoorlockSensor.java
1 //package iotcode.DoorlockSensor;
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 DoorlockSensor 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 didAlreadyClose = new AtomicBoolean(true);
36         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
37         private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
38         private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
39         private AtomicBoolean didBind = new AtomicBoolean(false);
40         private AtomicBoolean didDoorLockConfigureReporting = new AtomicBoolean(false); //made by Jiawei
41         static Semaphore gettingLatestDataMutex = new Semaphore(1);
42
43         private List < SmartthingsSensorCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorCallback > ();
44
45         private int sensorId = 0;
46
47         @config private IoTSet<IoTDeviceAddress> DoorlockSensorUdpAddress; //
48         @config private IoTSet<IoTZigbeeAddress> DoorlockSensorZigbeeAddress;//
49
50         public DoorlockSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
51                 DoorlockSensorUdpAddress = dSet;
52                 DoorlockSensorZigbeeAddress = zigSet;
53         }
54
55         public DoorlockSensor() {
56         }
57
58         public void init() {
59
60                 if (didAlreadyInit.compareAndSet(false, true) == false) {
61                         return; // already init
62                 }
63
64                 didAlreadyClose.set(false);
65
66                 try {
67                         Iterator itrUdp = DoorlockSensorUdpAddress.iterator();
68                         Iterator itrZig = DoorlockSensorZigbeeAddress.iterator();
69
70                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
71
72                         // DEBUG
73                         System.out.println("DEBUG: Allocate iterators to print out addresses!");
74                         Iterator itrDebugUdp = DoorlockSensorUdpAddress.iterator();
75                         IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
76                         System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
77                         System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
78                         System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
79
80                         Iterator itrDebugZig = DoorlockSensorZigbeeAddress.iterator();
81                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
82                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
83
84                         zigConnection.registerCallback(this);
85                         System.out.println("Register callback!");
86                         zigConnection.init();
87                         System.out.println("Initialized!");
88
89
90             
91                         //made by changwoo
92                         sleep(10);
93
94             // System.out.println("BroadcastingRouteRecordRequest ");
95                         // zigConnection.sendBroadcastingRouteRecordRequest(0x0001);
96             // sleep(6);
97
98                         System.out.println("Sending Management Permit Joining Request");
99                         // for(int z=0; z<3; z++){
100                                 zigConnection.sendManagementPermitJoiningRequest(0x0002, 0x0036, 0x00);
101                                 sleep(0);
102                         // }
103
104
105             while(!didBind.get()){
106                                 System.out.println("Sending Bind Request");
107                                 zigConnection.sendBindRequest(0x0003, 0x0101, 0x02);
108                                 sleep(0);
109                         }
110                         
111                         while(!didDoorLockConfigureReporting.get()){
112                                 System.out.println("Sending Door Lock: Configure Reporting");
113                                 zigConnection.sendConfigureReportingCommand(0x0004, 0x0101, 0x0104, 0x01, 0x02, 0x0000, 0x30, 0x0000, 0x100E, null);
114                                 sleep(0);
115                         }
116
117                         while(true){
118                                 Scanner in = new Scanner(System.in);
119                                 System.out.println("\nUnlock door: 0");
120                                 System.out.println("Lock door: 1");
121                                 System.out.println("Read status: 2 (or anything else)");
122                                 String str = in.next();
123                                 if(str.equals("1")) {
124                                         System.out.println("the doorlock sensor is locking");
125                                         zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 0);
126                                         sleep(0);
127                                 }else if(str.equals("0")){
128                                         System.out.println("the doorlock sensor is unlocking");
129                                         zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 1);
130                                         sleep(0);
131                                 }else{
132                                         System.out.println("Let's see the doorlock sensor's status currently");
133                                         zigConnection.sendReadDoorStatusRequest(0x0005, 0x0101, 0x0104, 0x02, 0x10, 0x00, 0x0000);
134                                         sleep(0);
135                                 }
136                         }
137
138
139                 } catch (Exception e) {
140                         e.printStackTrace();
141                 }
142         }
143
144         //made by changwoo
145         private void sleep(int multipleTime){
146                 if(multipleTime<=0){
147                         multipleTime=1;
148                 }
149                 try{
150                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
151                 } catch(Exception e){
152                         e.printStackTrace();
153                 }
154         }
155
156         // made by Jiawei
157     //public int getStatus() {
158         public int getValue() {
159                 int tmp = 0;
160
161                 try {
162                         gettingLatestDataMutex.acquire();
163                         tmp = detectedValue;
164
165                 } catch (Exception e) {
166                         e.printStackTrace();
167                 }
168                 gettingLatestDataMutex.release();
169
170                 return tmp;
171         }
172
173         public boolean isActiveValue() {
174
175                 int tmp = getValue();
176                 if (tmp == 1)
177                         detectStatus = true;    // Door is locked
178                 else
179                         detectStatus = false;   // Door is not locked/not fully locked
180                 return detectStatus;
181         }
182
183         public void close() {
184
185                 if (didAlreadyClose.compareAndSet(false, true) == false) {
186                         return; // already init
187                 }
188
189                 didAlreadyInit.set(false);
190
191
192                 try {
193                         zigConnection.close();
194                 } catch (Exception e) {
195                         e.printStackTrace();
196                 }
197         }
198
199         public void Finalize() {
200                 if (!didClose) {
201                         close();
202                 }
203         }
204
205         public void setId(int id) {
206
207                 sensorId = id;
208
209         }
210
211         public int getId() {
212
213                 return sensorId;
214
215         }
216
217
218         public long getTimestampOfLastReading() {
219
220                 Date tmp = null;
221                 try {
222                         gettingLatestDataMutex.acquire();
223                         tmp = (Date)timestampOfLastDetecting.clone();
224
225                 } catch (Exception e) {
226                         e.printStackTrace();
227                 }
228                 gettingLatestDataMutex.release();
229                 long retLong = tmp.getTime();
230
231                 return retLong;
232         }
233
234         public void newMessageAvailable(IoTZigbeeMessage _zm) {
235                 
236                 //made by yuting
237                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
238                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
239                         if (message.getSucceeded()) {
240                                 didBind.set(true);
241                         }
242                 }
243                 else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse){
244                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
245                         if (message.getAllSuccess()) {
246                                 didDoorLockConfigureReporting.set(true);
247                         }
248                 }
249                 else if (_zm instanceof IoTZigbeeMessageZclReadAttributesResponse) {
250                         IoTZigbeeMessageZclReadAttributesResponse message = (IoTZigbeeMessageZclReadAttributesResponse)_zm;
251                         List <IoTZigbeeMessageZclReadAttributesResponse.Attribute> attrList = message.getAttributes();
252
253                         if (attrList.size() == 1) {
254                                 if(attrList.get(0).getAttributeId() == 0) {
255                                         byte[] data = attrList.get(0).getData();
256                                         int value = data[0];
257
258                                         try {
259                                                 gettingLatestDataMutex.acquire();
260                                                 detectedValue = value;
261                                                 timestampOfLastDetecting = new Date();
262                                         } catch (Exception e) {
263                                                 e.printStackTrace();
264                                         }
265                                         gettingLatestDataMutex.release();
266
267                                         try {
268                                                 for (SmartthingsSensorCallback cb : callbackList) {
269                                                         cb.newReadingAvailable(this.getValue(), this.isActiveValue());
270                                                 }
271                                         } catch (Exception e) {
272                                                 e.printStackTrace();
273                                         }
274                                 }       
275                         }
276                 }
277         }
278
279         //public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
280         public void registerCallback(SmartthingsSensorCallback _callbackTo) {
281                 callbackList.add(_callbackTo);
282         }
283 }