Skip to content

Commit 591c4ac

Browse files
committed
Updated handling of cluster commands
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
1 parent 307416d commit 591c4ac

File tree

49 files changed

+793
-968
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+793
-968
lines changed

com.zsmartsystems.zigbee.autocode/src/main/java/com/zsmartsystems/zigbee/autocode/ZigBeeZclClusterGenerator.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,10 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
189189
out.println();
190190
}
191191

192-
out.println(" // Attribute initialisation");
193192
out.println(" @Override");
194193
out.println(" protected Map<Integer, ZclAttribute> initializeAttributes() {");
195-
out.println(" Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>("
196-
+ attributesServer + ");");
194+
out.println(
195+
" Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(" + attributesServer + ");");
197196

198197
if (attributesServer != 0) {
199198
out.println();
@@ -217,14 +216,51 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
217216
}
218217
}
219218
}
220-
221-
// TODO: Add client attributes
222-
223219
out.println();
224220
out.println(" return attributeMap;");
225221
out.println(" }");
226222
out.println();
227223

224+
// TODO: Add client attributes
225+
226+
if (commandsServer != 0) {
227+
out.println(" @Override");
228+
out.println(" protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {");
229+
out.println(" Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>("
230+
+ commandsServer + ");");
231+
out.println();
232+
for (final ZigBeeXmlCommand command : cluster.commands) {
233+
if (command.source.equalsIgnoreCase("server")) {
234+
out.println(" commandMap.put(0x" + String.format("%04X", command.code) + ", "
235+
+ stringToUpperCamelCase(command.name) + ".class);");
236+
}
237+
}
238+
out.println();
239+
240+
out.println(" return commandMap;");
241+
out.println(" }");
242+
out.println();
243+
}
244+
245+
if (commandsClient != 0) {
246+
out.println(" @Override");
247+
out.println(" protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {");
248+
out.println(" Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>("
249+
+ commandsClient + ");");
250+
out.println();
251+
for (final ZigBeeXmlCommand command : cluster.commands) {
252+
if (command.source.equalsIgnoreCase("client")) {
253+
out.println(" commandMap.put(0x" + String.format("%04X", command.code) + ", "
254+
+ stringToUpperCamelCase(command.name) + ".class);");
255+
}
256+
}
257+
out.println();
258+
259+
out.println(" return commandMap;");
260+
out.println(" }");
261+
out.println();
262+
}
263+
228264
out.println(" /**");
229265
out.println(" * Default constructor to create a " + cluster.name + " cluster.");
230266
out.println(" *");
@@ -397,42 +433,6 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
397433
out.println(" }");
398434
}
399435

400-
if (commandsServer > 0) {
401-
out.println();
402-
out.println(" @Override");
403-
out.println(" public ZclCommand getCommandFromId(int commandId) {");
404-
out.println(" switch (commandId) {");
405-
for (final ZigBeeXmlCommand command : cluster.commands) {
406-
if (command.source.equalsIgnoreCase("client")) {
407-
out.println(" case 0x" + String.format("%02X", command.code) + ": // "
408-
+ stringToConstant(command.name));
409-
out.println(" return new " + stringToUpperCamelCase(command.name) + "();");
410-
}
411-
}
412-
out.println(" default:");
413-
out.println(" return null;");
414-
out.println(" }");
415-
out.println(" }");
416-
}
417-
418-
if (commandsClient > 0) {
419-
out.println();
420-
out.println(" @Override");
421-
out.println(" public ZclCommand getResponseFromId(int commandId) {");
422-
out.println(" switch (commandId) {");
423-
for (final ZigBeeXmlCommand command : cluster.commands) {
424-
if (command.source.equalsIgnoreCase("server")) {
425-
out.println(" case 0x" + String.format("%02X", command.code) + ": // "
426-
+ stringToConstant(command.name));
427-
out.println(" return new " + stringToUpperCamelCase(command.name) + "();");
428-
}
429-
}
430-
out.println(" default:");
431-
out.println(" return null;");
432-
out.println(" }");
433-
out.println(" }");
434-
}
435-
436436
out.println("}");
437437

438438
out.flush();

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/zcl/ZclCluster.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Set;
1515
import java.util.TreeSet;
1616
import java.util.concurrent.Callable;
17+
import java.util.concurrent.ConcurrentHashMap;
1718
import java.util.concurrent.CopyOnWriteArraySet;
1819
import java.util.concurrent.ExecutionException;
1920
import java.util.concurrent.Future;
@@ -120,6 +121,18 @@ public abstract class ZclCluster {
120121
*/
121122
protected Map<Integer, ZclAttribute> attributes = initializeAttributes();
122123

124+
/**
125+
* Map of server side commands supported by the cluster. This contains all server commands, even if they are not
126+
* supported by the remote device.
127+
*/
128+
protected Map<Integer, Class<? extends ZclCommand>> serverCommands = initializeServerCommands();
129+
130+
/**
131+
* Map of client side commands supported by the cluster. This contains all client commands, even if they are not
132+
* supported by the remote device.
133+
*/
134+
protected Map<Integer, Class<? extends ZclCommand>> clientCommands = initializeClientCommands();
135+
123136
/**
124137
* The {@link ZclAttributeNormalizer} is used to normalize attribute data types to ensure that data types are
125138
* consistent with the ZCL definition. This ensures that the application can rely on consistent and deterministic
@@ -141,6 +154,26 @@ public abstract class ZclCluster {
141154
*/
142155
protected abstract Map<Integer, ZclAttribute> initializeAttributes();
143156

157+
/**
158+
* Abstract method called when the cluster starts to initialise the list of server side commands defined in this
159+
* cluster by the cluster library
160+
*
161+
* @return a {@link Map} of all server side commands this cluster is known to support
162+
*/
163+
protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {
164+
return new ConcurrentHashMap<>(0);
165+
}
166+
167+
/**
168+
* Abstract method called when the cluster starts to initialise the list of client side commands defined in this
169+
* cluster by the cluster library
170+
*
171+
* @return a {@link Map} of all client side commands this cluster is known to support
172+
*/
173+
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
174+
return new ConcurrentHashMap<>(0);
175+
}
176+
144177
/**
145178
* Creates a cluster
146179
*
@@ -931,20 +964,33 @@ public void handleCommand(ZclCommand command) {
931964
* found, null is returned.
932965
*
933966
* @param commandId the command ID
934-
* @return the {@link ZclCommand} or null if no command found.
967+
* @return the {@link ZclCommand} or null if no command was found.
935968
*/
936969
public ZclCommand getCommandFromId(int commandId) {
937-
return null;
970+
return getCommand(commandId, clientCommands);
938971
}
939972

940973
/**
941974
* Gets a response from the command ID (ie a command from server to client). If no command with the requested id is
942975
* found, null is returned.
943976
*
944977
* @param commandId the command ID
945-
* @return the {@link ZclCommand} or null if no command found.
978+
* @return the {@link ZclCommand} or null if no command was found.
946979
*/
947980
public ZclCommand getResponseFromId(int commandId) {
981+
return getCommand(commandId, serverCommands);
982+
}
983+
984+
private ZclCommand getCommand(int commandId, Map<Integer, Class<? extends ZclCommand>> commands) {
985+
if (!commands.containsKey(commandId)) {
986+
return null;
987+
}
988+
989+
try {
990+
return commands.get(commandId).getConstructor().newInstance();
991+
} catch (Exception e) {
992+
logger.debug("Error instantiating cluster command {}, id={}", clusterName, commandId);
993+
}
948994
return null;
949995
}
950996

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/zcl/clusters/ZclAlarmsCluster.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* <p>
4646
* Code is auto-generated. Modifications may be overwritten!
4747
*/
48-
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
48+
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T20:57:36Z")
4949
public class ZclAlarmsCluster extends ZclCluster {
5050
/**
5151
* The ZigBee Cluster Library Cluster ID
@@ -67,16 +67,37 @@ public class ZclAlarmsCluster extends ZclCluster {
6767
*/
6868
public static final int ATTR_ALARMCOUNT = 0x0000;
6969

70-
// Attribute initialisation
7170
@Override
7271
protected Map<Integer, ZclAttribute> initializeAttributes() {
73-
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(1);
72+
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(1);
7473

7574
attributeMap.put(ATTR_ALARMCOUNT, new ZclAttribute(ZclClusterType.ALARMS, ATTR_ALARMCOUNT, "Alarm Count", ZclDataType.UNSIGNED_16_BIT_INTEGER, false, true, false, false));
7675

7776
return attributeMap;
7877
}
7978

79+
@Override
80+
protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {
81+
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(2);
82+
83+
commandMap.put(0x0000, AlarmCommand.class);
84+
commandMap.put(0x0001, GetAlarmResponse.class);
85+
86+
return commandMap;
87+
}
88+
89+
@Override
90+
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
91+
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(4);
92+
93+
commandMap.put(0x0000, ResetAlarmCommand.class);
94+
commandMap.put(0x0001, ResetAllAlarmsCommand.class);
95+
commandMap.put(0x0002, GetAlarmCommand.class);
96+
commandMap.put(0x0003, ResetAlarmLogCommand.class);
97+
98+
return commandMap;
99+
}
100+
80101
/**
81102
* Default constructor to create a Alarms cluster.
82103
*
@@ -245,32 +266,4 @@ public Future<CommandResult> getAlarmResponse(Integer status, Integer alarmCode,
245266

246267
return send(command);
247268
}
248-
249-
@Override
250-
public ZclCommand getCommandFromId(int commandId) {
251-
switch (commandId) {
252-
case 0x00: // RESET_ALARM_COMMAND
253-
return new ResetAlarmCommand();
254-
case 0x01: // RESET_ALL_ALARMS_COMMAND
255-
return new ResetAllAlarmsCommand();
256-
case 0x02: // GET_ALARM_COMMAND
257-
return new GetAlarmCommand();
258-
case 0x03: // RESET_ALARM_LOG_COMMAND
259-
return new ResetAlarmLogCommand();
260-
default:
261-
return null;
262-
}
263-
}
264-
265-
@Override
266-
public ZclCommand getResponseFromId(int commandId) {
267-
switch (commandId) {
268-
case 0x00: // ALARM_COMMAND
269-
return new AlarmCommand();
270-
case 0x01: // GET_ALARM_RESPONSE
271-
return new GetAlarmResponse();
272-
default:
273-
return null;
274-
}
275-
}
276269
}

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/zcl/clusters/ZclBasicCluster.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* <p>
3232
* Code is auto-generated. Modifications may be overwritten!
3333
*/
34-
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
34+
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T21:33:25Z")
3535
public class ZclBasicCluster extends ZclCluster {
3636
/**
3737
* The ZigBee Cluster Library Cluster ID
@@ -125,10 +125,9 @@ public class ZclBasicCluster extends ZclCluster {
125125
*/
126126
public static final int ATTR_SWBUILDID = 0x4000;
127127

128-
// Attribute initialisation
129128
@Override
130129
protected Map<Integer, ZclAttribute> initializeAttributes() {
131-
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(14);
130+
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(14);
132131

133132
attributeMap.put(ATTR_ZCLVERSION, new ZclAttribute(ZclClusterType.BASIC, ATTR_ZCLVERSION, "ZCL Version", ZclDataType.UNSIGNED_8_BIT_INTEGER, true, true, false, false));
134133
attributeMap.put(ATTR_APPLICATIONVERSION, new ZclAttribute(ZclClusterType.BASIC, ATTR_APPLICATIONVERSION, "Application Version", ZclDataType.UNSIGNED_8_BIT_INTEGER, true, true, false, false));
@@ -148,6 +147,15 @@ protected Map<Integer, ZclAttribute> initializeAttributes() {
148147
return attributeMap;
149148
}
150149

150+
@Override
151+
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
152+
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(1);
153+
154+
commandMap.put(0x0000, ResetToFactoryDefaultsCommand.class);
155+
156+
return commandMap;
157+
}
158+
151159
/**
152160
* Default constructor to create a Basic cluster.
153161
*
@@ -1142,12 +1150,4 @@ public String getSwBuildId(final long refreshPeriod) {
11421150
public Future<CommandResult> resetToFactoryDefaultsCommand() {
11431151
return send(new ResetToFactoryDefaultsCommand());
11441152
}
1145-
1146-
@Override
1147-
public ZclCommand getResponseFromId(int commandId) {
1148-
switch (commandId) {
1149-
default:
1150-
return null;
1151-
}
1152-
}
11531153
}

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/zcl/clusters/ZclBinaryInputBasicCluster.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* <p>
3030
* Code is auto-generated. Modifications may be overwritten!
3131
*/
32-
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
32+
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T21:33:25Z")
3333
public class ZclBinaryInputBasicCluster extends ZclCluster {
3434
/**
3535
* The ZigBee Cluster Library Cluster ID
@@ -165,10 +165,9 @@ public class ZclBinaryInputBasicCluster extends ZclCluster {
165165
*/
166166
public static final int ATTR_APPLICATIONTYPE = 0x0100;
167167

168-
// Attribute initialisation
169168
@Override
170169
protected Map<Integer, ZclAttribute> initializeAttributes() {
171-
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(9);
170+
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(9);
172171

173172
attributeMap.put(ATTR_ACTIVETEXT, new ZclAttribute(ZclClusterType.BINARY_INPUT_BASIC, ATTR_ACTIVETEXT, "Active Text", ZclDataType.CHARACTER_STRING, false, true, true, false));
174173
attributeMap.put(ATTR_DESCRIPTION, new ZclAttribute(ZclClusterType.BINARY_INPUT_BASIC, ATTR_DESCRIPTION, "Description", ZclDataType.CHARACTER_STRING, false, true, true, false));

0 commit comments

Comments
 (0)