diff --git a/config/Config.d.ts b/config/Config.d.ts index fb9c42c..e6d2d34 100644 --- a/config/Config.d.ts +++ b/config/Config.d.ts @@ -6,9 +6,10 @@ declare module "node-config-ts" { cameras: Cameras } interface Cameras { - camera_a: Cameraa + door: Door + garage: Door } - interface Cameraa { + interface Door { hostname: string onvif_port: string username: string diff --git a/config/default.json b/config/default.json index c729cd6..deae52a 100644 --- a/config/default.json +++ b/config/default.json @@ -1,22 +1,31 @@ { - "mqtt":{ - "broker_host":"", - "topic_root":"", - "username":"", - "password":"" + "mqtt": { + "broker_host": "tcp://192.168.1.1:8272", + "topic_root": "camera", + "username": "cam", + "password": "wi" }, - "cameras":{ - "camera_a": { - "hostname":"", - "onvif_port":"", - "username":"", - "password":"", - "connect":{ - "retries":5, - "delay":5000 - } + "cameras": { + "door": { + "hostname": "192.168.1.2", + "onvif_port": "8000", + "username": "admin", + "password": "password", + "connect": { + "retries": 5, + "delay": 5000 } + }, + "garage": { + "hostname": "192.168.1.183", + "onvif_port": "80", + "username": "admin", + "password": "c", + "connect": { + "retries": 5, + "delay": 5000 + } + } } - - -} \ No newline at end of file + } + diff --git a/src/camera.ts b/src/camera.ts index 68493a5..4d2944d 100644 --- a/src/camera.ts +++ b/src/camera.ts @@ -114,6 +114,18 @@ export default class Camera { }); } + public setErrorEventHandler(handler: any): any { + this._cam.on('eventsError', handler); + this._cam.on('error', handler); + } + + public enablePing(): void { + setInterval(async () => { + const cameraDataTime = await this.getSystemDateAndTime(); + logger.info(`[${this._name}] Camera Time is ${cameraDataTime.info}`); + }, 3600 * 1000); + } + private async processCamMessage({ camMessage, xml }: { camMessage: any; xml: any }): Promise { try { logger.debug(`CamMessage:::${util.inspect(camMessage, true, 6, true)}`); diff --git a/src/logger.ts b/src/logger.ts index 90d8183..c478bc2 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -5,6 +5,9 @@ import pino from 'pino'; const transport = pino.transport({ target: 'pino-pretty', - options: { destination: 1 }, // use 2 for stderr + options: { + destination: 1, + translateTime: 'SYS:standard', + }, // use 2 for stderr }); export const logger = pino(transport); diff --git a/src/main.ts b/src/main.ts index f520f7d..448205e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,11 +29,13 @@ console.log( ); let mqtt: MQTTService; +let allCameras: { [camName: string]: Camera }; /** Main function */ const main = async () => { logger.info(`onvif-mqtt v${version}`); + allCameras = {}; mqtt = await MQTTService.getMQTT(); await mqtt.ping(version); logger.info(`Connected to MQTT`); @@ -66,9 +68,16 @@ const main = async () => { cam.setEventHandler(async (camEvent: CamEvent) => { await mqtt.processEvent(camEvent); }); - + + cam.setErrorEventHandler((event: any) => { + logger.error(event); + }); + + cam.enablePing(); + logger.info(`[${name}] Event handler added`); - await mqtt.notify(`[${name}] Event handler added`) + await mqtt.notify(`[${name}] Event handler added`); + allCameras[name] = cam; } }; diff --git a/src/mqtt-service.ts b/src/mqtt-service.ts index c6a3bcb..636929f 100644 --- a/src/mqtt-service.ts +++ b/src/mqtt-service.ts @@ -56,7 +56,7 @@ export default class MQTTService { public async notify(text: string): Promise { const topic = config.mqtt.topic_root; - const data = { name: 'onvif-mqtt', action: 'notifiy', text}; + const data = { name: 'onvif-mqtt', action: 'notifiy', text }; await this._client.publish(topic, JSON.stringify(data)); } }