Skip to content

Commit

Permalink
Merge pull request #19 from meju25/MySensors
Browse files Browse the repository at this point in the history
My sensors - added possibility to not revert state if no ack received
  • Loading branch information
tobof authored Jun 13, 2016
2 parents 97497db + 87e00bd commit 0054cef
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,16 @@
<description>The ID of the child of a node in the MySensors network
</description>
</parameter>
<parameter name="requestAck" type="boolean" required="true">
<parameter name="requestAck" type="boolean" required="false">
<label>Request ACK</label>
<description>Request ACK from Actuator</description>
<default>false</default>
</parameter>
<parameter name="revertState" type="boolean" required="false">
<label>Revert State</label>
<description>Revert state if no Ack received</description>
<default>true</default>
</parameter>
</config-description>
</thing-type>

Expand Down Expand Up @@ -581,6 +586,16 @@
<description>The ID of the child of a node in the MySensors network
</description>
</parameter>
<parameter name="requestAck" type="boolean" required="false">
<label>Request ACK</label>
<description>Request ACK from Actuator</description>
<default>false</default>
</parameter>
<parameter name="revertState" type="boolean" required="false">
<label>Revert State</label>
<description>Revert state if no Ack received</description>
<default>true</default>
</parameter>
</config-description>
</thing-type>

Expand Down Expand Up @@ -681,6 +696,16 @@
<description>The ID of the child of a node in the MySensors network
</description>
</parameter>
<parameter name="requestAck" type="boolean" required="false">
<label>Request ACK</label>
<description>Request ACK from Actuator</description>
<default>false</default>
</parameter>
<parameter name="revertState" type="boolean" required="false">
<label>Revert State</label>
<description>Revert state if no Ack received</description>
<default>true</default>
</parameter>
</config-description>
</thing-type>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public class MySensorsBindingConstants {
public final static int RESET_TIME = 5000;

public final static Map<Number, String> CHANNEL_MAP = new HashMap<Number, String>() {
/**
*
*/
private static final long serialVersionUID = -7970323220036599380L;

{
put(MYSENSORS_SUBTYPE_V_TEMP, CHANNEL_TEMP);
put(MYSENSORS_SUBTYPE_V_HUM, CHANNEL_HUM);
Expand Down Expand Up @@ -243,6 +248,11 @@ public class MySensorsBindingConstants {
};

public final static Map<Number, String> CHANNEL_MAP_INTERNAL = new HashMap<Number, String>() {
/**
*
*/
private static final long serialVersionUID = 6273187523631143905L;

{
put(MYSENSORS_SUBTYPE_I_VERSION, CHANNEL_VERSION);
put(MYSENSORS_SUBTYPE_I_BATTERY_LEVEL, CHANNEL_BATTERY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class MySensorsSensorConfiguration {
public String nodeId;
public String childId;
public boolean requestAck;
public boolean revertState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void statusUpdateReceived(MySensorsStatusUpdateEvent event) {
MySensorsMessage msg = event.getData();

// logger.debug("updateRecieved: " + msg.getDebugInfo());
// Do we get an ACK?
if (msg.getAck() == 1) {
logger.debug("ACK received!");
logger.debug(String.format("ACK received! Node: %d, Child: %d", msg.nodeId, msg.childId));
mysCon.removeMySensorsOutboundMessage(msg);
}

Expand Down Expand Up @@ -200,7 +200,7 @@ private void answerITimeMessage(MySensorsMessage msg) {
logger.info("I_TIME request received from {}, answering...", msg.nodeId);

String time = Long.toString(System.currentTimeMillis() / 1000);
MySensorsMessage newMsg = new MySensorsMessage(msg.nodeId, msg.childId, MYSENSORS_MSG_TYPE_INTERNAL, 0,
MySensorsMessage newMsg = new MySensorsMessage(msg.nodeId, msg.childId, MYSENSORS_MSG_TYPE_INTERNAL, 0, false,
MYSENSORS_SUBTYPE_I_TIME, time);
mysCon.addMySensorsOutboundMessage(newMsg);

Expand All @@ -214,7 +214,7 @@ private void answerITimeMessage(MySensorsMessage msg) {
private void answerIConfigMessage(MySensorsMessage msg) {
logger.info("I_CONFIG request received from {}, answering...", msg.nodeId);

MySensorsMessage newMsg = new MySensorsMessage(msg.nodeId, msg.childId, MYSENSORS_MSG_TYPE_INTERNAL, 0,
MySensorsMessage newMsg = new MySensorsMessage(msg.nodeId, msg.childId, MYSENSORS_MSG_TYPE_INTERNAL, 0, false,
MYSENSORS_SUBTYPE_I_CONFIG, iConfig);
mysCon.addMySensorsOutboundMessage(newMsg);

Expand All @@ -228,7 +228,7 @@ private void answerIDRequest() {

int newId = getFreeId();
givenIds.add(newId);
MySensorsMessage newMsg = new MySensorsMessage(255, 255, 3, 0, 4, newId + "");
MySensorsMessage newMsg = new MySensorsMessage(255, 255, 3, 0, false, 4, newId + "");
mysCon.addMySensorsOutboundMessage(newMsg);
logger.info("New Node in the MySensors network has requested an ID. ID is: {}", newId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class MySensorsHandler extends BaseThingHandler implements MySensorsUpdat
private int nodeId = 0;
private int childId = 0;
private boolean requestAck = false;
private boolean revertState = true;

private Map<Integer, String> oldMsgContent = new HashMap<>();

Expand All @@ -61,7 +62,9 @@ public void initialize() {
nodeId = Integer.parseInt(configuration.nodeId);
childId = Integer.parseInt(configuration.childId);
requestAck = configuration.requestAck;

revertState = configuration.revertState;
logger.debug(
String.format("Configuration: node %d, chiledId: %d, revertState: %b", nodeId, childId, revertState));
updateStatus(ThingStatus.ONLINE);
}

Expand Down Expand Up @@ -93,7 +96,6 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (requestAck) {
int_requestack = 1;
}

if (channelUID.getId().equals(CHANNEL_STATUS)) {

subType = MYSENSORS_SUBTYPE_V_STATUS;
Expand Down Expand Up @@ -180,8 +182,8 @@ public void handleCommand(ChannelUID channelUID, Command command) {
msgPayload = "";
}

MySensorsMessage newMsg = new MySensorsMessage(nodeId, childId, MYSENSORS_MSG_TYPE_SET, int_requestack, subType,
msgPayload);
MySensorsMessage newMsg = new MySensorsMessage(nodeId, childId, MYSENSORS_MSG_TYPE_SET, int_requestack,
revertState, subType, msgPayload);

String oldPayload = oldMsgContent.get(subType);
if (oldPayload == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ public void removeMySensorsOutboundMessage(MySensorsMessage msg) {
if (iterator != null) {
while (iterator.hasNext()) {
MySensorsMessage msgInQueue = iterator.next();
// logger.debug("Msg in Queue: " + msgInQueue.getDebugInfo());
if (msgInQueue.getNodeId() == msg.getNodeId() && msgInQueue.getChildId() == msg.getChildId()
&& msgInQueue.getMsgType() == msg.getMsgType() && msgInQueue.getSubType() == msg.getSubType()
&& msgInQueue.getAck() == msg.getAck() && msgInQueue.getMsg().equals(msg.getMsg())) {
iterator.remove();
// logger.debug("Message removed: " + msg.getDebugInfo());
} else {
logger.debug("Message NOT removed");
logger.debug("Message NOT removed: " + msg.getDebugInfo());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MySensorsMessage {
public int childId = 0;
public int msgType = 0;
public int ack = 0;
public boolean revert = true;
public int subType = 0;
public String msg = "";
public String oldMsg = "";
Expand All @@ -34,22 +35,24 @@ public MySensorsMessage() {

}

public MySensorsMessage(int nodeId, int childId, int msgType, int ack, int subType, String msg) {
public MySensorsMessage(int nodeId, int childId, int msgType, int ack, boolean revert, int subType, String msg) {
this.nodeId = nodeId;
this.childId = childId;
this.msgType = msgType;
this.ack = ack;
this.revert = revert;
this.subType = subType;
this.msg = msg;
}

public void printDebug() {
logger.debug("nodeId: " + this.nodeId);
logger.debug("childId: " + this.childId);
logger.debug("msgType: " + this.msgType);
logger.debug("ack: " + this.ack);
logger.debug("subType: " + this.subType);
logger.debug("msg: " + this.msg);
logger.debug(String.format("nodeId: %d, childId: %d, msgType: %d, ack: %d, revert: %b, subType: %d ,msg: %s",
this.nodeId, this.childId, this.msgType, this.ack, this.revert, this.subType, this.msg));
}

public String getDebugInfo() {
return String.format("nodeId: %d, childId: %d, msgType: %d, ack: %d, revert: %b, subType: %d ,msg: %s",
this.nodeId, this.childId, this.msgType, this.ack, this.revert, this.subType, this.msg);
}

public int getNodeId() {
Expand Down Expand Up @@ -80,6 +83,10 @@ public int getAck() {
return ack;
}

public boolean getRevert() {
return revert;
}

public void setAck(int ack) {
this.ack = ack;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class MySensorsWriter implements MySensorsUpdateListener, Runnab
protected ExecutorService executor = Executors.newSingleThreadExecutor();
protected Future<?> future = null;

private static final MySensorsMessage I_VERSION_MESSAGE = new MySensorsMessage(0, 0, 3, 0, 2, "");
private static final MySensorsMessage I_VERSION_MESSAGE = new MySensorsMessage(0, 0, 3, 0, false, 2, "");

protected int sendDelay = 0;

Expand Down Expand Up @@ -69,13 +69,15 @@ public void run() {
logger.warn("NO ACK from nodeId: " + msg.getNodeId());
if (msg.getOldMsg().isEmpty()) {
logger.debug("No old status know to revert to!");
} else {
} else if (msg.getRevert()) {
logger.debug("Reverting status!");
msg.setMsg(msg.getOldMsg());
MySensorsStatusUpdateEvent event = new MySensorsStatusUpdateEvent(msg);
for (MySensorsUpdateListener mySensorsEventListener : mysCon.updateListeners) {
mySensorsEventListener.statusUpdateReceived(event);
}
} else if (!msg.getRevert()) {
logger.debug("Not reverted due to configuration!");
}
continue;
}
Expand Down

0 comments on commit 0054cef

Please sign in to comment.