2016-04-09 16:15:17 +00:00
|
|
|
"use strict"
|
|
|
|
|
2016-04-09 16:35:33 +00:00
|
|
|
var dgram = require('dgram')
|
2016-04-09 16:15:17 +00:00
|
|
|
|
2016-04-09 16:35:33 +00:00
|
|
|
function EnttecODE(device_id, options) {
|
|
|
|
var self = this
|
2016-04-09 16:15:17 +00:00
|
|
|
|
2016-04-09 16:35:33 +00:00
|
|
|
self.header = new Buffer([65, 114, 116, 45, 78, 101, 116, 0, 0, 80, 0, 14])
|
2016-06-07 21:13:36 +00:00
|
|
|
self.sequence = new Buffer([0])
|
|
|
|
self.physical = new Buffer([0])
|
|
|
|
self.universe_id = new Buffer([0x00, 0x00])
|
|
|
|
self.length = new Buffer([0x02, 0x00])
|
2016-04-09 16:15:17 +00:00
|
|
|
|
2017-07-30 15:17:05 +00:00
|
|
|
self.universe = new Buffer(513)
|
2016-06-07 21:13:36 +00:00
|
|
|
self.universe.fill(0)
|
2016-04-09 16:15:17 +00:00
|
|
|
|
2016-04-09 16:35:33 +00:00
|
|
|
self.sleepTime = 24
|
2016-04-09 16:15:17 +00:00
|
|
|
|
2016-04-09 16:35:33 +00:00
|
|
|
options = options || {}
|
2016-06-07 21:13:36 +00:00
|
|
|
self.universe_id.writeInt16BE(options.universe || 0, 0)
|
2016-04-09 16:35:33 +00:00
|
|
|
self.host = device_id || '127.0.0.1'
|
|
|
|
self.port = options.port || 6454
|
|
|
|
self.dev = dgram.createSocket('udp4')
|
2018-01-11 21:05:02 +00:00
|
|
|
self.dev.bind(function() {
|
|
|
|
self.dev.setBroadcast(true)
|
|
|
|
})
|
2016-04-09 16:35:33 +00:00
|
|
|
self.start()
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.send_universe = function() {
|
2016-04-09 16:35:33 +00:00
|
|
|
var pkg = Buffer.concat([
|
|
|
|
this.header,
|
|
|
|
this.sequence,
|
|
|
|
this.physical,
|
2016-06-07 21:13:36 +00:00
|
|
|
this.universe_id,
|
2016-04-09 16:35:33 +00:00
|
|
|
this.length,
|
2017-07-30 15:17:05 +00:00
|
|
|
this.universe.slice(1)
|
2016-04-09 16:35:33 +00:00
|
|
|
])
|
|
|
|
|
2016-06-06 19:56:46 +00:00
|
|
|
this.dev.send(pkg, 0, pkg.length, this.port, this.host)
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.start = function() {
|
2016-04-09 16:35:33 +00:00
|
|
|
this.timeout = setInterval(this.send_universe.bind(this), this.sleepTime)
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.stop = function() {
|
2016-04-09 16:35:33 +00:00
|
|
|
clearInterval(this.timeout)
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.close = function(cb) {
|
2016-04-09 16:35:33 +00:00
|
|
|
this.stop()
|
|
|
|
cb(null)
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.update = function(u) {
|
2016-04-09 16:35:33 +00:00
|
|
|
for (var c in u) {
|
|
|
|
this.universe[c] = u[c]
|
|
|
|
}
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.updateAll = function(v) {
|
2017-07-30 15:17:05 +00:00
|
|
|
for (var i = 1; i <= 512; i++) {
|
2016-04-09 16:35:33 +00:00
|
|
|
this.universe[i] = v
|
|
|
|
}
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnttecODE.prototype.get = function(c) {
|
2016-04-09 16:35:33 +00:00
|
|
|
return this.universe[c]
|
2016-04-09 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EnttecODE
|