1 package iotcode.WaterLeakSensor;
3 // Standard Java Packages
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.concurrent.CopyOnWriteArrayList;
7 import java.util.concurrent.Semaphore;
10 import iotcode.annotation.*;
11 import iotruntime.slave.*;
12 import iotcode.interfaces.*;
13 import iotruntime.zigbee.*;
15 /** Class Smartthings sensor driver for Smartthings sensor devices.
17 * @author Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
21 public class WaterLeakSensor implements IoTZigbeeCallback, SmartthingsSensor {
23 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
25 private IoTZigbee zigConnection = null;
26 private boolean didClose; // make sure that the clean up was done correctly
27 private boolean detectStatus = false;
29 private int detectedValue = 0;
30 private Date timestampOfLastDetecting = null;
32 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
33 private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
34 private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
35 private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
36 static Semaphore gettingLatestDataMutex = new Semaphore(1);
38 private List < SmartthingsSensorSmartCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorSmartCallback > ();
40 private int sensorId = 0;
42 @config private IoTSet<IoTDeviceAddress> waterleakSensorUdpAddress;
43 @config private IoTSet<IoTZigbeeAddress> waterleakSensorZigbeeAddress;
45 // TODO: Test constructor
46 //public WaterLeakSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
47 //waterleakSensorUdpAddress = dSet;
48 //waterleakSensorZigbeeAddress = zigSet;
50 public WaterLeakSensor() {
55 if (didAlreadyInit.compareAndSet(false, true) == false) {
56 return; // already init
59 didAlreadyClose.set(false);
62 Iterator itrUdp = waterleakSensorUdpAddress.iterator();
63 Iterator itrZig = waterleakSensorZigbeeAddress.iterator();
65 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
68 System.out.println("DEBUG: Allocate iterators to print out addresses!");
69 Iterator itrDebugUdp = waterleakSensorUdpAddress.iterator();
70 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
71 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
72 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
73 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
75 Iterator itrDebugZig = waterleakSensorZigbeeAddress.iterator();
76 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
77 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
79 zigConnection.registerCallback(this);
80 System.out.println("Register callback!");
82 System.out.println("Initialized!");
86 System.out.println("Sending Management Permit Joining Request");
87 for(int z=0; z<3; z++){
88 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
93 while (!didWriteAttrb.get()) {
94 System.out.println("Sending Write Attribute Request");
95 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
100 System.out.println("Sending Enrollment Reponse");
101 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
104 } catch (Exception e) {
110 private void sleep(int multipleTime){
115 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
116 } catch(Exception e){
121 public void close() {
123 if (didAlreadyClose.compareAndSet(false, true) == false) {
124 return; // already init
127 didAlreadyInit.set(false);
131 zigConnection.close();
132 } catch (Exception e) {
137 public void Finalize() {
143 public void setId(int id) {
155 public int getValue() {
159 gettingLatestDataMutex.acquire();
162 } catch (Exception e) {
165 gettingLatestDataMutex.release();
171 // - 24 = no leak = false
172 // - 25 = leak = true
173 public boolean isActiveValue() {
175 int tmp = getValue();
178 else // Getting 24 here
179 detectStatus = false;
184 public long getTimestampOfLastReading() {
188 gettingLatestDataMutex.acquire();
189 tmp = (Date)timestampOfLastDetecting.clone();
191 } catch (Exception e) {
194 gettingLatestDataMutex.release();
195 long retLong = tmp.getTime();
200 public void newMessageAvailable(IoTZigbeeMessage _zm) {
203 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
204 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
205 if(message.getSuccessOrFail()){
209 gettingLatestDataMutex.acquire();
210 detectedValue = message.getStatus();
211 timestampOfLastDetecting = new Date();
212 } catch (Exception e) {
215 gettingLatestDataMutex.release();
217 for (SmartthingsSensorSmartCallback cb : callbackList) {
218 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
220 } catch (Exception e) {
227 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
228 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
229 if (message.getSuccessOrFail()) {
230 didWriteAttrb.set(true);
235 public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
236 callbackList.add(_callbackTo);