From 075170e4b914da1a7ec2be9f7e3c7deab12c21ad Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Fri, 24 Jun 2022 03:06:34 +1000 Subject: [PATCH] [ipcamera] Fix multiple parts handling for Dahua events (#12964) * [ipcamera] Fix multiple part handling for Dahua events * [ipcamera] Call processSettings only when data contains no boundary Signed-off-by: Jimmy Tanagra --- .../ipcamera/internal/DahuaHandler.java | 82 +++++++++++-------- 1 file changed, 46 insertions(+), 36 deletions(-) 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 bc89c8f5793..46fb1199ea9 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 @@ package org.openhab.binding.ipcamera.internal; 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 @@ import io.netty.util.ReferenceCountUtil; 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); @@ -204,6 +207,41 @@ public class DahuaHandler extends ChannelDuplexHandler { } } + private void processSettings(String 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); + } else if (content.contains("table.MotionDetect[" + nvrChannel + "].Enable=false")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF); + } + + // determine if the audio alarm is turned on or off. + if (content.contains("table.AudioDetect[0].MutationDetect=true")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON); + } else if (content.contains("table.AudioDetect[0].MutationDetect=false")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF); + } + + // Handle AudioMutationThreshold alarm + if (content.contains("table.AudioDetect[0].MutationThreold=")) { + String value = ipCameraHandler.returnValueFromString(content, "table.AudioDetect[0].MutationThreold="); + ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value)); + } + + // CrossLineDetection alarm on/off + if (content.contains("table.VideoAnalyseRule[0][1].Enable=true")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.ON); + } else if (content.contains("table.VideoAnalyseRule[0][1].Enable=false")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.OFF); + } + // Privacy Mode on/off + if (content.contains("table.LeLensMask[0].Enable=true")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.ON); + } else if (content.contains("table.LeLensMask[0].Enable=false")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF); + } + } + // This handles the incoming http replies back from the camera. @Override public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception { @@ -212,42 +250,14 @@ public class DahuaHandler extends ChannelDuplexHandler { } try { String content = msg.toString(); - if (content.startsWith("--myboundary") || content.startsWith("-- myboundary")) { - processEvent(content); - 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); - } else if (content.contains("table.MotionDetect[" + nvrChannel + "].Enable=false")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF); - } - - // determine if the audio alarm is turned on or off. - if (content.contains("table.AudioDetect[0].MutationDetect=true")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON); - } else if (content.contains("table.AudioDetect[0].MutationDetect=false")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF); - } - - // Handle AudioMutationThreshold alarm - if (content.contains("table.AudioDetect[0].MutationThreold=")) { - String value = ipCameraHandler.returnValueFromString(content, "table.AudioDetect[0].MutationThreold="); - ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value)); - } - - // CrossLineDetection alarm on/off - if (content.contains("table.VideoAnalyseRule[0][1].Enable=true")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.ON); - } else if (content.contains("table.VideoAnalyseRule[0][1].Enable=false")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.OFF); - } - // Privacy Mode on/off - if (content.contains("table.LeLensMask[0].Enable=true")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.ON); - } else if (content.contains("table.LeLensMask[0].Enable=false")) { - ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF); + String[] events = boundaryPattern.split(content); + if (events.length > 1) { + for (int i = 1; i < events.length; i++) { + processEvent(events[i]); + } + } else { + processSettings(content); } } finally { ReferenceCountUtil.release(msg);