1 | /*
|
2 |
|
3 | BSD LICENSED
|
4 |
|
5 | Copyright (c) 2011, Janne Julkunen
|
6 | All rights reserved.
|
7 |
|
8 | Redistribution and use in source and binary forms, with or without modification,
|
9 | are permitted provided that the following conditions are met:
|
10 |
|
11 | * Redistributions of source code must retain the above copyright notice, this
|
12 | list of conditions and the following disclaimer.
|
13 |
|
14 | * Redistributions in binary form must reproduce the above copyright notice,
|
15 | this list of conditions and the following disclaimer in the documentation
|
16 | and/or other materials provided with the distribution.
|
17 |
|
18 | *Neither the name of the Enlightened Linux Solutions nor the names of its
|
19 | contributors may be used to endorse or promote products derived from this
|
20 | software without specific prior written permission.
|
21 |
|
22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
25 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
26 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
27 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
31 | OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
33 | */
|
34 |
|
35 | var timeout = null;
|
36 |
|
37 | var exec = require('child_process').exec;
|
38 |
|
39 | var update = function(cb) {
|
40 | if(!cb)
|
41 | console.log("Reading 1-wire sensors");
|
42 |
|
43 | var execute_string = "./data/bin/temperatures-update.sh";
|
44 |
|
45 | var child = exec(execute_string, function(error, stdout, stderr) {
|
46 | if((error) && (cb))
|
47 | cb(null);
|
48 | else if(cb) {
|
49 | cb("1-wire", "1-Wire", "Status Info");
|
50 |
|
51 | timeout = setTimeout(update, 60000);
|
52 | }
|
53 | });
|
54 | };
|
55 |
|
56 | exports.setup = function(cb) {
|
57 | return update(cb);
|
58 | };
|
59 |
|
60 | exports.execute = function(req, res) {
|
61 | console.log("Executing 1-wire command: " + req.params[0]);
|
62 |
|
63 | var execute_string = "./data/bin/temperatures-fetch.sh";
|
64 |
|
65 | var child = exec(execute_string, function(error, stdout, stderr) {
|
66 | if(error !== null) {
|
67 | res.send('null');
|
68 | } else {
|
69 | var sensors = new Array();
|
70 |
|
71 | var info = stdout.split('\n');
|
72 |
|
73 | for(var i = 0; i < info.length; i++) {
|
74 | var tmp = info[i].split(" ");
|
75 |
|
76 | if(tmp.length == 4) {
|
77 | sensors.push({"sensor": tmp[0], "current": tmp[1],
|
78 | "lowest": tmp[2], "highest": tmp[3]});
|
79 | }
|
80 | }
|
81 |
|
82 | res.send({"sensors": sensors});
|
83 | }
|
84 | });
|
85 | };
|
86 |
|