This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
100 lines (78 loc) · 2.33 KB
/
index.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
99
100
const miio = require('miio');
let Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-mi-camera', 'MiCamera', MiCamera);
}
function MiCamera(log, config) {
this.log = log;
this.name = config.name || 'Mi Camera';
this.ip = config.ip;
this.token = config.token;
if (!this.ip) {
throw new Error('Your must provide IP address of the camera.');
}
if (!this.token) {
throw new Error('Your must provide token of the camera.');
}
this.service = new Service.Switch(this.name);
this.serviceInfo = new Service.AccessoryInformation();
this.serviceInfo
.setCharacteristic(Characteristic.Manufacturer, 'Xiaomi')
.setCharacteristic(Characteristic.Model, 'Mi Camera')
.setCharacteristic(Characteristic.SerialNumber, '33CB-8AE2-487111E7A919');
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
this.discover();
}
MiCamera.prototype = {
discover: async function() {
var accessory = this;
var log = this.log;
log.debug('Discovering Mi Camera at "%s"', this.ip);
this.device = await miio.device({
address: this.ip,
token: this.token,
model: 'chuangmi.camera.xiaobai'
});
},
getPowerState: async function(callback) {
if (!this.device) {
callback(new Error('No camera is discovered.'));
return;
}
var log = this.log;
await this.device.call('get_devicestatus', [{'alarmsensitivity': '', 'infraredlight': '', 'cameraprompt': '', 'ledstatus': '', 'wakeuplevel': '', 'recordtype': ''}])
.then(function(data) {
log.debug(data);
data.forEach(function(item, index) {
if (item.sysstatus && item.sysstatus == 'sleep') {
callback(null, false);
return;
}
if (item.wakeuplevel && item.wakeuplevel == '2') {
callback(null, true);
return;
}
});
})
.catch(console.error);
},
setPowerState: async function(state, callback) {
if (!this.device) {
callback(new Error('No camera is discovered.'));
return;
}
await this.device.call('set_sysstatus', [{'cmd':(state) ? 'normal' : 'sleep'}]);
callback();
},
identify: function(callback) {
callback();
},
getServices: function() {
return [this.serviceInfo, this.service];
}
};