forked from ideino/ideino-linino-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlboard.js
252 lines (230 loc) · 6.17 KB
/
htmlboard.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//HtmlBoard
//CONST
var BUFFER_SIZE = 1000, //items -> is the size of the buffer containing the write commands
BUFFER_SPEED = 25, //millis -> is the speed for sending requests from the buffer to the board
READ_RETRY_SPEED = 500, //millis -> is the speed for retry to send the read command
PORT = 9812;
var io = require('socket.io').listen(PORT,{log:false}),
utils = require('./utils/utils'),
socket,
writeBuffer = []; //buffer for managing the write request sent to the board
var board,
logger;
io.set('transports',['xhr-polling']);
io.set('log level',1);
io.sockets.on('connection', function (client) {
var address = client.handshake.address;
socket = client;
logger.info("HtmlBoard Client Connect from " + address.address + ":" + address.port);
setInterval(function(){
sendWriteRequest();
},BUFFER_SPEED);
socket.on('disconnect', function () {
logger.info("HtmlBoard Client Disconnect from " + address.address + ":" + address.port);
socket = undefined;
});
socket.on('command',function(data){
logger.debug("Received command from HtmlBoard Client: "+JSON.stringify(data));
parseCommand(data);
});
socket.on('info',function(data){
logger.debug("Received info request from HtmlBoard Client: "+JSON.stringify(data));
parseInfoRequest(data);
});
});
/***
* Function used in node app to interact with html component
*/
function write(id, param, value) {
try {
if(typeof(socket) != 'undefined' ){
//when check connection is ok, emit the request
var cmd = {command:[{cmd: 'write', id: id, param: param, value: value}]}
//socket.emit('command', cmd); //when connection is ok, emit the request
pushWriteRequest(cmd);
}
}
catch(err){
logger.error("HTML BOARD WRITE ERROR - " +err.message);
}
}
function read(id, param, callback) {
try {
//verify socket connection, recursive way
if(typeof(socket) == 'undefined' ){
setTimeout(function(){
read(id,param,callback);
}, READ_RETRY_SPEED);
return;
}
var cmd = {command:[{cmd: 'read', param: param, id: id}]}
socket.emit('command', cmd, readback(id, callback)); //when connection is ok, emit the request
}
catch(err){
logger.error("HTML BOARD READ ERROR - " +err.message);
}
}
/***
* Interface to the Board
*/
function boardDigitalRead(pin){
if(typeof(board) != 'undefined' ){
board.digitalRead(pin,function(data){
if(typeof(socket) != 'undefined')
socket.emit('read-back-'+pin, {cmd: "read-back", pin: pin, value: data.value});
});
}
}
function boardAnalogRead(pin){
if(typeof(board) != 'undefined'){
board.analogRead(pin,function(data){
if(typeof(socket) != 'undefined')
socket.emit('read-back-'+pin, {cmd: "read-back", pin: pin, value: data.value});
});
}
}
function boardVirtualRead(pin,value){
if(typeof(board) != 'undefined'){
board.virtualRead(pin,function(data){
if(typeof(socket) != 'undefined')
socket.emit('read-back-'+pin, {cmd: "read-back", pin: pin, value: data.value});
});
}
}
function boardDigitalWrite(pin,value){
if(typeof(board) != 'undefined'){
board.digitalWrite(pin,value);
}
}
function boardAnalogWrite(pin,value){
if(typeof(board) != 'undefined'){
board.analogWrite(pin,value);
}
}
function boardPinMode(pin, mode){
if(typeof(board) != 'undefined'){
board.pinMode(pin,mode);
}
}
function boardServoWrite(pin,value){
if(typeof(board) != 'undefined'){
board.servoWrite(pin, value);
}
}
function boardVirtualWrite(pin,value){
if(typeof(board) != 'undefined'){
board.virtualWrite(pin, value);
}
}
function boardGetLayout(){
if(typeof(board) != 'undefined'){
if(typeof(socket) != 'undefined')
socket.emit('info-back-layout', {info: "layout-back", value: board.pin});
}
}
/***
* Set Functions
***/
//set the board object
function setBoard(b){
board = b;
}
//set the logger object
function setLogger(l){
logger = l;
}
/***
* System Functions
***/
//parse the command from the client-html
//example json command -> { command : [{ func : "dw", pin : "D13", value : 1 }]}
function parseCommand(c){
switch(c.command[0].func.toLowerCase()){
case "pm":
boardPinMode(c.command[0].pin, c.command[0].mode);
break;
case "dw":
boardDigitalWrite(c.command[0].pin, c.command[0].value);
break;
case "dr":
boardDigitalRead(c.command[0].pin);
break;
case "aw":
boardAnalogWrite(c.command[0].pin, c.command[0].value);
break;
case "ar":
boardAnalogRead(c.command[0].pin);
break;
case "sw":
boardServoWrite(c.command[0].pin, c.command[0].value);
break;
case "vr":
boardAnalogRead(c.command[0].pin);
break;
case "vw":
boardServoWrite(c.command[0].pin, c.command[0].value);
break;
}
/*
switch(c.command[0].cmd.toLowerCase()){
case "mode":
boardPinMode(c.command[0].pin, c.command[0].mode);
break;
case "write":
if( c.command[0].pin.toLowerCase().startsWith('d') )
boardDigitalWrite(c.command[0].pin, c.command[0].value);
else
boardAnalogWrite(c.command[0].pin, c.command[0].value);
break;
case "read":
if( c.command[0].pin.toLowerCase().startsWith('d') )
boardDigitalRead(c.command[0].pin);
else
boardAnalogRead(c.command[0].pin);
break;
}
*/
}
//parse the info request from the client-html
//example json info request -> { info : "layout"}
function parseInfoRequest(i){
switch(i.info.toLowerCase()){
case "layout":
boardGetLayout();
break;
case "pin_status":
boardDigitalWrite(c.command[0].pin, c.command[0].value);
break;
}
}
//push write command for bridge inside a buffer
function pushWriteRequest(cmd){
if(writeBuffer.length < BUFFER_SIZE){
writeBuffer.push(cmd);
}
}
//get the command from the buffer and send it to the bridge
function sendWriteRequest(){
if(typeof(socket) != 'undefined' && writeBuffer.length > 0){
socket.emit('command', writeBuffer.shift());
}
}
function readback(mpin, callback){
if(typeof(socket) != 'undefined' ){
socket.on('read-back-'+mpin,function(data){
//console.log(data);
//console.log(JSON.parse(data));
//callback(JSON.parse(data));
callback(data);
});
}
}
exports.setBoard = setBoard;
exports.setLogger = setLogger;
exports.read = read;
exports.write = write;
exports.HIGH = utils.HIGH;
exports.LOW = utils.LOW;
exports.TRUE = utils.TRUE;
exports.FALSE = utils.FALSE;
exports.PARAMS = utils.PARAMS;