Skip to content

Commit

Permalink
Updated handling of cluster commands
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson committed Mar 13, 2019
1 parent ab6d9a2 commit e310f38
Show file tree
Hide file tree
Showing 49 changed files with 793 additions and 968 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,10 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
out.println();
}

out.println(" // Attribute initialisation");
out.println(" @Override");
out.println(" protected Map<Integer, ZclAttribute> initializeAttributes() {");
out.println(" Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>("
+ attributesServer + ");");
out.println(
" Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(" + attributesServer + ");");

if (attributesServer != 0) {
out.println();
Expand All @@ -217,14 +216,51 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
}
}
}

// TODO: Add client attributes

out.println();
out.println(" return attributeMap;");
out.println(" }");
out.println();

// TODO: Add client attributes

if (commandsServer != 0) {
out.println(" @Override");
out.println(" protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {");
out.println(" Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>("
+ commandsServer + ");");
out.println();
for (final ZigBeeXmlCommand command : cluster.commands) {
if (command.source.equalsIgnoreCase("server")) {
out.println(" commandMap.put(0x" + String.format("%04X", command.code) + ", "
+ stringToUpperCamelCase(command.name) + ".class);");
}
}
out.println();

out.println(" return commandMap;");
out.println(" }");
out.println();
}

if (commandsClient != 0) {
out.println(" @Override");
out.println(" protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {");
out.println(" Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>("
+ commandsClient + ");");
out.println();
for (final ZigBeeXmlCommand command : cluster.commands) {
if (command.source.equalsIgnoreCase("client")) {
out.println(" commandMap.put(0x" + String.format("%04X", command.code) + ", "
+ stringToUpperCamelCase(command.name) + ".class);");
}
}
out.println();

out.println(" return commandMap;");
out.println(" }");
out.println();
}

out.println(" /**");
out.println(" * Default constructor to create a " + cluster.name + " cluster.");
out.println(" *");
Expand Down Expand Up @@ -397,42 +433,6 @@ private void generateZclClusterClasses(ZigBeeXmlCluster cluster, String packageR
out.println(" }");
}

if (commandsServer > 0) {
out.println();
out.println(" @Override");
out.println(" public ZclCommand getCommandFromId(int commandId) {");
out.println(" switch (commandId) {");
for (final ZigBeeXmlCommand command : cluster.commands) {
if (command.source.equalsIgnoreCase("client")) {
out.println(" case 0x" + String.format("%02X", command.code) + ": // "
+ stringToConstant(command.name));
out.println(" return new " + stringToUpperCamelCase(command.name) + "();");
}
}
out.println(" default:");
out.println(" return null;");
out.println(" }");
out.println(" }");
}

if (commandsClient > 0) {
out.println();
out.println(" @Override");
out.println(" public ZclCommand getResponseFromId(int commandId) {");
out.println(" switch (commandId) {");
for (final ZigBeeXmlCommand command : cluster.commands) {
if (command.source.equalsIgnoreCase("server")) {
out.println(" case 0x" + String.format("%02X", command.code) + ": // "
+ stringToConstant(command.name));
out.println(" return new " + stringToUpperCamelCase(command.name) + "();");
}
}
out.println(" default:");
out.println(" return null;");
out.println(" }");
out.println(" }");
}

out.println("}");

out.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -120,6 +121,18 @@ public abstract class ZclCluster {
*/
protected Map<Integer, ZclAttribute> attributes = initializeAttributes();

/**
* Map of server side commands supported by the cluster. This contains all server commands, even if they are not
* supported by the remote device.
*/
protected Map<Integer, Class<? extends ZclCommand>> serverCommands = initializeServerCommands();

/**
* Map of client side commands supported by the cluster. This contains all client commands, even if they are not
* supported by the remote device.
*/
protected Map<Integer, Class<? extends ZclCommand>> clientCommands = initializeClientCommands();

/**
* The {@link ZclAttributeNormalizer} is used to normalize attribute data types to ensure that data types are
* consistent with the ZCL definition. This ensures that the application can rely on consistent and deterministic
Expand All @@ -141,6 +154,26 @@ public abstract class ZclCluster {
*/
protected abstract Map<Integer, ZclAttribute> initializeAttributes();

/**
* Abstract method called when the cluster starts to initialise the list of server side commands defined in this
* cluster by the cluster library
*
* @return a {@link Map} of all server side commands this cluster is known to support
*/
protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {
return new ConcurrentHashMap<>(0);
}

/**
* Abstract method called when the cluster starts to initialise the list of client side commands defined in this
* cluster by the cluster library
*
* @return a {@link Map} of all client side commands this cluster is known to support
*/
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
return new ConcurrentHashMap<>(0);
}

/**
* Creates a cluster
*
Expand Down Expand Up @@ -933,20 +966,33 @@ public void handleCommand(ZclCommand command) {
* found, null is returned.
*
* @param commandId the command ID
* @return the {@link ZclCommand} or null if no command found.
* @return the {@link ZclCommand} or null if no command was found.
*/
public ZclCommand getCommandFromId(int commandId) {
return null;
return getCommand(commandId, clientCommands);
}

/**
* Gets a response from the command ID (ie a command from server to client). If no command with the requested id is
* found, null is returned.
*
* @param commandId the command ID
* @return the {@link ZclCommand} or null if no command found.
* @return the {@link ZclCommand} or null if no command was found.
*/
public ZclCommand getResponseFromId(int commandId) {
return getCommand(commandId, serverCommands);
}

private ZclCommand getCommand(int commandId, Map<Integer, Class<? extends ZclCommand>> commands) {
if (!commands.containsKey(commandId)) {
return null;
}

try {
return commands.get(commandId).getConstructor().newInstance();
} catch (Exception e) {
logger.debug("Error instantiating cluster command {}, id={}", clusterName, commandId);
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* <p>
* Code is auto-generated. Modifications may be overwritten!
*/
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T20:57:36Z")
public class ZclAlarmsCluster extends ZclCluster {
/**
* The ZigBee Cluster Library Cluster ID
Expand All @@ -67,16 +67,37 @@ public class ZclAlarmsCluster extends ZclCluster {
*/
public static final int ATTR_ALARMCOUNT = 0x0000;

// Attribute initialisation
@Override
protected Map<Integer, ZclAttribute> initializeAttributes() {
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(1);
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(1);

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

return attributeMap;
}

@Override
protected Map<Integer, Class<? extends ZclCommand>> initializeServerCommands() {
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(2);

commandMap.put(0x0000, AlarmCommand.class);
commandMap.put(0x0001, GetAlarmResponse.class);

return commandMap;
}

@Override
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(4);

commandMap.put(0x0000, ResetAlarmCommand.class);
commandMap.put(0x0001, ResetAllAlarmsCommand.class);
commandMap.put(0x0002, GetAlarmCommand.class);
commandMap.put(0x0003, ResetAlarmLogCommand.class);

return commandMap;
}

/**
* Default constructor to create a Alarms cluster.
*
Expand Down Expand Up @@ -245,32 +266,4 @@ public Future<CommandResult> getAlarmResponse(Integer status, Integer alarmCode,

return send(command);
}

@Override
public ZclCommand getCommandFromId(int commandId) {
switch (commandId) {
case 0x00: // RESET_ALARM_COMMAND
return new ResetAlarmCommand();
case 0x01: // RESET_ALL_ALARMS_COMMAND
return new ResetAllAlarmsCommand();
case 0x02: // GET_ALARM_COMMAND
return new GetAlarmCommand();
case 0x03: // RESET_ALARM_LOG_COMMAND
return new ResetAlarmLogCommand();
default:
return null;
}
}

@Override
public ZclCommand getResponseFromId(int commandId) {
switch (commandId) {
case 0x00: // ALARM_COMMAND
return new AlarmCommand();
case 0x01: // GET_ALARM_RESPONSE
return new GetAlarmResponse();
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* <p>
* Code is auto-generated. Modifications may be overwritten!
*/
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T21:33:25Z")
public class ZclBasicCluster extends ZclCluster {
/**
* The ZigBee Cluster Library Cluster ID
Expand Down Expand Up @@ -125,10 +125,9 @@ public class ZclBasicCluster extends ZclCluster {
*/
public static final int ATTR_SWBUILDID = 0x4000;

// Attribute initialisation
@Override
protected Map<Integer, ZclAttribute> initializeAttributes() {
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(14);
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(14);

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

@Override
protected Map<Integer, Class<? extends ZclCommand>> initializeClientCommands() {
Map<Integer, Class<? extends ZclCommand>> commandMap = new ConcurrentHashMap<>(1);

commandMap.put(0x0000, ResetToFactoryDefaultsCommand.class);

return commandMap;
}

/**
* Default constructor to create a Basic cluster.
*
Expand Down Expand Up @@ -1142,12 +1150,4 @@ public String getSwBuildId(final long refreshPeriod) {
public Future<CommandResult> resetToFactoryDefaultsCommand() {
return send(new ResetToFactoryDefaultsCommand());
}

@Override
public ZclCommand getResponseFromId(int commandId) {
switch (commandId) {
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* <p>
* Code is auto-generated. Modifications may be overwritten!
*/
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-09T15:23:12Z")
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2019-02-26T21:33:25Z")
public class ZclBinaryInputBasicCluster extends ZclCluster {
/**
* The ZigBee Cluster Library Cluster ID
Expand Down Expand Up @@ -165,10 +165,9 @@ public class ZclBinaryInputBasicCluster extends ZclCluster {
*/
public static final int ATTR_APPLICATIONTYPE = 0x0100;

// Attribute initialisation
@Override
protected Map<Integer, ZclAttribute> initializeAttributes() {
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<Integer, ZclAttribute>(9);
Map<Integer, ZclAttribute> attributeMap = new ConcurrentHashMap<>(9);

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

0 comments on commit e310f38

Please sign in to comment.