2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal.onvif;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import io.netty.channel.ChannelDuplexHandler;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.handler.codec.http.HttpContent;
23 import io.netty.handler.codec.http.LastHttpContent;
24 import io.netty.handler.timeout.IdleStateEvent;
25 import io.netty.util.CharsetUtil;
26 import io.netty.util.ReferenceCountUtil;
29 * The {@link OnvifCodec} is used by Netty to decode Onvif traffic into message Strings.
32 * @author Matthew Skinner - Initial contribution
35 public class OnvifCodec extends ChannelDuplexHandler {
36 private final Logger logger = LoggerFactory.getLogger(getClass());
37 private String incomingMessage = "";
38 private OnvifConnection onvifConnection;
40 OnvifCodec(OnvifConnection onvifConnection) {
41 this.onvifConnection = onvifConnection;
45 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
46 if (msg == null || ctx == null) {
50 if (msg instanceof HttpContent content) {
51 incomingMessage += content.content().toString(CharsetUtil.UTF_8);
53 if (msg instanceof LastHttpContent) {
54 onvifConnection.processReply(incomingMessage);
58 ReferenceCountUtil.release(msg);
63 public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
67 if (evt instanceof IdleStateEvent) {
68 IdleStateEvent e = (IdleStateEvent) evt;
69 logger.trace("IdleStateEvent received {}", e.state());
70 onvifConnection.setIsConnected(false);
73 logger.trace("Other ONVIF netty channel event occured {}", evt);
78 public void exceptionCaught(@Nullable ChannelHandlerContext ctx, @Nullable Throwable cause) {
79 if (ctx == null || cause == null) {
82 logger.debug("Exception on ONVIF connection: {}", cause.getMessage());