This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
55 lines (49 loc) · 1.42 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
var ds18b20 = require("ds18b20-raspi");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(
"homebridge-ds18b20",
"DS18B20",
TemperatureAccessory
);
};
function TemperatureAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.device = config["device"];
this.pollInterval = config["pollInterval"];
this.offsetC = config["offsetC"] || 0;
this.service = new Service.TemperatureSensor(this.name);
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({ minValue: -100, maxValue: 125 })
.on("get", this.getState.bind(this));
if (this.pollInterval) {
setInterval(
function() {
ds18b20.readC(this.device, 2, function(err, value) {
if (!err) {
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.updateValue(value + this.offsetC);
}
}.bind(this));
}.bind(this),
this.pollInterval
);
}
}
TemperatureAccessory.prototype.getState = function(callback) {
ds18b20.readC(this.device, 2, function(err, value) {
if (!err) {
callback(null, value + this.offsetC);
} else {
callback(err);
}
}.bind(this));
};
TemperatureAccessory.prototype.getServices = function() {
return [this.service];
};