-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (117 loc) · 3.54 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
130
131
132
133
134
135
const axios = require('axios');
const packageJson = require('./package.json');
let Service, Characteristic;
/**
* @param {API} homebridge
*/
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-co-sensor', 'COSensor', COSensorAccessory);
};
/**
* @param {function} log
* @param {Object} config
* @constructor
*/
function COSensorAccessory(log, config) {
this.log = log;
this.name = config.name;
this.url = config.url;
this.threshold = config.threshold || 30; // Default threshold value of 30 ppm
this.pollingInterval = config.pollingInterval || 60; // Default polling interval of 60 seconds
this.coLevelPath = config.coLevelPath || 'co_level'; // Custom JSON path for CO level
this.service = new Service.CarbonMonoxideSensor(this.name);
this.service
.getCharacteristic(Characteristic.CarbonMonoxideDetected)
.on('get', this.getCOStatus.bind(this));
this.service
.getCharacteristic(Characteristic.CarbonMonoxideLevel)
.on('get', this.getCOLevel.bind(this));
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(Characteristic.Model, 'CO_Sensor')
.setCharacteristic(Characteristic.SerialNumber, packageJson.version);
// Update CO status and level by interval
setInterval(() => {
this.getCOStatus((err, status) => {
if (err) {
this.log(`Failed to fetch CO status: ${err}`);
return;
}
this.service
.getCharacteristic(Characteristic.CarbonMonoxideDetected)
.updateValue(status);
});
this.getCOLevel((err, level) => {
if (err) {
this.log(`Failed to fetch CO level: ${err}`);
return;
}
this.service
.getCharacteristic(Characteristic.CarbonMonoxideLevel)
.updateValue(level);
});
}, this.pollingInterval * 1000);
}
/**
* @name COSensorAccessory#getCOStatus
* @function
*/
COSensorAccessory.prototype.getCOStatus = function (callback) {
axios.get(this.url)
.then(response => {
const data = response.data;
const coLevel = this.getCOValueFromData(data);
this.log(`CO level: ${coLevel}`);
const isCODetected = coLevel > this.threshold; // Compare with the threshold
callback(null, isCODetected);
})
.catch(error => {
this.log(`Failed to fetch CO level: ${error}`);
callback(error);
});
};
/**
* @name COSensorAccessory#getCOLevel
* @function
*/
COSensorAccessory.prototype.getCOLevel = function (callback) {
axios.get(this.url)
.then(response => {
const data = response.data;
const coLevel = this.getCOValueFromData(data);
this.log(`CO level: ${coLevel}`);
callback(null, coLevel);
})
.catch(error => {
this.log(`Failed to fetch CO level: ${error}`);
callback(error);
});
};
/**
* @name COSensorAccessory#getCOValueFromData
* @function
*/
COSensorAccessory.prototype.getCOValueFromData = function (data) {
let coLevel = 0;
try {
const pathParts = this.coLevelPath.split('.');
let value = data;
for (const part of pathParts) {
value = value[part];
}
coLevel = parseFloat(value);
} catch (error) {
this.log(`Failed to extract CO level from data: ${error}`);
}
return coLevel;
};
/**
* @name COSensorAccessory#getServices
* @function
*/
COSensorAccessory.prototype.getServices = function () {
return [this.service, this.informationService];
};