2 * Copyright (c) 2010-2024 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.ConnectException;
18 import java.net.InetSocketAddress;
19 import java.nio.charset.StandardCharsets;
20 import java.security.MessageDigest;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.SecureRandom;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Base64;
26 import java.util.Date;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Random;
30 import java.util.TimeZone;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.locks.ReentrantLock;
36 import org.eclipse.jdt.annotation.NonNullByDefault;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.openhab.binding.ipcamera.internal.Helper;
39 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.thing.ChannelUID;
42 import org.openhab.core.types.StateOption;
43 import org.openhab.core.util.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import io.netty.bootstrap.Bootstrap;
48 import io.netty.buffer.ByteBuf;
49 import io.netty.buffer.Unpooled;
50 import io.netty.channel.Channel;
51 import io.netty.channel.ChannelFuture;
52 import io.netty.channel.ChannelFutureListener;
53 import io.netty.channel.ChannelInitializer;
54 import io.netty.channel.ChannelOption;
55 import io.netty.channel.ConnectTimeoutException;
56 import io.netty.channel.EventLoopGroup;
57 import io.netty.channel.nio.NioEventLoopGroup;
58 import io.netty.channel.socket.SocketChannel;
59 import io.netty.channel.socket.nio.NioSocketChannel;
60 import io.netty.handler.codec.http.DefaultFullHttpRequest;
61 import io.netty.handler.codec.http.FullHttpRequest;
62 import io.netty.handler.codec.http.HttpClientCodec;
63 import io.netty.handler.codec.http.HttpHeaderValues;
64 import io.netty.handler.codec.http.HttpMethod;
65 import io.netty.handler.codec.http.HttpVersion;
66 import io.netty.handler.timeout.IdleStateHandler;
69 * The {@link OnvifConnection} This is a basic Netty implementation for connecting and communicating to ONVIF cameras.
71 * @author Matthew Skinner - Initial contribution
72 * @author Kai Kreuzer - Improve handling for certain cameras
76 public class OnvifConnection {
77 public enum RequestType {
87 CreatePullPointSubscription,
91 GetServiceCapabilities,
107 GetConfigurationOptions,
116 private final Logger logger = LoggerFactory.getLogger(getClass());
117 private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
118 private @Nullable Bootstrap bootstrap;
119 private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup(2);
120 private ReentrantLock connecting = new ReentrantLock();
121 private String ipAddress = "";
122 private String user = "";
123 private String password = "";
124 private int onvifPort = 80;
125 private String deviceXAddr = "http://" + ipAddress + "/onvif/device_service";
126 private String eventXAddr = "http://" + ipAddress + "/onvif/device_service";
127 private String mediaXAddr = "http://" + ipAddress + "/onvif/device_service";
128 @SuppressWarnings("unused")
129 private String imagingXAddr = "http://" + ipAddress + "/onvif/device_service";
130 private String ptzXAddr = "http://" + ipAddress + "/onvif/ptz_service";
131 public String subscriptionXAddr = "http://" + ipAddress + "/onvif/device_service";
132 private boolean isConnected = false;
133 private int mediaProfileIndex = 0;
134 private String rtspUri = "";
135 private IpCameraHandler ipCameraHandler;
136 private boolean supportsEvents = false; // camera has replied that it can do events
137 // Use/skip events even if camera support them. API cameras skip, as their own methods give better results.
138 private boolean usingEvents = false;
140 // These hold the cameras PTZ position in the range that the camera uses, ie
142 private Float panRangeMin = -1.0f;
143 private Float panRangeMax = 1.0f;
144 private Float tiltRangeMin = -1.0f;
145 private Float tiltRangeMax = 1.0f;
146 private Float zoomMin = 0.0f;
147 private Float zoomMax = 1.0f;
148 // These hold the PTZ values for updating openHABs controls in 0-100 range
149 private Float currentPanPercentage = 0.0f;
150 private Float currentTiltPercentage = 0.0f;
151 private Float currentZoomPercentage = 0.0f;
152 private Float currentPanCamValue = 0.0f;
153 private Float currentTiltCamValue = 0.0f;
154 private Float currentZoomCamValue = 0.0f;
155 private String ptzNodeToken = "000";
156 private String ptzConfigToken = "000";
157 private int presetTokenIndex = 0;
158 private List<String> presetTokens = new LinkedList<>();
159 private List<String> presetNames = new LinkedList<>();
160 private List<String> mediaProfileTokens = new LinkedList<>();
161 private boolean ptzDevice = true;
163 public OnvifConnection(IpCameraHandler ipCameraHandler, String ipAddress, String user, String password) {
164 this.ipCameraHandler = ipCameraHandler;
165 if (!ipAddress.isEmpty()) {
167 this.password = password;
168 getIPandPortFromUrl(ipAddress);
172 private String getXml(RequestType requestType) {
174 switch (requestType) {
176 return "<AbsoluteMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
177 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><Position><PanTilt x=\""
178 + currentPanCamValue + "\" y=\"" + currentTiltCamValue
179 + "\" space=\"http://www.onvif.org/ver10/tptz/PanTiltSpaces/PositionGenericSpace\">\n"
180 + "</PanTilt>\n" + "<Zoom x=\"" + currentZoomCamValue
181 + "\" space=\"http://www.onvif.org/ver10/tptz/ZoomSpaces/PositionGenericSpace\">\n"
182 + "</Zoom>\n" + "</Position>\n"
183 + "<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"
184 + "</Speed></AbsoluteMove>";
185 case AddPTZConfiguration: // not tested to work yet
186 return "<AddPTZConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
187 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><ConfigurationToken>"
188 + ptzConfigToken + "</ConfigurationToken></AddPTZConfiguration>";
189 case ContinuousMoveLeft:
190 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
191 + mediaProfileTokens.get(mediaProfileIndex)
192 + "</ProfileToken><Velocity><PanTilt x=\"-0.5\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
193 case ContinuousMoveRight:
194 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
195 + mediaProfileTokens.get(mediaProfileIndex)
196 + "</ProfileToken><Velocity><PanTilt x=\"0.5\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
197 case ContinuousMoveUp:
198 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
199 + mediaProfileTokens.get(mediaProfileIndex)
200 + "</ProfileToken><Velocity><PanTilt x=\"0\" y=\"-0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
201 case ContinuousMoveDown:
202 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
203 + mediaProfileTokens.get(mediaProfileIndex)
204 + "</ProfileToken><Velocity><PanTilt x=\"0\" y=\"0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
206 return "<Stop xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
207 + mediaProfileTokens.get(mediaProfileIndex)
208 + "</ProfileToken><PanTilt>true</PanTilt><Zoom>true</Zoom></Stop>";
209 case ContinuousMoveIn:
210 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
211 + mediaProfileTokens.get(mediaProfileIndex)
212 + "</ProfileToken><Velocity><Zoom x=\"0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
213 case ContinuousMoveOut:
214 return "<ContinuousMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
215 + mediaProfileTokens.get(mediaProfileIndex)
216 + "</ProfileToken><Velocity><Zoom x=\"-0.5\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Velocity></ContinuousMove>";
217 case CreatePullPointSubscription:
218 return "<CreatePullPointSubscription xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><InitialTerminationTime>PT600S</InitialTerminationTime></CreatePullPointSubscription>";
219 case GetCapabilities:
220 return "<GetCapabilities xmlns=\"http://www.onvif.org/ver10/device/wsdl\"><Category>All</Category></GetCapabilities>";
222 case GetDeviceInformation:
223 return "<GetDeviceInformation xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/>";
225 return "<GetProfiles xmlns=\"http://www.onvif.org/ver10/media/wsdl\"/>";
226 case GetServiceCapabilities:
227 return "<GetServiceCapabilities xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"></GetServiceCapabilities>";
229 return "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver10/media/wsdl\"><ProfileToken>"
230 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetSnapshotUri>";
232 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>"
233 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetStreamUri>";
234 case GetSystemDateAndTime:
235 return "<GetSystemDateAndTime xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/>";
237 return "<Subscribe xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"><ConsumerReference><Address>http://"
238 + ipCameraHandler.hostIp + ":" + SERVLET_PORT + "/ipcamera/"
239 + ipCameraHandler.getThing().getUID().getId()
240 + "/OnvifEvent</Address></ConsumerReference></Subscribe>";
242 return "<Unsubscribe xmlns=\"http://docs.oasis-open.org/wsn/b-2/\"></Unsubscribe>";
244 return "<PullMessages xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><Timeout>PT8S</Timeout><MessageLimit>1</MessageLimit></PullMessages>";
245 case GetEventProperties:
246 return "<GetEventProperties xmlns=\"http://www.onvif.org/ver10/events/wsdl\"/>";
247 case RelativeMoveLeft:
248 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
249 + mediaProfileTokens.get(mediaProfileIndex)
250 + "</ProfileToken><Translation><PanTilt x=\"0.05000000\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
251 case RelativeMoveRight:
252 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
253 + mediaProfileTokens.get(mediaProfileIndex)
254 + "</ProfileToken><Translation><PanTilt x=\"-0.05000000\" y=\"0\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
256 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
257 + mediaProfileTokens.get(mediaProfileIndex)
258 + "</ProfileToken><Translation><PanTilt x=\"0\" y=\"0.100000000\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
259 case RelativeMoveDown:
260 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
261 + mediaProfileTokens.get(mediaProfileIndex)
262 + "</ProfileToken><Translation><PanTilt x=\"0\" y=\"-0.100000000\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
264 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
265 + mediaProfileTokens.get(mediaProfileIndex)
266 + "</ProfileToken><Translation><Zoom x=\"0.0240506344\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
267 case RelativeMoveOut:
268 return "<RelativeMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
269 + mediaProfileTokens.get(mediaProfileIndex)
270 + "</ProfileToken><Translation><Zoom x=\"-0.0240506344\" xmlns=\"http://www.onvif.org/ver10/schema\"/></Translation></RelativeMove>";
272 return "<Renew xmlns=\"http://docs.oasis-open.org/wsn/b-2\"><TerminationTime>PT10S</TerminationTime></Renew>";
273 case GetConfigurations:
274 return "<GetConfigurations xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"></GetConfigurations>";
275 case GetConfigurationOptions:
276 return "<GetConfigurationOptions xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ConfigurationToken>"
277 + ptzConfigToken + "</ConfigurationToken></GetConfigurationOptions>";
278 case GetConfiguration:
279 return "<GetConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><PTZConfigurationToken>"
280 + ptzConfigToken + "</PTZConfigurationToken></GetConfiguration>";
281 case SetConfiguration:// not tested to work yet
282 return "<SetConfiguration xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><PTZConfiguration><NodeToken>"
284 + "</NodeToken><DefaultAbsolutePantTiltPositionSpace>AbsolutePanTiltPositionSpace</DefaultAbsolutePantTiltPositionSpace><DefaultAbsoluteZoomPositionSpace>AbsoluteZoomPositionSpace</DefaultAbsoluteZoomPositionSpace></PTZConfiguration></SetConfiguration>";
286 return "<GetNodes xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"></GetNodes>";
288 return "<GetStatus xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
289 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetStatus>";
291 return "<GotoPreset xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
292 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken><PresetToken>"
293 + presetTokens.get(presetTokenIndex) + "</PresetToken></GotoPreset>";
295 return "<GetPresets xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
296 + mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetPresets>";
298 } catch (IndexOutOfBoundsException e) {
300 logger.debug("IndexOutOfBoundsException occured, camera is not connected via ONVIF: {}",
303 logger.debug("IndexOutOfBoundsException occured, {}", e.getMessage());
309 public void processReply(String message) {
310 logger.trace("ONVIF reply is: {}", message);
311 if (message.contains("PullMessagesResponse")) {
312 eventRecieved(message);
313 sendOnvifRequest(RequestType.PullMessages, subscriptionXAddr);
314 } else if (message.contains("RenewResponse")) {
315 } else if (message.contains("GetSystemDateAndTimeResponse")) {// 1st to be sent.
316 setIsConnected(true);// Instar profile T only cameras need this
317 parseDateAndTime(message);
318 logger.debug("openHAB UTC dateTime is: {}", getUTCdateTime());
319 } else if (message.contains("GetCapabilitiesResponse")) {// 2nd to be sent.
321 sendOnvifRequest(RequestType.GetProfiles, mediaXAddr);
322 } else if (message.contains("GetProfilesResponse")) {// 3rd to be sent.
323 setIsConnected(true);
324 parseProfiles(message);
325 sendOnvifRequest(RequestType.GetSnapshotUri, mediaXAddr);
326 sendOnvifRequest(RequestType.GetStreamUri, mediaXAddr);
328 sendPTZRequest(RequestType.GetNodes);
330 if (usingEvents) {// stops API cameras from getting sent ONVIF events.
331 sendOnvifRequest(RequestType.GetEventProperties, eventXAddr);
332 sendOnvifRequest(RequestType.GetServiceCapabilities, eventXAddr);
334 } else if (message.contains("GetServiceCapabilitiesResponse")) {
335 if (message.contains("WSSubscriptionPolicySupport=\"true\"")) {
336 sendOnvifRequest(RequestType.Subscribe, eventXAddr);
338 } else if (message.contains("GetEventPropertiesResponse")) {
339 sendOnvifRequest(RequestType.CreatePullPointSubscription, eventXAddr);
340 } else if (message.contains("CreatePullPointSubscriptionResponse")) {
341 supportsEvents = true;
342 subscriptionXAddr = Helper.fetchXML(message, "SubscriptionReference>", "Address>");
343 logger.debug("subscriptionXAddr={}", subscriptionXAddr);
344 sendOnvifRequest(RequestType.PullMessages, subscriptionXAddr);
345 } else if (message.contains("GetStatusResponse")) {
346 processPTZLocation(message);
347 } else if (message.contains("GetPresetsResponse")) {
348 parsePresets(message);
349 } else if (message.contains("GetConfigurationsResponse")) {
350 sendPTZRequest(RequestType.GetPresets);
351 ptzConfigToken = Helper.fetchXML(message, "PTZConfiguration", "token=\"");
352 logger.debug("ptzConfigToken={}", ptzConfigToken);
353 sendPTZRequest(RequestType.GetConfigurationOptions);
354 } else if (message.contains("GetNodesResponse")) {
355 sendPTZRequest(RequestType.GetStatus);
356 ptzNodeToken = Helper.fetchXML(message, "", "token=\"");
357 logger.debug("ptzNodeToken={}", ptzNodeToken);
358 sendPTZRequest(RequestType.GetConfigurations);
359 } else if (message.contains("GetDeviceInformationResponse")) {
360 logger.debug("GetDeviceInformationResponse received");
361 } else if (message.contains("GetSnapshotUriResponse")) {
362 String url = Helper.fetchXML(message, ":MediaUri", ":Uri");
363 if (!url.isBlank()) {
364 logger.debug("GetSnapshotUri: {}", url);
365 if (ipCameraHandler.snapshotUri.isEmpty()
366 && !"ffmpeg".equals(ipCameraHandler.cameraConfig.getSnapshotUrl())) {
367 ipCameraHandler.snapshotUri = ipCameraHandler.getCorrectUrlFormat(url);
368 if (ipCameraHandler.getPortFromShortenedUrl(url) != ipCameraHandler.cameraConfig.getPort()) {
369 logger.warn("ONVIF is reporting the snapshot does not match the things configured port of:{}",
370 ipCameraHandler.cameraConfig.getPort());
374 } else if (message.contains("GetStreamUriResponse")) {
375 String xml = StringUtils.unEscapeXml(Helper.fetchXML(message, ":MediaUri", ":Uri>"));
378 logger.debug("GetStreamUri: {}", rtspUri);
379 if (ipCameraHandler.cameraConfig.getFfmpegInput().isEmpty()) {
380 ipCameraHandler.rtspUri = rtspUri;
384 logger.trace("Unhandled ONVIF reply is: {}", message);
389 * The {@link removeIPandPortFromUrl} Will throw away all text before the cameras IP, also removes the IP and the
391 * leaving just the URL.
393 * @author Matthew Skinner - Initial contribution
395 String removeIPandPortFromUrl(String url) {
396 int index = url.indexOf("//");
397 if (index != -1) {// now remove the :port
398 index = url.indexOf("/", index + 2);
401 logger.debug("We hit an issue parsing url: {}", url);
404 return url.substring(index);
407 String extractIPportFromUrl(String url) {
408 int startIndex = url.indexOf("//") + 2;
409 int endIndex = url.indexOf("/", startIndex);// skip past any :port to the slash /
410 if (startIndex != -1 && endIndex != -1) {
411 return url.substring(startIndex, endIndex);
413 logger.debug("We hit an issue extracting IP:PORT from url: {}", url);
417 int extractPortFromUrl(String url) {
418 int startIndex = url.indexOf("//") + 2;// skip past http://
419 startIndex = url.indexOf(":", startIndex);
420 if (startIndex == -1) {// no port defined so use port 80
423 int endIndex = url.indexOf("/", startIndex);// skip past any :port to the slash /
424 if (endIndex == -1) {
427 return Integer.parseInt(url.substring(startIndex + 1, endIndex));
430 void parseXAddr(String message) {
431 // Normally I would search '<tt:XAddr>' instead but Foscam needed this work around.
432 String temp = Helper.fetchXML(message, "<tt:Device", "tt:XAddr");
433 if (!temp.isEmpty()) {
435 logger.debug("deviceXAddr: {}", deviceXAddr);
437 temp = Helper.fetchXML(message, "<tt:Events", "tt:XAddr");
438 if (!temp.isEmpty()) {
439 subscriptionXAddr = eventXAddr = temp;
440 logger.debug("eventsXAddr: {}", eventXAddr);
442 temp = Helper.fetchXML(message, "<tt:Media", "tt:XAddr");
443 if (!temp.isEmpty()) {
445 logger.debug("mediaXAddr: {}", mediaXAddr);
448 ptzXAddr = Helper.fetchXML(message, "<tt:PTZ", "tt:XAddr");
449 if (ptzXAddr.isEmpty()) {
451 logger.debug("Camera has no ONVIF PTZ support.");
452 List<org.openhab.core.thing.Channel> removeChannels = new ArrayList<>();
453 org.openhab.core.thing.Channel channel = ipCameraHandler.getThing().getChannel(CHANNEL_PAN);
454 if (channel != null) {
455 removeChannels.add(channel);
457 channel = ipCameraHandler.getThing().getChannel(CHANNEL_TILT);
458 if (channel != null) {
459 removeChannels.add(channel);
461 channel = ipCameraHandler.getThing().getChannel(CHANNEL_ZOOM);
462 if (channel != null) {
463 removeChannels.add(channel);
465 ipCameraHandler.removeChannels(removeChannels);
467 logger.debug("ptzXAddr: {}", ptzXAddr);
471 private void parseDateAndTime(String message) {
472 String minute = Helper.fetchXML(message, "UTCDateTime", "Minute>");
473 String hour = Helper.fetchXML(message, "UTCDateTime", "Hour>");
474 String second = Helper.fetchXML(message, "UTCDateTime", "Second>");
475 String day = Helper.fetchXML(message, "UTCDateTime", "Day>");
476 String month = Helper.fetchXML(message, "UTCDateTime", "Month>");
477 String year = Helper.fetchXML(message, "UTCDateTime", "Year>");
478 logger.debug("Camera UTC dateTime is: {}-{}-{}T{}:{}:{}", year, month, day, hour, minute, second);
481 private String getUTCdateTime() {
482 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
483 format.setTimeZone(TimeZone.getTimeZone("UTC"));
484 return format.format(new Date());
487 String createNonce() {
488 Random nonce = new SecureRandom();
489 return "" + nonce.nextInt();
492 String encodeBase64(String raw) {
493 return Base64.getEncoder().encodeToString(raw.getBytes());
496 String createDigest(String nOnce, String dateTime) {
497 String beforeEncryption = nOnce + dateTime + password;
498 MessageDigest msgDigest;
499 byte[] encryptedRaw = null;
501 msgDigest = MessageDigest.getInstance("SHA-1");
503 msgDigest.update(beforeEncryption.getBytes(StandardCharsets.UTF_8));
504 encryptedRaw = msgDigest.digest();
505 } catch (NoSuchAlgorithmException e) {
507 return Base64.getEncoder().encodeToString(encryptedRaw);
510 public void sendOnvifRequest(RequestType requestType, String xAddr) {
511 logger.trace("Sending ONVIF request: {} to {}", requestType, xAddr);
512 int port = extractPortFromUrl(xAddr);
513 String security = "";
514 String extraEnvelope = "";
515 String headerTo = "";
516 String getXmlCache = getXml(requestType);
517 if (requestType.equals(RequestType.CreatePullPointSubscription) || requestType.equals(RequestType.PullMessages)
518 || requestType.equals(RequestType.Renew) || requestType.equals(RequestType.Unsubscribe)) {
519 headerTo = "<a:To s:mustUnderstand=\"1\">" + xAddr + "</a:To>";
520 extraEnvelope = " xmlns:a=\"http://www.w3.org/2005/08/addressing\"";
523 if (!password.isEmpty() && !requestType.equals(RequestType.GetSystemDateAndTime)) {
524 String nonce = createNonce();
525 String dateTime = getUTCdateTime();
526 String digest = createDigest(nonce, dateTime);
527 security = "<Security s:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>"
529 + "</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">"
531 + "</Password><Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">"
532 + encodeBase64(nonce)
533 + "</Nonce><Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
534 + dateTime + "</Created></UsernameToken></Security>";
535 headers = "<s:Header>" + security + headerTo + "</s:Header>";
536 } else {// GetSystemDateAndTime must not be password protected as per spec.
539 FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, new HttpMethod("POST"),
540 removeIPandPortFromUrl(xAddr));
541 String actionString = Helper.fetchXML(getXmlCache, requestType.toString(), "xmlns=\"");
542 request.headers().add("Content-Type",
543 "application/soap+xml; charset=utf-8; action=\"" + actionString + "/" + requestType + "\"");
544 request.headers().add("Charset", "utf-8");
545 // Tapo brand have different ports for the event xAddr to the other xAddr, can't use 1 port for all ONVIF calls.
546 request.headers().set("Host", ipAddress + ":" + port);
547 request.headers().set("Connection", HttpHeaderValues.CLOSE);
548 request.headers().set("Accept-Encoding", "gzip, deflate");
549 String fullXml = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"" + extraEnvelope + ">"
551 + "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
552 + getXmlCache + "</s:Body></s:Envelope>";
553 request.headers().add("SOAPAction", "\"" + actionString + "/" + requestType + "\"");
554 ByteBuf bbuf = Unpooled.copiedBuffer(fullXml, StandardCharsets.UTF_8);
555 request.headers().set("Content-Length", bbuf.readableBytes());
556 request.content().clear().writeBytes(bbuf);
558 Bootstrap localBootstap = bootstrap;
559 if (localBootstap == null) {
560 mainEventLoopGroup = new NioEventLoopGroup(2);
561 localBootstap = new Bootstrap();
562 localBootstap.group(mainEventLoopGroup);
563 localBootstap.channel(NioSocketChannel.class);
564 localBootstap.option(ChannelOption.SO_KEEPALIVE, true);
565 localBootstap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
566 localBootstap.option(ChannelOption.SO_SNDBUF, 1024 * 8);
567 localBootstap.option(ChannelOption.SO_RCVBUF, 1024 * 1024);
568 localBootstap.option(ChannelOption.TCP_NODELAY, true);
569 localBootstap.handler(new ChannelInitializer<SocketChannel>() {
572 public void initChannel(SocketChannel socketChannel) throws Exception {
573 socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 18));
574 socketChannel.pipeline().addLast("HttpClientCodec", new HttpClientCodec());
575 socketChannel.pipeline().addLast("OnvifCodec", new OnvifCodec(getHandle()));
578 bootstrap = localBootstap;
580 if (!mainEventLoopGroup.isShuttingDown()) {
581 // Tapo brand have different ports for the event xAddr to the other xAddr, can't use 1 port for all calls.
582 localBootstap.connect(new InetSocketAddress(ipAddress, port)).addListener(new ChannelFutureListener() {
584 public void operationComplete(@Nullable ChannelFuture future) {
585 if (future == null) {
588 if (future.isDone() && future.isSuccess()) {
589 Channel ch = future.channel();
590 ch.writeAndFlush(request);
591 } else { // an error occurred
592 if (future.isDone() && !future.isCancelled()) {
593 Throwable cause = future.cause();
594 String msg = cause.getMessage();
595 logger.debug("Connect failed - cause is: {}", cause.getMessage());
596 if (cause instanceof ConnectTimeoutException) {
597 usingEvents = false;// Prevent Unsubscribe from being sent
598 ipCameraHandler.cameraCommunicationError(
599 "Camera timed out when trying to connect to the ONVIF port:" + port);
600 } else if ((cause instanceof ConnectException) && msg != null
601 && msg.contains("Connection refused")) {
602 usingEvents = false;// Prevent Unsubscribe from being sent
603 ipCameraHandler.cameraCommunicationError(
604 "Camera refused to connect when using ONVIF to port:" + port);
607 ipCameraHandler.cameraCommunicationError("Camera failed to connect due to being cancelled");
613 logger.debug("ONVIF message not sent as connection is shutting down");
617 OnvifConnection getHandle() {
621 void getIPandPortFromUrl(String url) {
622 int beginIndex = url.indexOf(":");
623 int endIndex = url.indexOf("/", beginIndex);
624 if (beginIndex >= 0 && endIndex == -1) {// 192.168.1.1:8080
625 ipAddress = url.substring(0, beginIndex);
626 onvifPort = Integer.parseInt(url.substring(beginIndex + 1));
627 } else if (beginIndex >= 0 && endIndex > beginIndex) {// 192.168.1.1:8080/foo/bar
628 ipAddress = url.substring(0, beginIndex);
629 onvifPort = Integer.parseInt(url.substring(beginIndex + 1, endIndex));
630 } else {// 192.168.1.1
632 deviceXAddr = "http://" + ipAddress + "/onvif/device_service";
633 logger.debug("No ONVIF Port found when parsing: {}", url);
636 deviceXAddr = "http://" + ipAddress + ":" + onvifPort + "/onvif/device_service";
639 public void gotoPreset(int index) {
641 if (index > 0) {// 0 is reserved for HOME as cameras seem to start at preset 1.
642 if (presetTokens.isEmpty()) {
643 logger.warn("Camera did not report any ONVIF preset locations, updating preset tokens now.");
644 sendPTZRequest(RequestType.GetPresets);
646 presetTokenIndex = index - 1;
647 sendPTZRequest(RequestType.GotoPreset);
653 public void eventRecieved(String eventMessage) {
654 String topic = Helper.fetchXML(eventMessage, "Topic", "tns1:");
655 if (topic.isEmpty()) {
656 logger.debug("No ONVIF Events occured in the last 8 seconds");
659 String dataName = Helper.fetchXML(eventMessage, "tt:Data", "Name=\"");
660 String dataValue = Helper.fetchXML(eventMessage, "tt:Data", "Value=\"");
661 logger.debug("ONVIF Event Topic: {}, Data: {}, Value: {}", topic, dataName, dataValue);
663 case "RuleEngine/CellMotionDetector/Motion":
664 if ("true".equals(dataValue)) {
665 ipCameraHandler.motionDetected(CHANNEL_CELL_MOTION_ALARM);
666 } else if ("false".equals(dataValue)) {
667 ipCameraHandler.noMotionDetected(CHANNEL_CELL_MOTION_ALARM);
670 case "VideoSource/MotionAlarm":
671 if ("true".equals(dataValue)) {
672 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
673 } else if ("false".equals(dataValue)) {
674 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
677 case "AudioAnalytics/Audio/DetectedSound":
678 if ("true".equals(dataValue)) {
679 ipCameraHandler.audioDetected();
680 } else if ("false".equals(dataValue)) {
681 ipCameraHandler.noAudioDetected();
684 case "RuleEngine/FieldDetector/ObjectsInside":
685 if ("true".equals(dataValue)) {
686 ipCameraHandler.motionDetected(CHANNEL_FIELD_DETECTION_ALARM);
687 } else if ("false".equals(dataValue)) {
688 ipCameraHandler.noMotionDetected(CHANNEL_FIELD_DETECTION_ALARM);
691 case "RuleEngine/LineDetector/Crossed":
692 if ("ObjectId".equals(dataName)) {
693 ipCameraHandler.motionDetected(CHANNEL_LINE_CROSSING_ALARM);
695 ipCameraHandler.noMotionDetected(CHANNEL_LINE_CROSSING_ALARM);
698 case "RuleEngine/TamperDetector/Tamper":
699 if ("true".equals(dataValue)) {
700 ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.ON);
701 } else if ("false".equals(dataValue)) {
702 ipCameraHandler.changeAlarmState(CHANNEL_TAMPER_ALARM, OnOffType.OFF);
705 case "Device/HardwareFailure/StorageFailure":
706 if ("true".equals(dataValue)) {
707 ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.ON);
708 } else if ("false".equals(dataValue)) {
709 ipCameraHandler.changeAlarmState(CHANNEL_STORAGE_ALARM, OnOffType.OFF);
712 case "VideoSource/ImageTooDark/AnalyticsService":
713 case "VideoSource/ImageTooDark/ImagingService":
714 case "VideoSource/ImageTooDark/RecordingService":
715 if ("true".equals(dataValue)) {
716 ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.ON);
717 } else if ("false".equals(dataValue)) {
718 ipCameraHandler.changeAlarmState(CHANNEL_TOO_DARK_ALARM, OnOffType.OFF);
721 case "VideoSource/GlobalSceneChange/AnalyticsService":
722 case "VideoSource/GlobalSceneChange/ImagingService":
723 case "VideoSource/GlobalSceneChange/RecordingService":
724 if ("true".equals(dataValue)) {
725 ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.ON);
726 } else if ("false".equals(dataValue)) {
727 ipCameraHandler.changeAlarmState(CHANNEL_SCENE_CHANGE_ALARM, OnOffType.OFF);
730 case "VideoSource/ImageTooBright/AnalyticsService":
731 case "VideoSource/ImageTooBright/ImagingService":
732 case "VideoSource/ImageTooBright/RecordingService":
733 if ("true".equals(dataValue)) {
734 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.ON);
735 } else if ("false".equals(dataValue)) {
736 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BRIGHT_ALARM, OnOffType.OFF);
739 case "VideoSource/ImageTooBlurry/AnalyticsService":
740 case "VideoSource/ImageTooBlurry/ImagingService":
741 case "VideoSource/ImageTooBlurry/RecordingService":
742 if ("true".equals(dataValue)) {
743 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.ON);
744 } else if ("false".equals(dataValue)) {
745 ipCameraHandler.changeAlarmState(CHANNEL_TOO_BLURRY_ALARM, OnOffType.OFF);
748 case "RuleEngine/MyRuleDetector/Visitor":
749 if ("true".equals(dataValue)) {
750 ipCameraHandler.changeAlarmState(CHANNEL_DOORBELL, OnOffType.ON);
751 } else if ("false".equals(dataValue)) {
752 ipCameraHandler.changeAlarmState(CHANNEL_DOORBELL, OnOffType.OFF);
755 case "RuleEngine/MyRuleDetector/VehicleDetect":
756 if ("true".equals(dataValue)) {
757 ipCameraHandler.changeAlarmState(CHANNEL_CAR_ALARM, OnOffType.ON);
758 } else if ("false".equals(dataValue)) {
759 ipCameraHandler.changeAlarmState(CHANNEL_CAR_ALARM, OnOffType.OFF);
762 case "RuleEngine/MyRuleDetector/DogCatDetect":
763 if ("true".equals(dataValue)) {
764 ipCameraHandler.changeAlarmState(CHANNEL_ANIMAL_ALARM, OnOffType.ON);
765 } else if ("false".equals(dataValue)) {
766 ipCameraHandler.changeAlarmState(CHANNEL_ANIMAL_ALARM, OnOffType.OFF);
769 case "RuleEngine/MyRuleDetector/FaceDetect":
770 if ("true".equals(dataValue)) {
771 ipCameraHandler.changeAlarmState(CHANNEL_FACE_DETECTED, OnOffType.ON);
772 } else if ("false".equals(dataValue)) {
773 ipCameraHandler.changeAlarmState(CHANNEL_FACE_DETECTED, OnOffType.OFF);
776 case "RuleEngine/MyRuleDetector/PeopleDetect":
777 if ("true".equals(dataValue)) {
778 ipCameraHandler.changeAlarmState(CHANNEL_HUMAN_ALARM, OnOffType.ON);
779 } else if ("false".equals(dataValue)) {
780 ipCameraHandler.changeAlarmState(CHANNEL_HUMAN_ALARM, OnOffType.OFF);
784 logger.debug("Please report this camera has an un-implemented ONVIF event. Topic: {}", topic);
788 public boolean supportsPTZ() {
792 public void getStatus() {
794 sendPTZRequest(RequestType.GetStatus);
798 public Float getAbsolutePan() {
799 return currentPanPercentage;
802 public Float getAbsoluteTilt() {
803 return currentTiltPercentage;
806 public Float getAbsoluteZoom() {
807 return currentZoomPercentage;
810 public void setAbsolutePan(Float panValue) {// Value is 0-100% of cameras range
812 currentPanPercentage = panValue;
813 currentPanCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * panValue + panRangeMin);
817 public void setAbsoluteTilt(Float tiltValue) {// Value is 0-100% of cameras range
819 currentTiltPercentage = tiltValue;
820 currentTiltCamValue = ((((panRangeMin - panRangeMax) * -1) / 100) * tiltValue + tiltRangeMin);
824 public void setAbsoluteZoom(Float zoomValue) {// Value is 0-100% of cameras range
826 currentZoomPercentage = zoomValue;
827 currentZoomCamValue = ((((zoomMin - zoomMax) * -1) / 100) * zoomValue + zoomMin);
831 public void absoluteMove() { // Camera wont move until PTZ values are set, then call this.
833 sendPTZRequest(RequestType.AbsoluteMove);
837 public void setSelectedMediaProfile(int mediaProfileIndex) {
838 this.mediaProfileIndex = mediaProfileIndex;
841 List<String> listOfResults(String message, String heading, String key) {
842 List<String> results = new LinkedList<>();
844 for (int startLookingFromIndex = 0; startLookingFromIndex != -1;) {
845 startLookingFromIndex = message.indexOf(heading, startLookingFromIndex);
846 if (startLookingFromIndex >= 0) {
847 temp = Helper.fetchXML(message.substring(startLookingFromIndex), heading, key);
848 if (!temp.isEmpty()) {
849 logger.trace("String was found: {}", temp);
852 return results;// key string must not exist so stop looking.
854 startLookingFromIndex += temp.length();
860 void parsePresets(String message) {
861 List<StateOption> presets = new ArrayList<>();
862 int counter = 1;// Presets start at 1 not 0. HOME may be added to index 0.
863 presetTokens = listOfResults(message, "<tptz:Preset", "token=\"");
864 presetNames = listOfResults(message, "<tptz:Preset", "<tt:Name>");
865 if (presetTokens.size() != presetNames.size()) {
866 logger.warn("Camera did not report the same number of Tokens and Names for PTZ presets");
869 for (String value : presetNames) {
870 presets.add(new StateOption(Integer.toString(counter++), value));
872 ipCameraHandler.stateDescriptionProvider
873 .setStateOptions(new ChannelUID(ipCameraHandler.getThing().getUID(), CHANNEL_GOTO_PRESET), presets);
876 void parseProfiles(String message) {
877 mediaProfileTokens = listOfResults(message, "<trt:Profiles", "token=\"");
878 if (mediaProfileIndex >= mediaProfileTokens.size()) {
880 "You have set the media profile to {} when the camera reported {} profiles. Falling back to mainstream 0.",
881 mediaProfileIndex, mediaProfileTokens.size());
882 mediaProfileIndex = 0;
886 void processPTZLocation(String result) {
887 logger.debug("Processing new PTZ location now");
889 int beginIndex = result.indexOf("x=\"");
890 int endIndex = result.indexOf("\"", (beginIndex + 3));
891 if (beginIndex >= 0 && endIndex >= 0) {
892 currentPanCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
893 currentPanPercentage = (((panRangeMin - currentPanCamValue) * -1) / ((panRangeMin - panRangeMax) * -1))
895 logger.debug("Pan is updating to: {} and the cam value is {}", Math.round(currentPanPercentage),
899 "Binding could not determin the cameras current PTZ location. Not all cameras respond to GetStatus requests.");
903 beginIndex = result.indexOf("y=\"");
904 endIndex = result.indexOf("\"", (beginIndex + 3));
905 if (beginIndex >= 0 && endIndex >= 0) {
906 currentTiltCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
907 currentTiltPercentage = (((tiltRangeMin - currentTiltCamValue) * -1) / ((tiltRangeMin - tiltRangeMax) * -1))
909 logger.debug("Tilt is updating to: {} and the cam value is {}", Math.round(currentTiltPercentage),
910 currentTiltCamValue);
915 beginIndex = result.lastIndexOf("x=\"");
916 endIndex = result.indexOf("\"", (beginIndex + 3));
917 if (beginIndex >= 0 && endIndex >= 0) {
918 currentZoomCamValue = Float.parseFloat(result.substring(beginIndex + 3, endIndex));
919 currentZoomPercentage = (((zoomMin - currentZoomCamValue) * -1) / ((zoomMin - zoomMax) * -1)) * 100;
920 logger.debug("Zoom is updating to: {} and the cam value is {}", Math.round(currentZoomPercentage),
921 currentZoomCamValue);
927 public void sendPTZRequest(RequestType requestType) {
929 logger.debug("ONVIF was not connected when a PTZ request was made, connecting now");
930 connect(usingEvents);
932 sendOnvifRequest(requestType, ptzXAddr);
935 public void sendEventRequest(RequestType requestType) {
936 sendOnvifRequest(requestType, eventXAddr);
939 public void connect(boolean useEvents) {
943 logger.debug("Connecting {} to ONVIF", ipAddress);
944 threadPool = Executors.newScheduledThreadPool(2);
945 sendOnvifRequest(RequestType.GetSystemDateAndTime, deviceXAddr);
946 usingEvents = useEvents;
947 sendOnvifRequest(RequestType.GetCapabilities, deviceXAddr);
954 public boolean isConnected() {
963 public boolean getEventsSupported() {
964 return supportsEvents;
967 public void setIsConnected(boolean isConnected) {
970 this.isConnected = isConnected;
976 private void cleanup() {
977 if (!isConnected && !mainEventLoopGroup.isShuttingDown()) {
979 mainEventLoopGroup.shutdownGracefully();
980 mainEventLoopGroup.awaitTermination(3, TimeUnit.SECONDS);
981 } catch (InterruptedException e) {
982 logger.warn("ONVIF was not cleanly shutdown, due to being interrupted");
984 logger.debug("Eventloop is shutdown: {}", mainEventLoopGroup.isShutdown());
986 threadPool.shutdown();
991 public void disconnect() {
992 connecting.lock();// Lock out multiple disconnect()/connect() attempts as we try to send Unsubscribe.
994 if (bootstrap != null) {
995 if (isConnected && usingEvents && !mainEventLoopGroup.isShuttingDown()) {
996 // Only makes sense to send if connected
997 // Some cameras may continue to send events even when they can't reach a server.
998 sendOnvifRequest(RequestType.Unsubscribe, subscriptionXAddr);
1000 // give time for the Unsubscribe request to be sent, shutdownGracefully will try to send it first.
1001 threadPool.schedule(this::cleanup, 50, TimeUnit.MILLISECONDS);
1006 isConnected = false;// isConnected is not thread safe, connecting.lock() used as fix.
1008 connecting.unlock();