-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·86 lines (71 loc) · 2.93 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
var Service, Characteristic;
const CO2Monitor = require('node-co2-monitor');
const monitor = new CO2Monitor({ "debug": false });
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-co2monitor", "Co2Monitor", Co2Monitor);
}
function Co2Monitor(log, config) {
this.log = log;
// Configuration
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "TFA Dostmann";
this.model = config["model"] || "AirCO2NTROL";
this.serial = config["serial"] || "";
this.humidity = config["humidity"];
this.co2threshold = config["co2threshold"] || 800;
this.lastUpdateAt = config["lastUpdateAt"] || null;
}
monitor.connect((err) => {
if (err) {
return console.error(err.stack);
}
monitor.transfer();
});
Co2Monitor.prototype = {
getTemperatureState: function (callback) {
callback(null, monitor.temperature);
},
getHumidityState: function (callback) {
callback(null, monitor.humidity);
},
getCarbonDioxideState: function (callback) {
callback(null, monitor.co2);
},
getCarbonDioxideDetected: function (callback) {
callback(null, monitor.co2 ? (monitor.co2 > this.co2threshold) : false);
},
getServices: function () {
var services = [],
informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(informationService);
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({ minValue: -273, maxValue: 200 })
.on("get", this.getTemperatureState.bind(this));
services.push(this.temperatureService);
this.carbonDioxideService = new Service.CarbonDioxideSensor(this.name);
this.carbonDioxideService
.getCharacteristic(Characteristic.CarbonDioxideDetected)
.on("get", this.getCarbonDioxideDetected.bind(this));
this.carbonDioxideService
.getCharacteristic(Characteristic.CarbonDioxideLevel)
.on("get", this.getCarbonDioxideState.bind(this));
services.push(this.carbonDioxideService);
if (this.humidity !== false) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({ minValue: 0, maxValue: 100 })
.on("get", this.getHumidityState.bind(this));
services.push(this.humidityService);
}
return services;
}
};