-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempSensor.js
231 lines (212 loc) · 7.17 KB
/
tempSensor.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* DS18B20 temperature sensor module.
* @module mc-tempsensor
* @see module:mc-tempsensor
* @author Oscar Djupfeldt
*/
var fs = require('fs');
var path = require('path');
var winston = require('winston');
var sensorDirectory = '/sys/bus/w1/devices/';
var logDirectory = './logs';
var converters = [];
var input;
var settings = {defaultPath: true, installKernelMod: false};
if (!fs.existsSync(logDirectory)) {
fs.mkdir(logDirectory);
}
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ name:'tempSensor', filename: logDirectory + '/tempSensor.log' })
]
});
var modprobe = function(error, stdout, stderr) {
if (error) {
logger.error('MODPROB ERROR: ', error);
logger.error('MODPROB STDERR: ', stderr);
}
};
/**
* Reads the raw data from the sensors.
*
* @function readTemp
* @param {function} callback Function to call when all sensor data has bean read or failed
* @throws {Error} 'Callback function required' Thrown if no callback function is provided
*/
var readTemp = function(callback) {
if (!callback || typeof callback !== 'function') {
throw new Error('Callback function required');
}
var response = [];
for (var i in input) {
try {
var path;
if (settings.defaultPath) {
fs.accessSync(sensorDirectory + input[i] + '/w1_slave');
path = sensorDirectory + input[i] + '/w1_slave';
} else {
fs.accessSync(input[i]);
path = input[i];
}
var data = fs.readFileSync(path, 'utf8');
response.push(data);
} catch (Error) {
response.push({error: Error});
}
}
callback(undefined, response);
};
/**
* Parses the raw data from the sensors.
*
* @function parseTemp
* @param {array} input An array containing the data to parse
* @returns {array} An array with parsed data
*/
var parseTemp = function(input) {
var response = [];
for (var i in input) {
var data = input[i];
if (data.error) {
response.push({error: data.error});
continue;
}
if (data.length === 0) {
response.push({error: 'Could not read data'});
continue;
}
try {
var crc = data.match(/(crc=)[a-z0-9]*/g)[0];
crc = crc.split('=')[1];
var available = data.match(/([A-Z])\w+/g)[0];
var temperature = 'n/a';
if (available === 'YES') {
if (data.match(/(t=)[0-9]{5}/g)) {
temperature = data.match(/(t=)[0-9]{5}/g)[0];
} else if (data.match(/(t=)[0-9]{4}/g)) {
temperature = data.match(/(t=)[0-9]{4}/g)[0];
} else if (data.match(/(t=)[0-9]{3}/g)) {
temperature = data.match(/(t=)[0-9]{3}/g)[0];
}
temperature = temperature.split('=')[1];
temperature = parseInt(temperature);
}
var temp = {
crc: crc,
available: available,
temperature: {
raw: temperature
},
time: Date.now()
};
for (var name in converters) {
temp.temperature[name] = converters[name](temperature);
}
response.push(temp);
} catch (error) {
response.push({error: error});
}
}
return response;
};
/**
* Adds a temperature scale converter function. Conversion functions should accept one parameter and return a value
* representing the converted temperature. The input to the function is the raw temperature from the DS18B20 sensor,
* an integer value that is 1000 * X, where X is the temperature in Celcius with four decimals.
*
* @function readAndParse
* @param {function} callback Function that is called when readAndParse finishes
* @throws {Error} 'Callback function required' Thrown if no callback function is provided
*/
var readAndParse = function(callback) {
if (!callback || typeof callback !== 'function') {
throw new Error('Callback function required');
}
readTemp(function (err, data) {
if (!err) {
var temp = parseTemp(data);
callback(undefined, temp);
} else {
err.tempSensorMessage = 'Error when reading temperature';
logger.error(err.tempSensorMessage, err);
callback(err);
}
});
};
var getDirectories = function(srcpath) {
return fs.readdirSync(srcpath).filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory());
};
/**
* Adds a temperature scale converter function. Conversion functions should accept one parameter and return a value
* representing the converted temperature. The input to the function is the raw temperature from the DS18B20 sensor,
* an integer value that is 1000 * X, where X is the temperature in Celcius with four decimals.
*
* @function addConverter
* @param {string} name Name of the converter, this is used to identify the output of {@link parseTemp}
* @param {function} converterFunc Function implementing the conversion
* @throws {Error} 'Name must be provided' Thrown if no name is provided
* @throws {Error} 'Converter function was not a function' Thrown if converterFunc is not a function
*/
var addConverter = function(name, converterFunc) {
if (!name) {
throw new Error('Name must be provided');
}
if (typeof converterFunc === 'function') {
converters[name] = converterFunc;
} else {
throw new Error('Converter function was not a function');
}
};
/**
* Initializes the temp sensor. The module can use one or more available DS18B20 sensors. Depending on the type of
* parameter one, different behaviors are achieved. Passing a string is equivalent to passing a {string} {array} with one
* element. Passing {undefined} is equivalent to passing a {string} {array} with all installed sensors.
*
* @function init
* @param {array} sensors Sensors to use, can be either a string, an array of strings or undefined. If settings.defaultPath
* is set to true, the values are the ID of the sensor, otherwise the full path to the sensor output.
* @param {object} newSettings object representing changes to settings
* @param {boolean} newSettings.defaultPath Use default path or not. Defaults to true, prepends '/sys/bus/w1/devices/'
* and appends '/w1_slave' to the sensor. If set to false, the values of sensor is left untouched.
* @param {boolean} newSettings.installKernelMod Defaults to false. If set to true, the kernel modules w1-gpio and
* w1-therm are installed when init is called.
* @param {function} callback Function to be called when init is finished
*/
var init = function(sensors, newSettings, callback) {
if (newSettings) {
settings = newSettings;
}
if (!sensors) {
settings.defaultPath = true;
input = getDirectories(sensorDirectory);
} else if (typeof sensors === 'string') {
input = [sensors];
} else {
input = sensors;
}
if (settings.installKernelMod) {
exec('modprobe w1-gpio', modprobe);
exec('modprobe w1-therm', modprobe);
}
if (callback) {
callback(undefined);
}
};
var convertToCelcius = function(raw) {
return raw/1000;
};
var convertToFahrenheit = function(raw) {
var c = convertToCelcius(raw);
return c * (9/5) + 32;
};
addConverter('celcius', convertToCelcius);
addConverter('fahrenheit', convertToFahrenheit);
module.exports = {
init: init,
readTemp: readTemp,
parseTemp: parseTemp,
readAndParse: readAndParse,
addConverter: addConverter,
logDirectory: logDirectory
};