@danidoble/webserial-vending-commands
Version:
WebSerial Vending Commands
118 lines (117 loc) • 4 kB
JavaScript
var C = Object.defineProperty;
var c = (l, e, t) => e in l ? C(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
var h = (l, e, t) => c(l, typeof e != "symbol" ? e + "" : e, t);
import { p as m } from "./parser-DRICA5j-.js";
const i = class i {
serialLockerCmdMaker(e) {
let r;
try {
r = new Uint8Array(e.length + 8), r.set(e, 2), r[0] = 2, r[1] = e.length + 4, r[r.length - 2] = 3;
let n = 0;
for (let a = 1; a < e.length; a++)
n += e[a], n *= parseInt(Math.pow(2, a - 1).toString());
r[e.length + 2] = n % 256, r[e.length + 3] = 666 * 3 % 256, r[e.length + 4] = 666 * 8 % 256;
let s = 0;
for (let a = 3; a < e.length + 5; a++)
s += r[a];
r[e.length + 5] = s % 256;
let o = 0;
for (let a = 0; a < r.length - 1; a++)
o ^= r[a];
r[r.length - 1] = o;
} catch (n) {
throw r = new Uint8Array(0), n instanceof Error ? new Error(`Error generating command: ${n.message}`) : new Error("Error generating command: Unknown error");
}
return r;
}
serialLockerHexCmd(e) {
const t = this.serialLockerCmdMaker(e), r = [];
for (let n = 0; n < t.length; n++) {
const o = t[n].toString(16).padStart(2, "0");
r.push(o);
}
return r;
}
validateCell(e = 1) {
let t;
if (typeof e == "string" ? t = parseInt(e) : t = e, isNaN(t) || t < 1 || t > 90) throw new Error("Invalid cell number");
return t;
}
serialLockerGetStatusCellCmd({
cell: e = 1,
channel: t = 1
} = {}) {
return e = this.validateCell(e), this.build(new Uint8Array([16, t, e]));
}
serialLockerGetLightScanCmd({ since: e = 0, until: t = 10, channel: r = 1 } = {}) {
return this.build(new Uint8Array([32, r, e, t]));
}
parseCellToColumnRow(e) {
const t = Math.floor((e - 1) / 10) + 1;
let r = e % 8;
return r === 0 && (r = 8), [t, r];
}
serialLockerGetConfigureCellCmd({
enable: e = !0,
column: t = 0,
row: r = 10,
channel: n = 1
} = {}) {
if (t < 0 || t > 8) throw new Error("Invalid column number");
if (r < 0 || r > 10) throw new Error("Invalid row number");
let s = 1;
return e || (s = 0), this.build(new Uint8Array([48, n, t, r, s]));
}
serialLockerGetOpenCmd({ cell: e = 1, channel: t = 1 } = {}) {
e = this.validateCell(e);
const r = 666, n = r % 256, s = Math.floor(r / 3) % 256;
return this.build(new Uint8Array([64, t, e, n, s]));
}
build(e) {
return i.asHex ? this.serialLockerHexCmd(e) : m(this.serialLockerCmdMaker(e));
}
static connection({ channel: e = 1 } = {}) {
return i.instance.build(new Uint8Array([0, e]));
}
static openCell({ cell: e = 1, channel: t = 1 } = {}) {
return e = i.instance.validateCell(e), i.instance.serialLockerGetOpenCmd({ cell: e, channel: t });
}
static statusCell({ cell: e = 1, channel: t = 1 } = {}) {
return e = i.instance.validateCell(e), i.instance.serialLockerGetStatusCellCmd({ cell: e, channel: t });
}
static lightScan({ since: e = 0, until: t = 10, channel: r = 1 } = {}) {
if (e < 0 || e > 10) throw new Error("Invalid since number");
if (t < 0 || t > 10) throw new Error("Invalid until number");
return i.instance.serialLockerGetLightScanCmd({
since: e,
until: t,
channel: r
});
}
static enableCell({ cell: e = 1, channel: t = 1 } = {}) {
e = i.instance.validateCell(e);
const [r, n] = i.instance.parseCellToColumnRow(e);
return i.instance.serialLockerGetConfigureCellCmd({
enable: !0,
column: r,
row: n,
channel: t
});
}
static disableCell({ cell: e = 1, channel: t = 1 } = {}) {
e = i.instance.validateCell(e);
const [r, n] = i.instance.parseCellToColumnRow(e);
return i.instance.serialLockerGetConfigureCellCmd({
enable: !1,
column: r,
row: n,
channel: t
});
}
};
h(i, "asHex", !1), h(i, "instance");
let u = i;
u.instance = new u();
export {
u as Locker
};