1 // Standard Java Packages
3 import java.util.concurrent.atomic.AtomicBoolean;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.Semaphore;
8 //import iotchecker.qual.*;
9 //import iotcode.annotation.*;
12 import iotruntime.slave.*;
13 import iotcode.interfaces.*;
14 import iotruntime.zigbee.*;
16 /** Class Smartthings sensor driver for Smartthings sensor devices.
18 * @author Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
22 public class WaterLeakSensor implements IoTZigbeeCallback, SmartthingsSensor {
24 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
26 private IoTZigbee zigConnection = null;
27 private boolean didClose; // make sure that the clean up was done correctly
28 private boolean detectStatus = false;
30 private int detectedValue = 0;
31 private Date timestampOfLastDetecting = null;
33 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
34 private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
35 private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
36 private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
37 static Semaphore gettingLatestDataMutex = new Semaphore(1);
39 private List < SmartthingsSensorCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorCallback > ();
41 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
42 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
44 public WaterLeakSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
46 devZigbeeAddress = zigSet;
51 if (didAlreadyInit.compareAndSet(false, true) == false) {
52 return; // already init
55 didAlreadyClose.set(false);
58 Iterator itrUdp = devUdpAddress.iterator();
59 Iterator itrZig = devZigbeeAddress.iterator();
61 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
64 System.out.println("DEBUG: Allocate iterators to print out addresses!");
65 Iterator itrDebugUdp = devUdpAddress.iterator();
66 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
67 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
68 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
69 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
71 Iterator itrDebugZig = devZigbeeAddress.iterator();
72 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
73 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
75 zigConnection.registerCallback(this);
76 System.out.println("Register callback!");
78 System.out.println("Initialized!");
82 System.out.println("Sending Management Permit Joining Request");
83 for(int z=0; z<3; z++){
84 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
89 while (!didWriteAttrb.get()) {
90 System.out.println("Sending Write Attribute Request");
91 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
96 System.out.println("Sending Enrollment Reponse");
97 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
100 } catch (Exception e) {
106 private void sleep(int multipleTime){
111 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
112 } catch(Exception e){
117 public void close() {
119 if (didAlreadyClose.compareAndSet(false, true) == false) {
120 return; // already init
123 didAlreadyInit.set(false);
127 zigConnection.close();
128 } catch (Exception e) {
133 public void Finalize() {
139 public int getValue() {
143 gettingLatestDataMutex.acquire();
146 } catch (Exception e) {
149 gettingLatestDataMutex.release();
155 // - 24 = no leak = false
156 // - 25 = leak = true
157 public boolean isActiveValue() {
159 int tmp = getValue();
162 else // Getting 24 here
163 detectStatus = false;
168 public long getTimestampOfLastReading() {
172 gettingLatestDataMutex.acquire();
173 tmp = (Date)timestampOfLastDetecting.clone();
175 } catch (Exception e) {
178 gettingLatestDataMutex.release();
179 long retLong = tmp.getTime();
184 public void newMessageAvailable(IoTZigbeeMessage _zm) {
187 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
188 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
189 if(message.getSuccessOrFail()){
193 gettingLatestDataMutex.acquire();
194 detectedValue = message.getStatus();
195 timestampOfLastDetecting = new Date();
196 } catch (Exception e) {
199 gettingLatestDataMutex.release();
201 for (SmartthingsSensorCallback cb : callbackList) {
202 cb.newReadingAvailable(this.getValue(), this.isActiveValue());
204 } catch (Exception e) {
211 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
212 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
213 if (message.getSuccessOrFail()) {
214 didWriteAttrb.set(true);
219 public void registerCallback(SmartthingsSensorCallback _callbackTo) {
220 callbackList.add(_callbackTo);