Cleaning up benchmarks and drivers code.
[iot2.git] / benchmarks / drivers / Java / DoorlockSensor / 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 // IoT Packages
10 import iotcode.annotation.*;
11 import iotruntime.slave.*;
12 import iotcode.interfaces.*;
13 import iotruntime.zigbee.*;
14
15 /** Class Smartthings sensor driver for Smartthings sensor devices.
16  *
17  * @author      Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
18  * @version     1.0
19  * @since       2016-12-01
20  */
21 public class DoorlockSensor implements IoTZigbeeCallback, SmartthingsSensor {
22
23         private final int TIMEOUT_FOR_RESEND_MSEC = 900;
24
25         private IoTZigbee zigConnection = null;
26         private boolean didClose; // make sure that the clean up was done correctly
27         private boolean detectStatus = false;
28
29         private int detectedValue = 0;
30         private Date timestampOfLastDetecting = null;
31
32         private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
33         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
34         private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
35         private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
36         private AtomicBoolean didBind = new AtomicBoolean(false);
37         private AtomicBoolean didDoorLockConfigureReporting = new AtomicBoolean(false); // made by Jiawei
38         static Semaphore gettingLatestDataMutex = new Semaphore(1);
39
40         private List < SmartthingsSensorSmartCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorSmartCallback > ();
41
42         private int sensorId = 0;
43
44         @config private IoTSet<IoTDeviceAddress> DoorlockSensorUdpAddress;
45         @config private IoTSet<IoTZigbeeAddress> DoorlockSensorZigbeeAddress;
46
47         public DoorlockSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
48                 DoorlockSensorUdpAddress = dSet;
49                 DoorlockSensorZigbeeAddress = zigSet;
50         }
51
52         public DoorlockSensor() {
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 = DoorlockSensorUdpAddress.iterator();
65                         Iterator itrZig = DoorlockSensorZigbeeAddress.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 = DoorlockSensorUdpAddress.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 = DoorlockSensorZigbeeAddress.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
87             
88                         //made by changwoo
89                         sleep(10);
90
91                         System.out.println("Sending Management Permit Joining Request");
92                         // TODO: Might/might not need to send this 3 times
93                         // for(int z=0; z<3; z++){
94                                 zigConnection.sendManagementPermitJoiningRequest(0x0002, 0x0036, 0x00);
95                                 sleep(0);
96                         // }
97
98
99             while(!didBind.get()){
100                                 System.out.println("Sending Bind Request");
101                                 zigConnection.sendBindRequest(0x0003, 0x0101, 0x02);
102                                 sleep(0);
103                         }
104                         
105                         while(!didDoorLockConfigureReporting.get()){
106                                 System.out.println("Sending Door Lock: Configure Reporting");
107                                 zigConnection.sendConfigureReportingCommand(0x0004, 0x0101, 0x0104, 0x01, 0x02, 0x0000, 0x30, 0x0000, 0x100E, null);
108                                 sleep(0);
109                         }
110
111                         while(true){
112                                 Scanner in = new Scanner(System.in);
113                                 System.out.println("\nUnlock door: 0");
114                                 System.out.println("Lock door: 1");
115                                 System.out.println("Read status: 2 (or anything else)");
116                                 String str = in.next();
117                                 if(str.equals("1")) {
118                                         System.out.println("the doorlock sensor is locking");
119                                         zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 0);
120                                         sleep(0);
121                                 }else if(str.equals("0")){
122                                         System.out.println("the doorlock sensor is unlocking");
123                                         zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 1);
124                                         sleep(0);
125                                 }else{
126                                         System.out.println("Let's see the doorlock sensor's status currently");
127                                         zigConnection.sendReadDoorStatusRequest(0x0005, 0x0101, 0x0104, 0x02, 0x10, 0x00, 0x0000);
128                                         sleep(0);
129                                 }
130                         }
131
132
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                 }
136         }
137
138         // made by Changwoo
139         private void sleep(int multipleTime){
140                 if(multipleTime<=0){
141                         multipleTime=1;
142                 }
143                 try{
144                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
145                 } catch(Exception e){
146                         e.printStackTrace();
147                 }
148         }
149
150         // made by Jiawei
151         public int getValue() {
152                 int tmp = 0;
153
154                 try {
155                         gettingLatestDataMutex.acquire();
156                         tmp = detectedValue;
157
158                 } catch (Exception e) {
159                         e.printStackTrace();
160                 }
161                 gettingLatestDataMutex.release();
162
163                 return tmp;
164         }
165
166         public boolean isActiveValue() {
167
168                 int tmp = getValue();
169                 if (tmp == 1)
170                         detectStatus = true;    // Door is locked
171                 else
172                         detectStatus = false;   // Door is not locked/not fully locked
173                 return detectStatus;
174         }
175
176         public void close() {
177
178                 if (didAlreadyClose.compareAndSet(false, true) == false) {
179                         return; // already init
180                 }
181
182                 didAlreadyInit.set(false);
183
184
185                 try {
186                         zigConnection.close();
187                 } catch (Exception e) {
188                         e.printStackTrace();
189                 }
190         }
191
192         public void Finalize() {
193                 if (!didClose) {
194                         close();
195                 }
196         }
197
198         public void setId(int id) {
199
200                 sensorId = id;
201
202         }
203
204         public int getId() {
205
206                 return sensorId;
207
208         }
209
210
211         public long getTimestampOfLastReading() {
212
213                 Date tmp = null;
214                 try {
215                         gettingLatestDataMutex.acquire();
216                         tmp = (Date)timestampOfLastDetecting.clone();
217
218                 } catch (Exception e) {
219                         e.printStackTrace();
220                 }
221                 gettingLatestDataMutex.release();
222                 long retLong = tmp.getTime();
223
224                 return retLong;
225         }
226
227         public void newMessageAvailable(IoTZigbeeMessage _zm) {
228                 
229                 // made by Yuting
230                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
231                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
232                         if (message.getSucceeded()) {
233                                 didBind.set(true);
234                         }
235                 }
236                 else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse){
237                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
238                         if (message.getAllSuccess()) {
239                                 didDoorLockConfigureReporting.set(true);
240                         }
241                 }
242                 else if (_zm instanceof IoTZigbeeMessageZclReadAttributesResponse) {
243                         IoTZigbeeMessageZclReadAttributesResponse message = (IoTZigbeeMessageZclReadAttributesResponse)_zm;
244                         List <IoTZigbeeMessageZclReadAttributesResponse.Attribute> attrList = message.getAttributes();
245
246                         if (attrList.size() == 1) {
247                                 if(attrList.get(0).getAttributeId() == 0) {
248                                         byte[] data = attrList.get(0).getData();
249                                         int value = data[0];
250
251                                         try {
252                                                 gettingLatestDataMutex.acquire();
253                                                 detectedValue = value;
254                                                 timestampOfLastDetecting = new Date();
255                                         } catch (Exception e) {
256                                                 e.printStackTrace();
257                                         }
258                                         gettingLatestDataMutex.release();
259
260                                         try {
261                                                 for (SmartthingsSensorSmartCallback cb : callbackList) {
262                                                         cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
263                                                 }
264                                         } catch (Exception e) {
265                                                 e.printStackTrace();
266                                         }
267                                 }       
268                         }
269                 }
270         }
271
272         public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
273                 callbackList.add(_callbackTo);
274         }
275 }