-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
175 lines (144 loc) · 3.55 KB
/
util.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
/* Response ptr */
var res_ptr;
/* Requested data */
var req_data;
/* Serial port Name */
var portName = '/dev/ttyUSB0';
/* Buffer to receive serial */
var buffer = '';
var sys = require('sys'),
fs = require('fs'),
qs = require('querystring'),
url = require('url'),
serial = require('serialport'),
util = exports;
/* define serial port */
var serialPort = serial.SerialPort;
var sp = new serialPort(portName, {
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
}, true, function (err) {
if (err != null)
console.log(err.toString());
});
/* define serial port event callbacks */
/* Serial port data event */
sp.on('data', function (data) {
if (req_data == null)
return;
console.log("Received serial data: " + data);
buffer += data.toString()
/* TEMP returns as char array */
if (req_data['cmd'] == 'TEMP' &&
buffer.indexOf('\n') === -1)
return;
if (req_data['cmd'] == 'STATUS') {
var dec = data.readUInt8(0)
var binStr = parseInt(dec).toString(2);
buffer = pad(8, binStr, '0');
}
res_ptr.simpleJSON(200, {
data: buffer
});
buffer = '';
req_data = null;
res_ptr = null;
});
/* Serial port close event */
sp.on('close', function (err) {
console.log('serial port has been closed');
});
/* Serial port error event */
sp.on('error', function (err) {
console.error("serial port error", err);
});
/* Serial port open event */
sp.on('open', function () {
console.log('serial port has been opened');
});
/* define util map */
util.getMap = [];
util.get = function(path, handler) {
util.getMap[path] = handler;
};
util.not_found = function(req, res) {
var not_found_msg = 'Not Found';
res.writeHead(404, {
'Content-Type': 'text/plain',
'Content-Length': not_found_msg.length
});
res.write(not_found_msg);
res.end();
};
util.staticHandler = function(filename) {
var body;
function loadResponseData(callback) {
fs.readFile(filename, function(err, data) {
if (err) {
sys.debug('Error loadinf file ' + filename);
} else {
sys.debug('loading file ' + filename);
body = data;
}
callback();
});
}
return function(req, res) {
loadResponseData(function() {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': body.length
});
res.write(body);
res.end();
});
};
};
function writeCmdToSerial (data, res) {
var dec = 0;
if (data.cmd == 'STATUS') {
dec = 91;
} else if (data.cmd == 'TEMP') {
dec = 98;
} else if (data.cmd == 'ON') {
dec = (100 + parseInt(data['arg']));
} else if (data.cmd == 'OFF') {
dec = (110 + parseInt(data['arg']));
}
console.log('DEC: ' + dec + ' HEX: 0x' + dec.toString(16));
sp.write(String.fromCharCode(dec), function (err, bytesWritten) {
if (bytesWritten == null) {
res.simpleJSON(200, {
data: 'KO'
});
} else if (data.cmd == 'ON' || data.cmd == 'OFF') {
res.simpleJSON(200, {
data: 'OK'
});
} else {
/* On other cases we need to
* wait for the async response */
res_ptr = res;
req_data = data;
}
});
}
function pad(width, string, padding) {
return (width <= string.length) ?
string :
pad(width, padding + string, padding)
}
util.get('/', util.staticHandler('index.html'));
util.get('/client.js', util.staticHandler('client.js'));
util.get('/jquery-1.4.2.js', util.staticHandler('jquery-1.4.2.js'));
util.get('/command', function(req, res) {
var data = qs.parse(url.parse(req.url).query);
if (data.arg == null || data.arg.length == 0)
data.arg = 0;
console.log("Received command: " + data.cmd);
console.log("Received arg: " + data.arg);
writeCmdToSerial(data, res);
});