Skip to content

Commit

Permalink
Merge pull request #13 from mbwhite/main
Browse files Browse the repository at this point in the history
feat: updated to ping camera
  • Loading branch information
mbwhite authored Aug 7, 2024
2 parents 8bd7961 + becaa03 commit dd81fe6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
5 changes: 3 additions & 2 deletions config/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 27 additions & 18 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}


}
}

12 changes: 12 additions & 0 deletions src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CamEvent | undefined> {
try {
logger.debug(`CamMessage:::${util.inspect(camMessage, true, 6, true)}`);
Expand Down
5 changes: 4 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
13 changes: 11 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/mqtt-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class MQTTService {
public async notify(text: string): Promise<void> {
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));
}
}

0 comments on commit dd81fe6

Please sign in to comment.