-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorState.java
366 lines (335 loc) · 10.3 KB
/
SensorState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package io.polestar.data.sensors;
import org.netkernel.layer0.nkf.INKFLocale;
import org.netkernel.layer0.nkf.INKFRequestContext;
import org.netkernel.mod.hds.IHDSMutator;
import org.netkernel.mod.hds.IHDSReader;
class SensorState
{
private Object mValue;
private long mLastUpdated;
private long mLastModified;
private long mErrorLastModified;
private String mUserError;
private String mValueError;
private String mStaleError;
private long mErrorLastModifiedPublic;
private String mErrorPublic;
public SensorState()
{ mValue=null;
mLastUpdated=0;
mLastModified=0;
mErrorLastModified=0;
mUserError=null;
mValueError=null;
mStaleError="No value received";
}
public SensorState(IHDSReader aState)
{
mValue=aState.getFirstValue("value");
mUserError=(String)aState.getFirstValueOrNull("userError");
mValueError=(String)aState.getFirstValueOrNull("valueError");
mStaleError=(String)aState.getFirstValueOrNull("staleError");
mErrorPublic=(String)aState.getFirstValueOrNull("error");
mLastUpdated=(Long)aState.getFirstValue("lastUpdated");
mLastModified=(Long)aState.getFirstValue("lastModified");
Long errorLastModified=(Long)aState.getFirstValueOrNull("errorLastModified");
if (errorLastModified==null)
{ mErrorLastModified=0L;
}
Long errorLastModifiedPublic=(Long)aState.getFirstValueOrNull("errorLastModifiedPublic");
if (errorLastModifiedPublic==null)
{ mErrorLastModifiedPublic=0L;
}
mErrorLastModified=errorLastModified;
}
public void serializeToHDS(IHDSMutator aMutator, long aNow)
{ if (mUserError!=null)
{ aMutator.addNode("userError", mUserError);
}
if (mValueError!=null)
{ aMutator.addNode("valueError", mValueError);
}
if (mStaleError!=null)
{ aMutator.addNode("staleError", mStaleError);
}
String error=getError();
if (error!=null)
{ aMutator.addNode("error", error);
}
aMutator.addNode("value", getValue());
aMutator.addNode("lastUpdated", getLastUpdated());
aMutator.addNode("lastModified", getLastModified());
aMutator.addNode("errorLastModified", mErrorLastModified);
aMutator.addNode("errorLastModifiedPublic", mErrorLastModifiedPublic);
}
private static boolean isDifferent(Object a, Object b)
{ boolean different=(a==null && b!=null) || (a!=null && b==null) || (a!=null && !a.equals(b));
return different;
}
public void setValue(Object aValue, long aNow, IHDSReader sensorDef, INKFRequestContext aContext)
{ if (isDifferent(aValue,mValue) && aValue!=null)
{ mValue=aValue;
if (aNow>mLastModified)
{ mLastModified=aNow;
testValue(sensorDef, aNow);
}
}
if (aNow>mLastUpdated)
{ mLastUpdated=aNow;
poll(sensorDef,aNow,aContext);
}
}
public void setUserError(String aError, long aNow)
{ if (isDifferent(aError,mUserError))
{ mErrorLastModified=aNow;
//System.out.println("USER "+mUserError+"->"+aError);
mUserError=aError;
}
}
public boolean setValueError(String aError, long aNow)
{ if (isDifferent(aError,mValueError))
{ mErrorLastModified=aNow;
//System.out.println("VALUE "+mValueError+"->"+aError);
mValueError=aError;
return true;
}
return false;
}
public boolean setStaleError(String aError, long aNow)
{ if (isDifferent(aError,mStaleError))
{ mErrorLastModified=aNow;
//System.out.println("STALE "+mStaleError+"->"+aError);
mStaleError=aError;
return true;
}
return false;
}
private void testValue(IHDSReader sensorDef, long aNow)
{ if (sensorDef!=null)
{ String error;
error=testForGreaterThan(sensorDef);
if (error==null)
{ error=testForLessThan(sensorDef);
}
if (error==null)
{ error=testIfEquals(sensorDef);
}
if (error==null)
{ error=testIfNotEquals(sensorDef);
}
setValueError(error, aNow);
}
}
public void poll(IHDSReader sensorDef, long aNow, INKFRequestContext aContext)
{ //boolean result=false;
if (sensorDef!=null)
{
String error;
error=testForNoReadings(sensorDef,aNow);
if (error==null)
{ error=testForNoUpdates(sensorDef,aNow);
}
//if (setStaleError(error, aNow)) result=true;
setStaleError(error, aNow);
Long errorOnlyAfter=(Long)sensorDef.getFirstValueOrNull("errorOnlyAfter");
if (errorOnlyAfter!=null)
{ if (mErrorLastModifiedPublic<mErrorLastModified)
{
String errorInternal=getErrorInternal();
if (errorInternal==null)
{
if (isDifferent(errorInternal, mErrorPublic))
{ mErrorLastModifiedPublic=aNow;
mErrorPublic=null;
//System.out.println("error cleared");
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear "+sensorDef.getFirstValue("name"));
}
else
{ mErrorLastModified=mErrorLastModifiedPublic;
//System.out.println("error cleared but not different");
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear but not modified "+sensorDef.getFirstValue("name"));
}
}
else
{ if (aNow-mErrorLastModified>errorOnlyAfter*1000L)
{
if (isDifferent(errorInternal, mErrorPublic))
{ mErrorLastModifiedPublic=aNow;
mErrorPublic=errorInternal;
//System.out.println("error made public");
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error made public "+sensorDef.getFirstValue("name"));
}
else
{ mErrorLastModified=mErrorLastModifiedPublic;
//System.out.println("error made public but not different");
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() public but not different "+sensorDef.getFirstValue("name"));
}
}
else
{ //System.out.println("error but not yet");
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error but not yet public "+sensorDef.getFirstValue("name"));
}
}
}
}
else
{ String errorInternal=getErrorInternal();
if (isDifferent(errorInternal, mErrorPublic))
{ mErrorLastModifiedPublic=mErrorLastModified;
mErrorPublic=getErrorInternal();
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error "+(mErrorPublic!=null)+" "+sensorDef.getFirstValue("name"));
}
}
}
}
private String testForGreaterThan(IHDSReader sensorDef)
{ String error=null;
Number errorIfGreaterThan=(Number)sensorDef.getFirstValueOrNull("errorIfGreaterThan");
if (errorIfGreaterThan!=null && mValue instanceof Number)
{ Number nn=(Number)mValue;
boolean isError=false;
String errorText="Value has exceeded "+errorIfGreaterThan;
Number errorClearIfLessThan=(Number)sensorDef.getFirstValueOrNull("errorClearIfLessThan");
if (errorClearIfLessThan!=null)
{ //hysteresis
if (mValueError!=null && mValueError.equals(errorText))
{ if (nn.doubleValue()>=errorClearIfLessThan.doubleValue())
{ isError=true;
}
}
else
{ if (nn.doubleValue()>errorIfGreaterThan.doubleValue())
{ isError=true;
}
}
}
else
{ if (nn.doubleValue()>errorIfGreaterThan.doubleValue())
{ isError=true;
}
}
if (isError)
{ error=errorText;
}
}
return error;
}
private String testForLessThan(IHDSReader sensorDef)
{ String error=null;
Number errorIfLessThan=(Number)sensorDef.getFirstValueOrNull("errorIfLessThan");
if (errorIfLessThan!=null && mValue instanceof Number)
{ Number nn=(Number)mValue;
boolean isError=false;
String errorText="Value has fallen below "+errorIfLessThan;
Number errorClearIfGreaterThan=(Number)sensorDef.getFirstValueOrNull("errorClearIfGreaterThan");
if (errorClearIfGreaterThan!=null)
{ //hysteresis
if (mValueError!=null && mValueError.equals(errorText))
{ if (nn.doubleValue()<=errorClearIfGreaterThan.doubleValue())
{ isError=true;
}
}
else
{ if (nn.doubleValue()<errorIfLessThan.doubleValue())
{ isError=true;
}
}
}
else
{ if (nn.doubleValue()<errorIfLessThan.doubleValue())
{ isError=true;
}
}
if (isError)
{ error=errorText;
}
}
return error;
}
private String testIfEquals(IHDSReader sensorDef)
{ String error=null;
Object errorIfEquals=sensorDef.getFirstValueOrNull("errorIfEquals");
if (errorIfEquals!=null && errorIfEquals instanceof Number && mValue instanceof Number)
{ Number nn=(Number)mValue;
if (nn.doubleValue()==((Number)errorIfEquals).doubleValue())
{ error="Value equals "+errorIfEquals;
}
}
if (errorIfEquals!=null && errorIfEquals instanceof Boolean && mValue instanceof Boolean)
{ Boolean nn=(Boolean)mValue;
if (nn.equals(errorIfEquals))
{ error="Value equals "+errorIfEquals;
}
}
return error;
}
private String testIfNotEquals(IHDSReader sensorDef)
{ String error=null;
Object errorIfNotEquals=sensorDef.getFirstValueOrNull("errorIfNotEquals");
if (errorIfNotEquals!=null && errorIfNotEquals instanceof Number && mValue instanceof Number)
{ Number nn=(Number)mValue;
if (nn.doubleValue()!=((Number)errorIfNotEquals).doubleValue())
{ error="Value doesn't equal "+errorIfNotEquals;
}
}
if (errorIfNotEquals!=null && errorIfNotEquals instanceof Boolean && mValue instanceof Boolean)
{ Boolean nn=(Boolean)mValue;
if (!nn.equals(errorIfNotEquals))
{ error="Value doesn't equal "+errorIfNotEquals;
}
}
return error;
}
private String testForNoReadings(IHDSReader sensorDef, long aNow)
{ String error=null;
Long errorIfNoReadingsFor=(Long)sensorDef.getFirstValueOrNull("errorIfNoReadingsFor");
if (errorIfNoReadingsFor!=null)
{ if (aNow-mLastUpdated>=errorIfNoReadingsFor*1000L)
{ error="No fresh readings";
}
}
return error;
}
private String testForNoUpdates(IHDSReader sensorDef, long aNow)
{ String error=null;
Long errorIfNotModifiedFor=(Long)sensorDef.getFirstValueOrNull("errorIfNotModifiedFor");
if (errorIfNotModifiedFor!=null)
{ if (aNow-mLastModified>=errorIfNotModifiedFor*1000L)
{ error="Not modified for too long";
}
}
return error;
}
public Object getValue()
{ return mValue;
}
public long getLastModified()
{ return mLastModified;
}
public long getLastUpdated()
{ return mLastUpdated;
}
public long getErrorLastModified()
{ return mErrorLastModifiedPublic;
}
public boolean hasError()
{ return getError()!=null;
}
public String getError()
{ return mErrorPublic;
}
private String getErrorInternal()
{
String error=null;
if (mUserError!=null)
{ error=mUserError;
}
else if (mStaleError!=null)
{ error=mStaleError;
}
else if (mValueError!=null)
{ error=mValueError;
}
return error;
}
}