forked from ideino/ideino-linino-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.js
69 lines (56 loc) · 1.48 KB
/
dashboard.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
//Board
var io = require('socket.io').listen(9811),
utils = require('./utils/utils'),
socket;
io.set('transports',['xhr-polling','websocket']);
io.set('log level',1);
io.sockets.on('connection', function (client) {
var address = client.handshake.address;
client.on('join',function(name){
socket = client;
console.log("Client Connected from " + address.address + ":" + address.port);
});
client.on('disconnect', function () {
console.log("Client Disconnect from " + address.address + ":" + address.port);
socket = undefined;
});
});
function write(id, value) {
try {
if(typeof(socket) != 'undefined' ){
//when check connection is ok, emit the request
var cmd = {command:[{cmd: 'write', id: id, value: value}]}
socket.emit('command', cmd); //when connection is ok, emit the request
}
}
catch(err){
console.log(err);
}
}
function read(id, callback) {
try {
//verify socket connection, recursive way
if(typeof(socket) == 'undefined' ){
setTimeout(function(){
digitalRead(pin,callback);
}, 50);
return;
}
var cmd = {command:[{cmd: 'read', pin: id}]}
socket.emit('command', cmd, readback(id, callback)); //when connection is ok, emit the request
}
catch(err){
console.log(err);
}
}
function readback(mpin, callback){
if(typeof(socket) != 'undefined' ){
socket.on('read-back-'+mpin,function(data){
callback(JSON.parse(data));
});
}
}
exports.read = read;
exports.write = write;
exports.HIGH = utils.HIGH;
exports.LOW = utils.LOW;