-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
71 lines (59 loc) · 2.02 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
const axios = require('axios');
module.exports = (api) => {
api.registerAccessory('PrometheusSensorPlugin', PrometheusSensorAccessory);
};
class PrometheusSensorAccessory {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// extract configuration
this.name = config.name;
this.url = config.url;
this.query = config.query;
this.type = config.type || 'temperature';
this.log.warn(this.type)
switch(this.type) {
case 'temperature':
// create a new Temperature Sensor service
this.service = new this.api.hap.Service.TemperatureSensor(this.name);
this.service.getCharacteristic(this.Characteristic.CurrentTemperature)
.onGet(this.handleCurrentTemperatureGet.bind(this));
break;
case 'occupancy':
// create a new Occupancy Sensor service
this.service = new this.api.hap.Service.OccupancySensor(this.name);
this.service.getCharacteristic(this.Characteristic.OccupancyDetected)
.onGet(this.handleOccupancyDetectedGet.bind(this));
break;
}
}
handleCurrentTemperatureGet() {
this.log.debug('Triggered GET CurrentTemperature');
return this.queryPrometheus().then((result) => {
this.log.debug('CurrentTemperature is ' + result)
return Number.parseFloat(result).toFixed(1);
});
}
handleOccupancyDetectedGet() {
this.log.debug('Triggered GET OccupancyDetected');
return this.queryPrometheus().then((result) => {
this.log.debug('OccupancyDetected is ' + result)
return parseInt(result);
});
}
queryPrometheus() {
let url = this.url + "/api/v1/query?query=" + this.query;
const response = axios.get(url)
return response.then((response) => {
return response.data["data"]["result"][0]["value"][1];
})
}
getServices() {
return [
this.service
];
}
}