Skip to content

Commit

Permalink
Handle OnWithTimedOffCommand (#481)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson authored Sep 16, 2019
1 parent 4485a72 commit 45da9c4
Showing 1 changed file with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
Expand All @@ -38,6 +42,7 @@
import com.zsmartsystems.zigbee.zcl.clusters.onoff.OffCommand;
import com.zsmartsystems.zigbee.zcl.clusters.onoff.OffWithEffectCommand;
import com.zsmartsystems.zigbee.zcl.clusters.onoff.OnCommand;
import com.zsmartsystems.zigbee.zcl.clusters.onoff.OnWithTimedOffCommand;
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType;

/**
Expand All @@ -56,6 +61,9 @@ public class ZigBeeConverterSwitchOnoff extends ZigBeeBaseChannelConverter

private ZclReportingConfig configReporting;

private ScheduledExecutorService updateScheduler;
private ScheduledFuture<?> updateTimer = null;

@Override
public boolean initializeDevice() {
ZclOnOffCluster clientCluster = (ZclOnOffCluster) endpoint.getOutputCluster(ZclOnOffCluster.CLUSTER_ID);
Expand All @@ -70,9 +78,11 @@ public boolean initializeDevice() {
CommandResult bindResponse = bind(serverCluster).get();
if (bindResponse.isSuccess()) {
// Configure reporting
CommandResult reportingResponse = serverCluster
.setOnOffReporting(REPORTING_PERIOD_DEFAULT_MIN, REPORTING_PERIOD_DEFAULT_MAX).get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_HIGH, REPORTING_PERIOD_DEFAULT_MAX);
ZclAttribute attribute = serverCluster.getAttribute(ZclOnOffCluster.ATTR_ONOFF);
CommandResult reportingResponse = attribute
.setReporting(configReporting.getReportingTimeMin(), configReporting.getReportingTimeMax())
.get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_HIGH, configReporting.getPollingPeriod());
} else {
logger.debug("{}: Error 0x{} setting server binding", endpoint.getIeeeAddress(),
Integer.toHexString(bindResponse.getStatusCode()));
Expand Down Expand Up @@ -100,6 +110,8 @@ public boolean initializeDevice() {

@Override
public boolean initializeConverter() {
updateScheduler = Executors.newSingleThreadScheduledExecutor();

clusterOnOffClient = (ZclOnOffCluster) endpoint.getOutputCluster(ZclOnOffCluster.CLUSTER_ID);
clusterOnOffServer = (ZclOnOffCluster) endpoint.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
if (clusterOnOffClient == null && clusterOnOffServer == null) {
Expand Down Expand Up @@ -135,6 +147,8 @@ public void disposeConverter() {
if (clusterOnOffServer != null) {
clusterOnOffServer.removeAttributeListener(this);
}

updateScheduler.shutdownNow();
}

@Override
Expand Down Expand Up @@ -216,6 +230,13 @@ public boolean commandReceived(ZclCommand command) {
clusterOnOffClient.sendDefaultResponse(command, ZclStatus.SUCCESS);
return true;
}
if (command instanceof OnWithTimedOffCommand) {
OnWithTimedOffCommand timedCommand = (OnWithTimedOffCommand) command;
updateChannelState(OnOffType.ON);
clusterOnOffClient.sendDefaultResponse(command, ZclStatus.SUCCESS);
startOffTimer(timedCommand.getOnTime() * 100);
return true;
}
if (command instanceof OffCommand || command instanceof OffWithEffectCommand) {
updateChannelState(OnOffType.OFF);
clusterOnOffClient.sendDefaultResponse(command, ZclStatus.SUCCESS);
Expand All @@ -224,4 +245,20 @@ public boolean commandReceived(ZclCommand command) {

return false;
}

private void startOffTimer(int delay) {
if (updateTimer != null) {
updateTimer.cancel(true);
updateTimer = null;
}

updateTimer = updateScheduler.schedule(new Runnable() {
@Override
public void run() {
logger.debug("{}: OnOff auto OFF timer expired", endpoint.getIeeeAddress());
updateChannelState(OnOffType.OFF);
updateTimer = null;
}
}, delay, TimeUnit.MILLISECONDS);
}
}

0 comments on commit 45da9c4

Please sign in to comment.