]> git.basschouten.com Git - openhab-addons.git/blob
f3e43c0fbf99909d33687eb994abf85f90419e5a
[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.openhab.binding.ipcamera.internal.onvif.OnvifConnection.RequestType;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import io.netty.channel.ChannelDuplexHandler;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.handler.codec.http.HttpContent;
24 import io.netty.handler.codec.http.HttpResponse;
25 import io.netty.handler.codec.http.LastHttpContent;
26 import io.netty.handler.timeout.IdleStateEvent;
27 import io.netty.util.CharsetUtil;
28 import io.netty.util.ReferenceCountUtil;
29
30 /**
31  * The {@link OnvifCodec} is used by Netty to decode Onvif traffic into message Strings.
32  *
33  * @author Matthew Skinner - Initial contribution
34  */
35 @NonNullByDefault
36 public class OnvifCodec extends ChannelDuplexHandler {
37     private final Logger logger = LoggerFactory.getLogger(getClass());
38     private String incomingMessage = "";
39     private OnvifConnection onvifConnection;
40     private RequestType requestType = RequestType.GetStatus;
41
42     OnvifCodec(OnvifConnection onvifConnection) {
43         this.onvifConnection = onvifConnection;
44     }
45
46     @Override
47     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
48         if (msg == null || ctx == null) {
49             return;
50         }
51         try {
52             if (msg instanceof HttpResponse response) {
53                 if (response.status().code() != 200) {
54                     logger.trace("ONVIF replied with code {} message is {}", response.status().code(), msg);
55                 }
56             }
57             if (msg instanceof HttpContent content) {
58                 incomingMessage += content.content().toString(CharsetUtil.UTF_8);
59             }
60             if (msg instanceof LastHttpContent) {
61                 onvifConnection.processReply(requestType, incomingMessage);
62                 ctx.close();
63             }
64         } finally {
65             ReferenceCountUtil.release(msg);
66         }
67     }
68
69     @Override
70     public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
71         if (ctx == null) {
72             return;
73         }
74         if (evt instanceof IdleStateEvent) {
75             IdleStateEvent e = (IdleStateEvent) evt;
76             logger.debug("IdleStateEvent received: {}", e.state());
77             onvifConnection.setIsConnected(false);
78             ctx.close();
79         } else {
80             logger.debug("ONVIF netty channel event occurred: {}", evt);
81         }
82     }
83
84     @Override
85     public void exceptionCaught(@Nullable ChannelHandlerContext ctx, @Nullable Throwable cause) {
86         if (ctx == null || cause == null) {
87             return;
88         }
89         logger.debug("Exception on ONVIF connection: {}", cause.getMessage());
90         ctx.close();
91     }
92
93     @Override
94     public void handlerRemoved(@Nullable ChannelHandlerContext ctx) {
95         if (requestType == RequestType.PullMessages) {
96             onvifConnection.pullMessageRequests.decrementAndGet();
97         }
98     }
99
100     public void setRequestType(RequestType requestType) {
101         this.requestType = requestType;
102         if (requestType == RequestType.PullMessages) {
103             onvifConnection.pullMessageRequests.incrementAndGet();
104         }
105     }
106
107     public RequestType getRequestType() {
108         return requestType;
109     }
110 }