diff --git a/libs/common.js b/libs/common.js index 1f8740c8..a993e20c 100644 --- a/libs/common.js +++ b/libs/common.js @@ -63,4 +63,14 @@ module.exports = { }) return newObject }, + createQueryStringFromObject: (theObject) => { + const string = [] + const keys = Object.keys(theObject) + keys.forEach((key) => { + const value = theObject[key] + if(value)string.push(`${key}=${value}`) + }) + return string.join('&') + } + } diff --git a/libs/control/ptz.js b/libs/control/ptz.js index aa8ba68c..c2e3e794 100644 --- a/libs/control/ptz.js +++ b/libs/control/ptz.js @@ -220,13 +220,18 @@ module.exports = function(s,config,lang){ let stopURL = controlBaseUrl + monitorConfig.details[`control_url_${options.direction}_stop`] let controlOptions = s.cameraControlOptionsFromUrl(stopURL,monitorConfig) let requestOptions = { - url : stopURL, - method : controlOptions.method, - auth : { - user : controlOptions.username, - pass : controlOptions.password + url : controlBaseUrl + controlOptions.path, + method : controlOptions.method + } + if(controlOptions.username && controlOptions.password){ + requestOptions.auth = { + user: controlOptions.username, + pass: controlOptions.password } } + if(controlOptions.postData){ + requestOptions.form = controlOptions.postData + } if(monitorConfig.details.control_digest_auth === '1'){ requestOptions.uri = sliceUrlAuth(requestOptions.url); delete requestOptions.url; @@ -253,13 +258,18 @@ module.exports = function(s,config,lang){ let controlURL = controlBaseUrl + monitorConfig.details[`control_url_${options.direction}`] let controlOptions = s.cameraControlOptionsFromUrl(controlURL,monitorConfig) let requestOptions = { - url: controlURL, - method: controlOptions.method, - auth: { + url: controlBaseUrl + controlOptions.path, + method: controlOptions.method + } + if(controlOptions.username && controlOptions.password){ + requestOptions.auth = { user: controlOptions.username, pass: controlOptions.password } } + if(controlOptions.postData){ + requestOptions.form = controlOptions.postData + } if(monitorConfig.details.control_digest_auth === '1'){ requestOptions.uri = sliceUrlAuth(requestOptions.url); delete requestOptions.url; diff --git a/libs/monitor.js b/libs/monitor.js index 6799d217..eee6a8e9 100644 --- a/libs/monitor.js +++ b/libs/monitor.js @@ -10,7 +10,7 @@ const connectionTester = require('connection-tester') const SoundDetection = require('shinobi-sound-detection') const async = require("async"); const URL = require('url') -const { copyObject, createQueue, queryStringToObject } = require('./common.js') +const { copyObject, createQueue, queryStringToObject, createQueryStringFromObject } = require('./common.js') module.exports = function(s,config,lang){ const { probeMonitor, @@ -447,15 +447,17 @@ module.exports = function(s,config,lang){ }else if(!URLobject.port){ URLobject.port = 80 } - options = { + const options = { host: URLobject.hostname, port: URLobject.port, - method: monitorConfig.details.control_url_method, - path: URLobject.pathname, - query: queryStringToObject(URLobject.query || ""), - }; - if(URLobject.query){ - options.path=options.path+'?'+URLobject.query + method: monitorConfig.details.control_url_method + } + const queryStringObjects = queryStringToObject(URLobject.query || "") + if (queryStringObjects && queryStringObjects.postData) { + options.postData = decodeURIComponent(queryStringObjects.postData) + options.path = URLobject.pathname + '?' + decodeURIComponent(createQueryStringFromObject(Object.assign(queryStringObjects,{postData: null}))) + } else { + options.path = URLobject.pathname } if(URLobject.username&&URLobject.password){ options.username = URLobject.username