-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
97 lines (84 loc) · 3.41 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
var Accessory, Service, Characteristic, UUIDGen;
const fs = require('fs');
const packageFile = require("./package.json");
module.exports = function(homebridge) {
if(!isConfig(homebridge.user.configPath(), "accessories", "RaspberryPiTemperature")) {
return;
}
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerAccessory('homebridge-raspberrypi-temperature', 'RaspberryPiTemperature', RaspberryPiTemperature);
}
function isConfig(configFile, type, name) {
var config = JSON.parse(fs.readFileSync(configFile));
if("accessories" === type) {
var accessories = config.accessories;
for(var i in accessories) {
if(accessories[i]['accessory'] === name) {
return true;
}
}
} else if("platforms" === type) {
var platforms = config.platforms;
for(var i in platforms) {
if(platforms[i]['platform'] === name) {
return true;
}
}
} else {
}
return false;
}
function RaspberryPiTemperature(log, config) {
if(null == config) {
return;
}
this.log = log;
this.name = config["name"];
if(config["file"]) {
this.readFile = config["file"];
} else {
this.readFile = "/sys/class/thermal/thermal_zone0/temp";
}
if(config["updateInterval"] && config["updateInterval"] > 0) {
this.updateInterval = config["updateInterval"];
} else {
this.updateInterval = null;
}
this.multiplier = config["multiplier"] || 1000;
this.temperatureMeasurement = (typeof config["temperatureMeasurement"] === 'undefined') ? 'fahrenheit' : config["temperatureMeasurement"];
}
RaspberryPiTemperature.prototype = {
getServices: function() {
var that = this;
var infoService = new Service.AccessoryInformation();
infoService
.setCharacteristic(Characteristic.Manufacturer, "RaspberryPi")
.setCharacteristic(Characteristic.Model, "3B")
.setCharacteristic(Characteristic.SerialNumber, "Undefined")
.setCharacteristic(Characteristic.FirmwareRevision, packageFile.version);
var raspberrypiService = new Service.TemperatureSensor(that.name);
var currentTemperatureCharacteristic = raspberrypiService.getCharacteristic(Characteristic.CurrentTemperature);
function getCurrentTemperature() {
var data = fs.readFileSync(that.readFile, "utf-8");
var temperatureVal = parseFloat(data) / that.multiplier;
that.log.debug("update currentTemperatureCharacteristic value: " + temperatureVal);
return (that.temperatureMeasurement === 'celsius') ? fahrenheitToCelsius(temperatureVal) : temperatureVal;
}
function fahrenheitToCelsius(fahrenheit) {
return Math.round((fahrenheit - 32) / 1.8, 3);
}
currentTemperatureCharacteristic.updateValue(getCurrentTemperature());
if(that.updateInterval) {
setInterval(() => {
currentTemperatureCharacteristic.updateValue(getCurrentTemperature());
}, that.updateInterval);
}
currentTemperatureCharacteristic.on('get', (callback) => {
callback(null, getCurrentTemperature());
});
return [infoService, raspberrypiService];
}
}