camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
50 lines (49 loc) • 1.8 kB
JavaScript
import { pad } from '../../internal/utils';
import { ProxyClient } from '../../internal/ProxyClient';
import { DefaultClient } from '../DefaultClient';
export class AxisCameraStationEvents {
sourceKey;
client;
constructor(clientOptions, sourceKey) {
this.sourceKey = sourceKey;
this.client = new DefaultClient(clientOptions);
}
getClient(proxyParams) {
return proxyParams ? new ProxyClient(this.client, proxyParams) : this.client;
}
async sendEvent(data, eventType, options) {
const dateString = this.getDate();
const event = {
addExternalDataRequest: {
occurrenceTime: dateString,
source: this.sourceKey,
externalDataType: eventType,
data: data,
},
};
const eventData = JSON.stringify(event);
const agent = this.getClient(options?.proxyParams);
const res = await agent.post({
path: '/Acs/Api/ExternalDataFacade/AddExternalData',
data: eventData,
headers: {
'Content-Type': 'application/json',
'Content-Length': eventData.length.toString(),
},
timeout: options?.timeout,
});
if (!res.ok) {
throw new Error(`ACS status code: ${res.status}`);
}
}
getDate() {
const date = new Date();
const year = date.getUTCFullYear();
const month = pad(date.getUTCMonth() + 1, 2);
const day = pad(date.getUTCDate(), 2);
const hours = pad(date.getUTCHours(), 2);
const minutes = pad(date.getUTCMinutes(), 2);
const seconds = pad(date.getUTCSeconds(), 2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
}