Skip to content

Commit

Permalink
[caddx] Add new channels and support for ignoring zone status transit…
Browse files Browse the repository at this point in the history
…ions (openhab#10923)

* Cleanup of binding configuration classes
* Added context to CaddxMessage
* Logging enhancements
* Added support for ignoring Zone Status Transitions

Signed-off-by: Georgios Moutsos <georgios.moutsos@gmail.com>
Signed-off-by: dw-8 <davy.wong.on+github@gmail.com>
  • Loading branch information
jossuar authored and dw-8 committed Jul 25, 2021
1 parent 583a7e1 commit a0a9bc1
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 153 deletions.
3 changes: 3 additions & 0 deletions bundles/org.openhab.binding.caddx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Caddx Alarm things support a variety of channels as seen below in the following
| panel_primary_keypad_function_without_pin | Switch | Configuration | Primary Keypad Function without PIN |
| panel_secondary_keypad_function | Switch | Configuration | Secondary Keypad Function |
| panel_zone_bypass_toggle | Switch | Configuration | Zone Bypass Toggle |
| panel_ac_fail | Switch | Configuration | AC fail |
| panel_ac_power_on | Switch | Configuration | AC Power on |
| panel_low_battery_memory | Switch | Configuration | Low Battery Memory |
| partition_bypass_code_required | Switch | Partition Condition | Bypass code required |
| partition_fire_trouble | Switch | Partition Condition | Fire trouble |
| partition_fire | Switch | Partition Condition | Fire |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ public void stop() {

@SuppressWarnings("null")
private void messageDispatchLoop() {
int @Nullable [] expectedMessageNumbers = null;

@Nullable
int[] expectedMessageNumbers = null;
CaddxMessage outgoingMessage = null;
boolean skipTransmit = true;
CaddxMessageContext context = null;

try {
// loop until the thread is interrupted, sending out messages
while (!Thread.currentThread().isInterrupted()) {
// Initialize the state
outgoingMessage = null;
context = null;
expectedMessageNumbers = null;

if (!skipTransmit) {
Expand All @@ -203,6 +203,7 @@ private void messageDispatchLoop() {
out.flush();

expectedMessageNumbers = outgoingMessage.getReplyMessageNumbers();
context = outgoingMessage.getContext();

// Log message
if (logger.isDebugEnabled()) {
Expand Down Expand Up @@ -248,10 +249,12 @@ private void messageDispatchLoop() {
if (incomingMessage.hasAcknowledgementFlag()) {
if (incomingMessage.isChecksumCorrect()) {
// send ACK
transmitFirst(new CaddxMessage(CaddxMessageType.POSITIVE_ACKNOWLEDGE, ""));
transmitFirst(new CaddxMessage(CaddxMessageContext.NONE,
CaddxMessageType.POSITIVE_ACKNOWLEDGE, ""));
} else {
// Send NAK
transmitFirst(new CaddxMessage(CaddxMessageType.NEGATIVE_ACKNOWLEDGE, ""));
transmitFirst(new CaddxMessage(CaddxMessageContext.NONE,
CaddxMessageType.NEGATIVE_ACKNOWLEDGE, ""));
}
}
}
Expand Down Expand Up @@ -289,7 +292,10 @@ private void messageDispatchLoop() {
if (incomingMessage != null) {
if (incomingMessage.isChecksumCorrect()) {
for (CaddxPanelListener listener : listenerQueue) {
listener.caddxMessage(this, incomingMessage);
if (context != null) {
incomingMessage.setContext(context);
}
listener.caddxMessage(incomingMessage);
}
} else {
logger.warn(
Expand Down Expand Up @@ -367,7 +373,7 @@ private void offerCaddxMessage() throws InterruptedException {
logger.trace("Offering received message");

// Full message received in data byte array
CaddxMessage caddxMessage = new CaddxMessage(message, true);
CaddxMessage caddxMessage = new CaddxMessage(CaddxMessageContext.NONE, message, true);
if (!exchanger.offer(caddxMessage, 3, TimeUnit.SECONDS)) {
logger.debug("Offered message was not received");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ public CaddxMessage getCaddxMessage() {
public @Nullable Integer getKeypad() {
return keypad;
}

/**
* Returns a string representation of a CaddxEvent.
*
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

sb.append(String.format("partition: %d, zone: %d, keypad: %d\r\n", partition, zone, keypad));
sb.append(caddxMessage.toString());

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class CaddxMessage {
private final byte checksum2In;
private final byte checksum1Calc;
private final byte checksum2Calc;
private CaddxMessageContext context;

public CaddxMessage(byte[] message, boolean withChecksum) {
public CaddxMessage(CaddxMessageContext context, byte[] message, boolean withChecksum) {
if (withChecksum && message.length < 3) {
logger.debug("CaddxMessage: The message should be at least 3 bytes long.");
throw new IllegalArgumentException("The message should be at least 3 bytes long");
Expand All @@ -52,6 +53,7 @@ public CaddxMessage(byte[] message, boolean withChecksum) {
}

// Received data
this.context = context;
byte[] msg = message;

// Fill in the checksum
Expand Down Expand Up @@ -94,14 +96,15 @@ public CaddxMessage(byte[] message, boolean withChecksum) {
processCaddxMessage();
}

public CaddxMessage(CaddxMessageType type, String data) {
public CaddxMessage(CaddxMessageContext context, CaddxMessageType type, String data) {
int length = type.length;
String[] tokens = data.split("\\,");
if (length != 1 && tokens.length != length - 1) {
logger.debug("token.length should be length-1. token.length={}, length={}", tokens.length, length);
throw new IllegalArgumentException("CaddxMessage: data has not the correct format.");
}

this.context = context;
byte[] msg = new byte[length];
msg[0] = (byte) type.number;
for (int i = 0; i < length - 1; i++) {
Expand Down Expand Up @@ -133,6 +136,14 @@ public CaddxMessage(CaddxMessageType type, String data) {
processCaddxMessage();
}

public CaddxMessageContext getContext() {
return context;
}

public void setContext(CaddxMessageContext context) {
this.context = context;
}

public byte getChecksum1In() {
return checksum1In;
}
Expand Down Expand Up @@ -259,6 +270,9 @@ public String toString() {
return "Unknown message type";
}

sb.append(String.format("Context: %s", context.toString()));
sb.append(System.lineSeparator());

sb.append("Message: ");
sb.append(String.format("%2s", Integer.toHexString(message[0])));
sb.append(" ");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.caddx.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Used to map thing types from the binding string to a ENUM value.
*
* @author Georgios Moutsos - Initial contribution
*/
@NonNullByDefault
public enum CaddxMessageContext {
NONE,
DISCOVERY,
COMMAND;
}
Loading

0 comments on commit a0a9bc1

Please sign in to comment.