diff --git a/example-osd.js b/example-osd.js new file mode 100644 index 0000000..cdcf44d --- /dev/null +++ b/example-osd.js @@ -0,0 +1,148 @@ +var onvif = require("./lib/node-onvif") + +// get OSDs +const getOSDs = option => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + let params = { + ConfigurationToken: device.current_profile.video.source.token + } + device.services.media.getOSDs(params).then(res => { + console.log(JSON.stringify(res["data"]["GetOSDsResponse"]["OSDs"], null, 2)) + }) + }) + .catch(error => { + console.error(error) + }) +} + +// get OSD +const getOSD = (option, OSDToken) => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + device.services.media.getOSD({ OSDToken }).then(res => { + console.log(JSON.stringify(res["data"]["GetOSDResponse"], null, 2)) + }) + }) + .catch(error => { + console.error(error) + }) +} + +// get OSDOptions +const getOSDOptions = option => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + let params = { + ConfigurationToken: device.current_profile.video.source.token + } + device.services.media.getOSDOptions(params).then(res => { + console.log(JSON.stringify(res["data"]["GetOSDOptionsResponse"]["OSDOptions"], null, 2)) + }) + }) + .catch(error => { + console.error(error) + }) +} +// set OSD +const setOSD = (option, params) => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + params.VideoSourceConfigurationToken = device.current_profile.video.source.token + device.services.media + .setOSD(params) + .then(res => { + console.log(JSON.stringify(res["data"], null, 2)) + }) + .catch(err => console.log(err)) + }) + .catch(error => { + console.error(error) + }) +} +// create OSD +const createOSD = option => { +let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + let params = { + VideoSourceConfigurationToken: device.current_profile.video.source.token + } + device.services.media + .createOSD(params) + .then(res => { + console.log(JSON.stringify(res["data"], null, 2)) + }) + .catch(err => console.log(err)) + }) + .catch(error => { + console.error(error) + }) +} +// delete OSD +const deleteOSD = (option, OSDToken) => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + let params = { + OSDToken + } + device.services.media.deleteOSD(params).then(res => { + console.log(JSON.stringify(res["data"], null, 2)) + }) + }) + .catch(error => { + console.error(error) + }) +} + +// media.getSnapshotUri +const getSnapshotUri = option => { + let device = new onvif.OnvifDevice({ ...option }) + device + .init() + .then(info => { + let params = { + ProfileToken: device.profile_list[0].token + } + device.services.media.getSnapshotUri(params).then(res => { + console.log(JSON.stringify(res["data"], null, 2)) + }) + }) + .catch(error => { + console.error(error) + }) +} + +let xaddr = "http://192.168.100.70/onvif/device_service" //"http://192.168.98.66/onvif/device_service" // +let user = "admin" +let pass = "admin1234" //"admin123" // + +// ptz({ xaddr, user, pass }, { x: 1.0 }) + +// GetDeviceInfo({ xaddr, user, pass }) +getOSDs({ xaddr, user, pass }) +// getSnapshotUri({ xaddr, user, pass }) +// getOSD({ xaddr, user, pass }, "OsdToken_102") +// getOSDOptions({ xaddr, user, pass }) +// setOSD( +// { xaddr, user, pass }, +// { +// OSDToken: "OsdToken_104", +// OSDText: "北京可视通!!!" +// } +// ) +// deleteOSD({ xaddr, user, pass }, "OsdToken_104") +// createOSD({ xaddr, user, pass }) +// getUsers({ xaddr, user, pass }) +// createUsers({ xaddr, user, pass }) diff --git a/lib/modules/osd.js b/lib/modules/osd.js new file mode 100644 index 0000000..30cb5ec --- /dev/null +++ b/lib/modules/osd.js @@ -0,0 +1,345 @@ +"use strict" +const mUrl = require("url") +const mOnvifSoap = require("./soap.js") + +function osd(OnvifServiceMedia){ +/* ------------------------------------------------------------------ + * Method: getOSDs(params[, callback]) + * - params: + * - ConfigurationToken | String | required | a token of the Profile + * + * { + * 'ConfigurationToken': 'Profile1' + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.getOSDs = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["ConfigurationToken"], "string"))) { + reject(new Error('The "ConfigurationToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = "" + soap_body += "" + soap_body += + "" + params["ConfigurationToken"] + "" + soap_body += "" + let soap = this._createRequestSoap(soap_body) + + mOnvifSoap + .requestCommand(this.oxaddr, "GetOSDs", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +/* ------------------------------------------------------------------ + * Method: getOSD(params[, callback]) + * - params: + * - OSDToken | String | required | a token of the Profile + * + * { + * 'OSDToken': 'Profile1' + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.getOSD = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["OSDToken"], "string"))) { + reject(new Error('The "OSDToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = "" + soap_body += "" + soap_body += "" + params["OSDToken"] + "" + soap_body += "" + let soap = this._createRequestSoap(soap_body) + mOnvifSoap + .requestCommand(this.oxaddr, "GetOSD", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +/* ------------------------------------------------------------------ + * Method: getOSDOptions(params[, callback]) + * - params: + * - ConfigurationToken | String | required | a token of the Profile + * + * { + * 'ConfigurationToken': 'Profile1' + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.getOSDOptions = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["ConfigurationToken"], "string"))) { + reject(new Error('The "ConfigurationToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = "" + soap_body += "" + soap_body += + "" + params["ConfigurationToken"] + "" + soap_body += "" + let soap = this._createRequestSoap(soap_body) + + mOnvifSoap + .requestCommand(this.oxaddr, "GetOSDOptions", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +/* ------------------------------------------------------------------ + * Method: setOSD(params[, callback]) + * - params: + * - VideoSourceConfigurationToken | String | required | a token of the Profile + * - OSDToken | String | required | a token of the Profile + * - OSDText | String | required | a Osd text of the Profile + * + * { + * 'VideoSourceConfigurationToken': 'Profile1', + * 'OSDToken': 'Profile2', + * 'OSDText':"Profile3" + * + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.setOSD = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["OSDToken"], "string"))) { + reject(new Error('The "OSDToken" property was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["VideoSourceConfigurationToken"], "string"))) { + reject(new Error('The "VideoSourceConfigurationToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = ` + + ${ + params["VideoSourceConfigurationToken"] + } + Text + + Custom + + + + Plain + ${params["OSDText"]} + + + ` + + let soap = this._createRequestSoap(soap_body) + mOnvifSoap + .requestCommand(this.oxaddr, "SetOSD", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +/* ------------------------------------------------------------------ + * Method: createOSD(params[, callback]) + * - params: + * - VideoSourceConfigurationToken | String | required | a token of the Profile + * + * { + * 'VideoSourceConfigurationToken': 'Profile1' + * + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.createOSD = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["VideoSourceConfigurationToken"], "string"))) { + reject(new Error('The "OSDToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = ` + + + ${ + params["VideoSourceConfigurationToken"] + } + Text + + Custom + + + + Plain + OSD-KST! + + + + ` + + let soap = this._createRequestSoap(soap_body) + mOnvifSoap + .requestCommand(this.oxaddr, "CreateOSD", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +/* ------------------------------------------------------------------ + * Method: deleteOSD(params[, callback]) + * - params: + * - OSDToken | String | required | a token of the Profile + * + * { + * 'OSDToken': 'Profile1' + * } + * ---------------------------------------------------------------- */ +OnvifServiceMedia.prototype.deleteOSD = function(params, callback) { + let promise = new Promise((resolve, reject) => { + let err_msg = "" + if ((err_msg = mOnvifSoap.isInvalidValue(params, "object"))) { + reject(new Error('The value of "params" was invalid: ' + err_msg)) + return + } + + if ((err_msg = mOnvifSoap.isInvalidValue(params["OSDToken"], "string"))) { + reject(new Error('The "OSDToken" property was invalid: ' + err_msg)) + return + } + + let soap_body = "" + soap_body += "" + soap_body += "" + params["OSDToken"] + "" + soap_body += "" + let soap = this._createRequestSoap(soap_body) + mOnvifSoap + .requestCommand(this.oxaddr, "DeleteOSD", soap) + .then(result => { + resolve(result) + }) + .catch(error => { + reject(error) + }) + }) + if (callback) { + promise + .then(result => { + callback(null, result) + }) + .catch(error => { + callback(error) + }) + } else { + return promise + } +} + +} + +module.exports = osd diff --git a/lib/modules/service-media.js b/lib/modules/service-media.js index de1a316..efea36b 100644 --- a/lib/modules/service-media.js +++ b/lib/modules/service-media.js @@ -8,7 +8,8 @@ 'use strict'; const mUrl = require('url'); const mOnvifSoap = require('./soap.js'); - +const osd = require('./osd.js'); +osd(OnvifServiceMedia); /* ------------------------------------------------------------------ * Constructor: OnvifServiceMedia(params) * - params: @@ -1649,4 +1650,4 @@ OnvifServiceMedia.prototype.getSnapshotUri = function(params, callback) { } }; -module.exports = OnvifServiceMedia; \ No newline at end of file +module.exports = OnvifServiceMedia; diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..eb1115f --- /dev/null +++ b/yarn.lock @@ -0,0 +1,98 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +buffer-from@^1.0.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef" + integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A== + +concat-stream@^1.4.7: + version "1.6.2" + resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34" + integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw== + dependencies: + buffer-from "^1.0.0" + inherits "^2.0.3" + readable-stream "^2.2.2" + typedarray "^0.0.6" + +core-util-is@~1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" + integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= + +html@>=1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/html/-/html-1.0.0.tgz#a544fa9ea5492bfb3a2cca8210a10be7b5af1f61" + integrity sha1-pUT6nqVJK/s6LMqCEKEL57WvH2E= + dependencies: + concat-stream "^1.4.7" + +inherits@^2.0.3, inherits@~2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= + +isarray@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= + +process-nextick-args@~2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa" + integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw== + +readable-stream@^2.2.2: + version "2.3.6" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" + integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw== + dependencies: + core-util-is "~1.0.0" + inherits "~2.0.3" + isarray "~1.0.0" + process-nextick-args "~2.0.0" + safe-buffer "~5.1.1" + string_decoder "~1.1.1" + util-deprecate "~1.0.1" + +safe-buffer@~5.1.0, safe-buffer@~5.1.1: + version "5.1.2" + resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" + integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== + +sax@>=0.6.0: + version "1.2.4" + resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" + integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== + +string_decoder@~1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8" + integrity sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg== + dependencies: + safe-buffer "~5.1.0" + +typedarray@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" + integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= + +util-deprecate@~1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" + integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8= + +xml2js@>=0.4.17: + version "0.4.19" + resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.19.tgz#686c20f213209e94abf0d1bcf1efaa291c7827a7" + integrity sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q== + dependencies: + sax ">=0.6.0" + xmlbuilder "~9.0.1" + +xmlbuilder@~9.0.1: + version "9.0.7" + resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d" + integrity sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=