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