]> git.basschouten.com Git - openhab-addons.git/blob
6930a6e2cc872248f5eff3efe95240d5c7a5c0b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
16
17 import java.net.InetSocketAddress;
18 import java.nio.charset.StandardCharsets;
19 import java.security.MessageDigest;
20 import java.security.NoSuchAlgorithmException;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Base64;
24 import java.util.Date;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Random;
28 import java.util.TimeZone;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.locks.ReentrantLock;
33
34 import org.eclipse.jdt.annotation.NonNullByDefault;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.openhab.binding.ipcamera.internal.Helper;
37 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.types.StateOption;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import io.netty.bootstrap.Bootstrap;
45 import io.netty.buffer.ByteBuf;
46 import io.netty.buffer.Unpooled;
47 import io.netty.channel.Channel;
48 import io.netty.channel.ChannelFuture;
49 import io.netty.channel.ChannelFutureListener;
50 import io.netty.channel.ChannelInitializer;
51 import io.netty.channel.ChannelOption;
52 import io.netty.channel.EventLoopGroup;
53 import io.netty.channel.nio.NioEventLoopGroup;
54 import io.netty.channel.socket.SocketChannel;
55 import io.netty.channel.socket.nio.NioSocketChannel;
56 import io.netty.handler.codec.http.DefaultFullHttpRequest;
57 import io.netty.handler.codec.http.FullHttpRequest;
58 import io.netty.handler.codec.http.HttpClientCodec;
59 import io.netty.handler.codec.http.HttpHeaderValues;
60 import io.netty.handler.codec.http.HttpMethod;
61 import io.netty.handler.codec.http.HttpRequest;
62 import io.netty.handler.codec.http.HttpVersion;
63 import io.netty.handler.timeout.IdleStateHandler;
64
65 /**
66  * The {@link OnvifConnection} This is a basic Netty implementation for connecting and communicating to ONVIF cameras.
67  *
68  *
69  *
70  * @author Matthew Skinner - Initial contribution
71  */
72
73 @NonNullByDefault
74 public class OnvifConnection {
75     public static enum RequestType {
76         AbsoluteMove,
77         AddPTZConfiguration,
78         ContinuousMoveLeft,
79         ContinuousMoveRight,
80         ContinuousMoveUp,
81         ContinuousMoveDown,
82         Stop,
83         ContinuousMoveIn,
84         ContinuousMoveOut,
85         CreatePullPointSubscription,
86         GetCapabilities,
87         GetDeviceInformation,
88         GetProfiles,
89         GetServiceCapabilities,
90         GetSnapshotUri,
91         GetStreamUri,
92         GetSystemDateAndTime,
93         Subscribe,
94         Unsubscribe,
95         PullMessages,
96         GetEventProperties,
97         RelativeMoveLeft,
98         RelativeMoveRight,
99         RelativeMoveUp,
100         RelativeMoveDown,
101         RelativeMoveIn,
102         RelativeMoveOut,
103         Renew,
104         GetConfigurations,
105         GetConfigurationOptions,
106         GetConfiguration,
107         SetConfiguration,
108         GetNodes,
109         GetStatus,
110         GotoPreset,
111         GetPresets
112     }
113
114     private final Logger logger = LoggerFactory.getLogger(getClass());
115     private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
116     private @Nullable Bootstrap bootstrap;
117     private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup(2);
118     private ReentrantLock connecting = new ReentrantLock();
119     private String ipAddress = "";
120     private String user = "";
121     private String password = "";
122     private int onvifPort = 80;
123     private String deviceXAddr = "http://" + ipAddress + "/onvif/device_service";
124     private String eventXAddr = "http://" + ipAddress + "/onvif/device_service";
125     private String mediaXAddr = "http://" + ipAddress + "/onvif/device_service";
126     @SuppressWarnings("unused")
127     private String imagingXAddr = "http://" + ipAddress + "/onvif/device_service";
128     private String ptzXAddr = "http://" + ipAddress + "/onvif/ptz_service";
129     private String subscriptionXAddr = "http://" + ipAddress + "/onvif/device_service";
130     private boolean isConnected = false;
131     private int mediaProfileIndex = 0;
132     private String snapshotUri = "";
133     private String rtspUri = "";
134     private IpCameraHandler ipCameraHandler;
135     private boolean usingEvents = false;
136
137     // These hold the cameras PTZ position in the range that the camera uses, ie
138     // mine is -1 to +1
139     private Float panRangeMin = -1.0f;
140     private Float panRangeMax = 1.0f;
141     private Float tiltRangeMin = -1.0f;
142     private Float tiltRangeMax = 1.0f;
143     private Float zoomMin = 0.0f;
144     private Float zoomMax = 1.0f;
145     // These hold the PTZ values for updating Openhabs controls in 0-100 range
146     private Float currentPanPercentage = 0.0f;
147     private Float currentTiltPercentage = 0.0f;
148     private Float currentZoomPercentage = 0.0f;
149     private Float currentPanCamValue = 0.0f;
150     private Float currentTiltCamValue = 0.0f;
151     private Float currentZoomCamValue = 0.0f;
152     private String ptzNodeToken = "000";
153     private String ptzConfigToken = "000";
154     private int presetTokenIndex = 0;
155     private List<String> presetTokens = new LinkedList<>();
156     private List<String> presetNames = new LinkedList<>();
157     private List<String> mediaProfileTokens = new LinkedList<>();
158     private boolean ptzDevice = true;
159
160     public OnvifConnection(IpCameraHandler ipCameraHandler, String ipAddress, String user, String password) {
161         this.ipCameraHandler = ipCameraHandler;
162         if (!ipAddress.isEmpty()) {
163             this.user = user;
164             this.password = password;
165             getIPandPortFromUrl(ipAddress);
166         }
167     }
168
169     private String getXml(RequestType requestType) {
170         try {
171             switch (requestType) {
172                 case AbsoluteMove:
173                     return "<AbsoluteMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
174                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><Position><PanTilt x=\""
175                             + currentPanCamValue + "\" y=\"" + currentTiltCamValue
176                             + "\" space=\"http://www.onvif.org/ver10/tptz/PanTiltSpaces/PositionGenericSpace\">\n"
177                             + "</PanTilt>\n" + "<Zoom x=\"" + currentZoomCamValue
178                             + "\" space=\"http://www.onvif.org/ver10/tptz/ZoomSpaces/PositionGenericSpace\">\n"
179                             + "</Zoom>\n" + "</Position>\n"
180                             + "<Speed><PanTilt x=\"0.1\" y=\"0.1\" space=\"http://www.onvif.org/ver10/tptz/PanTiltSpaces/GenericSpeedSpace\"></PanTilt><Zoom x=\"1.0\" space=\"http://www.onvif.org/ver10/tptz/ZoomSpaces/ZoomGenericSpeedSpace\"></Zoom>\n"
181                             + "</Speed></AbsoluteMove>";
182                 case AddPTZConfiguration: // not tested to work yet
183                     return "<AddPTZConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
184                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><ConfigurationToken>"
185                             + ptzConfigToken + "</ConfigurationToken></AddPTZConfiguration>";
186                 case ContinuousMoveLeft:
187                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
188                             + mediaProfileTokens.get(mediaProfileIndex)
189                             + "</ProfileToken><Velocity><PanTilt x=\"-0.5\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
190                 case ContinuousMoveRight:
191                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
192                             + mediaProfileTokens.get(mediaProfileIndex)
193                             + "</ProfileToken><Velocity><PanTilt x=\"0.5\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
194                 case ContinuousMoveUp:
195                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
196                             + mediaProfileTokens.get(mediaProfileIndex)
197                             + "</ProfileToken><Velocity><PanTilt x=\"0\" y=\"-0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
198                 case ContinuousMoveDown:
199                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
200                             + mediaProfileTokens.get(mediaProfileIndex)
201                             + "</ProfileToken><Velocity><PanTilt x=\"0\" y=\"0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
202                 case Stop:
203                     return "<Stop xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
204                             + mediaProfileTokens.get(mediaProfileIndex)
205                             + "</ProfileToken><PanTilt>true</PanTilt><Zoom>true</Zoom></Stop>";
206                 case ContinuousMoveIn:
207                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
208                             + mediaProfileTokens.get(mediaProfileIndex)
209                             + "</ProfileToken><Velocity><Zoom x=\"0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
210                 case ContinuousMoveOut:
211                     return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
212                             + mediaProfileTokens.get(mediaProfileIndex)
213                             + "</ProfileToken><Velocity><Zoom x=\"-0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
214                 case CreatePullPointSubscription:
215                     return "<CreatePullPointSubscription xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><InitialTerminationTime>PT600S</InitialTerminationTime></CreatePullPointSubscription>";
216                 case GetCapabilities:
217                     return "<GetCapabilities xmlns=\"http://www.onvif.org/ver10/device/wsdl\"><Category>All</Category></GetCapabilities>";
218
219                 case GetDeviceInformation:
220                     return "<GetDeviceInformation xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/>";
221                 case GetProfiles:
222                     return "<GetProfiles xmlns=\"http://www.onvif.org/ver10/media/wsdl\"/>";
223                 case GetServiceCapabilities:
224                     return "<GetServiceCapabilities xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"></GetServiceCapabilities>";
225                 case GetSnapshotUri:
226                     return "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver10/media/wsdl\"><ProfileToken>"
227                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetSnapshotUri>";
228                 case GetStreamUri:
229                     return "<GetStreamUri xmlns=\"http://www.onvif.org/ver10/media/wsdl\"><StreamSetup><Stream xmlns=\"http://www.onvif.org/ver10/schema\">RTP-Unicast</Stream><Transport xmlns=\"http://www.onvif.org/ver10/schema\"><Protocol>RTSP</Protocol></Transport></StreamSetup><ProfileToken>"
230                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetStreamUri>";
231                 case GetSystemDateAndTime:
232                     return "<GetSystemDateAndTime xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/>";
233                 case Subscribe:
234                     return "<Subscribe xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"><ConsumerReference><Address>http://"
235                             + ipCameraHandler.hostIp + ":" + SERVLET_PORT + "/ipcamera/"
236                             + ipCameraHandler.getThing().getUID().getId()
237                             + "/OnvifEvent</Address></ConsumerReference></Subscribe>";
238                 case Unsubscribe:
239                     return "<Unsubscribe xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"></Unsubscribe>";
240                 case PullMessages:
241                     return "<PullMessages xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><Timeout>PT8S</Timeout><MessageLimit>1</MessageLimit></PullMessages>";
242                 case GetEventProperties:
243                     return "<GetEventProperties xmlns=\"http://www.onvif.org/ver10/events/wsdl\"/>";
244                 case RelativeMoveLeft:
245                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
246                             + mediaProfileTokens.get(mediaProfileIndex)
247                             + "</ProfileToken><Translation><PanTilt x=\"0.05000000\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
248                 case RelativeMoveRight:
249                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
250                             + mediaProfileTokens.get(mediaProfileIndex)
251                             + "</ProfileToken><Translation><PanTilt x=\"-0.05000000\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
252                 case RelativeMoveUp:
253                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
254                             + mediaProfileTokens.get(mediaProfileIndex)
255                             + "</ProfileToken><Translation><PanTilt x=\"0\" y=\"0.100000000\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
256                 case RelativeMoveDown:
257                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
258                             + mediaProfileTokens.get(mediaProfileIndex)
259                             + "</ProfileToken><Translation><PanTilt x=\"0\" y=\"-0.100000000\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
260                 case RelativeMoveIn:
261                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
262                             + mediaProfileTokens.get(mediaProfileIndex)
263                             + "</ProfileToken><Translation><Zoom x=\"0.0240506344\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
264                 case RelativeMoveOut:
265                     return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
266                             + mediaProfileTokens.get(mediaProfileIndex)
267                             + "</ProfileToken><Translation><Zoom x=\"-0.0240506344\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
268                 case Renew:
269                     return "<Renew xmlns=\"http://docs.oasis-open.org/wsn/b-2\"><TerminationTime>PT1M</TerminationTime></Renew>";
270                 case GetConfigurations:
271                     return "<GetConfigurations xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"></GetConfigurations>";
272                 case GetConfigurationOptions:
273                     return "<GetConfigurationOptions xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ConfigurationToken>"
274                             + ptzConfigToken + "</ConfigurationToken></GetConfigurationOptions>";
275                 case GetConfiguration:
276                     return "<GetConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><PTZConfigurationToken>"
277                             + ptzConfigToken + "</PTZConfigurationToken></GetConfiguration>";
278                 case SetConfiguration:// not tested to work yet
279                     return "<SetConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><PTZConfiguration><NodeToken>"
280                             + ptzNodeToken
281                             + "</NodeToken><DefaultAbsolutePantTiltPositionSpace>AbsolutePanTiltPositionSpace</DefaultAbsolutePantTiltPositionSpace><DefaultAbsoluteZoomPositionSpace>AbsoluteZoomPositionSpace</DefaultAbsoluteZoomPositionSpace></PTZConfiguration></SetConfiguration>";
282                 case GetNodes:
283                     return "<GetNodes xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"></GetNodes>";
284                 case GetStatus:
285                     return "<GetStatus xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
286                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetStatus>";
287                 case GotoPreset:
288                     return "<GotoPreset xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
289                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><PresetToken>"
290                             + presetTokens.get(presetTokenIndex) + "</PresetToken></GotoPreset>";
291                 case GetPresets:
292                     return "<GetPresets xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
293                             + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetPresets>";
294             }
295         } catch (IndexOutOfBoundsException e) {
296             if (!isConnected) {
297                 logger.debug("IndexOutOfBoundsException occured, camera is not connected via ONVIF: {}",
298                         e.getMessage());
299             } else {
300                 logger.debug("IndexOutOfBoundsException occured, {}", e.getMessage());
301             }
302         }
303         return "notfound";
304     }
305
306     public void processReply(String message) {
307         logger.trace("Onvif reply is:{}", message);
308         if (message.contains("PullMessagesResponse")) {
309             eventRecieved(message);
310         } else if (message.contains("RenewResponse")) {
311             sendOnvifRequest(requestBuilder(RequestType.PullMessages, subscriptionXAddr));
312         } else if (message.contains("GetSystemDateAndTimeResponse")) {// 1st to be sent.
313             connecting.lock();
314             try {
315                 isConnected = true;
316             } finally {
317                 connecting.unlock();
318             }
319             parseDateAndTime(message);
320             logger.debug("Openhabs UTC dateTime is:{}", getUTCdateTime());
321         } else if (message.contains("GetCapabilitiesResponse")) {// 2nd to be sent.
322             parseXAddr(message);
323             sendOnvifRequest(requestBuilder(RequestType.GetProfiles, mediaXAddr));
324         } else if (message.contains("GetProfilesResponse")) {// 3rd to be sent.
325             connecting.lock();
326             try {
327                 isConnected = true;
328             } finally {
329                 connecting.unlock();
330             }
331             parseProfiles(message);
332             sendOnvifRequest(requestBuilder(RequestType.GetSnapshotUri, mediaXAddr));
333             sendOnvifRequest(requestBuilder(RequestType.GetStreamUri, mediaXAddr));
334             if (ptzDevice) {
335                 sendPTZRequest(RequestType.GetNodes);
336             }
337             if (usingEvents) {// stops API cameras from getting sent ONVIF events.
338                 sendOnvifRequest(requestBuilder(RequestType.GetEventProperties, eventXAddr));
339                 sendOnvifRequest(requestBuilder(RequestType.GetServiceCapabilities, eventXAddr));
340             }
341         } else if (message.contains("GetServiceCapabilitiesResponse")) {
342             if (message.contains("WSSubscriptionPolicySupport=\"true\"")) {
343                 sendOnvifRequest(requestBuilder(RequestType.Subscribe, eventXAddr));
344             }
345         } else if (message.contains("GetEventPropertiesResponse")) {
346             sendOnvifRequest(requestBuilder(RequestType.CreatePullPointSubscription, eventXAddr));
347         } else if (message.contains("CreatePullPointSubscriptionResponse")) {
348             subscriptionXAddr = Helper.fetchXML(message, "SubscriptionReference>", "Address>");
349             logger.debug("subscriptionXAddr={}", subscriptionXAddr);
350             sendOnvifRequest(requestBuilder(RequestType.PullMessages, subscriptionXAddr));
351         } else if (message.contains("GetStatusResponse")) {
352             processPTZLocation(message);
353         } else if (message.contains("GetPresetsResponse")) {
354             parsePresets(message);
355         } else if (message.contains("GetConfigurationsResponse")) {
356             sendPTZRequest(RequestType.GetPresets);
357             ptzConfigToken = Helper.fetchXML(message, "PTZConfiguration", "token=\"");
358             logger.debug("ptzConfigToken={}", ptzConfigToken);
359             sendPTZRequest(RequestType.GetConfigurationOptions);
360         } else if (message.contains("GetNodesResponse")) {
361             sendPTZRequest(RequestType.GetStatus);
362             ptzNodeToken = Helper.fetchXML(message, "", "token=\"");
363             logger.debug("ptzNodeToken={}", ptzNodeToken);
364             sendPTZRequest(RequestType.GetConfigurations);
365         } else if (message.contains("GetDeviceInformationResponse")) {
366             logger.debug("GetDeviceInformationResponse recieved");
367         } else if (message.contains("GetSnapshotUriResponse")) {
368             snapshotUri = removeIPfromUrl(Helper.fetchXML(message, ":MediaUri", ":Uri"));
369             logger.debug("GetSnapshotUri:{}", snapshotUri);
370             if (ipCameraHandler.snapshotUri.isEmpty()
371                     && !"ffmpeg".equals(ipCameraHandler.cameraConfig.getSnapshotUrl())) {
372                 ipCameraHandler.snapshotUri = snapshotUri;
373             }
374         } else if (message.contains("GetStreamUriResponse")) {
375             rtspUri = Helper.fetchXML(message, ":MediaUri", ":Uri>");
376             logger.debug("GetStreamUri:{}", rtspUri);
377             if (ipCameraHandler.cameraConfig.getFfmpegInput().isEmpty()) {
378                 ipCameraHandler.rtspUri = rtspUri;
379             }
380         }
381     }
382
383     HttpRequest requestBuilder(RequestType requestType, String xAddr) {
384         logger.trace("Sending ONVIF request:{}", requestType);
385         String security = "";
386         String extraEnvelope = "";
387         String headerTo = "";
388         String getXmlCache = getXml(requestType);
389         if (requestType.equals(RequestType.CreatePullPointSubscription) || requestType.equals(RequestType.PullMessages)
390                 || requestType.equals(RequestType.Renew) || requestType.equals(RequestType.Unsubscribe)) {
391             headerTo = "<a:To s:mustUnderstand=\"1\">" + xAddr + "</a:To>";
392             extraEnvelope = " xmlns:a=\"http://www.w3.org/2005/08/addressing\"";
393         }
394         String headers;
395         if (!password.isEmpty() && !requestType.equals(RequestType.GetSystemDateAndTime)) {
396             String nonce = createNonce();
397             String dateTime = getUTCdateTime();
398             String digest = createDigest(nonce, dateTime);
399             security = "<Security s:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>"
400                     + user
401                     + "</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">"
402                     + digest
403                     + "</Password><Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">"
404                     + encodeBase64(nonce)
405                     + "</Nonce><Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
406                     + dateTime + "</Created></UsernameToken></Security>";
407             headers = "<s:Header>" + security + headerTo + "</s:Header>";
408         } else {// GetSystemDateAndTime must not be password protected as per spec.
409             headers = "";
410         }
411         FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, new HttpMethod("POST"),
412                 removeIPfromUrl(xAddr));
413         String actionString = Helper.fetchXML(getXmlCache, requestType.toString(), "xmlns=\"");
414         request.headers().add("Content-Type",
415                 "application/soap+xml; charset=utf-8; action=\"" + actionString + "/" + requestType + "\"");
416         request.headers().add("Charset", "utf-8");
417         request.headers().set("Host", ipAddress + ":" + onvifPort);
418         request.headers().set("Connection", HttpHeaderValues.CLOSE);
419         request.headers().set("Accept-Encoding", "gzip, deflate");
420         String fullXml = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"" + extraEnvelope + ">"
421                 + headers
422                 + "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
423                 + getXmlCache + "</s:Body></s:Envelope>";
424         request.headers().add("SOAPAction", "\"" + actionString + "/" + requestType + "\"");
425         ByteBuf bbuf = Unpooled.copiedBuffer(fullXml, StandardCharsets.UTF_8);
426         request.headers().set("Content-Length", bbuf.readableBytes());
427         request.content().clear().writeBytes(bbuf);
428         return request;
429     }
430
431     /**
432      * The {@link removeIPfromUrl} Will throw away all text before the cameras IP, also removes the IP and the PORT
433      * leaving just the URL.
434      *
435      * @author Matthew Skinner - Initial contribution
436      */
437     String removeIPfromUrl(String url) {
438         int index = url.indexOf("//");
439         if (index != -1) {// now remove the :port
440             index = url.indexOf("/", index + 2);
441         }
442         if (index == -1) {
443             logger.debug("We hit an issue parsing url:{}", url);
444             return "";
445         }
446         return url.substring(index);
447     }
448
449     String extractIPportFromUrl(String url) {
450         int startIndex = url.indexOf("//") + 2;
451         int endIndex = url.indexOf("/", startIndex);// skip past any :port to the slash /
452         if (startIndex != -1 && endIndex != -1) {
453             return url.substring(startIndex, endIndex);
454         }
455         logger.debug("We hit an issue extracting IP:PORT from url:{}", url);
456         return "";
457     }
458
459     void parseXAddr(String message) {
460         // Normally I would search '<tt:XAddr>' instead but Foscam needed this work around.
461         String temp = Helper.fetchXML(message, "<tt:Device", "tt:XAddr");
462         if (!temp.isEmpty()) {
463             deviceXAddr = temp;
464             logger.debug("deviceXAddr:{}", deviceXAddr);
465         }
466         temp = Helper.fetchXML(message, "<tt:Events", "tt:XAddr");
467         if (!temp.isEmpty()) {
468             subscriptionXAddr = eventXAddr = temp;
469             logger.debug("eventsXAddr:{}", eventXAddr);
470         }
471         temp = Helper.fetchXML(message, "<tt:Media", "tt:XAddr");
472         if (!temp.isEmpty()) {
473             mediaXAddr = temp;
474             logger.debug("mediaXAddr:{}", mediaXAddr);
475         }
476
477         ptzXAddr = Helper.fetchXML(message, "<tt:PTZ", "tt:XAddr");
478         if (ptzXAddr.isEmpty()) {
479             ptzDevice = false;
480             logger.debug("Camera has no ONVIF PTZ support.");
481             List<org.openhab.core.thing.Channel> removeChannels = new ArrayList<>();
482             org.openhab.core.thing.Channel channel = ipCameraHandler.getThing().getChannel(CHANNEL_PAN);
483             if (channel != null) {
484                 removeChannels.add(channel);
485             }
486             channel = ipCameraHandler.getThing().getChannel(CHANNEL_TILT);
487             if (channel != null) {
488                 removeChannels.add(channel);
489             }
490             channel = ipCameraHandler.getThing().getChannel(CHANNEL_ZOOM);
491             if (channel != null) {
492                 removeChannels.add(channel);
493             }
494             ipCameraHandler.removeChannels(removeChannels);
495         } else {
496             logger.debug("ptzXAddr:{}", ptzXAddr);
497         }
498     }
499
500     private void parseDateAndTime(String message) {
501         String minute = Helper.fetchXML(message, "UTCDateTime", "Minute>");
502         String hour = Helper.fetchXML(message, "UTCDateTime", "Hour>");
503         String second = Helper.fetchXML(message, "UTCDateTime", "Second>");
504         String day = Helper.fetchXML(message, "UTCDateTime", "Day>");
505         String month = Helper.fetchXML(message, "UTCDateTime", "Month>");
506         String year = Helper.fetchXML(message, "UTCDateTime", "Year>");
507         logger.debug("Cameras  UTC dateTime is:{}-{}-{}T{}:{}:{}", year, month, day, hour, minute, second);
508     }
509
510     private String getUTCdateTime() {
511         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
512         format.setTimeZone(TimeZone.getTimeZone("UTC"));
513         return format.format(new Date());
514     }
515
516     String createNonce() {
517         Random nonce = new Random();
518         return "" + nonce.nextInt();
519     }
520
521     String encodeBase64(String raw) {
522         return Base64.getEncoder().encodeToString(raw.getBytes());
523     }
524
525     String createDigest(String nOnce, String dateTime) {
526         String beforeEncryption = nOnce + dateTime + password;
527         MessageDigest msgDigest;
528         byte[] encryptedRaw = null;
529         try {
530             msgDigest = MessageDigest.getInstance("SHA-1");
531             msgDigest.reset();
532             msgDigest.update(beforeEncryption.getBytes(StandardCharsets.UTF_8));
533             encryptedRaw = msgDigest.digest();
534         } catch (NoSuchAlgorithmException e) {
535         }
536         return Base64.getEncoder().encodeToString(encryptedRaw);
537     }
538
539     @SuppressWarnings("null")
540     public void sendOnvifRequest(HttpRequest request) {
541         if (bootstrap == null) {
542             mainEventLoopGroup = new NioEventLoopGroup(2);
543             bootstrap = new Bootstrap();
544             bootstrap.group(mainEventLoopGroup);
545             bootstrap.channel(NioSocketChannel.class);
546             bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
547             bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
548             bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 8);
549             bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024);
550             bootstrap.option(ChannelOption.TCP_NODELAY, true);
551             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
552
553                 @Override
554                 public void initChannel(SocketChannel socketChannel) throws Exception {
555                     socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 70));
556                     socketChannel.pipeline().addLast("HttpClientCodec", new HttpClientCodec());
557                     socketChannel.pipeline().addLast("OnvifCodec", new OnvifCodec(getHandle()));
558                 }
559             });
560         }
561         if (!mainEventLoopGroup.isShuttingDown()) {
562             bootstrap.connect(new InetSocketAddress(ipAddress, onvifPort)).addListener(new ChannelFutureListener() {
563
564                 @Override
565                 public void operationComplete(@Nullable ChannelFuture future) {
566                     if (future == null) {
567                         return;
568                     }
569                     if (future.isSuccess()) {
570                         Channel ch = future.channel();
571                         ch.writeAndFlush(request);
572                     } else { // an error occured
573                         logger.debug("Camera is not reachable on ONVIF port:{} or the port may be wrong.", onvifPort);
574                         if (isConnected) {
575                             disconnect();
576                         }
577                     }
578                 }
579             });
580         } else {
581             logger.debug("ONVIF message not sent as connection is shutting down");
582         }
583     }
584
585     OnvifConnection getHandle() {
586         return this;
587     }
588
589     void getIPandPortFromUrl(String url) {
590         int beginIndex = url.indexOf(":");
591         int endIndex = url.indexOf("/", beginIndex);
592         if (beginIndex >= 0 && endIndex == -1) {// 192.168.1.1:8080
593             ipAddress = url.substring(0, beginIndex);
594             onvifPort = Integer.parseInt(url.substring(beginIndex + 1));
595         } else if (beginIndex >= 0 && endIndex > beginIndex) {// 192.168.1.1:8080/foo/bar
596             ipAddress = url.substring(0, beginIndex);
597             onvifPort = Integer.parseInt(url.substring(beginIndex + 1, endIndex));
598         } else {// 192.168.1.1
599             ipAddress = url;
600             deviceXAddr = "http://" + ipAddress + "/onvif/device_service";
601             logger.debug("No Onvif Port found when parsing:{}", url);
602             return;
603         }
604         deviceXAddr = "http://" + ipAddress + ":" + onvifPort + "/onvif/device_service";
605     }
606
607     public void gotoPreset(int index) {
608         if (ptzDevice) {
609             if (index > 0) {// 0 is reserved for HOME as cameras seem to start at preset 1.
610                 if (presetTokens.isEmpty()) {
611                     logger.warn("Camera did not report any ONVIF preset locations, updating preset tokens now.");
612                     sendPTZRequest(RequestType.GetPresets);
613                 } else {
614                     presetTokenIndex = index - 1;
615                     sendPTZRequest(RequestType.GotoPreset);
616                 }
617             }
618         }
619     }
620
621     public void eventRecieved(String eventMessage) {
622         String topic = Helper.fetchXML(eventMessage, "Topic", "tns1:");
623         if (topic.isEmpty()) {
624             sendOnvifRequest(requestBuilder(RequestType.Renew, subscriptionXAddr));
625             return;
626         }
627         String dataName = Helper.fetchXML(eventMessage, "tt:Data", "Name=\"");
628         String dataValue = Helper.fetchXML(eventMessage, "tt:Data", "Value=\"");
629         logger.debug("Onvif Event Topic:{}, Data:{}, Value:{}", topic, dataName, dataValue);
630         switch (topic) {
631             case "RuleEngine/CellMotionDetector/Motion":
632                 if ("true".equals(dataValue)) {
633                     ipCameraHandler.motionDetected(CHANNEL_CELL_MOTION_ALARM);
634                 } else if ("false".equals(dataValue)) {
635                     ipCameraHandler.noMotionDetected(CHANNEL_CELL_MOTION_ALARM);
636                 }
637                 break;
638             case "VideoSource/MotionAlarm":
639                 if ("true".equals(dataValue)) {
640                     ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
641                 } else if ("false".equals(dataValue)) {
642                     ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
643                 }
644                 break;
645             case "AudioAnalytics/Audio/DetectedSound":
646                 if ("true".equals(dataValue)) {
647                     ipCameraHandler.audioDetected();
648                 } else if ("false".equals(dataValue)) {
649                     ipCameraHandler.noAudioDetected();
650                 }
651                 break;
652             case "RuleEngine/FieldDetector/ObjectsInside":
653                 if ("true".equals(dataValue)) {
654                     ipCameraHandler.motionDetected(CHANNEL_FIELD_DETECTION_ALARM);
655                 } else if ("false".equals(dataValue)) {
656                     ipCameraHandler.noMotionDetected(CHANNEL_FIELD_DETECTION_ALARM);
657                 }
658                 break;
659             case "RuleEngine/LineDetector/Crossed":
660                 if ("ObjectId".equals(dataName)) {
661                     ipCameraHandler.motionDetected(CHANNEL_LINE_CROSSING_ALARM);
662                 } else {
663                     ipCameraHandler.noMotionDetected(CHANNEL_LINE_CROSSING_ALARM);
664                 }
665                 break;
666             case "RuleEngine/TamperDetector/Tamper":
667                 if ("true".equals(dataValue)) {
668                     ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.ON);
669                 } else if ("false".equals(dataValue)) {
670                     ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.OFF);
671                 }
672                 break;
673             case "Device/HardwareFailure/StorageFailure":
674                 if ("true".equals(dataValue)) {
675                     ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.ON);
676                 } else if ("false".equals(dataValue)) {
677                     ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.OFF);
678                 }
679                 break;
680             case "VideoSource/ImageTooDark/AnalyticsService":
681             case "VideoSource/ImageTooDark/ImagingService":
682             case "VideoSource/ImageTooDark/RecordingService":
683                 if ("true".equals(dataValue)) {
684                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.ON);
685                 } else if ("false".equals(dataValue)) {
686                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.OFF);
687                 }
688                 break;
689             case "VideoSource/GlobalSceneChange/AnalyticsService":
690             case "VideoSource/GlobalSceneChange/ImagingService":
691             case "VideoSource/GlobalSceneChange/RecordingService":
692                 if ("true".equals(dataValue)) {
693                     ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.ON);
694                 } else if ("false".equals(dataValue)) {
695                     ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.OFF);
696                 }
697                 break;
698             case "VideoSource/ImageTooBright/AnalyticsService":
699             case "VideoSource/ImageTooBright/ImagingService":
700             case "VideoSource/ImageTooBright/RecordingService":
701                 if ("true".equals(dataValue)) {
702                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.ON);
703                 } else if ("false".equals(dataValue)) {
704                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.OFF);
705                 }
706                 break;
707             case "VideoSource/ImageTooBlurry/AnalyticsService":
708             case "VideoSource/ImageTooBlurry/ImagingService":
709             case "VideoSource/ImageTooBlurry/RecordingService":
710                 if ("true".equals(dataValue)) {
711                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.ON);
712                 } else if ("false".equals(dataValue)) {
713                     ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.OFF);
714                 }
715                 break;
716             case "RuleEngine/MyRuleDetector/Visitor":
717                 if ("true".equals(dataValue)) {
718                     ipCameraHandler.changeAlarmState(CHANNEL_DOORBELL, OnOffType.ON);
719                 } else if ("false".equals(dataValue)) {
720                     ipCameraHandler.changeAlarmState(CHANNEL_DOORBELL, OnOffType.OFF);
721                 }
722                 break;
723             case "RuleEngine/MyRuleDetector/VehicleDetect":
724                 if ("true".equals(dataValue)) {
725                     ipCameraHandler.changeAlarmState(CHANNEL_CAR_ALARM, OnOffType.ON);
726                 } else if ("false".equals(dataValue)) {
727                     ipCameraHandler.changeAlarmState(CHANNEL_CAR_ALARM, OnOffType.OFF);
728                 }
729                 break;
730             case "RuleEngine/MyRuleDetector/DogCatDetect":
731                 if ("true".equals(dataValue)) {
732                     ipCameraHandler.changeAlarmState(CHANNEL_ANIMAL_ALARM, OnOffType.ON);
733                 } else if ("false".equals(dataValue)) {
734                     ipCameraHandler.changeAlarmState(CHANNEL_ANIMAL_ALARM, OnOffType.OFF);
735                 }
736                 break;
737             case "RuleEngine/MyRuleDetector/FaceDetect":
738                 if ("true".equals(dataValue)) {
739                     ipCameraHandler.changeAlarmState(CHANNEL_FACE_DETECTED, OnOffType.ON);
740                 } else if ("false".equals(dataValue)) {
741                     ipCameraHandler.changeAlarmState(CHANNEL_FACE_DETECTED, OnOffType.OFF);
742                 }
743                 break;
744             case "RuleEngine/MyRuleDetector/PeopleDetect":
745                 if ("true".equals(dataValue)) {
746                     ipCameraHandler.changeAlarmState(CHANNEL_HUMAN_ALARM, OnOffType.ON);
747                 } else if ("false".equals(dataValue)) {
748                     ipCameraHandler.changeAlarmState(CHANNEL_HUMAN_ALARM, OnOffType.OFF);
749                 }
750                 break;
751             default:
752                 logger.debug("Please report this camera has an un-implemented ONVIF event. Topic:{}", topic);
753         }
754         sendOnvifRequest(requestBuilder(RequestType.Renew, subscriptionXAddr));
755     }
756
757     public boolean supportsPTZ() {
758         return ptzDevice;
759     }
760
761     public void getStatus() {
762         if (ptzDevice) {
763             sendPTZRequest(RequestType.GetStatus);
764         }
765     }
766
767     public Float getAbsolutePan() {
768         return currentPanPercentage;
769     }
770
771     public Float getAbsoluteTilt() {
772         return currentTiltPercentage;
773     }
774
775     public Float getAbsoluteZoom() {
776         return currentZoomPercentage;
777     }
778
779     public void setAbsolutePan(Float panValue) {// Value is 0-100% of cameras range
780         if (ptzDevice) {
781             currentPanPercentage = panValue;
782             currentPanCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * panValue + panRangeMin);
783         }
784     }
785
786     public void setAbsoluteTilt(Float tiltValue) {// Value is 0-100% of cameras range
787         if (ptzDevice) {
788             currentTiltPercentage = tiltValue;
789             currentTiltCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * tiltValue + tiltRangeMin);
790         }
791     }
792
793     public void setAbsoluteZoom(Float zoomValue) {// Value is 0-100% of cameras range
794         if (ptzDevice) {
795             currentZoomPercentage = zoomValue;
796             currentZoomCamValue = ((((zoomMin - zoomMax) * -1) / 100) * zoomValue + zoomMin);
797         }
798     }
799
800     public void absoluteMove() { // Camera wont move until PTZ values are set, then call this.
801         if (ptzDevice) {
802             sendPTZRequest(RequestType.AbsoluteMove);
803         }
804     }
805
806     public void setSelectedMediaProfile(int mediaProfileIndex) {
807         this.mediaProfileIndex = mediaProfileIndex;
808     }
809
810     List<String> listOfResults(String message, String heading, String key) {
811         List<String> results = new LinkedList<>();
812         String temp = "";
813         for (int startLookingFromIndex = 0; startLookingFromIndex != -1;) {
814             startLookingFromIndex = message.indexOf(heading, startLookingFromIndex);
815             if (startLookingFromIndex >= 0) {
816                 temp = Helper.fetchXML(message.substring(startLookingFromIndex), heading, key);
817                 if (!temp.isEmpty()) {
818                     logger.trace("String was found:{}", temp);
819                     results.add(temp);
820                 } else {
821                     return results;// key string must not exist so stop looking.
822                 }
823                 startLookingFromIndex += temp.length();
824             }
825         }
826         return results;
827     }
828
829     void parsePresets(String message) {
830         List<StateOption> presets = new ArrayList<>();
831         int counter = 1;// Presets start at 1 not 0. HOME may be added to index 0.
832         presetTokens = listOfResults(message, "<tptz:Preset", "token=\"");
833         presetNames = listOfResults(message, "<tptz:Preset", "<tt:Name>");
834         if (presetTokens.size() != presetNames.size()) {
835             logger.warn("Camera did not report the same number of Tokens and Names for PTZ presets");
836             return;
837         }
838         for (String value : presetNames) {
839             presets.add(new StateOption(Integer.toString(counter++), value));
840         }
841         ipCameraHandler.stateDescriptionProvider
842                 .setStateOptions(new ChannelUID(ipCameraHandler.getThing().getUID(), CHANNEL_GOTO_PRESET), presets);
843     }
844
845     void parseProfiles(String message) {
846         mediaProfileTokens = listOfResults(message, "<trt:Profiles", "token=\"");
847         if (mediaProfileIndex >= mediaProfileTokens.size()) {
848             logger.warn(
849                     "You have set the media profile to {} when the camera reported {} profiles. Falling back to mainstream 0.",
850                     mediaProfileIndex, mediaProfileTokens.size());
851             mediaProfileIndex = 0;
852         }
853     }
854
855     void processPTZLocation(String result) {
856         logger.debug("Processing new PTZ location now");
857
858         int beginIndex = result.indexOf("x=\"");
859         int endIndex = result.indexOf("\"", (beginIndex + 3));
860         if (beginIndex >= 0 && endIndex >= 0) {
861             currentPanCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
862             currentPanPercentage = (((panRangeMin - currentPanCamValue) * -1) / ((panRangeMin - panRangeMax) * -1))
863                     * 100;
864             logger.debug("Pan is updating to:{} and the cam value is {}", Math.round(currentPanPercentage),
865                     currentPanCamValue);
866         } else {
867             logger.warn(
868                     "Binding could not determin the cameras current PTZ location. Not all cameras respond to GetStatus requests.");
869             return;
870         }
871
872         beginIndex = result.indexOf("y=\"");
873         endIndex = result.indexOf("\"", (beginIndex + 3));
874         if (beginIndex >= 0 && endIndex >= 0) {
875             currentTiltCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
876             currentTiltPercentage = (((tiltRangeMin - currentTiltCamValue) * -1) / ((tiltRangeMin - tiltRangeMax) * -1))
877                     * 100;
878             logger.debug("Tilt is updating to:{} and the cam value is {}", Math.round(currentTiltPercentage),
879                     currentTiltCamValue);
880         } else {
881             return;
882         }
883
884         beginIndex = result.lastIndexOf("x=\"");
885         endIndex = result.indexOf("\"", (beginIndex + 3));
886         if (beginIndex >= 0 && endIndex >= 0) {
887             currentZoomCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
888             currentZoomPercentage = (((zoomMin - currentZoomCamValue) * -1) / ((zoomMin - zoomMax) * -1)) * 100;
889             logger.debug("Zoom is updating to:{} and the cam value is {}", Math.round(currentZoomPercentage),
890                     currentZoomCamValue);
891         } else {
892             return;
893         }
894     }
895
896     public void sendPTZRequest(RequestType requestType) {
897         if (!isConnected) {
898             logger.debug("ONVIF was not connected when a PTZ request was made, connecting now");
899             connect(usingEvents);
900         }
901         sendOnvifRequest(requestBuilder(requestType, ptzXAddr));
902     }
903
904     public void sendEventRequest(RequestType requestType) {
905         sendOnvifRequest(requestBuilder(requestType, eventXAddr));
906     }
907
908     public void connect(boolean useEvents) {
909         connecting.lock();
910         try {
911             if (!isConnected) {
912                 logger.debug("Connecting {} to ONVIF", ipAddress);
913                 threadPool = Executors.newScheduledThreadPool(2);
914                 sendOnvifRequest(requestBuilder(RequestType.GetSystemDateAndTime, deviceXAddr));
915                 usingEvents = useEvents;
916                 sendOnvifRequest(requestBuilder(RequestType.GetCapabilities, deviceXAddr));
917             }
918         } finally {
919             connecting.unlock();
920         }
921     }
922
923     public boolean isConnected() {
924         connecting.lock();
925         try {
926             return isConnected;
927         } finally {
928             connecting.unlock();
929         }
930     }
931
932     private void cleanup() {
933         if (!isConnected && !mainEventLoopGroup.isShuttingDown()) {
934             try {
935                 mainEventLoopGroup.shutdownGracefully();
936                 mainEventLoopGroup.awaitTermination(3, TimeUnit.SECONDS);
937             } catch (InterruptedException e) {
938                 logger.warn("ONVIF was not cleanly shutdown, due to being interrupted");
939             } finally {
940                 logger.debug("Eventloop is shutdown:{}", mainEventLoopGroup.isShutdown());
941                 bootstrap = null;
942                 threadPool.shutdown();
943             }
944         }
945     }
946
947     public void disconnect() {
948         connecting.lock();// Lock out multiple disconnect()/connect() attempts as we try to send Unsubscribe.
949         try {
950             isConnected = false;// isConnected is not thread safe, connecting.lock() used as fix.
951             if (bootstrap != null) {
952                 if (usingEvents && !mainEventLoopGroup.isShuttingDown()) {
953                     // Some cameras may continue to send events even when they can't reach a server.
954                     sendOnvifRequest(requestBuilder(RequestType.Unsubscribe, subscriptionXAddr));
955                 }
956                 // give time for the Unsubscribe request to be sent, shutdownGracefully will try to send it first.
957                 threadPool.schedule(this::cleanup, 50, TimeUnit.MILLISECONDS);
958             } else {
959                 cleanup();
960             }
961         } finally {
962             connecting.unlock();
963         }
964     }
965 }