This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
129 lines (104 loc) · 3.42 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var Service, Characteristic
let Api = require('./api.js').Api
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-canary', 'Canary', Canary, true)
}
let Canary = function (log, config, api) {
this.log = log
this.api = api
this.name = config['name']
this.serial = config['serial']
this.model = config['model']
this.backend = new Api(log, config['username'], config['password'])
this.log(`Adding Canary ${this.model} (${this.serial})`)
}
Canary.prototype.getStateTemperature = function (callback) {
this.readSensorWithCallback('temperature', callback)
}
Canary.prototype.getStateHumidity = function (callback) {
this.readSensorWithCallback('humidity', callback)
}
Canary.prototype.getStateBatteryLevel = function (callback) {
this.readSensorWithCallback('battery', callback)
}
Canary.prototype.getStateAirQuality = function (callback) {
this.readSensorWithCallback('air_quality', (err, val) => {
if (err) {
callback(err)
return
}
let quality
if (val <= 0.3) {
quality = 1
} else if (val <= 0.4) {
quality = 2
} else if (val <= 0.5) {
quality = 3
} else if (val <= 0.6) {
quality = 4
} else {
quality = 5
}
callback(null, quality)
})
}
Canary.prototype.readSensorWithCallback = function (sensor, callback) {
this.readSensor(sensor)
.then(val => callback(null, val))
.catch(err => {
this.log(err)
callback(err)
})
}
Canary.prototype.readSensor = async function (sensor) {
let id = await this.deviceId()
let sensors = await this.backend.readings(id, this.model)
for (var i in sensors) {
if (sensors[i].sensor_type === sensor) {
return Promise.resolve(sensors[i].value)
}
}
return Promise.reject(new Error(`Invalid sensor ${sensor} not found`))
}
Canary.prototype.deviceId = async function () {
let devices = await this.backend.devices()
for (var i in devices) {
if (devices[i].serial_number === this.serial) {
return Promise.resolve(devices[i].id)
}
}
return Promise.reject(new Error(`Device with serial ${this.serialNumber} not found`))
}
Canary.prototype.getServices = function () {
let info = new Service.AccessoryInformation()
info
.setCharacteristic(Characteristic.Manufacturer, 'Canary')
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber)
let services = [info]
if (this.model === 'AllInOne') {
let temp = new Service.TemperatureSensor(this.name + ' Temperature')
let humidity = new Service.HumiditySensor(this.name + ' Humidity')
let airq = new Service.AirQualitySensor(this.name + ' Air Quality')
temp
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getStateTemperature.bind(this))
humidity
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getStateHumidity.bind(this))
airq
.getCharacteristic(Characteristic.AirQuality)
.on('get', this.getStateAirQuality.bind(this))
services.push(...[temp, humidity, airq])
}
if (this.model === 'Flex') {
let battery = new Service.BatteryService(this.name + ' Battery')
battery
.getCharacteristic(Characteristic.BatteryLevel)
.on('get', this.getStateBatteryLevel.bind(this))
services.push(battery)
}
return services
}