Skip to content

Commit

Permalink
Merge pull request openhab#7 from digitaldan/zwave-initialisation
Browse files Browse the repository at this point in the history
Zwave initialisation
  • Loading branch information
cdjackson committed Dec 2, 2014
2 parents d3073dc + 6741692 commit 0163e5a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessageClass;
Expand Down Expand Up @@ -46,7 +48,7 @@ public class ZWaveThermostatFanModeCommandClass extends ZWaveCommandClass
private static final byte THERMOSTAT_FAN_MODE_SUPPORTED_GET = 0x4;
private static final byte THERMOSTAT_FAN_MODE_SUPPORTED_REPORT = 0x5;

private final Map<FanModeType, FanMode> fanModes = new HashMap<FanModeType, FanMode>();
private final Set<FanModeType> fanModeTypes = new HashSet<FanModeType>();

@XStreamOmitField
private boolean initialiseDone = false;
Expand Down Expand Up @@ -114,8 +116,7 @@ public void handleApplicationCommandRequest(SerialMessage serialMessage,
// (n)th bit is set. n is the index for the fanMode type enumeration.
FanModeType fanModeTypeToAdd = FanModeType.getFanModeType(index);
if(fanModeTypeToAdd != null){
FanMode newFanMode = new FanMode(fanModeTypeToAdd);
this.fanModes.put(fanModeTypeToAdd, newFanMode);
fanModeTypes.add(fanModeTypeToAdd);
logger.debug("NODE {}: Added Fan Mode type {} ({})", this.getNode().getNodeId(), fanModeTypeToAdd.getLabel(), index);
}
else {
Expand Down Expand Up @@ -160,13 +161,10 @@ protected void processThermostatFanModeReport(SerialMessage serialMessage, int o
}

// fanMode type seems to be supported, add it to the list.
FanMode fanMode = fanModes.get(fanModeType);
if (fanMode == null) {
fanMode = new FanMode(fanModeType);
this.fanModes.put(fanModeType, fanMode);
}
fanMode.setInitialised();

if(!fanModeTypes.contains(fanModeType))
fanModeTypes.add(fanModeType);

dynamicDone = true;
logger.debug("NODE {}: Thermostat Fan Mode Report value = {}", this.getNode().getNodeId(), fanModeType.getLabel());
ZWaveCommandClassValueEvent zEvent = new ZWaveCommandClassValueEvent(this.getNode().getNodeId(), endpoint, this.getCommandClass(), new BigDecimal(value));
this.getController().notifyEventListeners(zEvent);
Expand Down Expand Up @@ -237,11 +235,11 @@ public SerialMessage getSupportedMessage() {
@Override
public SerialMessage setValueMessage(int value) {

if(fanModes.isEmpty()) {
if(fanModeTypes.isEmpty()) {
return this.getSupportedMessage();
}

if(!fanModes.containsKey(FanModeType.getFanModeType(value))) {
if(!fanModeTypes.contains(FanModeType.getFanModeType(value))) {
logger.error("NODE {}: Unsupported fanMode type {}", value, this.getNode().getNodeId());

return null;
Expand Down Expand Up @@ -325,29 +323,4 @@ public String getLabel() {
return label;
}
}

/**
* Class to hold fan state
* @author Chris Jackson
*/
private class FanMode {
FanModeType fanModeType;
boolean initialised = false;

public FanMode(FanModeType type) {
fanModeType = type;
}

public FanModeType getFanModeType() {
return fanModeType;
}

public void setInitialised() {
initialised = true;
}

public boolean getInitialised() {
return initialised;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessageClass;
Expand All @@ -21,6 +23,7 @@
import org.openhab.binding.zwave.internal.protocol.ZWaveController;
import org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint;
import org.openhab.binding.zwave.internal.protocol.ZWaveNode;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveThermostatModeCommandClass.ModeType;
import org.openhab.binding.zwave.internal.protocol.event.ZWaveCommandClassValueEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,8 +45,8 @@ public class ZWaveThermostatFanStateCommandClass extends ZWaveCommandClass
private static final byte THERMOSTAT_FAN_STATE_GET = 0x2;
private static final byte THERMOSTAT_FAN_STATE_REPORT = 0x3;

private final Map<FanStateType, FanState> fanStates = new HashMap<FanStateType, FanState>();

private final Set<FanStateType> fanStateTypes = new HashSet<FanStateType>();
@XStreamOmitField
private boolean dynamicDone = false;

Expand Down Expand Up @@ -117,13 +120,11 @@ protected void processThermostatFanStateReport(SerialMessage serialMessage, int
}

// fanState type seems to be supported, add it to the list.
FanState fanState = fanStates.get(fanStateType);
if (fanState == null) {
fanState = new FanState(fanStateType);
fanStates.put(fanStateType, fanState);
}
fanState.setInitialised();

if(!fanStateTypes.contains(fanStateType))
fanStateTypes.add(fanStateType);

dynamicDone = true;

logger.debug("NODE {}: Thermostat fan state Report value = {}", this.getNode().getNodeId(), fanStateType.getLabel());
ZWaveCommandClassValueEvent zEvent = new ZWaveCommandClassValueEvent(this.getNode().getNodeId(), endpoint, this.getCommandClass(), new BigDecimal(value));
this.getController().notifyEventListeners(zEvent);
Expand Down Expand Up @@ -234,29 +235,4 @@ public String getLabel() {
return label;
}
}

/**
* Class to hold fan state
* @author Chris Jackson
*/
private class FanState {
FanStateType fanStateType;
boolean initialised = false;

public FanState(FanStateType type) {
fanStateType = type;
}

public FanStateType getFanStateType() {
return fanStateType;
}

public void setInitialised() {
initialised = true;
}

public boolean getInitialised() {
return initialised;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.SerialMessage.SerialMessageClass;
Expand Down Expand Up @@ -46,7 +48,7 @@ public class ZWaveThermostatModeCommandClass extends ZWaveCommandClass
private static final byte THERMOSTAT_MODE_SUPPORTED_GET = 0x4;
private static final byte THERMOSTAT_MODE_SUPPORTED_REPORT = 0x5;

private final Map<ModeType, Mode> modes = new HashMap<ModeType, Mode>();
private final Set<ModeType> modeTypes = new HashSet<ModeType>();

@XStreamOmitField
private boolean initialiseDone = false;
Expand Down Expand Up @@ -113,8 +115,7 @@ public void handleApplicationCommandRequest(SerialMessage serialMessage,
// (n)th bit is set. n is the index for the mode type enumeration.
ModeType modeTypeToAdd = ModeType.getModeType(index);
if(modeTypeToAdd != null){
Mode newMode = new Mode(modeTypeToAdd);
this.modes.put(modeTypeToAdd, newMode);
this.modeTypes.add(modeTypeToAdd);
logger.debug("NODE {}: Added mode type {} ({})", this.getNode().getNodeId(), modeTypeToAdd.getLabel(), index);
}
else {
Expand Down Expand Up @@ -159,13 +160,12 @@ protected void processThermostatModeReport(SerialMessage serialMessage, int offs
}

// mode type seems to be supported, add it to the list.
Mode mode = modes.get(modeType);
if (mode == null) {
mode = new Mode(modeType);
modes.put(modeType, mode);
if (!modeTypes.contains(modeType)) {
modeTypes.add(modeType);
}
mode.setInitialised();


dynamicDone = true;

logger.debug("NODE {}: Thermostat Mode Report, value = {}", this.getNode().getNodeId(), modeType.getLabel());
ZWaveCommandClassValueEvent zEvent = new ZWaveCommandClassValueEvent(this.getNode().getNodeId(), endpoint, this.getCommandClass(), new BigDecimal(value));
this.getController().notifyEventListeners(zEvent);
Expand Down Expand Up @@ -235,16 +235,15 @@ public SerialMessage getSupportedMessage() {
@Override
public SerialMessage setValueMessage(int value) {

logger.debug("NODE {}: setValueMessage {}, modeType empty {}", this.getNode().getNodeId(), value, modes.isEmpty());
logger.debug("NODE {}: setValueMessage {}, modeType empty {}", this.getNode().getNodeId(), value, modeTypes.isEmpty());

//if we do not have any mode types yet, get them
if(modes.isEmpty()) {
if(modeTypes.isEmpty()) {
return this.getSupportedMessage();
}

if(!modes.containsKey(ModeType.getModeType(value))){
if(modeTypes.contains(ModeType.getModeType(value))){
logger.error("NODE {}: Unsupported mode type {}", this.getNode().getNodeId(), value);

return null;
}

Expand Down Expand Up @@ -334,29 +333,4 @@ public String getLabel() {
return label;
}
}

/**
* Class to hold fan state
* @author Chris Jackson
*/
private class Mode {
ModeType modeType;
boolean initialised = false;

public Mode(ModeType type) {
modeType = type;
}

public ModeType getModeType() {
return modeType;
}

public void setInitialised() {
initialised = true;
}

public boolean getInitialised() {
return initialised;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class ZWaveThermostatOperatingStateCommandClass extends ZWaveCommandClass
private static final byte THERMOSTAT_OPERATING_STATE_GET = 0x2;
private static final byte THERMOSTAT_OPERATING_STATE_REPORT = 0x3;

private final Map<OperatingStateType, OperatingState> operatingStates = new HashMap<OperatingStateType, OperatingState>();

@XStreamOmitField
private boolean dynamicDone = false;

Expand Down Expand Up @@ -116,14 +114,8 @@ protected void processThermostatOperatingStateReport(SerialMessage serialMessage
return;
}

// operatingState type seems to be supported, add it to the list.
OperatingState state = operatingStates.get(operatingStateType);
if (state == null) {
state = new OperatingState(operatingStateType);
operatingStates.put(operatingStateType, state);
}
state.setInitialised();

dynamicDone = true;

logger.debug("NODE {}: Operating State Type = {} ({})", this.getNode().getNodeId(), operatingStateType.getLabel(), value);

logger.debug("NODE {}: Thermostat Operating State Report value = {}", this.getNode().getNodeId(), operatingStateType.getLabel());
Expand Down Expand Up @@ -236,29 +228,4 @@ public String getLabel() {
return label;
}
}

/**
* Class to hold operating state
* @author Chris Jackson
*/
private class OperatingState {
OperatingStateType operatingStateType;
boolean initialised = false;

public OperatingState(OperatingStateType type) {
operatingStateType = type;
}

public OperatingStateType getModeType() {
return operatingStateType;
}

public void setInitialised() {
initialised = true;
}

public boolean getInitialised() {
return initialised;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class ZWaveThermostatSetpointCommandClass extends ZWaveCommandClass
private static final byte THERMOSTAT_SETPOINT_SUPPORTED_GET = 0x4;
private static final byte THERMOSTAT_SETPOINT_SUPPORTED_REPORT = 0x5;

//Some Thermostats (Honywell) will not report on all the setpoints they claim to support.
//If we try this many times for the initial dynamic queries, give up.
private static final int MAX_DYNAMIC_TRIES = 5;

private final Map<SetpointType, Setpoint> setpoints = new HashMap<SetpointType, Setpoint>();

@XStreamOmitField
Expand Down Expand Up @@ -206,10 +210,12 @@ public Collection<SerialMessage> getDynamicValues(boolean refresh) {
ArrayList<SerialMessage> result = new ArrayList<SerialMessage>();
for (Map.Entry<SetpointType, Setpoint> entry : this.setpoints.entrySet()) {
if(refresh == true || entry.getValue().getInitialised() == false) {
if(getMessage(entry.getValue().getSetpointType()) == null){
SerialMessage mesg = getMessage(entry.getValue().getSetpointType());
entry.getValue().incrementInitCount();
if(mesg == null){
logger.warn("NODE {}: Ignoring null setpointType in setpointTypes", this.getNode().getNodeId());
} else {
result.add(getMessage(entry.getValue().getSetpointType()));
result.add(mesg);
}
}
}
Expand Down Expand Up @@ -239,7 +245,7 @@ public SerialMessage getMessage(SetpointType setpointType) {
return null;
}

logger.debug("NODE {}: Creating new message for application command THERMOSTAT_SETPOINT_GET", this.getNode().getNodeId());
logger.debug("NODE {}: Creating new message for application command THERMOSTAT_SETPOINT_GET ({})", this.getNode().getNodeId(),setpointType.getLabel());
SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.SendData, SerialMessagePriority.Get);
byte[] payload = {
(byte) this.getNode().getNodeId(),
Expand Down Expand Up @@ -402,7 +408,8 @@ public String getLabel() {
private class Setpoint {
SetpointType setpointType;
boolean initialised = false;

int initCount = 0;

public Setpoint(SetpointType type) {
setpointType = type;
}
Expand All @@ -413,11 +420,20 @@ public SetpointType getSetpointType() {

public void setInitialised() {
initialised = true;
initCount = 0;
}

public boolean getInitialised() {
return initialised;
}

public void incrementInitCount(){
initCount++;
if(initCount >= MAX_DYNAMIC_TRIES) {
setInitialised();
logger.warn("Reached max tries to init the setpont {}, this will be our last attempt ", setpointType.getLabel());
}
}
}

/**
Expand Down

0 comments on commit 0163e5a

Please sign in to comment.