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