Skip to content

Instantly share code, notes, and snippets.

@willasaywhat
Last active December 18, 2015 15:35
Show Gist options
  • Save willasaywhat/c38a195cb6c1a1334d15 to your computer and use it in GitHub Desktop.
Save willasaywhat/c38a195cb6c1a1334d15 to your computer and use it in GitHub Desktop.
Node REPL for Christmas Lights (usage: .lights 255 255) Note: Lowercase hex is on purpose; light box won't accept it in uppercase.
var dgram = require('dgram');
var repl = require('repl');
var lightswitch_socket = dgram.createSocket("udp4");
var replServer = repl.start({
prompt: 'Lights > '
});
replServer.defineCommand('lights', {
help: 'Set lights value from ',
action: function (channels){
chans = channels.split(' ');
channel1 = chans[0];
channel2 = chans[1];
var value1 = parseInt(channel1);
var value1_in_hex = value1.toString(16);
if (value1 <= 16) value1_in_hex = "0" + value1_in_hex;
var value2 = parseInt(channel2);
var value2_in_hex = value2.toString(16);
if (value2 <= 16) value2_in_hex = "0" + value2_in_hex;
//var buffer = new Buffer("FFFF000000000000\x0d\x0a");
//var buffer = new Buffer(value1_in_hex+value2_in_hex+"000000000000\x0d\x0a");
var buffer = new Buffer(value1_in_hex+value2_in_hex+"FFFFFFFFFFFF\x0d\x0a");
//console.log(value1_in_hex+value2_in_hex+"000000000000");
//var buffer = new Buffer("FFFFFFFFFFFFFFFF\x0d\x0a");
lightswitch_socket.send(buffer, 0, 18, 9802, '192.168.10.41');
}
});
replServer.on('exit', function() {
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment