Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[digiplex] Fix runtime exceptions #17864

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.digiplex.internal.communication;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.digiplex.internal.communication.events.AreaEvent;
import org.openhab.binding.digiplex.internal.communication.events.AreaEventType;
import org.openhab.binding.digiplex.internal.communication.events.GenericEvent;
Expand All @@ -35,15 +36,14 @@
public class DigiplexResponseResolver {

private static final String OK = "&ok";
// TODO: handle failures
private static final String FAIL = "&fail";

public static DigiplexResponse resolveResponse(String message) {
if (message.length() < 4) { // sanity check: try to filter out malformed responses
return new UnknownResponse(message);
}

int zoneNo, areaNo;
Integer zoneNo, areaNo;
String commandType = message.substring(0, 2);
switch (commandType) {
case "CO": // communication status
Expand All @@ -53,24 +53,36 @@ public static DigiplexResponse resolveResponse(String message) {
return CommunicationStatus.OK;
}
case "ZL": // zone label
zoneNo = Integer.valueOf(message.substring(2, 5));
zoneNo = getZoneOrArea(message);
if (zoneNo == null) {
return new UnknownResponse(message);
}
if (message.contains(FAIL)) {
return ZoneLabelResponse.failure(zoneNo);
} else {
return ZoneLabelResponse.success(zoneNo, message.substring(5).trim());
}
case "AL": // area label
areaNo = Integer.valueOf(message.substring(2, 5));
areaNo = getZoneOrArea(message);
if (areaNo == null) {
return new UnknownResponse(message);
}
if (message.contains(FAIL)) {
return AreaLabelResponse.failure(areaNo);
} else {
return AreaLabelResponse.success(areaNo, message.substring(5).trim());
}
case "RZ": // zone status
zoneNo = Integer.valueOf(message.substring(2, 5));
zoneNo = getZoneOrArea(message);
if (zoneNo == null) {
return new UnknownResponse(message);
}
if (message.contains(FAIL)) {
return ZoneStatusResponse.failure(zoneNo);
} else {
if (message.length() < 10) {
return new UnknownResponse(message);
}
return ZoneStatusResponse.success(zoneNo, // zone number
ZoneStatus.fromMessage(message.charAt(5)), // status
toBoolean(message.charAt(6)), // alarm
Expand All @@ -79,10 +91,16 @@ public static DigiplexResponse resolveResponse(String message) {
toBoolean(message.charAt(9))); // battery low
}
case "RA": // area status
areaNo = Integer.valueOf(message.substring(2, 5));
areaNo = getZoneOrArea(message);
if (areaNo == null) {
return new UnknownResponse(message);
}
if (message.contains(FAIL)) {
return AreaStatusResponse.failure(areaNo);
} else {
if (message.length() < 12) {
return new UnknownResponse(message);
}
return AreaStatusResponse.success(areaNo, // zone number
AreaStatus.fromMessage(message.charAt(5)), // status
toBoolean(message.charAt(6)), // zone in memory
Expand All @@ -95,7 +113,10 @@ public static DigiplexResponse resolveResponse(String message) {
case "AA": // area arm
case "AQ": // area quick arm
case "AD": // area disarm
areaNo = Integer.valueOf(message.substring(2, 5));
areaNo = getZoneOrArea(message);
if (areaNo == null) {
return new UnknownResponse(message);
}
if (message.contains(FAIL)) {
return AreaArmDisarmResponse.failure(areaNo, ArmDisarmType.fromMessage(commandType));
} else {
Expand All @@ -104,22 +125,38 @@ public static DigiplexResponse resolveResponse(String message) {
case "UL": // user label
case "PG": // PGM events
default:
if (message.startsWith("G")) {
if (message.startsWith("G") && message.length() >= 12) {
return resolveSystemEvent(message);
} else {
return new UnknownResponse(message);
}
}
}

private static @Nullable Integer getZoneOrArea(String message) {
if (message.length() < 5) {
return null;
}
try {
return Integer.valueOf(message.substring(2, 5));
} catch (NumberFormatException e) {
return null;
}
}

private static boolean toBoolean(char value) {
return value != 'O';
}

private static DigiplexResponse resolveSystemEvent(String message) {
int eventGroup = Integer.parseInt(message.substring(1, 4));
int eventNumber = Integer.parseInt(message.substring(5, 8));
int areaNumber = Integer.parseInt(message.substring(9, 12));
int eventGroup, eventNumber, areaNumber;
try {
eventGroup = Integer.parseInt(message.substring(1, 4));
eventNumber = Integer.parseInt(message.substring(5, 8));
areaNumber = Integer.parseInt(message.substring(9, 12));
} catch (NumberFormatException e) {
return new UnknownResponse(message);
}
switch (eventGroup) {
case 0:
return new ZoneStatusEvent(eventNumber, ZoneStatus.CLOSED, areaNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.binding.digiplex.internal.communication.ArmType;
import org.openhab.binding.digiplex.internal.communication.DigiplexMessageHandler;
import org.openhab.binding.digiplex.internal.communication.DigiplexRequest;
import org.openhab.binding.digiplex.internal.communication.UnknownResponse;
import org.openhab.binding.digiplex.internal.communication.events.AreaEvent;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.StringType;
Expand All @@ -45,6 +46,8 @@
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link DigiplexAreaHandler} is responsible for handling commands, which are
Expand All @@ -55,6 +58,8 @@
@NonNullByDefault
public class DigiplexAreaHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(DigiplexAreaHandler.class);

private @Nullable DigiplexAreaConfiguration config;
private @Nullable DigiplexBridgeHandler bridgeHandler;
private DigiplexAreaMessageHandler visitor = new DigiplexAreaMessageHandler();
Expand Down Expand Up @@ -308,5 +313,10 @@ public void handleAreaEvent(AreaEvent event) {
tempStatus.ifPresent(s -> updateState(AREA_STATUS, s.toStringType()));
}
}

@Override
public void handleUnknownResponse(UnknownResponse response) {
logger.debug("Unknown response: {}", response.message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openhab.binding.digiplex.internal.DigiplexBindingConstants;
import org.openhab.binding.digiplex.internal.communication.DigiplexMessageHandler;
import org.openhab.binding.digiplex.internal.communication.DigiplexRequest;
import org.openhab.binding.digiplex.internal.communication.UnknownResponse;
import org.openhab.binding.digiplex.internal.communication.ZoneStatusRequest;
import org.openhab.binding.digiplex.internal.communication.ZoneStatusResponse;
import org.openhab.binding.digiplex.internal.communication.events.ZoneEvent;
Expand All @@ -41,6 +42,8 @@
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link DigiplexZoneHandler} is responsible for handling commands, which are
Expand All @@ -51,6 +54,8 @@
@NonNullByDefault
public class DigiplexZoneHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(DigiplexZoneHandler.class);

private @Nullable DigiplexBridgeHandler bridgeHandler;
private DigiplexZoneMessageHandler messageHandler = new DigiplexZoneMessageHandler();
private int zoneNo;
Expand Down Expand Up @@ -236,5 +241,10 @@ public void handleZoneEvent(ZoneEvent event) {
updateAreaNo(event.getAreaNo());
}
}

@Override
public void handleUnknownResponse(UnknownResponse response) {
logger.debug("Unknown response: {}", response.message);
}
}
}
Loading