diff --git a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZclTimeServer.java b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZclTimeServer.java index 8cc8c5b530..7dd4eb9dc5 100644 --- a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZclTimeServer.java +++ b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZclTimeServer.java @@ -15,9 +15,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.zsmartsystems.zigbee.ZigBeeCommand; -import com.zsmartsystems.zigbee.ZigBeeCommandListener; import com.zsmartsystems.zigbee.ZigBeeNetworkManager; +import com.zsmartsystems.zigbee.zcl.ZclCommand; +import com.zsmartsystems.zigbee.zcl.ZclCommandListener; import com.zsmartsystems.zigbee.zcl.ZclStatus; import com.zsmartsystems.zigbee.zcl.clusters.ZclTimeCluster; import com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesCommand; @@ -34,7 +34,7 @@ * @author Chris Jackson * */ -public class ZclTimeServer implements ZigBeeCommandListener { +public class ZclTimeServer implements ZclCommandListener { /** * The {@link Logger}. */ @@ -64,14 +64,12 @@ public class ZclTimeServer implements ZigBeeCommandListener { protected ZclTimeServer(ZigBeeNetworkManager networkManager) { this.networkManager = networkManager; - networkManager.addCommandListener(this); } /** * Shuts down the time server and frees any resources */ protected void shutdown() { - networkManager.removeCommandListener(this); } /** @@ -137,16 +135,16 @@ protected void setDst(ZigBeeUtcTime dstStart, ZigBeeUtcTime dstEnd, int dstOffse } @Override - public void commandReceived(ZigBeeCommand command) { + public boolean commandReceived(ZclCommand command) { if (command.getClusterId() != ZclTimeCluster.CLUSTER_ID) { // TODO: This should check the message is addressed to the local NWK address - return; + return false; } if (command instanceof ReadAttributesCommand) { ReadAttributesCommand readRequest = (ReadAttributesCommand) command; if (readRequest.getCommandDirection() != ZclCommandDirection.CLIENT_TO_SERVER) { - return; + return false; } List records = new ArrayList<>(); @@ -216,6 +214,19 @@ public void commandReceived(ZigBeeCommand command) { readResponse.setTransactionId(command.getTransactionId()); networkManager.sendCommand(readResponse); + + return true; } + + return false; + } + + /** + * Adds a remote client to be managed by the local server + * + * @param timeClient the remote client + */ + public void addRemote(ZclTimeCluster timeClient) { + timeClient.addCommandListener(this); } } diff --git a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZigBeeTimeExtension.java b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZigBeeTimeExtension.java index 15b1d3222c..3f601fe289 100644 --- a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZigBeeTimeExtension.java +++ b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZigBeeTimeExtension.java @@ -66,7 +66,7 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor /** * Our time server that handles any requests from remote clients */ - private ZclTimeServer timeServer; + private ZclTimeServer localTimeServer; private ZigBeeUtcTime dstStart; private ZigBeeUtcTime dstEnd; @@ -90,7 +90,6 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor * The worker thread that is scheduled periodically to check the synchronisation of remote devices */ private ScheduledFuture workerJob; - /** * A map of all the time clients on the network */ @@ -100,6 +99,7 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor public ZigBeeStatus extensionInitialize(ZigBeeNetworkManager networkManager) { this.networkManager = networkManager; networkManager.addSupportedClientCluster(ZclTimeCluster.CLUSTER_ID); + networkManager.addSupportedServerCluster(ZclTimeCluster.CLUSTER_ID); networkManager.addNetworkNodeListener(this); return ZigBeeStatus.SUCCESS; @@ -109,9 +109,9 @@ public ZigBeeStatus extensionInitialize(ZigBeeNetworkManager networkManager) { public ZigBeeStatus extensionStartup() { logger.debug("Time extension: startup"); - timeServer = new ZclTimeServer(networkManager); - timeServer.setMaster(master); - timeServer.setSuperceding(superceding); + localTimeServer = new ZclTimeServer(networkManager); + localTimeServer.setMaster(master); + localTimeServer.setSuperceding(superceding); startWorker(); return ZigBeeStatus.SUCCESS; @@ -121,8 +121,8 @@ public ZigBeeStatus extensionStartup() { public void extensionShutdown() { networkManager.removeNetworkNodeListener(this); - if (timeServer != null) { - timeServer.shutdown(); + if (localTimeServer != null) { + localTimeServer.shutdown(); } logger.debug("Time extension: shutdown"); @@ -135,8 +135,8 @@ public void extensionShutdown() { */ public void setMaster(boolean master) { this.master = master; - if (timeServer != null) { - timeServer.setMaster(master); + if (localTimeServer != null) { + localTimeServer.setMaster(master); } } @@ -147,8 +147,8 @@ public void setMaster(boolean master) { */ public void setSuperceding(boolean superceding) { this.superceding = superceding; - if (timeServer != null) { - timeServer.setSuperceding(superceding); + if (localTimeServer != null) { + localTimeServer.setSuperceding(superceding); } } @@ -158,7 +158,7 @@ public void setSuperceding(boolean superceding) { * @return true if the server is a master clock source. */ public boolean isMaster() { - return timeServer.isMaster(); + return localTimeServer.isMaster(); } /** @@ -167,7 +167,7 @@ public boolean isMaster() { * @return true of the server is marked as a superceding server. */ public boolean isSuperceding() { - return timeServer.isSuperceding(); + return localTimeServer.isSuperceding(); } /** @@ -185,7 +185,7 @@ public void setDst(ZigBeeUtcTime dstStart, ZigBeeUtcTime dstEnd, int dstOffset) logger.debug("Time extension: set DST start={}, end={}, offset={}", dstStart, dstEnd, dstOffset); - timeServer.setDst(dstStart, dstEnd, dstOffset); + localTimeServer.setDst(dstStart, dstEnd, dstOffset); // Update all the clients synchronized (timeClients) { @@ -208,7 +208,7 @@ public ZigBeeUtcTime getLastUpdateTime(ZigBeeEndpointAddress address) { case CLIENT: return timeClients.get(address).getLastUpdate(); case SERVER: - return timeServer.getLastUpdateTime(address.getAddress()); + return localTimeServer.getLastUpdateTime(address.getAddress()); case NONE: default: return null; @@ -224,7 +224,7 @@ public ZigBeeUtcTime getLastUpdateTime(ZigBeeEndpointAddress address) { */ public ZigBeeTimeSetMethod getLastUpdateMethod(ZigBeeEndpointAddress address) { ZigBeeUtcTime clientTime = null; - ZigBeeUtcTime serverTime = timeServer.getLastUpdateTime(address.getAddress()); + ZigBeeUtcTime serverTime = localTimeServer.getLastUpdateTime(address.getAddress()); ZclTimeClient client = timeClients.get(address); if (client != null) { @@ -253,6 +253,10 @@ public void nodeAdded(ZigBeeNode node) { timeClients.put(timeServer.getZigBeeAddress(), timeClient); } } + ZclTimeCluster timeClient = (ZclTimeCluster) endpoint.getOutputCluster(ZclTimeCluster.CLUSTER_ID); + if (timeClient != null) { + localTimeServer.addRemote(timeClient); + } } startWorker();