forked from mizuka-ninomae/homebridge-co2-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 2.71 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
let Service, Characteristic;
const { CronJob } = require('cron');
const MH_Z19 = require('mh_z19');
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-co2-sensor-v2", "CO2Sensor", CO2SensorAccessory);
}
function CO2SensorAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.uart_path = config["uart_path"];
this.schedule = config["schedule"] || '*/5 * * * *';
this.warning_level = config["warning_level"] || 1500;
this.co2_values = new Array(1440).fill(0);
this.informationService = new Service.AccessoryInformation();
this.CarbonDioxideSensorService = new Service.CarbonDioxideSensor(this.name);
this.job = new CronJob({
cronTime: this.schedule,
onTick: () => {
new MH_Z19 (this.uart_path, function(error, co2_level, stderr) {
if (co2_level == null) {
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxideLevel, new Error(error));
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxidePeakLevel, new Error(error));
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxideDetected, new Error(error));
}
else {
this.co2_values.shift();
this.co2_values.push(co2_level);
let co2_peak_level = Math.max(...this.co2_values);
let co2_detected = (this.warning_level > co2_level) ? 0 : 1;
this.log(`>>> [Update] CarbonDioxideLevel => ${co2_level}`);
this.log(`>>> [Update] CarbonDioxidePeakLevel => ${co2_peak_level}`);
this.log(`>>> [Update] CarbonDioxideDetected => ${co2_detected}`);
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxideLevel, co2_level);
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxidePeakLevel, co2_peak_level);
this.CarbonDioxideSensorService
.updateCharacteristic(Characteristic.CarbonDioxideDetected, co2_detected);
}
}.bind(this))
},
runOnInit: true
})
this.job.start()
}
CO2SensorAccessory.prototype.getServices = function() {
this.informationService
.setCharacteristic(Characteristic.Identify, false)
.setCharacteristic(Characteristic.Manufacturer, 'Winsen')
.setCharacteristic(Characteristic.Model, 'MH-Z19B')
.setCharacteristic(Characteristic.SerialNumber, 'Raspberry Pi')
.setCharacteristic(Characteristic.FirmwareRevision, '2.0');
return [this.informationService, this.CarbonDioxideSensorService];
}