hhromic / e131-node

Node.js client/server library for the E1.31 (sACN) protocol
Apache License 2.0
28 stars 12 forks source link
e131 light-controller nodejs protocol sacn

Node.js library for the E1.31 (sACN) protocol

A Node.js module that provides simple client and server objects for communicating with devices using the E1.31 (sACN) protocol. A lot of information about E.131 (sACN) can be found on this Wiki article.

Installation

To install, use npm:

$ npm install e131

Client Class

The Client class implements a UDP client for sending E1.31 (sACN) traffic. The class constructor is as follows:

var e131 = require('e131');
var client = new e131.Client(arg, [port]);

The first argument can be a host address, name or universe number. If port is omitted, the default E1.31 port 5568 is used. If a universe is given, the client will automatically join the relevant Multicast group. The client automatically increments (and wraps around if necessary) the sequence number of the transmitted packet.

The client provides two methods:

Full code example for the Client class:

var e131 = require('e131');

var client = new e131.Client('192.168.1.12');  // or use a universe
var packet = client.createPacket(24);  // we want 8 RGB (x3) slots
var slotsData = packet.getSlotsData();
packet.setSourceName('test E1.31 client');
packet.setUniverse(0x01);  // make universe number consistent with the client
packet.setOption(packet.Options.PREVIEW, true);  // don't really change any fixture
packet.setPriority(packet.DEFAULT_PRIORITY);  // not strictly needed, done automatically

// slotsData is a Buffer view, you can use it directly
var color = 0;
function cycleColor() {
  for (var idx=0; idx<slotsData.length; idx++) {
    slotsData[idx] = color % 0xff;
    color = color + 90;
  }
  client.send(packet, function () {
    setTimeout(cycleColor, 125);
  });
}
cycleColor();

Server Class

The Server class implements a UDP server for receiving E1.31 (sACN) traffic. The class constructor is as follows:

var e131 = require('e131');
var server = new e131.Server([universes], [port]);

The universes argument can be an array (for joining multiple universes) or a single integer for joining a single universe. If universes is omitted, a single value of 1 is assumed.

Note: This library only uses one UDP socket and there is a maximum limit of 20 multicast memberships (universes) per single UDP socket. See issue #17 for more details.

If port is omitted, the default E1.31 port 5568 is used. The server will join the corresponding Multicast groups for each provided universe automatically and starts listening as soon as it is created. The server performs basic out-of-order detection on received packets. If an out-of-order packet is received, it is discarded.

The server supports the following events that you can listen to:

Full code example for the Server class:

var e131 = require('e131');

var server = new e131.Server([0x0001, 0x0002]);
server.on('listening', function() {
  console.log('server listening on port %d, universes %j', this.port, this.universes);
});
server.on('packet', function (packet) {
  var sourceName = packet.getSourceName();
  var sequenceNumber = packet.getSequenceNumber();
  var universe = packet.getUniverse();
  var slotsData = packet.getSlotsData();

  console.log('source="%s", seq=%d, universe=%d, slots=%d',
    sourceName, sequenceNumber, universe, slotsData.length);
  console.log('slots data = %s', slotsData.toString('hex'));
});

E1.31 (sACN) Packet Class

The E1.31 Packet class contains a number of useful setter methods:

Also the following getter methods are provided:

Available E1.31 framing options are:

Available constants in the Packet class are:

If a packet fails validation, the following errors can be returned: