-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththingService.js
executable file
·59 lines (43 loc) · 1.34 KB
/
thingService.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
var fs = require('fs'),
config = require('./config');
var readTemp = function(callback){
fs.readFile('/sys/bus/w1/devices/28-00000480aa5b/w1_slave', function(err, buffer)
{
if (err){
console.error(err);
process.exit(1);
}
// Read data from file (using fast node ASCII encoding).
var data = buffer.toString('ascii').split(" "); // Split by space
// Extract temperature from string and divide by 1000 to give celsius
var temp = parseFloat(data[data.length-1].split("=")[1])/1000.0;
// Round to one decimal place
temp = Math.round(temp * 10) / 10;
// Add date/time to temperature;
// Execute call back with data
callback(temp);
});
};
var createThing = function(){
var thing = {};
thing.settings = {
"name": 'Temperature',
"id": 3942879,
"iconType": "Thermometeer",
"position": config.getPosition(),
"information":[{
"header":"Temperature @ home"
}]};
var timeForUpdate = function() {
var update = function(data){
if(thing.socket){
//console.log(data);
thing.socket.emit('updateInfo', data +" °C");
}
};
readTemp(update);
};
var intervalId = setInterval(timeForUpdate, 1000);
return thing;
};
module.exports.thing = createThing();