Adding config file for sharing.
[iot2.git] / benchmarks / other / DoorlockAndOutlet / OutletSensor.java
1 //package iotcode.OutletSensor;
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 OutletSensor implements IoTZigbeeCallback, SmartthingsSensor {
25
26         private final int TIMEOUT_FOR_RESEND_MSEC = 900;
27
28         private IoTZigbee zigConnection = null;
29
30         private float Watts = 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);/////////yuting
42         private AtomicBoolean didConfigureReporting = new AtomicBoolean(false);/////////////yuting
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> OutletSensorUdpAddress; //
50         @config private IoTSet<IoTZigbeeAddress> OutletSensorZigbeeAddress;//
51
52         public OutletSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
53                 OutletSensorUdpAddress = dSet;
54                 OutletSensorZigbeeAddress = zigSet;
55         }
56
57         public OutletSensor() {
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 = OutletSensorUdpAddress.iterator();
70                         Iterator itrZig = OutletSensorZigbeeAddress.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 = OutletSensorUdpAddress.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 = OutletSensorZigbeeAddress.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                         sleep(10);
94
95                         //System.out.println("Sending BroadcastingRouteRecordRequest");
96                         //zigConnection.sendBroadcastingRouteRecordRequest(0x0001);
97                         
98
99                         System.out.println("Sending Management Permit Joining Request");
100                         for(int z=0; z<3; z++){
101                                 zigConnection.sendManagementPermitJoiningRequest(0x0002, 0x0036, 0x00);
102                                 sleep(0);
103                         }
104
105                         //made by yuting
106                         while(!didBind.get()){
107                                 System.out.println("Sending Bind Request");
108                                 zigConnection.sendBindRequest(0x0003,0x0B04, 0x01);// 0x0021, 0x00);// 0x0B04, 0x01);
109                                 sleep(0);
110                         }
111                         
112
113                         //made by yuting
114                         while(!didConfigureReporting.get()){
115                                 System.out.println("Sending Configure Reporting");
116                                 byte [] reportableChange= {0x0005};
117                                 zigConnection.sendConfigureReportingCommand(0x0004, 0x0B04, 0x0104, 0x01, 0x050b, 0x29, 0x0001, 0x0500, reportableChange); 
118                                 sleep(0);
119                         }
120                         
121
122
123                 } catch (Exception e) {
124                         e.printStackTrace();
125                 }
126         }
127
128         //made by changwoo
129         private void sleep(int multipleTime){
130                 if(multipleTime<=0){
131                         multipleTime=1;
132                 }
133                 try{
134                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
135                 } catch(Exception e){
136                         e.printStackTrace();
137                 }
138         }
139         //made by yuting
140         public float getWatts() {
141
142                 float tmp = 0;
143                 try {
144                         gettingLatestDataMutex.acquire();
145                         tmp = Watts;
146
147                 } catch (Exception e) {
148                         e.printStackTrace();
149                 }
150                 gettingLatestDataMutex.release();
151
152                 return tmp;
153         }
154         //made by yuting
155         public void TurnOn(){
156         try {
157                         System.out.println("the outlet sensor is turning on");
158                         zigConnection.sendChangeSwtichRequest(0x0005, 0x0006, 0x0104, 1, 0x01);
159                 }  catch(Exception e){
160                         e.printStackTrace();
161                 }
162         }
163         //made by yuting
164         public void TurnOff(){
165         try {
166                         System.out.println("the outlet sensor is turning off");
167                         zigConnection.sendChangeSwtichRequest(0x0005, 0x0006, 0x0104, 0, 0x01);
168                 }  catch(Exception e){
169                         e.printStackTrace();
170                 }
171         }
172
173         public void close() {
174
175                 if (didAlreadyClose.compareAndSet(false, true) == false) {
176                         return; // already init
177                 }
178
179                 didAlreadyInit.set(false);
180
181
182                 try {
183                         zigConnection.close();
184                 } catch (Exception e) {
185                         e.printStackTrace();
186                 }
187         }
188
189         public void Finalize() {
190                 if (!didClose) {
191                         close();
192                 }
193         }
194
195         public void setId(int id) {
196
197                 sensorId = id;
198
199         }
200
201         public int getId() {
202
203                 return sensorId;
204
205         }
206
207
208         public long getTimestampOfLastReading() {
209
210                 Date tmp = null;
211                 try {
212                         gettingLatestDataMutex.acquire();
213                         tmp = (Date)timestampOfLastDetecting.clone();
214
215                 } catch (Exception e) {
216                         e.printStackTrace();
217                 }
218                 gettingLatestDataMutex.release();
219                 long retLong = tmp.getTime();
220
221                 return retLong;
222         }
223
224         public void newMessageAvailable(IoTZigbeeMessage _zm) {
225                 
226                 //made by yuting
227                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
228                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
229                         if (message.getSucceeded()) {
230                                 didBind.set(true);
231                         }
232                 }
233                 //made by yuting
234                 else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse){
235                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
236                         if (message.getAllSuccess()) {
237                                 didConfigureReporting.set(true);
238                         }
239                 }
240                 //made by yuting
241                 else if (_zm instanceof IoTZigbeeMessageZclChangeSwitchResponse){
242                         IoTZigbeeMessageZclChangeSwitchResponse message = (IoTZigbeeMessageZclChangeSwitchResponse)_zm;
243                         if (message.getSuccessOrFail()) {
244                                 if (message.getStatus()==1){
245                                 System.out.println("change on/off response: turned on"); 
246                                 //System.out.println(message.getStatus());   
247                                 }
248                                 else if(message.getStatus()==0){
249                                 System.out.println("change on/off response: turned off");
250                                 }
251                         }
252                 }
253                 //made by yuting
254                 else if (_zm instanceof IoTZigbeeMessageZclReportAttributes) {
255                         IoTZigbeeMessageZclReportAttributes message = (IoTZigbeeMessageZclReportAttributes)_zm;
256                         List <IoTZigbeeMessageZclReportAttributes.Attribute> attrList = message.getAttributes();
257
258                         if (attrList.size() == 1) {
259                                 if(attrList.get(0).getAttributeId() == 2821) {
260                                         byte[] data = attrList.get(0).getData();
261                                         int value = (data[0] * 256) + data[1];
262
263                                         try {
264                                                 gettingLatestDataMutex.acquire();
265                                                 Watts = (float)value;
266                                                 timestampOfLastDetecting = new Date();
267                                         } catch (Exception e) {
268                                                 e.printStackTrace();
269                                         }
270                                         gettingLatestDataMutex.release();
271
272                                         try {
273                                                 for (SmartthingsSensorCallback cb : callbackList) {
274                                                         cb.newReadingAvailable(this);
275                                                 }
276                                         } catch (Exception e) {
277                                                 e.printStackTrace();
278                                         }
279                                 }       
280                         }
281                 }
282         }
283
284         //public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
285         public void registerCallback(SmartthingsSensorCallback _callbackTo) {
286                 callbackList.add(_callbackTo);
287         }
288 }