Skip to content

Commit

Permalink
feat: getServices
Browse files Browse the repository at this point in the history
  • Loading branch information
agsh committed Jun 29, 2024
1 parent acd8055 commit 1a0dea7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHECKED.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ONVIF Interfaces
- [ ] getCapabilities
- [ ] getServiceCapabilities
- [ ] getActiveSources
- [ ] getServices
- [x] getServices includeCapability wrapped into object
- [ ] getDeviceInformation
- [ ] getHostname
- [ ] getScopes
Expand Down
13 changes: 9 additions & 4 deletions src/compatibility/cam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
SetPresetOptions, RelativeMoveOptions, ContinuousMoveOptions,
} from '../ptz';
import { SetNTP } from '../interfaces/devicemgmt';
import { NetworkHost, NetworkHostType } from '../interfaces/onvif';
import { NetworkHostType } from '../interfaces/onvif';
import { GetOSDs } from '../interfaces/media.2';

export type Callback = (error: any, result?: any) => void;
Expand Down Expand Up @@ -115,9 +115,14 @@ export class Cam extends EventEmitter {
this.onvif.media.getVideoSources().then((result) => callback(null, result)).catch(callback);
}

getServices(includeCapability: boolean, callback: Callback) {
this.onvif.device.getServices(includeCapability)
.then((result) => callback(null, result)).catch(callback);
getServices(includeCapability: boolean | Callback, callback: Callback) {
if (callback) {
this.onvif.device.getServices({ includeCapability : includeCapability as boolean })
.then((result) => callback(null, result.service)).catch(callback);
} else {
this.onvif.device.getServices()
.then((result) => (includeCapability as Callback)(null, result.service)).catch(callback);
}
}

getDeviceInformation(callback: Callback) {
Expand Down
25 changes: 9 additions & 16 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@ import {
Onvif, OnvifServices, ReferenceToken, SetSystemDateAndTimeOptions,
} from './onvif';
import { linerase } from './utils';
import { SetNTP } from './interfaces/devicemgmt';
import { GetServices, GetServicesResponse, Service, SetNTP } from './interfaces/devicemgmt';
import { DNSInformation, IPAddress, NTPInformation } from './interfaces/onvif';

export interface OnvifService {
/** Namespace uri */
namespace: string;
/** Uri for requests */
XAddr: string;
/** Minor version */
minor: number;
/** Major version */
major: number;
}

export interface OnvifVersion {
/** Major version number */
major: number;
Expand Down Expand Up @@ -529,7 +518,7 @@ export interface NetworkInterface {
*/
export class Device {
private readonly onvif: Onvif;
#services: OnvifService[] = [];
#services: Service[] = [];
get services() {
return this.#services;
}
Expand Down Expand Up @@ -560,13 +549,14 @@ export class Device {
/**
* Returns information about services of the device.
*/
async getServices(includeCapability = true): Promise<OnvifService[]> {
async getServices({ includeCapability }: GetServices = { includeCapability : true }): Promise<GetServicesResponse> {
const [data] = await this.onvif.request({
body : '<GetServices xmlns="http://www.onvif.org/ver10/device/wsdl">'
+ `<IncludeCapability>${includeCapability}</IncludeCapability>`
+ '</GetServices>',
});
this.#services = linerase(data).getServicesResponse.service;
const result = linerase(data).getServicesResponse;
this.#services = result.service;
// ONVIF Profile T introduced Media2 (ver20) so cameras from around 2020/2021 will have
// two media entries in the ServicesResponse, one for Media (ver10/media) and one for Media2 (ver20/media)
// This is so that existing VMS software can still access the video via the orignal ONVIF Media API
Expand All @@ -575,6 +565,9 @@ export class Device {
// Look for services with namespaces and XAddr values
if (Object.prototype.hasOwnProperty.call(service, 'namespace') && Object.prototype.hasOwnProperty.call(service, 'XAddr')) {
// Only parse ONVIF namespaces. Axis cameras return Axis namespaces in GetServices
if (!service.namespace || !service.XAddr) {
return;
}
const parsedNamespace = url.parse(service.namespace);
if (parsedNamespace.hostname === 'www.onvif.org' && parsedNamespace.path) {
const namespaceSplitted = parsedNamespace.path.substring(1).split('/'); // remove leading Slash, then split
Expand All @@ -590,7 +583,7 @@ export class Device {
}
}
});
return this.#services;
return result;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ import { Cam as CamJs } from '../promises';
// const cams = await Discovery.probe({ timeout : 1000 });
// console.log(cams);

console.log(cam.activeSource);
// console.log(cam.activeSource);

// console.log(await camJs.getOSDs());
console.log(await cam.media.getOSDs({
configurationToken : 'vsconf',
// OSDToken : 'textOSD',
}));

console.log((await camJs.getOSDOptions()));
console.log((await cam.media.getOSDOptions({ })));
// console.log((await camJs.getOSDOptions()));
// console.log((await cam.media.getOSDOptions({ })));

console.log(await cam.device.getServices());
})().catch((e) => {
console.error(e);
console.log(e.rawPacket.toString());
Expand Down

0 comments on commit 1a0dea7

Please sign in to comment.