1 package iotcode.SpruceSensor;
3 // Standard Java Packages
4 import java.util.Iterator;
6 import java.util.ArrayList;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.concurrent.CopyOnWriteArrayList;
10 import java.util.concurrent.Semaphore;
13 import iotruntime.slave.*;
14 import iotcode.interfaces.MoistureSensor;
15 import iotcode.interfaces.MoistureSensorSmartCallback;
16 import iotruntime.zigbee.*;
17 import iotcode.annotation.*;
19 public class SpruceSensor implements IoTZigbeeCallback, MoistureSensor {
21 private final int TIMEOUT_FOR_RESEND_MSEC = 1000;
23 private IoTZigbee zigConnection = null;
24 private boolean didClose; // make sure that the clean up was done correctly
26 private float humidity = 0;
27 private Date timestampOfLastHumidity = null;
29 private AtomicBoolean didBind = new AtomicBoolean(false);
30 private AtomicBoolean didConfigureReporting = new AtomicBoolean(false);
31 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
32 private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
33 static Semaphore gettingLatestDataMutex = new Semaphore(1);
35 private List < MoistureSensorSmartCallback > callbackList = new CopyOnWriteArrayList < MoistureSensorSmartCallback > ();
37 private int sensorId = 0;
39 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
40 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
42 public SpruceSensor() {
47 if (didAlreadyInit.compareAndSet(false, true) == false) {
48 return; // already init
51 didAlreadyClose.set(false);
54 Iterator itrUdp = devUdpAddress.iterator();
55 Iterator itrZig = devZigbeeAddress.iterator();
57 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
60 System.out.println("DEBUG: Allocate iterators to print out addresses!");
61 Iterator itrDebugUdp = devUdpAddress.iterator();
62 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
63 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
64 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
65 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
67 Iterator itrDebugZig = devZigbeeAddress.iterator();
68 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
69 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
71 zigConnection.registerCallback(this);
72 System.out.println("Register callback!");
74 System.out.println("Initialized!");
76 while (!didBind.get()) {
77 zigConnection.sendBindRequest(0x0001, 0x0405, 0x01);
79 System.out.println("Sending bind request!");
80 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
81 } catch (Exception e) {
86 while (!didConfigureReporting.get()) {
87 zigConnection.sendConfigureReportingCommand(0x0001, 0x0405, 0x0104, 0x01, 0x0000, 0x21, 0x0001, 0x0001, null);
89 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
90 } catch (Exception e) {
94 } catch (Exception e) {
101 if (didAlreadyClose.compareAndSet(false, true) == false) {
102 return; // already init
105 didAlreadyInit.set(false);
109 zigConnection.close();
110 } catch (Exception e) {
115 public void Finalize() {
121 public void setId(int id) {
133 public float getMoisture() {
137 gettingLatestDataMutex.acquire();
140 } catch (Exception e) {
143 gettingLatestDataMutex.release();
148 public long getTimestampOfLastReading() {
152 gettingLatestDataMutex.acquire();
153 tmp = (Date)timestampOfLastHumidity.clone();
155 } catch (Exception e) {
158 gettingLatestDataMutex.release();
159 long retLong = tmp.getTime();
164 public void newMessageAvailable(IoTZigbeeMessage _zm) {
166 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
167 IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
168 if (message.getSucceeded()) {
172 } else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse) {
173 IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
174 if (message.getAllSuccess()) {
175 didConfigureReporting.set(true);
178 } else if (_zm instanceof IoTZigbeeMessageZclReportAttributes) {
179 IoTZigbeeMessageZclReportAttributes message = (IoTZigbeeMessageZclReportAttributes)_zm;
180 List <IoTZigbeeMessageZclReportAttributes.Attribute> attrList = message.getAttributes();
182 if (attrList.size() == 1) {
183 if (attrList.get(0).getAttributeId() == 0) {
184 byte[] data = attrList.get(0).getData();
186 int value = (data[0] * 256) + data[1];
189 gettingLatestDataMutex.acquire();
190 humidity = (float)value / (float)100.0;
191 timestampOfLastHumidity = new Date();
192 } catch (Exception e) {
195 gettingLatestDataMutex.release();
198 for (MoistureSensorSmartCallback cb : callbackList) {
199 cb.newReadingAvailable(this.getId(), this.getMoisture(), this.getTimestampOfLastReading());
201 } catch (Exception e) {
210 public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
211 callbackList.add(_callbackTo);