Skip to content

Commit

Permalink
Use ZclCommandListener to avoid duplicate responses
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson committed May 18, 2024
1 parent ad00f2b commit 4f2cb3d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +34,7 @@
* @author Chris Jackson
*
*/
public class ZclTimeServer implements ZigBeeCommandListener {
public class ZclTimeServer implements ZclCommandListener {
/**
* The {@link Logger}.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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<ReadAttributeStatusRecord> records = new ArrayList<>();
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4f2cb3d

Please sign in to comment.