Last active
February 9, 2017 07:22
-
-
Save solisoft/650ff22d693fe0b073d6d460f7c63fed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var HOST = '127.0.0.1'; | |
var PORT = 8888; | |
function rand(i) { | |
return parseInt((Math.random() * 10000) % i); | |
} | |
net.createServer(function(sock) { | |
// We have a connection - a socket object is assigned to the connection automatically | |
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort); | |
// Add a 'data' event handler to this instance of socket | |
sock.on('data', function(data) { | |
console.log('RCV Data : ' + sock.remoteAddress + ': ' + data.toString().trim()); | |
var data = data.toString().split("\n"); | |
for(var i = 0; i < data.length; i++) { | |
var cmd = data[i].trim(); | |
switch(cmd) { | |
case 'DETECT': | |
var out = ''; | |
out += Math.random() * 10 > 5 ? ' AVG' : ''; | |
out += Math.random() * 10 > 5 ? ' AVD' : ''; | |
out += Math.random() * 10 > 5 ? ' ARG' : ''; | |
out += Math.random() * 10 > 5 ? ' ARD' : ''; | |
sock.write("DETECT"+out+"\n"); | |
console.log("DETECT"+out+"\n"); | |
break; | |
case 'BOUSSOLE': | |
var out = parseInt(Math.random() * 360); | |
sock.write("BOUSSOLE "+out+"\n"); | |
console.log("BOUSSOLE "+out+"\n"); | |
break | |
case 'DIST_US': | |
var out = 'DIST_US ' + parseInt(Math.random() * 50) + ' ' + parseInt(Math.random() * 50) + ' ' + parseInt(Math.random() * 50) + "\n"; | |
sock.write(out); | |
console.log(out); | |
break; | |
case 'POS_GPS': | |
var lng = parseInt(Math.random() * 360) + '°' + parseInt(Math.random() * 60) + "'" + parseInt(Math.random() * 60) + '"'; | |
var lat = parseInt(Math.random() * 360) + '°' + parseInt(Math.random() * 60) + "'" + parseInt(Math.random() * 60) + '"'; | |
sock.write("POS_GPS " + lat + " " + lng +"\n"); | |
console.log("POS_GPS " + lat + " " + lng +"\n"); | |
break; | |
case 'ETAT_BATT': | |
var out = 'ETAT_BATT ' + parseInt(Math.random() * 24) + "\n"; | |
sock.write(out); | |
console.log(out); | |
break; | |
default: | |
sock.write("TRAME_OK\n"); | |
break; | |
} | |
} | |
sock.write(data.toString().trim()); | |
}); | |
// Add a 'close' event handler to this instance of socket | |
sock.on('close', function(data) { | |
console.log('CLOSED:'); | |
}); | |
}).listen(PORT, HOST); | |
console.log('Server listening on ' + HOST +':'+ PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment