-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.js
131 lines (116 loc) · 4.87 KB
/
sensor.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
'use strict';
const airthings_humidity = 0;
const airthings_temperature = 1;
const airthings_radon_st = 2;
const airthings_radon_lt = 3;
const airthings_pressure = 4;
const airthings_CO2 = 5;
const airthings_VOC = 6;
let Service, Characteristic;
var CustomCharacteristic;
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
CustomCharacteristic = require('./lib/CustomCharacteristic.js')(homebridge);
homebridge.registerAccessory('homebridge-airthings-wave', 'Airthings', AirthingsPlugin);
};
class AirthingsPlugin {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.waveplus = config.waveplus || false
this.name_temperature = config.name_temperature || this.name;
this.name_humidity = config.name_humidity || this.name;
this.refresh = config['refresh'] || 3600; // Update every hour
this.address = config.address;
this.path = config.path || "/home/pi/quary_wave.py";
this.devicePolling.bind(this);
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Airthings")
.setCharacteristic(Characteristic.Model, "Airthings Wave")
.setCharacteristic(Characteristic.SerialNumber, this.address)
.setCharacteristic(Characteristic.FirmwareRevision, require('./package.json').version);
this.humidityService = new Service.HumiditySensor(this.name_humidity);
/* this.humidityService
.getCharacteristic(Characteristic.CurrentHumidity)
.setProps({
minValue: 0,
maxValue: 100,
minStep: 0.1
});*/
this.temperatureService = new Service.TemperatureSensor(this.name_temperature);
/* this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100,
minStep: 0.1
});*/
this.temperatureService
.addCharacteristic(CustomCharacteristic.RadonLevelShortTermAverage);
this.temperatureService
.addCharacteristic(CustomCharacteristic.RadonLevelLongTermAverage);
if(this.waveplus) {
this.name_CO2 = config.name_CO2 || this.name;
this.carbonDioxideService = new Service.CarbonDioxideSensor(this.name_CO2);
this.carbonDioxideService
.getCharacteristic(Characteristic.CarbonDioxideLevel)
.setProps({
minValue: 0,
maxValue: 5000,
minStep: 1
});
this.carbonDioxideService
.addCharacteristic(CustomCharacteristic.VOCLevel);
this.humidityService
.addCharacteristic(CustomCharacteristic.Pressure);
}
setInterval(this.devicePolling.bind(this), this.refresh * 1000);
//this.temperatureService.log = this.log;
}
devicePolling() {
var strvalues
var valuest
var spawn = require("child_process").spawn;
var pythonProcess = spawn('python',[this.path, this.address]);
pythonProcess.stdout.on('data', (data) => {
strvalues = data.toString('utf8');
valuest = strvalues.split(' ');
this.log('Humidity: ',roundInt(valuest[airthings_humidity]));
this.log('Temperature: ',roundInt(valuest[airthings_temperature]));
this.log('Radon short term: ',roundInt(valuest[airthings_radon_st]));
this.log('Radon long term: ',roundInt(valuest[airthings_radon_lt]));
if(this.waveplus) {
this.log('Pressure: ',roundInt(valuest[airthings_pressure]));
this.log('Carbon dioxide: ',roundInt(valuest[airthings_CO2]));
this.log('Organics: ',roundInt(valuest[airthings_VOC]));
}
this.humidityService
.setCharacteristic(Characteristic.CurrentRelativeHumidity, roundInt(valuest[airthings_humidity]));
this.temperatureService
.setCharacteristic(Characteristic.CurrentTemperature, roundInt(valuest[airthings_temperature]));
this.temperatureService
.setCharacteristic(CustomCharacteristic.RadonLevelShortTermAverage, roundInt(valuest[airthings_radon_st]));
this.temperatureService
.setCharacteristic(CustomCharacteristic.RadonLevelLongTermAverage, roundInt(valuest[airthings_radon_lt]));
if(this.waveplus) {
this.humidityService
.setCharacteristic(CustomCharacteristic.Pressure, roundInt(valuest[airthings_pressure]));
this.carbonDioxideService
.setCharacteristic(Characteristic.CarbonDioxideLevel, roundInt(valuest[airthings_CO2]));
this.carbonDioxideService
.setCharacteristic(CustomCharacteristic.VOCLevel, roundInt(valuest[airthings_VOC]));
}
});
}
getServices() {
if(this.waveplus)
return [this.informationService, this.temperatureService, this.humidityService, this.carbonDioxideService]
else
return [this.informationService, this.temperatureService, this.humidityService]
}
}
function roundInt(string) {
return Math.round(parseFloat(string) * 10) / 10;
}