diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/DahuaHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/DahuaHandler.java index bc89c8f5793b4..10541ead7e608 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/DahuaHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/DahuaHandler.java @@ -15,6 +15,7 @@ import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*; import java.util.List; +import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -43,14 +44,16 @@ public class DahuaHandler extends ChannelDuplexHandler { private IpCameraHandler ipCameraHandler; private int nvrChannel; + private Pattern boundaryPattern; public DahuaHandler(IpCameraHandler handler, int nvrChannel) { ipCameraHandler = handler; this.nvrChannel = nvrChannel; + boundaryPattern = Pattern.compile("^-- ?myboundary$", Pattern.MULTILINE); } private void processEvent(String content) { - int startIndex = content.indexOf("Code=", 12) + 5;// skip --myboundary and Code= + int startIndex = content.indexOf("Code=") + 5;// skip Code= int endIndex = content.indexOf(";", startIndex + 1); if (startIndex == -1 || endIndex == -1) { ipCameraHandler.logger.debug("Code= not found in Dahua event. Content was:{}", content); @@ -212,11 +215,12 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms } try { String content = msg.toString(); + ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content); if (content.startsWith("--myboundary") || content.startsWith("-- myboundary")) { - processEvent(content); + // the first part of the match before --myboundary is empty, so skip 1 + boundaryPattern.splitAsStream(content).skip(1).forEach(event -> processEvent(event)); return; } - ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content); // determine if the motion detection is turned on or off. if (content.contains("table.MotionDetect[0].Enable=true")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);