forked from openhab/openhab1-addons
/
ZWaveAlarmCommandClass.java
395 lines (334 loc) · 11.6 KB
/
ZWaveAlarmCommandClass.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* Copyright (c) 2010-2015, openHAB.org and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.zwave.internal.protocol.commandclass;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.openhab.binding.zwave.internal.config.ZWaveDbCommandClass;
import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.ZWaveController;
import org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint;
import org.openhab.binding.zwave.internal.protocol.ZWaveNode;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessageClass;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessagePriority;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessageType;
import org.openhab.binding.zwave.internal.protocol.event.ZWaveCommandClassValueEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
/**
* Handles the Alarm command class.
* The event is reported as occurs (0xFF) or does not occur (0x00).
* @author Chris Jackson
* @since 1.6.0
*/
@XStreamAlias("alarmCommandClass")
public class ZWaveAlarmCommandClass extends ZWaveCommandClass
implements ZWaveGetCommands, ZWaveCommandClassInitialization, ZWaveCommandClassDynamicState {
@XStreamOmitField
private static final Logger logger = LoggerFactory.getLogger(ZWaveAlarmCommandClass.class);
private static final int MAX_SUPPORTED_VERSION = 2;
private static final int ALARM_GET = 0x04;
private static final int ALARM_REPORT = 0x05;
// Version 2
private static final int ALARM_SUPPORTED_GET = 0x07;
private static final int ALARM_SUPPORTED_REPORT = 0x08;
private final Map<AlarmType, Alarm> alarms = new HashMap<AlarmType, Alarm>();
@XStreamOmitField
private boolean initialiseDone = false;
@XStreamOmitField
private boolean dynamicDone = false;
private boolean isGetSupported = true;
/**
* Creates a new instance of the ZWaveAlarmCommandClass class.
* @param node the node this command class belongs to
* @param controller the controller to use
* @param endpoint the endpoint this Command class belongs to
*/
public ZWaveAlarmCommandClass(ZWaveNode node,
ZWaveController controller, ZWaveEndpoint endpoint) {
super(node, controller, endpoint);
}
/**
* {@inheritDoc}
*/
@Override
public CommandClass getCommandClass() {
return CommandClass.ALARM;
}
/**
* {@inheritDoc}
*/
@Override
public int getMaxVersion() {
return MAX_SUPPORTED_VERSION;
}
/**
* {@inheritDoc}
*/
@Override
public void handleApplicationCommandRequest(SerialMessage serialMessage,
int offset, int endpoint) {
logger.trace("Handle Message Alarm Request");
logger.debug("NODE {}: Received Alarm Request", this.getNode().getNodeId());
int command = serialMessage.getMessagePayloadByte(offset);
switch (command) {
case ALARM_GET:
logger.warn("Command {} not implemented.", command);
return;
case ALARM_REPORT:
logger.trace("Process Alarm Report");
int alarmTypeCode = serialMessage.getMessagePayloadByte(offset + 1);
int value = serialMessage.getMessagePayloadByte(offset + 2);
logger.debug("NODE {}: Alarm report - Value = {}", this.getNode().getNodeId(), value);
AlarmType alarmType = AlarmType.getAlarmType(alarmTypeCode);
if (alarmType == null) {
logger.error("NODE {}: Unknown Alarm Type = {}, ignoring report.", this.getNode().getNodeId(), alarmTypeCode);
return;
}
// Alarm type seems to be supported, add it to the list if it's not already there.
Alarm alarm = getAlarm(alarmTypeCode);
if(alarm != null) {
alarm.setInitialised();
logger.debug("NODE {}: Alarm Report: Type={}({}), Value={}",
this.getNode().getNodeId(),
alarm.getAlarmType().getLabel(), alarmTypeCode,
value);
}
ZWaveAlarmValueEvent zEvent = new ZWaveAlarmValueEvent(this.getNode().getNodeId(), endpoint, alarmType, value);
this.getController().notifyEventListeners(zEvent);
dynamicDone = true;
break;
case ALARM_SUPPORTED_REPORT:
logger.debug("NODE {}: Process Alarm Supported Report", this.getNode().getNodeId());
int numBytes = serialMessage.getMessagePayloadByte(offset + 1);
for(int i=0; i < numBytes; ++i ) {
for(int bit = 0; bit < 8; ++bit) {
if (((serialMessage.getMessagePayloadByte(offset + i + 2)) & (1 << bit)) == 0) {
continue;
}
int index = (i << 3) + bit;
if (index >= AlarmType.values().length) {
continue;
}
// (n)th bit is set. n is the index for the alarm type
// enumeration.
// Alarm type seems to be supported, add it to the list if it's not already there.
getAlarm(index);
}
}
initialiseDone = true;
break;
default:
logger.warn(String.format("Unsupported Command 0x%02X for command class %s (0x%02X).",
command,
this.getCommandClass().getLabel(),
this.getCommandClass().getKey()));
break;
}
}
private Alarm getAlarm(int alarmTypeCode) {
AlarmType alarmType = AlarmType.getAlarmType(alarmTypeCode);
if (alarmType == null) {
logger.error("NODE {}: Unknown Alarm Type = {}, ignoring report.", this.getNode().getNodeId(), alarmTypeCode);
return null;
}
// Add alarm to the list if it's not already there.
Alarm alarm = alarms.get(alarmType);
if (alarm == null) {
logger.debug("NODE {}: Adding new alarm type {}({})", this.getNode().getNodeId(),
alarmType.getLabel(), alarmTypeCode);
alarm = new Alarm(alarmType);
this.alarms.put(alarmType, alarm);
}
return alarm;
}
/**
* Gets a SerialMessage with the ALARM_GET command
* @return the serial message
*/
public SerialMessage getValueMessage() {
//TODO: Why does this return!!!???!!!
for (Map.Entry<AlarmType, Alarm> entry : this.alarms.entrySet()) {
return getMessage(entry.getValue().getAlarmType());
}
// in case there are no supported alarms, get them.
return getMessage(AlarmType.GENERAL);
}
/**
* Gets a SerialMessage with the SENSOR_ALARM_SUPPORTED_GET command
* @return the serial message, or null if the supported command is not supported.
*/
public SerialMessage getSupportedMessage() {
logger.debug("NODE {}: Creating new message for command SENSOR_ALARM_SUPPORTED_GET", this.getNode().getNodeId());
SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.ApplicationCommandHandler, SerialMessagePriority.High);
byte[] newPayload = { (byte) this.getNode().getNodeId(),
2,
(byte) getCommandClass().getKey(),
(byte) ALARM_SUPPORTED_GET };
result.setMessagePayload(newPayload);
return result;
}
/**
* Gets a SerialMessage with the ALARM_GET command
* @return the serial message
*/
public SerialMessage getMessage(AlarmType alarmType) {
if(isGetSupported == false) {
logger.debug("NODE {}: Node doesn't support get requests", this.getNode().getNodeId());
return null;
}
logger.debug("NODE {}: Creating new message for application command ALARM_GET", this.getNode().getNodeId());
SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.ApplicationCommandHandler, SerialMessagePriority.Get);
byte[] newPayload = { (byte) this.getNode().getNodeId(),
3,
(byte) getCommandClass().getKey(),
(byte) ALARM_GET,
(byte) alarmType.getKey()
};
result.setMessagePayload(newPayload);
return result;
}
/**
* Initializes the alarm sensor command class. Requests the supported alarm types.
*/
@Override
public Collection<SerialMessage> initialize(boolean refresh) {
ArrayList<SerialMessage> result = new ArrayList<SerialMessage>();
// If we're already initialized, then don't do it again unless we're refreshing
if(this.getVersion() > 1 && (refresh == true || initialiseDone == false)) {
result.add(this.getSupportedMessage());
}
return result;
}
/**
* Z-Wave AlarmType enumeration. The alarm type indicates the type
* of alarm that is reported.
*/
@XStreamAlias("alarmType")
public enum AlarmType {
GENERAL(0, "General"),
SMOKE(1, "Smoke"),
CARBON_MONOXIDE(2, "Carbon Monoxide"),
CARBON_DIOXIDE(3, "Carbon Dioxide"),
HEAT(4, "Heat"),
FLOOD(5, "Flood"),
ACCESS_CONTROL(6, "Access Control"),
BURGLAR(7, "Burglar"),
POWER_MANAGEMENT(8, "Power Management"),
SYSTEM(9, "System"),
EMERGENCY(10, "Emergency"),
COUNT(11, "Count");
/**
* A mapping between the integer code and its corresponding Alarm type
* to facilitate lookup by code.
*/
private static Map<Integer, AlarmType> codeToAlarmTypeMapping;
private int key;
private String label;
private AlarmType(int key, String label) {
this.key = key;
this.label = label;
}
private static void initMapping() {
codeToAlarmTypeMapping = new HashMap<Integer, AlarmType>();
for (AlarmType s : values()) {
codeToAlarmTypeMapping.put(s.key, s);
}
}
/**
* Lookup function based on the alarm type code.
* Returns null if the code does not exist.
* @param i the code to lookup
* @return enumeration value of the alarm type.
*/
public static AlarmType getAlarmType(int i) {
if (codeToAlarmTypeMapping == null) {
initMapping();
}
return codeToAlarmTypeMapping.get(i);
}
/**
* @return the key
*/
public int getKey() {
return key;
}
/**
* @return the label
*/
public String getLabel() {
return label;
}
}
/**
* Class to hold alarm state
* @author Chris Jackson
*/
private class Alarm {
AlarmType alarmType;
boolean initialised = false;
public Alarm(AlarmType type) {
alarmType = type;
}
public AlarmType getAlarmType() {
return alarmType;
}
public void setInitialised() {
initialised = true;
}
public boolean getInitialised() {
return initialised;
}
}
/**
* Z-Wave Alarm Event class. Indicates that an alarm value
* changed.
*/
public class ZWaveAlarmValueEvent extends ZWaveCommandClassValueEvent {
private AlarmType alarmType;
/**
* Constructor. Creates a instance of the ZWaveAlarmValueEvent class.
* @param nodeId the nodeId of the event
* @param endpoint the endpoint of the event.
* @param alarmType the alarm type that triggered the event;
* @param value the value for the event.
*/
private ZWaveAlarmValueEvent(int nodeId, int endpoint,
AlarmType alarmType, Object value) {
super(nodeId, endpoint, CommandClass.ALARM, value);
this.alarmType = alarmType;
}
/**
* Gets the alarm type for this alarm value event.
*/
public AlarmType getAlarmType() {
return alarmType;
}
}
@Override
public Collection<SerialMessage> getDynamicValues(boolean refresh) {
ArrayList<SerialMessage> result = new ArrayList<SerialMessage>();
for (Map.Entry<AlarmType, Alarm> entry : this.alarms.entrySet()) {
if(refresh == true || entry.getValue().getInitialised() == false) {
result.add(getMessage(entry.getValue().getAlarmType()));
}
}
return result;
}
@Override
public boolean setOptions (ZWaveDbCommandClass options) {
if(options.isGetSupported != null) {
isGetSupported = options.isGetSupported;
}
return true;
}
}