Make sure the switch states stay in sync across zwave controller nodes. Allow switches to be grouped in a hierarchy.

This commit is contained in:
MarkBryanMilligan 2021-07-19 15:22:06 -05:00
parent d8da4df1e3
commit 8221e8ebd5
3 changed files with 24 additions and 7 deletions

View File

@ -14,6 +14,7 @@ public class Switch {
private String room; private String room;
private String name; private String name;
private int nodeId; private int nodeId;
private int parentNodeId;
private int level; private int level;
private int gpioPin; private int gpioPin;
private boolean primary; private boolean primary;
@ -82,6 +83,14 @@ public class Switch {
nodeId = _nodeId; nodeId = _nodeId;
} }
public int getParentNodeId() {
return parentNodeId;
}
public void setParentNodeId(int _parentNodeId) {
parentNodeId = _parentNodeId;
}
public int getLevel() { public int getLevel() {
return level; return level;
} }

View File

@ -32,6 +32,7 @@ public class SwitchSerializer extends AbstractDaoSerializer<Switch>
d.put("room", _o.getRoom()); d.put("room", _o.getRoom());
d.put("name", _o.getName()); d.put("name", _o.getName());
d.put("node_id", _o.getNodeId()); d.put("node_id", _o.getNodeId());
d.put("parent_node_id", _o.getParentNodeId());
d.put("level", _o.getLevel()); d.put("level", _o.getLevel());
d.put("gpio_pin", _o.getGpioPin()); d.put("gpio_pin", _o.getGpioPin());
d.put("primary", _o.isPrimary()); d.put("primary", _o.isPrimary());
@ -53,6 +54,7 @@ public class SwitchSerializer extends AbstractDaoSerializer<Switch>
o.setRoom(DaoSerializer.getString(_d, "room")); o.setRoom(DaoSerializer.getString(_d, "room"));
o.setName(DaoSerializer.getString(_d, "name")); o.setName(DaoSerializer.getString(_d, "name"));
o.setNodeId(DaoSerializer.getInteger(_d, "node_id")); o.setNodeId(DaoSerializer.getInteger(_d, "node_id"));
o.setParentNodeId(DaoSerializer.getInteger(_d, "parent_node_id"));
o.setLevel(DaoSerializer.getInteger(_d, "level")); o.setLevel(DaoSerializer.getInteger(_d, "level"));
o.setGpioPin(DaoSerializer.getInteger(_d, "gpio_pin")); o.setGpioPin(DaoSerializer.getInteger(_d, "gpio_pin"));
o.setPrimary(DaoSerializer.getBoolean(_d, "primary")); o.setPrimary(DaoSerializer.getBoolean(_d, "primary"));

View File

@ -129,7 +129,7 @@ public class ZWaveApp {
if (CollectionUtils.anyQualify(mySwitches.values(), Switch::isThermometerUrlValid)) { if (CollectionUtils.anyQualify(mySwitches.values(), Switch::isThermometerUrlValid)) {
timer.scheduleAtFixedRate(new ThermostatTask(), 0, 30000); timer.scheduleAtFixedRate(new ThermostatTask(), 0, 30000);
} }
if (CollectionUtils.anyQualify(mySwitches.values(), Switch::isRelay)) { if (CollectionUtils.anyQualify(mySwitches.values(), _s->_s.isRelay() || _s.isRelayButton())) {
relayController = new RelayController(); relayController = new RelayController();
} }
List<Switch> securitySwitches = CollectionUtils.filter(mySwitches.values(), Switch::isSecurity); List<Switch> securitySwitches = CollectionUtils.filter(mySwitches.values(), Switch::isSecurity);
@ -314,13 +314,15 @@ public class ZWaveApp {
} else if (sw.isZWaveThermostat()) { } else if (sw.isZWaveThermostat()) {
controller.send(new ThermostatSetPointSetRequest((byte) sw.getNodeId(), sw.getThermostatMode() == ThermostatMode.COOL ? ThermostatSetPointIndex.COOLING : ThermostatSetPointIndex.HEATING, _level)); controller.send(new ThermostatSetPointSetRequest((byte) sw.getNodeId(), sw.getThermostatMode() == ThermostatMode.COOL ? ThermostatSetPointIndex.COOLING : ThermostatSetPointIndex.HEATING, _level));
} else if (sw.isRelay()) { } else if (sw.isRelay()) {
relayController.setRelay(sw.getGpioPin(), sw.getLevel() > 0); relayController.setRelay(sw.getGpioPin(), sw.getLowLevel() > 0 ? sw.getLevel() == 0 : sw.getLevel() > 0);
} else if (sw.isRelayButton()) { } else if (sw.isRelayButton()) {
relayController.setRelay(sw.getGpioPin(), true); logger.info("Toggling relay " + sw.getFullDisplay() + " on");
relayController.setRelay(sw.getGpioPin(), sw.getLowLevel() == 0);
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
relayController.setRelay(sw.getGpioPin(), false); logger.info("Toggling relay " + sw.getFullDisplay() + " off");
relayController.setRelay(sw.getGpioPin(), sw.getLowLevel() > 0);
} }
}, 250); }, 250);
} else { } else {
@ -349,8 +351,12 @@ public class ZWaveApp {
} }
public void updateSwitch(Switch _sw) { public void updateSwitch(Switch _sw) {
switches.put(_sw.getNodeId(), _sw); logger.info("Received update for switch {} level {}", _sw.getFullDisplay(), _sw.getLevel());
mySwitches.put(_sw.getNodeId(), _sw); Switch sw = CollectionUtils.filterOne(config.getSwitches(), _s->_s.getNodeId() == _sw.getNodeId());
if (sw != null) {
sw.setLevel( _sw.getLevel());
sw.setHold( _sw.isHold());
}
setSwitchLevel(_sw.getNodeId(), _sw.getLevel(), false); setSwitchLevel(_sw.getNodeId(), _sw.getLevel(), false);
} }
@ -435,7 +441,7 @@ public class ZWaveApp {
} }
private void setGroupSwitchLevel(Switch _primary, int _level) { private void setGroupSwitchLevel(Switch _primary, int _level) {
if ((_primary == null) || !config.isMySwitch(_primary)) if ((_primary == null) || !config.isMySwitch(_primary) || (controller == null))
return; return;
List<Switch> nodes = CollectionUtils.asArrayList(_primary); List<Switch> nodes = CollectionUtils.asArrayList(_primary);
nodes.addAll(CollectionUtils.filter(peers.get(_primary.getNodeId()), _p->!_p.isPrimary())); nodes.addAll(CollectionUtils.filter(peers.get(_primary.getNodeId()), _p->!_p.isPrimary()));