-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature-helper.js
More file actions
145 lines (107 loc) · 3.88 KB
/
temperature-helper.js
File metadata and controls
145 lines (107 loc) · 3.88 KB
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
var fs = require('fs');
var nconf = require('nconf');
var winston = require('winston');
var mapping = null;
var attachedTemperatureProbes = [];
var configFile = null;
var appLog = winston.loggers.get('application');
module.exports = {
setConfigFile: function(file) {
configFile = file;
},
getTempProbeMap: function () {
if( mapping != undefined && mapping != null )
return mapping;
if(configFile == null) {
appLog.error('Conguration file path was not provided.');
return;
}
nconf.use('file', {
file: configFile
});
nconf.load();
mapping = nconf.get('probeMap');
if(mapping == undefined) {
appLog.info('Creating probeMap section in config file (config.json).');
//we haven't created mapping configurations yet, let's do it now with empty mapping
mapping = {
hlt: '',
mt: '',
bk: ''
};
nconf.set('probeMap:hlt', '');
nconf.set('probeMap:mt', '');
nconf.set('probeMap:bk', '');
nconf.save(function(err) {
if(err) {
appLog.error('Error saving probe mapping data to file.');
}
});
}
return mapping;
},
setTempProbeMap: function (probeMap) {
if(configFile == null) {
appLog.error('Conguration file path was not provided.');
return;
}
nconf.use('file', {
file: configFile
});
nconf.load();
nconf.set('probeMap', probeMap);
nconf.save(function(err) {
if (err) {
appLog.error('Error saving probe mapping data to file.');
}
mapping = probeMap;
});
},
getAllAttachedTempProbes: function() {
if(attachedTemperatureProbes.length > 0)
return attachedTemperatureProbes;
//The 1wire file system (owfs) used by this system creates directories for each attached supported probe types.
//The temperature probes this system supports are the 18S20 and 18B20. These have a
//1wire family type ID of 10 and 28 respectively (see http://owfs.sourceforge.net/family.html).
//The directory names are prefixed with that ID, so we're going to search our 1wire directory
//for subdirectories with names that start with 10. or 28.
var files = fs.readdirSync('/mnt/1wire/');
for( var i = 0; i < files.length; i++ ) {
var family = files[i].split('.')[0];
if( family === '10' || family === '28' ){
attachedTemperatureProbes.push(files[i]);
}
}
return attachedTemperatureProbes;
},
getCurrentTemperature: function(probeId, tempCallback) {
if(mapping == undefined)
mapping = this.getTempProbeMap();
var vessel;
if(mapping.hlt == probeId) {
vessel = 'hlt';
}
if(mapping.mt == probeId) {
vessel = 'mt';
}
if(mapping.bk == probeId) {
vessel = 'bk';
}
var temperatureFile = '/mnt/1wire/' + probeId + '/temperature9';
fs.readFile(temperatureFile, 'utf8', function(err, data) {
var degF = data * (9.0/5.0) + 32;
degF = Math.round(degF * 10) / 10;
tempCallback(vessel, degF, probeId);
});
},
getCurrentTemperatureSync: function(vessel) {
if(mapping == null)
mapping = this.getTempProbeMap();
var probeId = mapping[vessel];
var temperatureFile = '/mnt/1wire/uncached/' + probeId + '/temperature10';
var degC = parseFloat(fs.readFileSync(temperatureFile, 'utf8'));
var degF = degC * (9.0/5.0) + 32;
degF = Math.round(degF * 100) / 100;
return degF;
}
};