This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
98 lines (80 loc) · 2.69 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const Homey = require('homey');
const UfvApi = require('./lib/ufvapi');
const UfvConstants = require('./lib/ufvconstants');
class UniFiVideo extends Homey.App {
async onInit() {
// Register snapshot image token
this.snapshotToken = new Homey.FlowToken('ufv_snapshot', {
type: 'image',
title: 'Snapshot',
});
Homey.ManagerFlow.registerToken(this.snapshotToken);
// Single API instance for all devices
this.api = new UfvApi();
// Subscribe to connection events
this.api.on(UfvConstants.EVENT_CONNECTION_ERROR, this._onConnectionError.bind(this));
this.api.on(UfvConstants.EVENT_CONNECTION_CLOSED, this._onConnectionClosed.bind(this));
// Subscribe to NVR events
this.api.on(UfvConstants.EVENT_NVR_CAMERA, this._onNvrCamera.bind(this));
this.api.on(UfvConstants.EVENT_NVR_HEALTH, this._onNvrHealth.bind(this));
this.api.on(UfvConstants.EVENT_NVR_MOTION, this._onNvrMotion.bind(this));
this.api.on(UfvConstants.EVENT_NVR_SERVER, this._onNvrServer.bind(this));
// Subscribe to credentials updates
Homey.ManagerSettings.on('set', key => {
if (key === 'ufv:credentials') {
this._login();
}
});
this._login();
// Enable remote debugging, if applicable
if (Homey.env.DEBUG) {
// eslint-disable-next-line global-require
require('inspector').open(9229, '0.0.0.0');
}
this.log('UniFi Video is running.');
}
_login() {
this.log('Logging in...');
// Validate NVR IP address
const nvrip = Homey.ManagerSettings.get('ufv:nvrip');
if (!nvrip) {
this.log('NVR IP address not set.');
return;
}
// Validate NVR credentials
const credentials = Homey.ManagerSettings.get('ufv:credentials');
if (!credentials) {
this.log('Credentials not set.');
return;
}
// Log in to NVR
this.api.login(nvrip, credentials.username, credentials.password)
.then(() => {
this.log('Logged in.');
this.api.subscribe();
})
.catch(error => this.log(error));
}
_onConnectionError(error) {
this.log(`Connection error: ${error.message}, retrying in 5s...`);
setTimeout(() => this._login(), 5000);
}
_onConnectionClosed() {
this.log('Connection closed, retrying in 5s...');
setTimeout(() => this._login(), 5000);
}
_onNvrCamera(camera) {
Homey.ManagerDrivers.getDriver('camera').onCamera(camera);
}
_onNvrHealth(health) {
Homey.ManagerDrivers.getDriver('nvr').onHealth(health);
}
_onNvrMotion(motion) {
Homey.ManagerDrivers.getDriver('camera').onMotion(motion);
}
_onNvrServer(server) {
Homey.ManagerDrivers.getDriver('nvr').onServer(server);
}
}
module.exports = UniFiVideo;