-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
78 lines (65 loc) · 2.38 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
"use strict";
var fs = require('fs');
var Service, Characteristic;
var temperatureService;
module.exports = function (homebridge)
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-pi", "PiTemperature", PiTemperatureAccessory);
}
function PiTemperatureAccessory(log, config)
{
this.log = log;
this.name = config['name'];
this.lastupdate = 0;
}
PiTemperatureAccessory.prototype =
{
getState: function (callback)
{
// Only fetch new data once per minute
if (this.lastupdate + 60 < (Date.now() / 1000 | 0))
{
var data = fs.readFileSync('/sys/class/thermal/thermal_zone0/temp', 'utf8');
if (typeof data == 'undefined') { return this.log("Failed to read temperature file"); }
this.temperature = (0.0+parseInt(data))/1000;
}
this.log("Raspberry Pi CPU/GPU temperature at " + this.temperature);
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, this.temperature);
callback(null, this.temperature);
},
identify: function (callback)
{
this.log("Identify requested!");
callback(); // success
},
getServices: function ()
{
var informationService = new Service.AccessoryInformation();
var data = fs.readFileSync('/proc/cpuinfo', 'utf8');
if (typeof data == 'undefined') { return this.log("Failed to read /proc/cpuinfo"); }
var model = data.match(/Hardware\s+\:\s*(\S+)/)[1] + "/" + data.match(/Revision\s+\:\s*(\S+)/)[1];
var serial = data.match(/Serial\s+\:\s*(\S+)/)[1];
informationService
.setCharacteristic(Characteristic.Manufacturer, "Raspberry")
.setCharacteristic(Characteristic.Model, model)
.setCharacteristic(Characteristic.SerialNumber, serial);
this.log("Model " + model + " Serial " + serial);
temperatureService = new Service.TemperatureSensor(this.name);
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this));
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({minValue: -30});
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({maxValue: 120});
return [informationService, temperatureService];
}
};
if (!Date.now)
{
Date.now = function() { return new Date().getTime(); }
}