Adding config file for sharing.
[iot2.git] / benchmarks / drivers / Java / SpruceSensor / SpruceSensor.java
1 package iotcode.SpruceSensor;
2
3 // Standard Java Packages
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.concurrent.CopyOnWriteArrayList;
10 import java.util.concurrent.Semaphore;
11
12 // IoT Packages
13 import iotruntime.slave.*;
14 import iotcode.interfaces.MoistureSensor;
15 import iotcode.interfaces.MoistureSensorSmartCallback;
16 import iotruntime.zigbee.*;
17 import iotcode.annotation.*;
18
19 public class SpruceSensor implements IoTZigbeeCallback, MoistureSensor {
20
21         private final int TIMEOUT_FOR_RESEND_MSEC = 1000;
22
23         private IoTZigbee zigConnection = null;
24         private boolean didClose;                                                                       // make sure that the clean up was done correctly
25
26         private float humidity = 0;
27         private Date timestampOfLastHumidity = null;
28
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);
34
35         private List < MoistureSensorSmartCallback > callbackList = new CopyOnWriteArrayList < MoistureSensorSmartCallback > ();
36
37         private int sensorId = 0;
38
39         @config private IoTSet<IoTDeviceAddress> devUdpAddress;
40         @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
41
42         public SpruceSensor() {
43         }
44
45         public void init() {
46
47                 if (didAlreadyInit.compareAndSet(false, true) == false) {
48                         return; // already init
49                 }
50
51                 didAlreadyClose.set(false);
52
53                 try {
54                         Iterator itrUdp = devUdpAddress.iterator();
55                         Iterator itrZig = devZigbeeAddress.iterator();
56
57                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
58
59                         // DEBUG
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());
66
67                         Iterator itrDebugZig = devZigbeeAddress.iterator();
68                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
69                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
70
71                         zigConnection.registerCallback(this);
72                         System.out.println("Register callback!");
73                         zigConnection.init();
74                         System.out.println("Initialized!");
75
76                         while (!didBind.get()) {
77                                 zigConnection.sendBindRequest(0x0001, 0x0405, 0x01);
78                                 try {
79                                     System.out.println("Sending bind request!");
80                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
81                                 } catch (Exception e) {
82                                         e.printStackTrace();
83                                 }
84                         }
85
86                         while (!didConfigureReporting.get()) {
87                                 zigConnection.sendConfigureReportingCommand(0x0001, 0x0405, 0x0104, 0x01, 0x0000, 0x21, 0x0001, 0x0001, null);
88                                 try {
89                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
90                                 } catch (Exception e) {
91                                         e.printStackTrace();
92                                 }
93                         }
94                 } catch (Exception e) {
95                         e.printStackTrace();
96                 }
97         }
98
99         public void close() {
100
101                 if (didAlreadyClose.compareAndSet(false, true) == false) {
102                         return; // already init
103                 }
104
105                 didAlreadyInit.set(false);
106
107
108                 try {
109                         zigConnection.close();
110                 } catch (Exception e) {
111                         e.printStackTrace();
112                 }
113         }
114
115         public void Finalize() {
116                 if (!didClose) {
117                         close();
118                 }
119         }
120
121         public void setId(int id) {
122
123                 sensorId = id;
124
125         }
126
127         public int getId() {
128
129                 return sensorId;
130
131         }
132
133         public float getMoisture() {
134
135                 float tmp = 0;
136                 try {
137                         gettingLatestDataMutex.acquire();
138                         tmp = humidity;
139
140                 } catch (Exception e) {
141                         e.printStackTrace();
142                 }
143                 gettingLatestDataMutex.release();
144
145                 return tmp;
146         }
147
148         public long getTimestampOfLastReading() {
149
150                 Date tmp = null;
151                 try {
152                         gettingLatestDataMutex.acquire();
153                         tmp = (Date)timestampOfLastHumidity.clone();
154
155                 } catch (Exception e) {
156                         e.printStackTrace();
157                 }
158                 gettingLatestDataMutex.release();
159                 long retLong = tmp.getTime();
160
161                 return retLong;
162         }
163
164         public void newMessageAvailable(IoTZigbeeMessage _zm) {
165
166                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
167                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
168                         if (message.getSucceeded()) {
169                                 didBind.set(true);
170                         }
171
172                 } else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse) {
173                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
174                         if (message.getAllSuccess()) {
175                                 didConfigureReporting.set(true);
176                         }
177
178                 } else if (_zm instanceof IoTZigbeeMessageZclReportAttributes) {
179                         IoTZigbeeMessageZclReportAttributes message = (IoTZigbeeMessageZclReportAttributes)_zm;
180                         List <IoTZigbeeMessageZclReportAttributes.Attribute> attrList = message.getAttributes();
181
182                         if (attrList.size() == 1) {
183                                 if (attrList.get(0).getAttributeId() == 0) {
184                                         byte[] data = attrList.get(0).getData();
185
186                                         int value = (data[0] * 256) + data[1];
187
188                                         try {
189                                                 gettingLatestDataMutex.acquire();
190                                                 humidity = (float)value / (float)100.0;
191                                                 timestampOfLastHumidity = new Date();
192                                         } catch (Exception e) {
193                                                 e.printStackTrace();
194                                         }
195                                         gettingLatestDataMutex.release();
196
197                                         try {
198                                                 for (MoistureSensorSmartCallback cb : callbackList) {
199                                                         cb.newReadingAvailable(this.getId(), this.getMoisture(), this.getTimestampOfLastReading());
200                                                 }
201                                         } catch (Exception e) {
202                                                 e.printStackTrace();
203                                         }
204                                 }
205                         }
206
207                 }
208         }
209
210         public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
211                 callbackList.add(_callbackTo);
212         }
213 }