Legible encoding for addressable packets for Javascript
Legible encoding for addressable packets for javascript
npm install leap-protocol
Encoding a packet:
const fs = require(fs);
const leap = require('leap-protocol');
const config = JSON.parse(fs.readFileSync('leap-config.json'));
const codec = new leap.Codec(config);
const packet = new leap.Packet("set", "led/red", true);
const encoded = codec.encode(packet);
...
Decoding a packet:
const fs = require(fs);
const leap = require('leap-protocol');
const config = JSON.parse(fs.readFileSync('leap-config.json'));
const codec = new leap.Codec(config);
...
// Note: if there is a remainder it will be stored back in bytes
const [received, packets] = codec.decode(received);
const data = codec.unpack(packets[0]);
Object.keys(data).forEach(function (address) {
// data[address] are the values in the unpacked data
// address provides the full string path of data. e.g. led/red
...
});
Instantiates a L3aP codec object for encoding packets to strings and decoding strings to packets.
Example:
const config = yaml.parse(fs.readFileSync("leap-protocol.yaml"));
const codec = new leap.Codec(config);
Determines whether the codec has a valid configuration. If the config is not valid, the codec cannot be used.
Example:
...
const codec = new leap.Codec(config);
if (codec.valid()) {
...
}
leap.Packet
object or a list of leap.packet
objects.Encodes one or more packets into a utf-8 byte string.
Example:
const packet_red = new leap.Packet("set", "led/red", true);
const packet_blue = new leap.Packet("set", "led/blue", true);
encoded = codec.encode([packet_red, packet_blue]);
Decodes a utf-8 byte string into one or more packets
Example:
const received_bytes += rx.read();
const [received_bytes, packets] = codec.decode(received_bytes);
for (packet of packets) {
...
}
leap.Packet
led/red
) mapping to thier respective values.Extracts a dictionary from a packet to map address paths to thier respective values.
Example:
if (packet.category === "set") {
const commands = codec.unpack(packet);
if ('led/red' in commands) {
led_red.set(commands['led/red']);
...
}
...
}
Constructs a L3aP packet for encoding. Note, payload can be an array and will set multiple fields at once when the path is a parent.
Example:
const accelerometer_packet = new leap.Packet("pub", "imu/accel", [accel_x, accel_y, accel_z]);
const disable_packet = new leap.Packet("set", "control/balance/disable");
...
Adds path to the packet and optionally a payload. This can be used to create compound packets which allows sets of data to be processed at the same time.
Example:
const sensor_packet = new leap.Packet("pub", "imu/accel", [accel_x, accel_y, accel_z]);
sensor_packet.add("barometer/pressure", baro_pressure);
...
The packet’s category string.
Example:
if (packet.category === "pub") {
update_model(codec.unpack(packet));
}
...
See the codec.unpack(packet)
method above.
Checks the contents of a config object for errors. Prints details of the first failure to stdout. Useful for regression testing.
Example:
...
function test_valid_config(self) {
const config = yaml.parse(fs.readFileSync("../leap-protocol.yaml"));
assert(leap.verify(config);
}
...
A command line tool is avaliable for L3aP:
npm install leap-cli -g
Generate a default config file:
leap generate filename
File names can have extension .yaml .json or .toml.
Verify the contents of your config file:
leap verify configfile
Files can have extension .yaml .json or .toml.
Encode a packet based on a config file:
leap encode configfile category address --payload payload
Example:
hoani@Hoani-CPU sandbox % leap encode config.yaml set item-1/child-1 1 --payload 10 1024.125
Encoded Packet ( set, item-1/child-1, [10,1024.125]):
S0001:0a:44800400
Decode a packet based on a config file:
leap decode configfile packet
Example:
hoani@Hoani-CPU sandbox % leap decode config.yaml S0001:0a:44800400
Decoded Packet <S0001:0a:44800400>:
category - set
address "item-1/child-1/grand-child-1" = 10
address "item-1/child-1/grand-child-2" = 1024.125
Help:
leap --help