1 package iotcode.MotionSensor;
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 iotchecker.qual.*;
11 import iotcode.annotation.*;
14 import iotruntime.slave.*;
15 import iotcode.interfaces.*;
16 import iotruntime.zigbee.*;
18 /** Class Smartthings sensor driver for Smartthings sensor devices.
20 * @author Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
24 public class MotionSensor implements IoTZigbeeCallback, SmartthingsSensor {
26 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
28 private IoTZigbee zigConnection = null;
29 private boolean didClose; // make sure that the clean up was done correctly
30 private boolean detectStatus = false;
32 private int detectedValue = 0;
33 private Date timestampOfLastDetecting = null;
35 private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
36 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
37 private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
38 private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
39 static Semaphore gettingLatestDataMutex = new Semaphore(1);
41 private List < SmartthingsSensorSmartCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorSmartCallback > ();
43 private int sensorId = 0;
45 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
46 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
48 public MotionSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
49 //devUdpAddress = dSet;
50 //devZigbeeAddress = zigSet;
55 if (didAlreadyInit.compareAndSet(false, true) == false) {
56 return; // already init
59 didAlreadyClose.set(false);
62 Iterator itrUdp = devUdpAddress.iterator();
63 Iterator itrZig = devZigbeeAddress.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 = devUdpAddress.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 = devZigbeeAddress.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!");
87 System.out.println("Sending Management Permit Joining Request");
88 for(int z=0; z<3; z++){
89 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
94 while (!didWriteAttrb.get()) {
95 System.out.println("Sending Write Attribute Request");
96 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
101 System.out.println("Sending Enrollment Reponse");
102 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
105 } catch (Exception e) {
111 private void sleep(int multipleTime){
116 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
117 } catch(Exception e){
122 public void close() {
124 if (didAlreadyClose.compareAndSet(false, true) == false) {
125 return; // already init
128 didAlreadyInit.set(false);
132 zigConnection.close();
133 } catch (Exception e) {
139 public void Finalize() {
145 public void setId(int id) {
157 public int getValue() {
161 gettingLatestDataMutex.acquire();
164 } catch (Exception e) {
167 gettingLatestDataMutex.release();
173 // - 24 = no motion = false
174 // - 26 = motion = true
175 // After getting 26, if there is no motion for ~12 seconds then we get back 24
176 public boolean isActiveValue() {
178 int tmp = getValue();
181 else // Getting 24 here
182 detectStatus = false;
187 public long getTimestampOfLastReading() {
191 gettingLatestDataMutex.acquire();
192 tmp = (Date)timestampOfLastDetecting.clone();
194 } catch (Exception e) {
197 gettingLatestDataMutex.release();
198 long retLong = tmp.getTime();
203 public void newMessageAvailable(IoTZigbeeMessage _zm) {
206 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
207 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
208 if(message.getSuccessOrFail()){
212 gettingLatestDataMutex.acquire();
213 detectedValue = message.getStatus();
214 timestampOfLastDetecting = new Date();
215 } catch (Exception e) {
218 gettingLatestDataMutex.release();
220 for (SmartthingsSensorSmartCallback cb : callbackList) {
221 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
223 } catch (Exception e) {
229 } else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
230 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
231 if (message.getSuccessOrFail()) {
232 didWriteAttrb.set(true);
237 public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
238 callbackList.add(_callbackTo);