-
Notifications
You must be signed in to change notification settings - Fork 2
/
temphum.js
94 lines (78 loc) · 1.7 KB
/
temphum.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
var exec = require('child_process').exec;
var mTemp;
var mHum;
var mTempInC;
var mIsEnabled;
var mOffset;
function temphum()
{
console.log("Initializing the temperature and humidity module");
mTemp = 0;
mHum = 0;
mTempInC = false;
mIsEnabled = false;
}
function updateTempHum()
{
exec("/tmp/omega_garage/checkHumidity/bin/checkHumidity 19 DHT22", getTempHumCallback);
}
function getTempHumCallback(error, stdout, stderr)
{
if(error)
console.log("Error getting temperature and humidity: " + JSON.stringify(error));
else if(stderr)
console.log("Std Error getting temperature and humidity: " + JSON.stringify(stderr));
else
{
var values = stdout.split("\n");
mTemp = parseFloat(values[1]) + mOffset;
mHum = parseFloat(values[0]);
console.log("Garage Temperature is: " + getTemp() + " " + getTempUnits());
console.log("Garage Humidity is: " + mHum + " %RH");
}
}
function getTemp()
{
if(mTempInC)
return mTemp;
else
{
return mTemp * 9/5 + 32;
}
}
function getTempUnits()
{
if(mTempInC)
return "C";
else
return "F";
}
//////////////////////////////////PUBLIC FUNCTIONS///////////////////////////////////
temphum.prototype.init = function (enabled, offset)
{
mIsEnabled = enabled;
mOffset = parseFloat(offset);
console.log(mOffset);
if(mIsEnabled)
{
setInterval(updateTempHum, 600000);
updateTempHum();
}
}
temphum.prototype.getTemperature = function ()
{
return getTemp();
}
temphum.prototype.getTemperatureInC = function ()
{
return mTemp;
}
temphum.prototype.getTempUnits = function ()
{
return getTempUnits();
}
temphum.prototype.getHumidity = function ()
{
return mHum;
}
module.exports = new temphum();