onvif-nvt-ts
Version:
Wrapper for ONVIF spec to control NVT (Network Video Transitter) devices. Forked and added TypeScript support from onvif-nvt.
272 lines • 15.1 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const soap_1 = __importDefault(require("../utils/soap"));
const Util = __importStar(require("../utils/util"));
/**
* @class
* This code is curently untested.
* <p>
* {@link https://www.onvif.org/specs/srv/analytics/ONVIF-Analytics-Service-Spec-v1712.pdf}<br>
* {@link https://www.onvif.org/ver20/analytics/wsdl/analytics.wsdl}<br>
* </p>
* <h3>Functions</h3>
* {@link Analytics#createAnalyticsModules},
* {@link Analytics#deleteAnalyticsModules},
* {@link Analytics#getAnalyticsModuleOptions},
* {@link Analytics#getAnalyticsModules},
* {@link Analytics#getServiceCapabilities},
* {@link Analytics#getSupportedAnalyticsModules},
* {@link Analytics#modifyAnalyticsModules},
* {@link Analytics#createRules},
* {@link Analytics#deleteRules},
* {@link Analytics#getRuleOptions},
* {@link Analytics#getRules},
* {@link Analytics#getSupportedRules},
* {@link Analytics#modifyRules}
* <br><br>
* <h3>Overview</h3>
*/
class Analytics {
constructor() {
this.soap = new soap_1.default();
this.timeDiff = 0;
this.serviceAddress = null;
this.username = null;
this.password = null;
this.namespaceAttributes = [
// TODO: for analytics2
// 'xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
'xmlns:tns1="http://www.onvif.org/ver10/topics"',
'xmlns:tan="http://www.onvif.org/ver10/analytics"',
'xmlns:ttr="http://www.onvif.org/ver10/analytics/radiometry"'
];
}
/**
* Call this function directly after instantiating an Analytics object.
* @param timeDiff The onvif device's time difference.
* @param serviceAddress An url object from url package - require('url').
* @param username Optional only if the device does NOT have a user.
* @param password Optional only if the device does NOT have a password.
*/
init(timeDiff, serviceAddress, username, password) {
this.timeDiff = timeDiff;
this.serviceAddress = serviceAddress;
this.username = username;
this.password = password;
}
/**
* Private function for creating a SOAP request.
* @param body The body of the xml.
*/
createRequest(body) {
return this.soap.createRequest({
body: body,
xmlns: this.namespaceAttributes,
diff: this.timeDiff,
username: this.username,
password: this.password
});
}
async buildRequest(methodName, xml) {
let errMsg = '';
if (typeof methodName === 'undefined' || methodName === null) {
throw new Error('The "methodName" argument for buildRequest is required.');
}
if ((errMsg = Util.isInvalidValue(methodName, 'string'))) {
throw new Error('The "methodName" argument for buildRequest is invalid:' + errMsg);
}
let soapBody = '';
if (typeof xml === 'undefined' || xml === null || xml === '') {
soapBody += `<tan:${methodName}/>`;
}
else {
soapBody += `<tan:${methodName}>`;
soapBody += xml;
soapBody += `</tan:${methodName}>`;
}
const soapEnvelope = this.createRequest(soapBody);
return await this.soap.makeRequest('analytics', this.serviceAddress, methodName, soapEnvelope);
}
async requestWithConfigurationToken(methodName, configurationToken, xml) {
let errMsg = '';
if ((errMsg = Util.isInvalidValue(configurationToken, 'string'))) {
throw new Error(`The "configurationToken" argument for ${methodName} is invalid: ` + errMsg);
}
if (typeof xml !== 'undefined' && xml !== null) {
if ((errMsg = Util.isInvalidValue(xml, 'xml'))) {
throw new Error(`The "xml" argument for ${methodName} is invalid: ` + errMsg);
}
}
let soapBody = '';
soapBody += '<tan:ConfigurationToken>' + configurationToken + '</tan:ConfigurationToken>';
if (typeof xml !== 'undefined' && xml !== null) {
soapBody += xml;
}
return await this.buildRequest(methodName, soapBody);
}
// ---------------------------------------------
// Analytics API
// ---------------------------------------------
/**
* @typedef AnalyticsModule-xml
* @property AnalyticsModule This object must be XML.
* @property AnalyticsModule.Name Name of the configuration.
* @property AnalyticsModule.Type The Type attribute specifies the type of rule and shall be equal to value of one of Name attributes of ConfigDescription elements returned by GetSupportedRules and GetSupportedAnalyticsModules command.
* @property AnalyticsModule.Parameters List of configuration parameters as defined in the corresponding description.
* @property AnalyticsModule.Parameters.SimpleItem Value name pair as defined by the corresponding description.
* @property AnalyticsModule.Parameters.SimpleItem.Name Item name.
* @property AnalyticsModule.Parameters.SimpleItem.Value Item value. The type is defined in the corresponding description.
* @property AnalyticsModule.Parameters.ElementItem Complex value structure.
* @property AnalyticsModule.Parameters.ElementItem.Name Item name.
* @property AnalyticsModule.Parameters.Extension ItemListExtension
*/
/**
* Add one or more analytics modules to an existing VideoAnalyticsConfiguration. The available supported types can be retrieved via GetSupportedAnalyticsModules, where the Name of the supported AnalyticsModules correspond to the type of an AnalyticsModule instance.<br>
* Pass unique module names which can be later used as reference. The Parameters of the analytics module must match those of the corresponding AnalyticsModuleDescription.<br>
* Although this method is mandatory a device implementation must not support adding modules. Instead it can provide a fixed set of predefined configurations via the media service function GetCompatibleVideoAnalyticsConfigurations.<br>
* The device shall ensure that a corresponding analytics engine starts operation when a client subscribes directly or indirectly for events produced by the analytics or rule engine or when a client requests the corresponding scene description stream. An analytics module must be attached to a Video source using the media profiles before it can be used. In case differing analytics configurations are attached to the same profile it is undefined which of the analytics module configuration becomes active if no stream is activated or multiple streams with different profiles are activated at the same time.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
* @param xml AnalyticsModule xml.
*/
createAnalyticsModules(configurationToken, xml) {
return this.requestWithConfigurationToken('CreateAnalyticsModules', configurationToken, xml);
}
/**
* Remove one or more analytics modules from a VideoAnalyticsConfiguration referenced by their names.
* @param {string} configurationToken Reference to an existing Video Analytics configuration.
* @param {string} analyticsModuleName Name of the AnalyticsModule to be deleted.
*/
deleteAnalyticsModules(configurationToken, analyticsModuleName) {
const soapBody = '<tan:AnalyticsModuleName>' + analyticsModuleName + '</tan:AnalyticsModuleName>';
return this.requestWithConfigurationToken('DeleteAnalyticsModules', configurationToken, soapBody);
}
/**
* Return the options for the supported analytics modules that specify an Option attribute.
* @param {string} configurationToken Reference to an existing AnalyticsConfiguration.
* @param {string} type Reference to an SupportedAnalyticsModule Type returned from GetSupportedAnalyticsModules.
* @param {callback=} callback Optional callback, instead of a Promise.
*/
getAnalyticsModuleOptions(configurationToken, type) {
const soapBody = '<tan:Type>' + type + '</tan:Type>';
return this.requestWithConfigurationToken('GetAnalyticsModuleOptions', configurationToken, soapBody);
}
/**
* List the currently assigned set of analytics modules of a VideoAnalyticsConfiguration.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
* @returns AnalyticsModule xml.
*/
getAnalyticsModules(configurationToken) {
return this.requestWithConfigurationToken('GetAnalyticsModules', configurationToken);
}
/**
* Returns the capabilities of the analytics service. The result is returned in a typed answer.
*/
getServiceCapabilities() {
return this.buildRequest('GetServiceCapabilities');
}
/**
* List all analytics modules that are supported by the given VideoAnalyticsConfiguration. The result of this method may depend on the overall Video analytics configuration of the device, which is available via the current set of profiles.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
*/
getSupportedAnalyticsModules(configurationToken) {
return this.requestWithConfigurationToken('GetSupportedAnalyticsModules', configurationToken);
}
/**
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
* @param xml AnalyticsModule xml.
*/
modifyAnalyticsModules(configurationToken, xml) {
return this.requestWithConfigurationToken('ModifyAnalyticsModules', configurationToken, xml);
}
/**
* @typedef Rule-xml
* @property Rule This object must be XML.
* @property Rule.Name Name of the configuration.
* @property Rule.Type The Type attribute specifies the type of rule and shall be equal to value of one of Name attributes of ConfigDescription elements returned by GetSupportedRules and GetSupportedAnalyticsModules command.
* @property Rule.Parameters List of configuration parameters as defined in the corresponding description.
* @property Rule.Parameters.SimpleItem Value name pair as defined by the corresponding description.
* @property Rule.Parameters.SimpleItem.Name Item name.
* @property Rule.Parameters.SimpleItem.Value Item value. The type is defined in the corresponding description.
* @property Rule.Parameters.ElementItem Complex value structure.
* @property Rule.Parameters.ElementItem.Name Item name.
* @property Rule.Parameters.Extension ItemListExtension
*/
/**
* Add one or more rules to an existing VideoAnalyticsConfiguration. The available supported types can be retrieved via GetSupportedRules, where the Name of the supported rule correspond to the type of an rule instance.<br>
* Pass unique module names which can be later used as reference. The Parameters of the rules must match those of the corresponding description.<br>
* Although this method is mandatory a device implementation must not support adding rules. Instead it can provide a fixed set of predefined configurations via the media service function GetCompatibleVideoAnalyticsConfigurations.
* @param configurationToken Reference to an SupportedAnalyticsModule Type returned from GetSupportedAnalyticsModules.
* @param xml Rule xml.
*/
createRules(configurationToken, xml) {
return this.requestWithConfigurationToken('CreateRules', configurationToken, xml);
}
/**
* Remove one or more rules from a VideoAnalyticsConfiguration.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
* @param ruleName References the specific rule to be deleted (e.g. "MyLineDetector").
* @param callback Optional callback, instead of a Promise.
*/
deleteRules(configurationToken, ruleName) {
const soapBody = '<tan:RuleName>' + ruleName + '</tan:RuleName>';
return this.requestWithConfigurationToken('DeleteRules', configurationToken, soapBody);
}
/**
* Return the options for the supported rules that specify an Option attribute.
* @param configurationToken Reference to an existing analytics configuration.
* @param ruleType Reference to an SupportedRule Type returned from GetSupportedRules.
* @param callback Optional callback, instead of a Promise.
*/
getRuleOptions(configurationToken, ruleType) {
const soapBody = '<tan:RuleType>' + ruleType + '</tan:RuleType>';
return this.requestWithConfigurationToken('GetRuleOptions', configurationToken, soapBody);
}
/**
* List the currently assigned set of rules of a VideoAnalyticsConfiguration.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
*/
getRules(configurationToken) {
return this.requestWithConfigurationToken('GetRules', configurationToken);
}
/**
* List all rules that are supported by the given VideoAnalyticsConfiguration. The result of this method may depend on the overall Video analytics configuration of the device, which is available via the current set of profiles.
* @param configurationToken References an existing Video Analytics configuration. The list of available tokens can be obtained via the Media service Media#getVideoAnalyticsConfigurations method.
* @returns The Rules xml.
*/
getSupportedRules(configurationToken) {
return this.requestWithConfigurationToken('GetSupportedRules', configurationToken);
}
/**
* Modify one or more rules of a VideoAnalyticsConfiguration. The rules are referenced by their names.
* @param configurationToken Reference to an existing VideoAnalyticsConfiguration.
* @param xml Rule xml.
* @param callback Optional callback, instead of a Promise.
*/
modifyRules(configurationToken, xml) {
return this.requestWithConfigurationToken('ModifyRules', configurationToken, xml);
}
}
exports.default = Analytics;
//# sourceMappingURL=analytics.js.map