Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lessondesk/marker-protocol

Package Overview
Dependencies
Maintainers
6
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lessondesk/marker-protocol - npm Package Compare versions

Comparing version 3.4.2 to 3.5.0

463

dist/index.js

@@ -1,2 +0,463 @@

function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e,r=t(require("events")),n=t(require("serialport")),i=function(t){function e(e){void 0===e&&(e={}),t.call(this,e),this.buffer=Buffer.alloc(0)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype._transform=function(t,e,r){var n=Buffer.concat([this.buffer,t]);if(n.length<6)console.debug("Packet received with length: "+n.length+" require at least 6 bytes, buffering..."),this.buffer=n;else{var i=n[4]<<8|n[5];n.length<i?this.buffer=n:n.length===i?(this.push(n),this.buffer=Buffer.alloc(0)):(this.push(n.slice(0,i)),this.buffer=n.slice(i,n.length))}return r()},e}(require("stream").Transform),o=require("binary-parser").Parser,u=require("stream").Transform,a=Object.freeze({AP_DUMP:9,AP_ERROR:3,AP_UPDATE:0,AP_VERSION:7,MARKER_INFO:8,MARKER_JOIN:6,MARKER_ADDED:4,MARKER_BUTTON:1,MARKER_BATTERY:2,MARKER_REMOVED:5});function f(t){return{type:t[0],serial:t.slice(1).reduce(function(t,e,r){return 2===r?t|e:(t|e)<<8})}}var c=(new o).array("markers",{type:(new o).array("marker",{type:"uint8",length:4,formatter:f}),length:function(t){return(t.bytes-11)/4}}),s=(new o).uint8("count").uint8("amp").uint8("channel"),l=(new o).uint8("code"),p=(new o).uint8("hardwareMajor").uint8("hardwareMinor").uint8("firmwareMajor").uint8("firmwareMinor"),h=(new o).string("gtin",{encoding:"hex",length:7}),R=(new o).array("marker",{type:"uint8",length:4,formatter:f}),A=(new o).array("marker",{type:"uint8",length:4,formatter:f}).uint8("count").uint8("linkId").uint8("index"),w=(new o).uint8("button",{formatter:function(t){switch(t){case 1:return 1;case 2:return 2;case 4:return 3;case 8:return 4}}}),m=(new o).uint16("battery",{formatter:function(t){var e=10*t;return{voltage:e,replace:e<2200}}}),y=(new o).array("marker",{type:"uint8",length:4,formatter:f}).uint8("count").uint8("index"),_=(new o).skip(3).uint8("version").uint16("bytes").uint8("type").array("source",{type:"uint8",length:4,formatter:f}).choice("data",{tag:"type",choices:(e={},e[a.AP_DUMP]=c,e[a.AP_ERROR]=l,e[a.AP_UPDATE]=s,e[a.AP_VERSION]=p,e[a.MARKER_INFO]=h,e[a.MARKER_JOIN]=R,e[a.MARKER_ADDED]=A,e[a.MARKER_BUTTON]=w,e[a.MARKER_BATTERY]=m,e[a.MARKER_REMOVED]=y,e)}),E=function(t){function e(){t.call(this,{objectMode:!0,writableObjectMode:!0})}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype._transform=function(t,e,r){try{return r(null,_.parse(t))}catch(t){return console.error(t),r(t,null)}},e}(u);module.exports=function(t){function e(e){var r=this;void 0===e&&(e=!1),t.call(this),this.acceptMarker=function(t,e){var r=Buffer.from([253,253,253,3,2]),n=Buffer.alloc(4);n.writeUInt8(121,0),n.writeUInt8((16711680&e)>>16,1),n.writeUInt8((65280&e)>>8,2),n.writeUInt8(255&e,3);var i=Buffer.concat([r,n]);t.write(i)},this.removeMarker=function(t,e){var r=Buffer.from([253,253,253,3,5]),n=Buffer.alloc(4);n.writeUInt8(121,0),n.writeUInt8((16711680&e)>>16,1),n.writeUInt8((65280&e)>>8,2),n.writeUInt8(255&e,3);var i=Buffer.concat([r,n]);t.write(i)},this.listMarkers=function(t){t.write(Buffer.from([253,253,253,3,6]))},this.handleApVersionRequest=function(t){t.write(Buffer.from([253,253,253,3,4]))},this.setAmpStateOn=function(t){t.write(Buffer.from([253,253,253,3,1,1]))},this.setApChannel=function(t,e){t.write(Buffer.from([253,253,253,3,e]))},this.openAccessPoint=function(t){var e=t.filter(function(t){return"FTDI"===t.manufacturer})[0],o=new n(e&&e.path||"/dev/ttyAMA0",{baudRate:115200,databits:8,stopBits:1,parity:"none"});o.pipe(new i).pipe(new E).on("data",function(t){Object.keys(a).forEach(function(e){a[e]===t.type&&(r.debug&&console.log(e,t),r.emit(e,t))})}),r.on("ACCEPT",function(t){return r.acceptMarker(o,t)}),r.on("REMOVE",function(t){return r.removeMarker(o,t)}),r.on("LIST",function(){return r.listMarkers(o)}),r.on("VERSION",function(){return r.handleApVersionRequest(o)}),r.on("CHANNEL",function(t){return r.setApChannel(o,t)})},this.debug=e,n.list().then(this.openAccessPoint).catch(console.error)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.setAmpStateOff=function(t){t.write(Buffer.from([253,253,253,3,1,0]))},e}(r);
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var EventEmitter = _interopDefault(require('events'));
var fs = _interopDefault(require('fs'));
var SerialPort = _interopDefault(require('serialport'));
var ref = require("stream");
var Transform = ref.Transform;
var Framer = /*@__PURE__*/(function (Transform) {
function Framer(options) {
if ( options === void 0 ) options = {};
Transform.call(this, options);
this.buffer = Buffer.alloc(0);
}
if ( Transform ) Framer.__proto__ = Transform;
Framer.prototype = Object.create( Transform && Transform.prototype );
Framer.prototype.constructor = Framer;
Framer.prototype._transform = function _transform (chunk, encoding, cb) {
// console.log(chunk);
var data = Buffer.concat([this.buffer, chunk]); // We need to have atleast 8 bytes in order to determine the message type.
if (data.length < 6) {
console.debug(("Packet received with length: " + (data.length) + " require at least 6 bytes, buffering..."));
this.buffer = data;
} else {
var packet_length = data[4] << 8 | data[5];
if (data.length < packet_length) {
this.buffer = data;
} else if (data.length === packet_length) {
this.push(data);
this.buffer = Buffer.alloc(0);
} else {
this.push(data.slice(0, packet_length));
this.buffer = data.slice(packet_length, data.length);
}
}
return cb();
};
return Framer;
}(Transform));
var obj;
var ref$1 = require("binary-parser");
var Parser = ref$1.Parser;
var ref$1$1 = require("stream");
var Transform$1 = ref$1$1.Transform;
/**
* This object contains flags for the different message types that can be sent by the Access Point to the
* Server or Host PC.
*/
var Packets = Object.freeze({
AP_DUMP: 0x09,
AP_ERROR: 0x03,
AP_UPDATE: 0x00,
AP_VERSION: 0x07,
MARKER_INFO: 0x08,
MARKER_JOIN: 0x06,
MARKER_ADDED: 0x04,
MARKER_BUTTON: 0x01,
MARKER_BATTERY: 0x02,
MARKER_REMOVED: 0x05
});
/**
* This object contains the different error flags that can be sent by the Access Point to the
* Server or Host PC.
*
* NB: Error Codes are forwarded for logging purposes only and cannot be handled by the
* receiving application.
*/
// const Status = Object.freeze({
// SUCCESS: 0x00,
// TIMEOUT: 0x01,
// BAD_PARAM: 0x02,
// NOMEM: 0x03,
// NO_FRAME: 0x04,
// NO_LINK: 0x05,
// NO_JOIN: 0x06,
// NO_CHANNEL: 0x07,
// NO_PEER_UNLINK: 0x08,
// TX_CCA_FAIL: 0x09,
// NO_PAYLOAD: 0x10,
// NO_AP_ADDRESS: 0x11,
// SMPL_NO_ACK: 0x12,
// UART_BUFF_ERROR: 0x13,
// UART_BUFF_OVERFLOW: 0x14,
// MARKER_ACCPT_TBL_OVERFLOW: 0x15,
// })
/**
* Parses the 4-Byte array containing the type and serial number of either a marker or
* an Access Point.
*
* The array has the following signature:
*
* [type], [serial_1], [serial_2], [serial_3]
* |____| |________________________________|
* Type Payload
*
* - Type: Either 128 (Access Point) or 121 (Marker)
* - Payload: 3 unsigned 8-bit integers that must be or'ed together.
*
* @param {*} arr the array received from the packet.
*/
function parseSerialNumber(arr) {
var type = arr[0];
var serial = arr.slice(1).reduce(function (acc, byte, index) { return index === 2 ? acc | byte : (acc | byte) << 8; });
return {
type: type,
serial: serial
};
}
/**
* This parser parses the list of markers currently connected to the Access Point.
*
* - markers: the list of currently connected marker serial numbers.
*/
var PR_AP_DUMP = new Parser().array("markers", {
type: new Parser().array("marker", {
type: "uint8",
length: 4,
formatter: parseSerialNumber
}),
length: function (parsed) {
return (parsed.bytes - 11) / 4;
}
});
/**
* This Parser parses all packets with the 'AP_UPDATE' flag, these are messages sent by the Access Point
* once every minute providing a status update. The data that is received is:
*
* - markers: the number of currently linked markers.
* - amp: the current state of the amp, this is either on (true) or off (false).
* - channel: the current channel the access point is listening on, values are [0, 3].
*/
var PR_AP_UPDATE = new Parser().uint8("count").uint8("amp").uint8("channel");
/**
* This Parser parses all packets with the 'PT_AP_ERROR' flag, these are messages sent by the Access Point
* once every minute providing a status update. The data that is received is:
*
* - code: the error code forwarded by the access point for logging purposes.
*/
var PR_AP_ERROR = new Parser().uint8("code");
/**
* This Parser parses all packets with the 'PT_AP_VERSION' flag, this messages is received as a response the to
* Hardware Version Request.
*
* - hardwareMajor: the hardware major version,
* - hardwareMinor: the hardware minor version.
* - firmwareMajor: the firmware major version.
* - firmwareMinor: the firmware minor version.
*/
var PR_AP_VERSION = new Parser().uint8("hardwareMajor").uint8("hardwareMinor").uint8("firmwareMajor").uint8("firmwareMinor");
/**
* This parser extracts detail about the marker that has just joined the AP, namely the GTIN.
*
* - gtin: the 14 digit gs1 GTIN number of the marker.
*/
var PR_MARKER_INFO = new Parser().string("gtin", {
encoding: "hex",
length: 7
});
/**
* Parses a marker join request, this contains the serial number of the marker that is attempting
* to joining the Access Point.
*
* Note: We don't format the marker serial number for this packet, as we need
* to send the raw bytes back to the access point to accept the join request.
*
* - marker: the marker serial number that is attempting to join the access point.
*/
var PR_MARKER_JOIN = new Parser().array("marker", {
type: "uint8",
length: 4,
formatter: parseSerialNumber
});
/**
* Parses a Marker Added message.
*
* - marker: the serial number of the added marker.
* - count: the number of currently linked markers.
* - index: the position in the AP's marker link table.
*/
var PR_MARKER_ADDED = new Parser().array("marker", {
type: "uint8",
length: 4,
formatter: parseSerialNumber
}).uint8("count").uint8("linkId").uint8("index");
/**
* Parses the Marker Input Message, this is received everytime a user pushes
* a button on one of the markers.
*
* - button: the button that was pushed on the marker [1, 4].
*/
var PR_MARKER_BUTTON = new Parser().uint8("button", {
formatter: function (data) {
switch (data) {
case 1:
return 1;
case 2:
return 2;
case 4:
return 3;
case 8:
return 4;
}
}
});
/**
*
* Parses the marker Battery Update Message. This message is sent periodically and
* returns the current battery level of the identified marker.
*
* - msb: the most significat byte.
* - lsb: the least significant byte.
*/
var PR_MARKER_BATTERY = new Parser().uint16("battery", {
formatter: function (data) {
var voltage = data * 10;
var replace = voltage < 2200 ? true : false;
return {
voltage: voltage,
replace: replace
};
}
});
/**
* Parses the Marker Removed Message, markers can be detached from the AP for a number of different reasons.
*
* - marker: the serial number of the added marker.
* - count: the number of currently linked markers.
* - index: the position in the AP's marker link table.
*/
var PR_MARKER_REMOVED = new Parser().array("marker", {
type: "uint8",
length: 4,
formatter: parseSerialNumber
}).uint8("count").uint8("index");
var PARSER = new Parser().skip(3).uint8("version").uint16("bytes").uint8("type").array("source", {
type: "uint8",
length: 4,
formatter: parseSerialNumber
}).choice("data", {
tag: "type",
choices: ( obj = {}, obj[Packets.AP_DUMP] = PR_AP_DUMP, obj[Packets.AP_ERROR] = PR_AP_ERROR, obj[Packets.AP_UPDATE] = PR_AP_UPDATE, obj[Packets.AP_VERSION] = PR_AP_VERSION, obj[Packets.MARKER_INFO] = PR_MARKER_INFO, obj[Packets.MARKER_JOIN] = PR_MARKER_JOIN, obj[Packets.MARKER_ADDED] = PR_MARKER_ADDED, obj[Packets.MARKER_BUTTON] = PR_MARKER_BUTTON, obj[Packets.MARKER_BATTERY] = PR_MARKER_BATTERY, obj[Packets.MARKER_REMOVED] = PR_MARKER_REMOVED, obj )
});
/**
*
* Messages sent by the Accesss Point to the Client will have the following general format:
*
* [sync_byte],[sync_byte], [sync_byte], [serial_1], [serial_2], [serial_3], [serial_4], [flag], [data_1] [data_2] [data_n]
* |__________________________________| |____________________________________________| |____| |________________________|
* Preamble The 4-Byte AP or Marker Serial Number PT Variable Length Data
*
* Where PT (Packet Type) is a 1-Byte flag indicating the message type. The values are defined as constants
* which are:
*
* - 0x00 - Access Point Update
* - 0x01 - Marker Button Input
* - 0x02 - Marker Battery Level
* - 0x03 - Access Point Error
* - 0x04 - Marker Added (Linked to Access Point)
* - 0x05 - Marker Removed (Removed from Access Point Marker Table)
* - 0x06 - Marker Join Request
* - 0x07 - Access Point Hardware and Firmware Version
*/
var Protocol = /*@__PURE__*/(function (Transform) {
function Protocol() {
Transform.call(this, {
objectMode: true,
writableObjectMode: true
});
}
if ( Transform ) Protocol.__proto__ = Transform;
Protocol.prototype = Object.create( Transform && Transform.prototype );
Protocol.prototype.constructor = Protocol;
/**
* Transforms the incoming byte chunks into one of the defined message types.
*
* @param {*} chunk the byte buffer containing the framed bytes.
* @param {*} encoding the encoding for the bytes if it were in string form, this can be ignored.
* @param {*} cb the callback passed by the
*/
Protocol.prototype._transform = function _transform (chunk, encoding, cb) {
// console.log(chunk);
try {
var data = PARSER.parse(chunk);
return cb(null, data);
} catch (ex) {
console.error(ex);
return cb(ex, null);
}
};
return Protocol;
}(Transform$1));
/**
* Accepts a marker join requests, linking the marker to the connected Access Point.
*
* [0xfd], [0xfd], [0xfd], [0x02], [mk_serial_1], [mk_serial_2],[mk_serial_3],[mk_serial_4]
* |____________________| |____| |______________________________________________________|
* Preamble Flag The 4-Byte Marker Serial Number
*
* @param {*} port the serial port interface.
* @param {*} serial the 4-byte marker serial number.
*/
var MarkerProtocol = /*@__PURE__*/(function (EventEmitter$$1) {
function MarkerProtocol(_debugProd) {
var this$1 = this;
if ( _debugProd === void 0 ) _debugProd = false;
EventEmitter$$1.call(this);
this.acceptMarker = function (port, serial) {
var header = Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x02]);
var payload = Buffer.alloc(4);
payload.writeUInt8(121, 0);
payload.writeUInt8((0x00ff0000 & serial) >> 16, 1);
payload.writeUInt8((0x0000ff00 & serial) >> 8, 2);
payload.writeUInt8(0x000000ff & serial, 3);
var packet = Buffer.concat([header, payload]);
port.write(packet);
};
this.removeMarker = function (port, serial) {
// Remove the marker from our list
//this.markers = this.markers.filter(marker => marker.serial !== serial)
//Remove marker from the AP list
var header = Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x05]);
var payload = Buffer.alloc(4);
payload.writeUInt8(121, 0);
payload.writeUInt8((0x00ff0000 & serial) >> 16, 1);
payload.writeUInt8((0x0000ff00 & serial) >> 8, 2);
payload.writeUInt8(0x000000ff & serial, 3);
var packet = Buffer.concat([header, payload]);
port.write(packet);
};
this.listMarkers = function (port) {
port.write(Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x06]));
};
this.handleApVersionRequest = function (port) {
port.write(Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x04]));
};
this.setAmpStateOn = function (port) {
port.write(Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x01, 0x01]));
};
this.setApChannel = function (port, channel) {
port.write(Buffer.from([0xfd, 0xfd, 0xfd, 0x03, channel]));
};
this.openAccessPoint = function (info) {
var ref = info.filter(function (device) { return device.manufacturer === "FTDI"; });
var usbDetected = ref[0];
var port;
if (usbDetected && usbDetected.path) {
if (fs.existsSync(usbDetected.path)) {
console.log('Receiever detected: ', usbDetected.path);
port = new SerialPort(usbDetected.path, {
baudRate: 115200,
databits: 8,
stopBits: 1,
parity: "none"
});
} else {
console.log('Receiever path detected but failed to open path.');
}
} else if (fs.existsSync("/dev/ttyAMA0")) {
console.log('Receiever detected: /dev/ttyAMA0');
if (!debugProd) {
this$1.debug = false;
}
port = new SerialPort("/dev/ttyAMA0", {
baudRate: 115200,
databits: 8,
stopBits: 1,
parity: "none"
});
} else {
console.log('Receiver not found... Retrying in 10s...');
setTimeout(function () {
SerialPort.list().then(this$1.openAccessPoint).catch(console.error);
}, 10000);
return;
}
var parser = port.pipe(new Framer()).pipe(new Protocol());
parser.on("data", function (packet) {
Object.keys(Packets).forEach(function (key) {
if (Packets[key] === packet.type) {
this$1.debug && console.log(key, packet);
this$1.emit(key, packet);
}
});
});
this$1.on("ACCEPT", function (serial) { return this$1.acceptMarker(port, serial); });
this$1.on("REMOVE", function (serial) { return this$1.removeMarker(port, serial); });
this$1.on("LIST", function () { return this$1.listMarkers(port); });
this$1.on("VERSION", function () { return this$1.handleApVersionRequest(port); });
this$1.on("CHANNEL", function (channel) { return this$1.setApChannel(port, channel); });
};
this.debug = true;
SerialPort.list().then(this.openAccessPoint).catch(console.error);
}
if ( EventEmitter$$1 ) MarkerProtocol.__proto__ = EventEmitter$$1;
MarkerProtocol.prototype = Object.create( EventEmitter$$1 && EventEmitter$$1.prototype );
MarkerProtocol.prototype.constructor = MarkerProtocol; // The function below is the receivers response to a marker join request ie. try to accept
/**
* [0xFD], [0xFD], [0xFD], [0x01], [0x00]
* |____________________| |____| |____|
* Preamble Flag Data
*
* @param {*} port the serial port interface.
*/
MarkerProtocol.prototype.setAmpStateOff = function setAmpStateOff (port) {
port.write(Buffer.from([0xfd, 0xfd, 0xfd, 0x03, 0x01, 0x00]));
};
return MarkerProtocol;
}(EventEmitter));
module.exports = MarkerProtocol;
//# sourceMappingURL=index.js.map

5

package.json
{
"name": "@lessondesk/marker-protocol",
"version": "3.4.2",
"version": "3.5.0",
"description": "Lesson Desk Group marker protocol",

@@ -10,3 +10,3 @@ "source": "src/index.js",

"fmt": "prettier-eslint --write \"src/**/*.js\"",
"build": "microbundle --format=cjs",
"build": "microbundle --format=cjs --target=node",
"test": "node ./index.js"

@@ -39,4 +39,5 @@ },

"events": "^3.0.0",
"serialport": "^8.0.6",
"stream": "0.0.2"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc