]> git.basschouten.com Git - openhab-addons.git/blob
7c54ad978d1ef3b4999ea117d80187c3ebf7474d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ipcamera.internal.onvif;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
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.HttpResponse;
24 import io.netty.handler.codec.http.LastHttpContent;
25 import io.netty.handler.timeout.IdleStateEvent;
26 import io.netty.util.CharsetUtil;
27 import io.netty.util.ReferenceCountUtil;
28
29 /**
30  * The {@link OnvifCodec} is used by Netty to decode Onvif traffic into message Strings.
31  *
32  * @author Matthew Skinner - Initial contribution
33  */
34 @NonNullByDefault
35 public class OnvifCodec extends ChannelDuplexHandler {
36     private final Logger logger = LoggerFactory.getLogger(getClass());
37     private String incomingMessage = "";
38     private OnvifConnection onvifConnection;
39
40     OnvifCodec(OnvifConnection onvifConnection) {
41         this.onvifConnection = onvifConnection;
42     }
43
44     @Override
45     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
46         if (msg == null || ctx == null) {
47             return;
48         }
49         try {
50             if (msg instanceof HttpResponse response) {
51                 if (response.status().code() != 200) {
52                     logger.trace("ONVIF replied with code {} message is {}", response.status().code(), msg);
53                 }
54             }
55             if (msg instanceof HttpContent content) {
56                 incomingMessage += content.content().toString(CharsetUtil.UTF_8);
57             }
58             if (msg instanceof LastHttpContent) {
59                 onvifConnection.processReply(incomingMessage);
60                 ctx.close();
61             }
62         } finally {
63             ReferenceCountUtil.release(msg);
64         }
65     }
66
67     @Override
68     public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
69         if (ctx == null) {
70             return;
71         }
72         if (evt instanceof IdleStateEvent) {
73             IdleStateEvent e = (IdleStateEvent) evt;
74             logger.debug("IdleStateEvent received: {}", e.state());
75             onvifConnection.setIsConnected(false);
76             ctx.close();
77         } else {
78             logger.debug("ONVIF netty channel event occurred: {}", evt);
79         }
80     }
81
82     @Override
83     public void exceptionCaught(@Nullable ChannelHandlerContext ctx, @Nullable Throwable cause) {
84         if (ctx == null || cause == null) {
85             return;
86         }
87         logger.debug("Exception on ONVIF connection: {}", cause.getMessage());
88         ctx.close();
89     }
90 }