37 lines
950 B
JavaScript
37 lines
950 B
JavaScript
|
var protocol = require('./protocol');
|
||
|
var dgram = require('dgram');
|
||
|
|
||
|
var on = 0;
|
||
|
var speed = 256;
|
||
|
|
||
|
|
||
|
function lsb(value) { return value & 0xFF; }
|
||
|
function msb(value) { return (value >> 8) & 0xFF; }
|
||
|
|
||
|
|
||
|
setInterval(function()
|
||
|
{
|
||
|
// 0x00, 0x10 = 4096
|
||
|
on += speed;
|
||
|
if (on <= 0 || on >= 4096)
|
||
|
speed = -speed;
|
||
|
|
||
|
var message = new Buffer([protocol.Command.SetMode, protocol.Mode.Static, lsb(on), msb(on)]);
|
||
|
var client = dgram.createSocket('udp4');
|
||
|
client.on('listening', function()
|
||
|
{
|
||
|
var address = client.address();
|
||
|
console.log('UDP client listening on ' + address.address + ":" + address.port);
|
||
|
});
|
||
|
|
||
|
client.on('message', function (message, remote)
|
||
|
{
|
||
|
console.log(remote.address + ':' + remote.port +' - ' + message.toString('hex'));
|
||
|
client.close();
|
||
|
});
|
||
|
|
||
|
client.send(message, 0, message.length, 3126, '10.138.2.12', function(err, bytes) {
|
||
|
if (err) throw err;
|
||
|
console.log('UDP message sent');
|
||
|
});
|
||
|
}, 200);
|