2 * Copyright (c) 2010-2022 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 static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
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;
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;
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;
66 * The {@link OnvifConnection} This is a basic Netty implementation for connecting and communicating to ONVIF cameras.
70 * @author Matthew Skinner - Initial contribution
74 public class OnvifConnection {
75 public static enum RequestType {
85 CreatePullPointSubscription,
89 GetServiceCapabilities,
105 GetConfigurationOptions,
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;
137 // These hold the cameras PTZ position in the range that the camera uses, ie
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;
160 public OnvifConnection(IpCameraHandler ipCameraHandler, String ipAddress, String user, String password) {
161 this.ipCameraHandler = ipCameraHandler;
162 if (!ipAddress.isEmpty()) {
164 this.password = password;
165 getIPandPortFromUrl(ipAddress);
169 private String getXml(RequestType requestType) {
171 switch (requestType) {
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>";
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>";
219 case GetDeviceInformation:
220 return "<GetDeviceInformation xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/>";
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>";
226 return "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver10/media/wsdl\"><ProfileToken>"
227 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetSnapshotUri>";
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\"/>";
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>";
239 return "<Unsubscribe xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"></Unsubscribe>";
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>";
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>";
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>";
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>"
281 + "</NodeToken><DefaultAbsolutePantTiltPositionSpace>AbsolutePanTiltPositionSpace</DefaultAbsolutePantTiltPositionSpace><DefaultAbsoluteZoomPositionSpace>AbsoluteZoomPositionSpace</DefaultAbsoluteZoomPositionSpace></PTZConfiguration></SetConfiguration>";
283 return "<GetNodes xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"></GetNodes>";
285 return "<GetStatus xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
286 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetStatus>";
288 return "<GotoPreset xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
289 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><PresetToken>"
290 + presetTokens.get(presetTokenIndex) + "</PresetToken></GotoPreset>";
292 return "<GetPresets xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
293 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetPresets>";
295 } catch (IndexOutOfBoundsException e) {
297 logger.debug("IndexOutOfBoundsException occured, camera is not connected via ONVIF: {}",
300 logger.debug("IndexOutOfBoundsException occured, {}", e.getMessage());
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.
319 sendOnvifRequest(requestBuilder(RequestType.GetCapabilities, deviceXAddr));
320 parseDateAndTime(message);
321 logger.debug("Openhabs UTC dateTime is:{}", getUTCdateTime());
322 } else if (message.contains("GetCapabilitiesResponse")) {// 2nd to be sent.
324 sendOnvifRequest(requestBuilder(RequestType.GetProfiles, mediaXAddr));
325 } else if (message.contains("GetProfilesResponse")) {// 3rd to be sent.
326 parseProfiles(message);
327 sendOnvifRequest(requestBuilder(RequestType.GetSnapshotUri, mediaXAddr));
328 sendOnvifRequest(requestBuilder(RequestType.GetStreamUri, mediaXAddr));
330 sendPTZRequest(RequestType.GetNodes);
332 if (usingEvents) {// stops API cameras from getting sent ONVIF events.
333 sendOnvifRequest(requestBuilder(RequestType.GetEventProperties, eventXAddr));
334 sendOnvifRequest(requestBuilder(RequestType.GetServiceCapabilities, eventXAddr));
336 } else if (message.contains("GetServiceCapabilitiesResponse")) {
337 if (message.contains("WSSubscriptionPolicySupport=\"true\"")) {
338 sendOnvifRequest(requestBuilder(RequestType.Subscribe, eventXAddr));
340 } else if (message.contains("GetEventPropertiesResponse")) {
341 sendOnvifRequest(requestBuilder(RequestType.CreatePullPointSubscription, eventXAddr));
342 } else if (message.contains("CreatePullPointSubscriptionResponse")) {
343 subscriptionXAddr = Helper.fetchXML(message, "SubscriptionReference>", "Address>");
344 logger.debug("subscriptionXAddr={}", subscriptionXAddr);
345 sendOnvifRequest(requestBuilder(RequestType.PullMessages, subscriptionXAddr));
346 } else if (message.contains("GetStatusResponse")) {
347 processPTZLocation(message);
348 } else if (message.contains("GetPresetsResponse")) {
349 parsePresets(message);
350 } else if (message.contains("GetConfigurationsResponse")) {
351 sendPTZRequest(RequestType.GetPresets);
352 ptzConfigToken = Helper.fetchXML(message, "PTZConfiguration", "token=\"");
353 logger.debug("ptzConfigToken={}", ptzConfigToken);
354 sendPTZRequest(RequestType.GetConfigurationOptions);
355 } else if (message.contains("GetNodesResponse")) {
356 sendPTZRequest(RequestType.GetStatus);
357 ptzNodeToken = Helper.fetchXML(message, "", "token=\"");
358 logger.debug("ptzNodeToken={}", ptzNodeToken);
359 sendPTZRequest(RequestType.GetConfigurations);
360 } else if (message.contains("GetDeviceInformationResponse")) {
361 logger.debug("GetDeviceInformationResponse recieved");
362 } else if (message.contains("GetSnapshotUriResponse")) {
363 snapshotUri = removeIPfromUrl(Helper.fetchXML(message, ":MediaUri", ":Uri"));
364 logger.debug("GetSnapshotUri:{}", snapshotUri);
365 if (ipCameraHandler.snapshotUri.isEmpty()
366 && !"ffmpeg".equals(ipCameraHandler.cameraConfig.getSnapshotUrl())) {
367 ipCameraHandler.snapshotUri = snapshotUri;
369 } else if (message.contains("GetStreamUriResponse")) {
370 rtspUri = Helper.fetchXML(message, ":MediaUri", ":Uri>");
371 logger.debug("GetStreamUri:{}", rtspUri);
372 if (ipCameraHandler.cameraConfig.getFfmpegInput().isEmpty()) {
373 ipCameraHandler.rtspUri = rtspUri;
378 HttpRequest requestBuilder(RequestType requestType, String xAddr) {
379 logger.trace("Sending ONVIF request:{}", requestType);
380 String security = "";
381 String extraEnvelope = "";
382 String headerTo = "";
383 String getXmlCache = getXml(requestType);
384 if (requestType.equals(RequestType.CreatePullPointSubscription) || requestType.equals(RequestType.PullMessages)
385 || requestType.equals(RequestType.Renew) || requestType.equals(RequestType.Unsubscribe)) {
386 headerTo = "<a:To s:mustUnderstand=\"1\">" + xAddr + "</a:To>";
387 extraEnvelope = " xmlns:a=\"http://www.w3.org/2005/08/addressing\"";
390 if (!password.isEmpty() && !requestType.equals(RequestType.GetSystemDateAndTime)) {
391 String nonce = createNonce();
392 String dateTime = getUTCdateTime();
393 String digest = createDigest(nonce, dateTime);
394 security = "<Security s:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>"
396 + "</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">"
398 + "</Password><Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">"
399 + encodeBase64(nonce)
400 + "</Nonce><Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
401 + dateTime + "</Created></UsernameToken></Security>";
402 headers = "<s:Header>" + security + headerTo + "</s:Header>";
403 } else {// GetSystemDateAndTime must not be password protected as per spec.
406 FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, new HttpMethod("POST"),
407 removeIPfromUrl(xAddr));
408 String actionString = Helper.fetchXML(getXmlCache, requestType.toString(), "xmlns=\"");
409 request.headers().add("Content-Type",
410 "application/soap+xml; charset=utf-8; action=\"" + actionString + "/" + requestType + "\"");
411 request.headers().add("Charset", "utf-8");
412 request.headers().set("Host", extractIPportFromUrl(xAddr));
413 request.headers().set("Connection", HttpHeaderValues.CLOSE);
414 request.headers().set("Accept-Encoding", "gzip, deflate");
415 String fullXml = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"" + extraEnvelope + ">"
417 + "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
418 + getXmlCache + "</s:Body></s:Envelope>";
419 request.headers().add("SOAPAction", "\"" + actionString + "/" + requestType + "\"");
420 ByteBuf bbuf = Unpooled.copiedBuffer(fullXml, StandardCharsets.UTF_8);
421 request.headers().set("Content-Length", bbuf.readableBytes());
422 request.content().clear().writeBytes(bbuf);
427 * The {@link removeIPfromUrl} Will throw away all text before the cameras IP, also removes the IP and the PORT
428 * leaving just the URL.
430 * @author Matthew Skinner - Initial contribution
432 String removeIPfromUrl(String url) {
433 int index = url.indexOf("//");
434 if (index != -1) {// now remove the :port
435 index = url.indexOf("/", index + 2);
438 logger.debug("We hit an issue parsing url:{}", url);
441 return url.substring(index);
444 String extractIPportFromUrl(String url) {
445 int startIndex = url.indexOf("//") + 2;
446 int endIndex = url.indexOf("/", startIndex);// skip past any :port to the slash /
447 if (startIndex != -1 && endIndex != -1) {
448 return url.substring(startIndex, endIndex);
450 logger.debug("We hit an issue extracting IP:PORT from url:{}", url);
454 void parseXAddr(String message) {
455 // Normally I would search '<tt:XAddr>' instead but Foscam needed this work around.
456 String temp = Helper.fetchXML(message, "<tt:Device", "tt:XAddr");
457 if (!temp.isEmpty()) {
459 logger.debug("deviceXAddr:{}", deviceXAddr);
461 temp = Helper.fetchXML(message, "<tt:Events", "tt:XAddr");
462 if (!temp.isEmpty()) {
463 subscriptionXAddr = eventXAddr = temp;
464 logger.debug("eventsXAddr:{}", eventXAddr);
466 temp = Helper.fetchXML(message, "<tt:Media", "tt:XAddr");
467 if (!temp.isEmpty()) {
469 logger.debug("mediaXAddr:{}", mediaXAddr);
472 ptzXAddr = Helper.fetchXML(message, "<tt:PTZ", "tt:XAddr");
473 if (ptzXAddr.isEmpty()) {
475 logger.trace("Camera must not support PTZ, it failed to give a <tt:PTZ><tt:XAddr>:{}", message);
477 logger.debug("ptzXAddr:{}", ptzXAddr);
481 private void parseDateAndTime(String message) {
482 String minute = Helper.fetchXML(message, "UTCDateTime", "Minute>");
483 String hour = Helper.fetchXML(message, "UTCDateTime", "Hour>");
484 String second = Helper.fetchXML(message, "UTCDateTime", "Second>");
485 String day = Helper.fetchXML(message, "UTCDateTime", "Day>");
486 String month = Helper.fetchXML(message, "UTCDateTime", "Month>");
487 String year = Helper.fetchXML(message, "UTCDateTime", "Year>");
488 logger.debug("Cameras UTC dateTime is:{}-{}-{}T{}:{}:{}", year, month, day, hour, minute, second);
491 private String getUTCdateTime() {
492 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
493 format.setTimeZone(TimeZone.getTimeZone("UTC"));
494 return format.format(new Date());
497 String createNonce() {
498 Random nonce = new Random();
499 return "" + nonce.nextInt();
502 String encodeBase64(String raw) {
503 return Base64.getEncoder().encodeToString(raw.getBytes());
506 String createDigest(String nOnce, String dateTime) {
507 String beforeEncryption = nOnce + dateTime + password;
508 MessageDigest msgDigest;
509 byte[] encryptedRaw = null;
511 msgDigest = MessageDigest.getInstance("SHA-1");
513 msgDigest.update(beforeEncryption.getBytes(StandardCharsets.UTF_8));
514 encryptedRaw = msgDigest.digest();
515 } catch (NoSuchAlgorithmException e) {
517 return Base64.getEncoder().encodeToString(encryptedRaw);
520 @SuppressWarnings("null")
521 public void sendOnvifRequest(HttpRequest request) {
522 if (bootstrap == null) {
523 mainEventLoopGroup = new NioEventLoopGroup(2);
524 bootstrap = new Bootstrap();
525 bootstrap.group(mainEventLoopGroup);
526 bootstrap.channel(NioSocketChannel.class);
527 bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
528 bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
529 bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 8);
530 bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024);
531 bootstrap.option(ChannelOption.TCP_NODELAY, true);
532 bootstrap.handler(new ChannelInitializer<SocketChannel>() {
535 public void initChannel(SocketChannel socketChannel) throws Exception {
536 socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 70));
537 socketChannel.pipeline().addLast("HttpClientCodec", new HttpClientCodec());
538 socketChannel.pipeline().addLast("OnvifCodec", new OnvifCodec(getHandle()));
542 if (!mainEventLoopGroup.isShuttingDown()) {
543 bootstrap.connect(new InetSocketAddress(ipAddress, onvifPort)).addListener(new ChannelFutureListener() {
546 public void operationComplete(@Nullable ChannelFuture future) {
547 if (future == null) {
550 if (future.isSuccess()) {
551 Channel ch = future.channel();
552 ch.writeAndFlush(request);
553 } else { // an error occured
554 logger.debug("Camera is not reachable on ONVIF port:{} or the port may be wrong.", onvifPort);
562 logger.debug("ONVIF message not sent as connection is shutting down");
566 OnvifConnection getHandle() {
570 void getIPandPortFromUrl(String url) {
571 int beginIndex = url.indexOf(":");
572 int endIndex = url.indexOf("/", beginIndex);
573 if (beginIndex >= 0 && endIndex == -1) {// 192.168.1.1:8080
574 ipAddress = url.substring(0, beginIndex);
575 onvifPort = Integer.parseInt(url.substring(beginIndex + 1));
576 } else if (beginIndex >= 0 && endIndex > beginIndex) {// 192.168.1.1:8080/foo/bar
577 ipAddress = url.substring(0, beginIndex);
578 onvifPort = Integer.parseInt(url.substring(beginIndex + 1, endIndex));
579 } else {// 192.168.1.1
581 logger.debug("No Onvif Port found when parsing:{}", url);
585 public void gotoPreset(int index) {
587 if (index > 0) {// 0 is reserved for HOME as cameras seem to start at preset 1.
588 if (presetTokens.isEmpty()) {
589 logger.warn("Camera did not report any ONVIF preset locations, updating preset tokens now.");
590 sendPTZRequest(RequestType.GetPresets);
592 presetTokenIndex = index - 1;
593 sendPTZRequest(RequestType.GotoPreset);
599 public void eventRecieved(String eventMessage) {
600 String topic = Helper.fetchXML(eventMessage, "Topic", "tns1:");
601 String dataName = Helper.fetchXML(eventMessage, "tt:Data", "Name=\"");
602 String dataValue = Helper.fetchXML(eventMessage, "tt:Data", "Value=\"");
603 if (!topic.isEmpty()) {
604 logger.debug("Onvif Event Topic:{}, Data:{}, Value:{}", topic, dataName, dataValue);
607 case "RuleEngine/CellMotionDetector/Motion":
608 if ("true".equals(dataValue)) {
609 ipCameraHandler.motionDetected(CHANNEL_CELL_MOTION_ALARM);
610 } else if ("false".equals(dataValue)) {
611 ipCameraHandler.noMotionDetected(CHANNEL_CELL_MOTION_ALARM);
614 case "VideoSource/MotionAlarm":
615 if ("true".equals(dataValue)) {
616 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
617 } else if ("false".equals(dataValue)) {
618 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
621 case "AudioAnalytics/Audio/DetectedSound":
622 if ("true".equals(dataValue)) {
623 ipCameraHandler.audioDetected();
624 } else if ("false".equals(dataValue)) {
625 ipCameraHandler.noAudioDetected();
628 case "RuleEngine/FieldDetector/ObjectsInside":
629 if ("true".equals(dataValue)) {
630 ipCameraHandler.motionDetected(CHANNEL_FIELD_DETECTION_ALARM);
631 } else if ("false".equals(dataValue)) {
632 ipCameraHandler.noMotionDetected(CHANNEL_FIELD_DETECTION_ALARM);
635 case "RuleEngine/LineDetector/Crossed":
636 if ("ObjectId".equals(dataName)) {
637 ipCameraHandler.motionDetected(CHANNEL_LINE_CROSSING_ALARM);
639 ipCameraHandler.noMotionDetected(CHANNEL_LINE_CROSSING_ALARM);
642 case "RuleEngine/TamperDetector/Tamper":
643 if ("true".equals(dataValue)) {
644 ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.ON);
645 } else if ("false".equals(dataValue)) {
646 ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.OFF);
649 case "Device/HardwareFailure/StorageFailure":
650 if ("true".equals(dataValue)) {
651 ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.ON);
652 } else if ("false".equals(dataValue)) {
653 ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.OFF);
656 case "VideoSource/ImageTooDark/AnalyticsService":
657 case "VideoSource/ImageTooDark/ImagingService":
658 case "VideoSource/ImageTooDark/RecordingService":
659 if ("true".equals(dataValue)) {
660 ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.ON);
661 } else if ("false".equals(dataValue)) {
662 ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.OFF);
665 case "VideoSource/GlobalSceneChange/AnalyticsService":
666 case "VideoSource/GlobalSceneChange/ImagingService":
667 case "VideoSource/GlobalSceneChange/RecordingService":
668 if ("true".equals(dataValue)) {
669 ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.ON);
670 } else if ("false".equals(dataValue)) {
671 ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.OFF);
674 case "VideoSource/ImageTooBright/AnalyticsService":
675 case "VideoSource/ImageTooBright/ImagingService":
676 case "VideoSource/ImageTooBright/RecordingService":
677 if ("true".equals(dataValue)) {
678 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.ON);
679 } else if ("false".equals(dataValue)) {
680 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.OFF);
683 case "VideoSource/ImageTooBlurry/AnalyticsService":
684 case "VideoSource/ImageTooBlurry/ImagingService":
685 case "VideoSource/ImageTooBlurry/RecordingService":
686 if ("true".equals(dataValue)) {
687 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.ON);
688 } else if ("false".equals(dataValue)) {
689 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.OFF);
694 sendOnvifRequest(requestBuilder(RequestType.Renew, subscriptionXAddr));
697 public boolean supportsPTZ() {
701 public void getStatus() {
703 sendPTZRequest(RequestType.GetStatus);
707 public Float getAbsolutePan() {
708 return currentPanPercentage;
711 public Float getAbsoluteTilt() {
712 return currentTiltPercentage;
715 public Float getAbsoluteZoom() {
716 return currentZoomPercentage;
719 public void setAbsolutePan(Float panValue) {// Value is 0-100% of cameras range
721 currentPanPercentage = panValue;
722 currentPanCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * panValue + panRangeMin);
726 public void setAbsoluteTilt(Float tiltValue) {// Value is 0-100% of cameras range
728 currentTiltPercentage = tiltValue;
729 currentTiltCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * tiltValue + tiltRangeMin);
733 public void setAbsoluteZoom(Float zoomValue) {// Value is 0-100% of cameras range
735 currentZoomPercentage = zoomValue;
736 currentZoomCamValue = ((((zoomMin - zoomMax) * -1) / 100) * zoomValue + zoomMin);
740 public void absoluteMove() { // Camera wont move until PTZ values are set, then call this.
742 sendPTZRequest(RequestType.AbsoluteMove);
746 public void setSelectedMediaProfile(int mediaProfileIndex) {
747 this.mediaProfileIndex = mediaProfileIndex;
750 List<String> listOfResults(String message, String heading, String key) {
751 List<String> results = new LinkedList<>();
753 for (int startLookingFromIndex = 0; startLookingFromIndex != -1;) {
754 startLookingFromIndex = message.indexOf(heading, startLookingFromIndex);
755 if (startLookingFromIndex >= 0) {
756 temp = Helper.fetchXML(message.substring(startLookingFromIndex), heading, key);
757 if (!temp.isEmpty()) {
758 logger.trace("String was found:{}", temp);
761 return results;// key string must not exist so stop looking.
763 startLookingFromIndex += temp.length();
769 void parsePresets(String message) {
770 List<StateOption> presets = new ArrayList<>();
771 int counter = 1;// Presets start at 1 not 0. HOME may be added to index 0.
772 presetTokens = listOfResults(message, "<tptz:Preset", "token=\"");
773 presetNames = listOfResults(message, "<tptz:Preset", "<tt:Name>");
774 if (presetTokens.size() != presetNames.size()) {
775 logger.warn("Camera did not report the same number of Tokens and Names for PTZ presets");
778 for (String value : presetNames) {
779 presets.add(new StateOption(Integer.toString(counter++), value));
781 ipCameraHandler.stateDescriptionProvider
782 .setStateOptions(new ChannelUID(ipCameraHandler.getThing().getUID(), CHANNEL_GOTO_PRESET), presets);
785 void parseProfiles(String message) {
786 mediaProfileTokens = listOfResults(message, "<trt:Profiles", "token=\"");
787 if (mediaProfileIndex >= mediaProfileTokens.size()) {
789 "You have set the media profile to {} when the camera reported {} profiles. Falling back to mainstream 0.",
790 mediaProfileIndex, mediaProfileTokens.size());
791 mediaProfileIndex = 0;
795 void processPTZLocation(String result) {
796 logger.debug("Processing new PTZ location now");
798 int beginIndex = result.indexOf("x=\"");
799 int endIndex = result.indexOf("\"", (beginIndex + 3));
800 if (beginIndex >= 0 && endIndex >= 0) {
801 currentPanCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
802 currentPanPercentage = (((panRangeMin - currentPanCamValue) * -1) / ((panRangeMin - panRangeMax) * -1))
804 logger.debug("Pan is updating to:{} and the cam value is {}", Math.round(currentPanPercentage),
808 "Binding could not determin the cameras current PTZ location. Not all cameras respond to GetStatus requests.");
812 beginIndex = result.indexOf("y=\"");
813 endIndex = result.indexOf("\"", (beginIndex + 3));
814 if (beginIndex >= 0 && endIndex >= 0) {
815 currentTiltCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
816 currentTiltPercentage = (((tiltRangeMin - currentTiltCamValue) * -1) / ((tiltRangeMin - tiltRangeMax) * -1))
818 logger.debug("Tilt is updating to:{} and the cam value is {}", Math.round(currentTiltPercentage),
819 currentTiltCamValue);
824 beginIndex = result.lastIndexOf("x=\"");
825 endIndex = result.indexOf("\"", (beginIndex + 3));
826 if (beginIndex >= 0 && endIndex >= 0) {
827 currentZoomCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
828 currentZoomPercentage = (((zoomMin - currentZoomCamValue) * -1) / ((zoomMin - zoomMax) * -1)) * 100;
829 logger.debug("Zoom is updating to:{} and the cam value is {}", Math.round(currentZoomPercentage),
830 currentZoomCamValue);
836 public void sendPTZRequest(RequestType requestType) {
838 logger.debug("ONVIF was not connected when a PTZ request was made, connecting now");
839 connect(usingEvents);
841 sendOnvifRequest(requestBuilder(requestType, ptzXAddr));
844 public void sendEventRequest(RequestType requestType) {
845 sendOnvifRequest(requestBuilder(requestType, eventXAddr));
848 public void connect(boolean useEvents) {
852 logger.debug("Connecting {} to ONVIF", ipAddress);
853 threadPool = Executors.newScheduledThreadPool(2);
854 sendOnvifRequest(requestBuilder(RequestType.GetSystemDateAndTime, deviceXAddr));
855 usingEvents = useEvents;
862 public boolean isConnected() {
871 private void cleanup() {
872 if (!isConnected && !mainEventLoopGroup.isShuttingDown()) {
874 mainEventLoopGroup.shutdownGracefully();
875 mainEventLoopGroup.awaitTermination(3, TimeUnit.SECONDS);
876 } catch (InterruptedException e) {
877 logger.warn("ONVIF was not cleanly shutdown, due to being interrupted");
879 logger.debug("Eventloop is shutdown:{}", mainEventLoopGroup.isShutdown());
881 threadPool.shutdown();
886 public void disconnect() {
887 connecting.lock();// Lock out multiple disconnect()/connect() attempts as we try to send Unsubscribe.
889 isConnected = false;// isConnected is not thread safe, connecting.lock() used as fix.
890 if (bootstrap != null) {
891 if (usingEvents && !mainEventLoopGroup.isShuttingDown()) {
892 // Some cameras may continue to send events even when they can't reach a server.
893 sendOnvifRequest(requestBuilder(RequestType.Unsubscribe, subscriptionXAddr));
895 // give time for the Unsubscribe request to be sent, shutdownGracefully will try to send it first.
896 threadPool.schedule(this::cleanup, 50, TimeUnit.MILLISECONDS);