2018-12-19 19:24:06 +00:00
|
|
|
const dgram = require('dgram');
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
const UNIVERSE_LEN = 512;
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
function BBDMX(deviceId = '127.0.0.1', options = {}) {
|
|
|
|
const self = this;
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
self.options = options;
|
|
|
|
self.universe = new Buffer(UNIVERSE_LEN + 1);
|
|
|
|
self.universe.fill(0);
|
|
|
|
self.host = deviceId;
|
|
|
|
self.port = self.options.port || 9930;
|
|
|
|
self.dev = dgram.createSocket('udp4');
|
|
|
|
self.sleepTime = 24;
|
|
|
|
self.start();
|
2016-11-19 21:10:57 +00:00
|
|
|
}
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
BBDMX.prototype.sendUniverse = function () {
|
|
|
|
let channel;
|
|
|
|
let messageBuffer = new Buffer(UNIVERSE_LEN.toString());
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
for (const i = 1; i <= UNIVERSE_LEN; i++) {
|
|
|
|
channel = new Buffer(' ' + this.universe[i]);
|
|
|
|
messageBuffer = Buffer.concat([messageBuffer, channel]);
|
|
|
|
}
|
|
|
|
this.dev.send(messageBuffer, 0, messageBuffer.length, this.port, this.host);
|
2016-11-18 01:46:39 +00:00
|
|
|
};
|
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
BBDMX.prototype.start = function () {
|
|
|
|
this.timeout = setInterval(this.sendUniverse.bind(this), this.sleepTime);
|
|
|
|
};
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
BBDMX.prototype.stop = function () {
|
|
|
|
clearInterval(this.timeout);
|
|
|
|
};
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
BBDMX.prototype.close = function (cb) {
|
|
|
|
this.stop();
|
|
|
|
cb(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
BBDMX.prototype.update = function (u) {
|
|
|
|
for (const c in u) {
|
|
|
|
this.universe[c] = u[c];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BBDMX.prototype.updateAll = function (v) {
|
|
|
|
for (const i = 1; i <= UNIVERSE_LEN; i++) {
|
|
|
|
this.universe[i] = v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BBDMX.prototype.get = function (c) {
|
|
|
|
return this.universe[c];
|
|
|
|
};
|
2016-11-18 01:46:39 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
module.exports = BBDMX;
|