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 MotionSensor 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 didAlreadyClose = new AtomicBoolean(true);
34 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
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 MotionSensor(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!");
83 System.out.println("Sending Management Permit Joining Request");
84 for(int z=0; z<3; z++){
85 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
90 while (!didWriteAttrb.get()) {
91 System.out.println("Sending Write Attribute Request");
92 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
97 System.out.println("Sending Enrollment Reponse");
98 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
101 } catch (Exception e) {
107 private void sleep(int multipleTime){
112 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
113 } catch(Exception e){
118 public void close() {
120 if (didAlreadyClose.compareAndSet(false, true) == false) {
121 return; // already init
124 didAlreadyInit.set(false);
128 zigConnection.close();
129 } catch (Exception e) {
135 public void Finalize() {
141 public int getValue() {
145 gettingLatestDataMutex.acquire();
148 } catch (Exception e) {
151 gettingLatestDataMutex.release();
157 // - 24 = no motion = false
158 // - 26 = motion = true
159 // After getting 26, if there is no motion for ~12 seconds then we get back 24
160 public boolean isActiveValue() {
162 int tmp = getValue();
165 else // Getting 24 here
166 detectStatus = false;
171 public long getTimestampOfLastReading() {
175 gettingLatestDataMutex.acquire();
176 tmp = (Date)timestampOfLastDetecting.clone();
178 } catch (Exception e) {
181 gettingLatestDataMutex.release();
182 long retLong = tmp.getTime();
187 public void newMessageAvailable(IoTZigbeeMessage _zm) {
190 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
191 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
192 if(message.getSuccessOrFail()){
196 gettingLatestDataMutex.acquire();
197 detectedValue = message.getStatus();
198 timestampOfLastDetecting = new Date();
199 } catch (Exception e) {
202 gettingLatestDataMutex.release();
204 for (SmartthingsSensorCallback cb : callbackList) {
205 cb.newReadingAvailable(this.getValue(), this.isActiveValue());
207 } catch (Exception e) {
213 } else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
214 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
215 if (message.getSuccessOrFail()) {
216 didWriteAttrb.set(true);
221 public void registerCallback(SmartthingsSensorCallback _callbackTo) {
222 callbackList.add(_callbackTo);