Skip to content

Commit 4f2cb3d

Browse files
committed
Use ZclCommandListener to avoid duplicate responses
Signed-off-by: Chris Jackson <[email protected]>
1 parent ad00f2b commit 4f2cb3d

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZclTimeServer.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18-
import com.zsmartsystems.zigbee.ZigBeeCommand;
19-
import com.zsmartsystems.zigbee.ZigBeeCommandListener;
2018
import com.zsmartsystems.zigbee.ZigBeeNetworkManager;
19+
import com.zsmartsystems.zigbee.zcl.ZclCommand;
20+
import com.zsmartsystems.zigbee.zcl.ZclCommandListener;
2121
import com.zsmartsystems.zigbee.zcl.ZclStatus;
2222
import com.zsmartsystems.zigbee.zcl.clusters.ZclTimeCluster;
2323
import com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesCommand;
@@ -34,7 +34,7 @@
3434
* @author Chris Jackson
3535
*
3636
*/
37-
public class ZclTimeServer implements ZigBeeCommandListener {
37+
public class ZclTimeServer implements ZclCommandListener {
3838
/**
3939
* The {@link Logger}.
4040
*/
@@ -64,14 +64,12 @@ public class ZclTimeServer implements ZigBeeCommandListener {
6464

6565
protected ZclTimeServer(ZigBeeNetworkManager networkManager) {
6666
this.networkManager = networkManager;
67-
networkManager.addCommandListener(this);
6867
}
6968

7069
/**
7170
* Shuts down the time server and frees any resources
7271
*/
7372
protected void shutdown() {
74-
networkManager.removeCommandListener(this);
7573
}
7674

7775
/**
@@ -137,16 +135,16 @@ protected void setDst(ZigBeeUtcTime dstStart, ZigBeeUtcTime dstEnd, int dstOffse
137135
}
138136

139137
@Override
140-
public void commandReceived(ZigBeeCommand command) {
138+
public boolean commandReceived(ZclCommand command) {
141139
if (command.getClusterId() != ZclTimeCluster.CLUSTER_ID) {
142140
// TODO: This should check the message is addressed to the local NWK address
143-
return;
141+
return false;
144142
}
145143

146144
if (command instanceof ReadAttributesCommand) {
147145
ReadAttributesCommand readRequest = (ReadAttributesCommand) command;
148146
if (readRequest.getCommandDirection() != ZclCommandDirection.CLIENT_TO_SERVER) {
149-
return;
147+
return false;
150148
}
151149

152150
List<ReadAttributeStatusRecord> records = new ArrayList<>();
@@ -216,6 +214,19 @@ public void commandReceived(ZigBeeCommand command) {
216214
readResponse.setTransactionId(command.getTransactionId());
217215

218216
networkManager.sendCommand(readResponse);
217+
218+
return true;
219219
}
220+
221+
return false;
222+
}
223+
224+
/**
225+
* Adds a remote client to be managed by the local server
226+
*
227+
* @param timeClient the remote client
228+
*/
229+
public void addRemote(ZclTimeCluster timeClient) {
230+
timeClient.addCommandListener(this);
220231
}
221232
}

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/app/time/ZigBeeTimeExtension.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor
6666
/**
6767
* Our time server that handles any requests from remote clients
6868
*/
69-
private ZclTimeServer timeServer;
69+
private ZclTimeServer localTimeServer;
7070

7171
private ZigBeeUtcTime dstStart;
7272
private ZigBeeUtcTime dstEnd;
@@ -90,7 +90,6 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor
9090
* The worker thread that is scheduled periodically to check the synchronisation of remote devices
9191
*/
9292
private ScheduledFuture<?> workerJob;
93-
9493
/**
9594
* A map of all the time clients on the network
9695
*/
@@ -100,6 +99,7 @@ public class ZigBeeTimeExtension implements ZigBeeNetworkExtension, ZigBeeNetwor
10099
public ZigBeeStatus extensionInitialize(ZigBeeNetworkManager networkManager) {
101100
this.networkManager = networkManager;
102101
networkManager.addSupportedClientCluster(ZclTimeCluster.CLUSTER_ID);
102+
networkManager.addSupportedServerCluster(ZclTimeCluster.CLUSTER_ID);
103103
networkManager.addNetworkNodeListener(this);
104104

105105
return ZigBeeStatus.SUCCESS;
@@ -109,9 +109,9 @@ public ZigBeeStatus extensionInitialize(ZigBeeNetworkManager networkManager) {
109109
public ZigBeeStatus extensionStartup() {
110110
logger.debug("Time extension: startup");
111111

112-
timeServer = new ZclTimeServer(networkManager);
113-
timeServer.setMaster(master);
114-
timeServer.setSuperceding(superceding);
112+
localTimeServer = new ZclTimeServer(networkManager);
113+
localTimeServer.setMaster(master);
114+
localTimeServer.setSuperceding(superceding);
115115

116116
startWorker();
117117
return ZigBeeStatus.SUCCESS;
@@ -121,8 +121,8 @@ public ZigBeeStatus extensionStartup() {
121121
public void extensionShutdown() {
122122
networkManager.removeNetworkNodeListener(this);
123123

124-
if (timeServer != null) {
125-
timeServer.shutdown();
124+
if (localTimeServer != null) {
125+
localTimeServer.shutdown();
126126
}
127127

128128
logger.debug("Time extension: shutdown");
@@ -135,8 +135,8 @@ public void extensionShutdown() {
135135
*/
136136
public void setMaster(boolean master) {
137137
this.master = master;
138-
if (timeServer != null) {
139-
timeServer.setMaster(master);
138+
if (localTimeServer != null) {
139+
localTimeServer.setMaster(master);
140140
}
141141
}
142142

@@ -147,8 +147,8 @@ public void setMaster(boolean master) {
147147
*/
148148
public void setSuperceding(boolean superceding) {
149149
this.superceding = superceding;
150-
if (timeServer != null) {
151-
timeServer.setSuperceding(superceding);
150+
if (localTimeServer != null) {
151+
localTimeServer.setSuperceding(superceding);
152152
}
153153
}
154154

@@ -158,7 +158,7 @@ public void setSuperceding(boolean superceding) {
158158
* @return true if the server is a master clock source.
159159
*/
160160
public boolean isMaster() {
161-
return timeServer.isMaster();
161+
return localTimeServer.isMaster();
162162
}
163163

164164
/**
@@ -167,7 +167,7 @@ public boolean isMaster() {
167167
* @return true of the server is marked as a superceding server.
168168
*/
169169
public boolean isSuperceding() {
170-
return timeServer.isSuperceding();
170+
return localTimeServer.isSuperceding();
171171
}
172172

173173
/**
@@ -185,7 +185,7 @@ public void setDst(ZigBeeUtcTime dstStart, ZigBeeUtcTime dstEnd, int dstOffset)
185185

186186
logger.debug("Time extension: set DST start={}, end={}, offset={}", dstStart, dstEnd, dstOffset);
187187

188-
timeServer.setDst(dstStart, dstEnd, dstOffset);
188+
localTimeServer.setDst(dstStart, dstEnd, dstOffset);
189189

190190
// Update all the clients
191191
synchronized (timeClients) {
@@ -208,7 +208,7 @@ public ZigBeeUtcTime getLastUpdateTime(ZigBeeEndpointAddress address) {
208208
case CLIENT:
209209
return timeClients.get(address).getLastUpdate();
210210
case SERVER:
211-
return timeServer.getLastUpdateTime(address.getAddress());
211+
return localTimeServer.getLastUpdateTime(address.getAddress());
212212
case NONE:
213213
default:
214214
return null;
@@ -224,7 +224,7 @@ public ZigBeeUtcTime getLastUpdateTime(ZigBeeEndpointAddress address) {
224224
*/
225225
public ZigBeeTimeSetMethod getLastUpdateMethod(ZigBeeEndpointAddress address) {
226226
ZigBeeUtcTime clientTime = null;
227-
ZigBeeUtcTime serverTime = timeServer.getLastUpdateTime(address.getAddress());
227+
ZigBeeUtcTime serverTime = localTimeServer.getLastUpdateTime(address.getAddress());
228228

229229
ZclTimeClient client = timeClients.get(address);
230230
if (client != null) {
@@ -253,6 +253,10 @@ public void nodeAdded(ZigBeeNode node) {
253253
timeClients.put(timeServer.getZigBeeAddress(), timeClient);
254254
}
255255
}
256+
ZclTimeCluster timeClient = (ZclTimeCluster) endpoint.getOutputCluster(ZclTimeCluster.CLUSTER_ID);
257+
if (timeClient != null) {
258+
localTimeServer.addRemote(timeClient);
259+
}
256260
}
257261

258262
startWorker();

0 commit comments

Comments
 (0)