|
| 1 | +var dgram = require('dgram'); |
| 2 | +var net = require('net'); |
| 3 | +var util = require('util'); |
| 4 | + |
| 5 | +module.exports = { |
| 6 | + |
| 7 | + discover: function(cb) { |
| 8 | + var c = dgram.createSocket("udp4"); |
| 9 | + var port = 56700; |
| 10 | + var found = false; |
| 11 | + c.on("error", function (err) { |
| 12 | + console.log("DGram error " + err); |
| 13 | + }); |
| 14 | + c.on("message", function (msg, rinfo) { |
| 15 | + if (msg.length > 4 && msg[3] == 0x54) { |
| 16 | + found = true; |
| 17 | + c.close(); // Don't do this when we want ongoing status messages |
| 18 | + cb(null, {address:rinfo.address, port:rinfo.port, family:rinfo.family} ); |
| 19 | + } |
| 20 | + }); |
| 21 | + c.bind(port, function() { |
| 22 | + c.setBroadcast(true); |
| 23 | + var intervalID; |
| 24 | + // Now send the discovery packets |
| 25 | + intervalID = setInterval(function() { |
| 26 | + if (found) { |
| 27 | + clearInterval(intervalID); |
| 28 | + } else { |
| 29 | + var message = new Buffer([0x24, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]); |
| 30 | + c.send(message, 0, message.length, port, "255.255.255.255", function(err, bytes) { |
| 31 | + if (err) { |
| 32 | + cb(err); |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | + }, 100); |
| 37 | + }); |
| 38 | + }, |
| 39 | + |
| 40 | + init: function(addr, cb) { |
| 41 | + var b = new bulb(addr); |
| 42 | + cb(null, b); |
| 43 | + }, |
| 44 | + |
| 45 | + discoverAndInit: function(cb) { |
| 46 | + module.exports.discover(function(err, addr) { |
| 47 | + if (err) { |
| 48 | + cb(err); |
| 49 | + } else { |
| 50 | + module.exports.init(addr, cb); |
| 51 | + } |
| 52 | + }); |
| 53 | + }, |
| 54 | + |
| 55 | +}; |
| 56 | + |
| 57 | +function bulb(addr) { |
| 58 | + |
| 59 | + this.address = null; |
| 60 | + this.port = null; |
| 61 | + var client = null; |
| 62 | + |
| 63 | + if (typeof addr == 'object' && typeof addr.address == 'string' && typeof addr.port == 'number') { |
| 64 | + this.address = addr.address; |
| 65 | + this.port = addr.port; |
| 66 | + } |
| 67 | + |
| 68 | + client = net.connect(56700, '10.1.0.80', function() { //'connect' listener |
| 69 | + //console.log('client connected'); |
| 70 | + }); |
| 71 | + client.on('data', function(data) { |
| 72 | + console.log(" - " + "TCP got data (" + data.length + " bytes)"); |
| 73 | + console.log(" - " + data.toString("hex")); |
| 74 | + }); |
| 75 | + client.on('end', function() { |
| 76 | + console.log('client disconnected'); |
| 77 | + }); |
| 78 | + |
| 79 | + var self = this; |
| 80 | + |
| 81 | + this.sendRawPacket = function(message) { |
| 82 | + client.write(message); |
| 83 | + }; |
| 84 | + |
| 85 | + this.lightsOn = function() { |
| 86 | + self.sendRawPacket(new Buffer([0x26, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x01, 0x00])); |
| 87 | + }; |
| 88 | + |
| 89 | + this.lightsOff = function() { |
| 90 | + self.sendRawPacket(new Buffer([0x26, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00])); |
| 91 | + }; |
| 92 | + |
| 93 | + this.red = function() { |
| 94 | + self.sendRawPacket(new Buffer([0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x9e, 0xd4, 0xff, 0xff, 0x8f, 0x02, 0xac, 0x0d, 0x13, 0x05, 0x00, 0x00])); |
| 95 | + }; |
| 96 | + |
| 97 | + this.purple = function() { |
| 98 | + self.sendRawPacket(new Buffer([0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x15, 0xcc, 0xff, 0xff, 0x8f, 0x02, 0xac, 0x0d, 0x13, 0x05, 0x00, 0x00])); |
| 99 | + }; |
| 100 | + |
| 101 | + this.brightWhite = function() { |
| 102 | + self.sendRawPacket(new Buffer([0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0x96, 0xf0, 0x0a, 0x90, 0x01, 0x00, 0x00])); |
| 103 | + }; |
| 104 | + |
| 105 | + this.getInfo = function() { |
| 106 | + self.sendRawPacket(new Buffer([0x24, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x73, 0xd5, 0x00, 0x23, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00])); |
| 107 | + }; |
| 108 | + |
| 109 | + this.close = function() { |
| 110 | + client.end(); |
| 111 | + }; |
| 112 | + |
| 113 | +} |
0 commit comments