UNPKG

@danidoble/webserial

Version:
775 lines (774 loc) 22.4 kB
import { D as o } from "./devices-CzEcntFZ.js"; import { v as p } from "./webserial-core-Bn6Ker2l.js"; function u(d = 100) { return new Promise((n) => setTimeout(() => n(), d)); } function D() { return "serial" in navigator; } function g() { return "geolocation" in navigator; } function w() { return "crypto" in window; } function M(d = 1) { return d * 1e3; } function y(d) { return d == null || d === ""; } class e { static enable = !1; static instance = null; static #t = null; static #r = 1; static #d() { if (e.enable === !1) throw new Error("Emulator is disabled"); return e.enable; } static #h(n) { return e.instance = n, e.#t = n.typeDevice, e.#r = n.deviceNumber, !0; } static #n(n = null) { return !e.#d() || n === null && e.instance === null ? !1 : (e.instance === null && n !== null && e.#h(n), !0); } static #l() { if (e.#t !== "locker") throw new Error("This function is only available for Locker devices"); return !0; } static #i() { if (e.#t !== "boardroid") throw new Error("This function is only available for Boardroid devices"); return !0; } static #s() { if (e.#t !== "jofemar") throw new Error("This function is only available for Jofemar devices"); return !0; } static #c() { if (e.#t === "locker") throw new Error("This function is not available for Locker devices"); return !0; } // static #withoutBoardroid() { // if (Emulator.#_device_type === 'boardroid') throw new Error(`This function is not available for Boardroid devices`); // // return true; // } // static #withoutJofemar() { // if (Emulator.#_device_type === 'jofemar') throw new Error(`This function is not available for Jofemar devices`); // // return true; // } static #e(n) { e.instance !== null && e.instance.__emulate({ code: n }); } static status(n = null) { if (!e.#n(n)) return !1; let s = []; switch (e.#t) { case "locker": s = ["0", "8"]; break; case "boardroid": s = ["2", (5 + e.#r).toString(16).toUpperCase()]; break; case "jofemar": s = ["6"]; break; default: return !1; } e.#e(s); } static dispensed(n = null) { if (!e.#n(n)) return !1; let s = []; switch (e.#t) { case "locker": s = ["0", "7", "4", "4", "4"]; break; case "boardroid": s = ["2", "D7", "A", "0", "0", "0", "0", "0", "0", "0", "0", "0", "F2"]; break; case "jofemar": s = ["6", "30"]; break; default: return !1; } e.#e(s); } static notDispensed(n = null) { if (!e.#n(n)) return !1; let s = []; switch (e.#t) { case "locker": s = ["0", "7", "5", "5", "5"]; break; case "boardroid": s = ["2", "D7", "A", "0", "0", "1", "0", "0", "0", "0", "0", "0", "F2"]; break; case "jofemar": s = ["6", "34"]; break; default: return !1; } e.#e(s); } static gateInactive(n = null) { if (!e.#n(n) || !this.#l()) return !1; this.#e(["0", "7", "5", "5", "5"]); } static gateConfigured(n = null) { if (!e.#n(n) || !this.#l()) return !1; this.#e(["0", "6"]); } static keyPressed(n = null) { if (!e.#n(n) || !e.#s()) return !1; const s = ["30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "2A", "23", "41", "42", "43", "44"], i = (128 + e.#r).toString(16), t = Math.floor(Math.random() * 15); e.#e(["2", i, "54", s[t]]); } static doorOpened(n = null) { if (!e.#n(n) || !this.#c()) return !1; let s = []; const i = (128 + e.#r).toString(16); switch (e.#t) { case "boardroid": s = ["2", "D8", "dc"]; break; case "jofemar": s = ["2", i, "50", "4F"]; break; } e.#e(s); } static doorClosed(n = null) { if (!e.#n(n) || !this.#c()) return !1; let s = []; const i = (128 + e.#r).toString(16); switch (e.#t) { case "boardroid": s = ["2", "D8", "db"]; break; case "jofemar": s = ["2", i, "50", "43"]; break; } e.#e(s); } static channelDisconnected(n = null) { if (!e.#n(n) || !e.#s()) return !1; const s = (128 + e.#r).toString(16); e.#e(["2", s, "43", "43", "43", "FD"]); } static channelConnected(n = null) { if (!e.#n(n) || !e.#s()) return !1; const s = (128 + e.#r).toString(16); e.#e(["2", s, "43", "43", "43", "FC"]); } static channelEmpty(n = null) { if (!e.#n(n) || !e.#s()) return !1; const s = (128 + e.#r).toString(16); e.#e(["2", s, "43", "43", "43", "FF"]); } static workingTemperature(n = null) { if (!e.#n(n) || !e.#s()) return !1; const s = (128 + e.#r).toString(16); e.#e(["2", s, "43", "54", "16"]); } static currentTemperature(n = null) { if (!e.#n(n) || !e.#c()) return !1; let s = []; const i = (128 + e.#r).toString(16); switch (e.#t) { case "boardroid": s = ["2", "D9", "44", "30"]; break; case "jofemar": s = ["2", i, "43", "74", "2B", "30", "39", "2E", "31", "7F", "43"]; break; } e.#e(s); } static ready(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "30"]); } static busy(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "31"]); } static invalidTray(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "32"]); } static invalidChannel(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "33"]); } static emptyChannel(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "34"]); } static elevatorJam(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "35"]); } static elevatorMalfunction(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "36"]); } static phototransistorFailure(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "37"]); } static allChannelsEmpty(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "38"]); } static productDetectorFailure(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "39"]); } static displayDisconnected(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "41"]); } static productUnderElevator(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "42"]); } static elevatorSettingAlarm(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "43"]); } static buttonPanelFailure(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "44"]); } static errorWritingEeprom(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "45"]); } static errorControlTemperature(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "46"]); } static thermometerDisconnected(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "47"]); } static thermometerMisconfigured(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "48"]); } static thermometerFailure(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "49"]); } static errorExtractorConsumption(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4A"]); } static channelSearchError(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4B"]); } static productExitMouthSearchError(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4C"]); } static elevatorInteriorLocked(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4D"]); } static productDetectorVerifierError(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4E"]); } static waitingForProductRecall(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4F"]); } static productExpiredByTemperature(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "50"]); } static faultyAutomaticDoor(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "51"]); } static rejectLever(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "A0", "1"]); } static resetCoinPurse(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "A0", "2"]); } static #f(n, s = null) { let i = n[Math.floor(Math.random() * 5)]; if (s !== null && !isNaN(parseFloat(s))) switch (s.toString()) { case "0.5": i = n[1]; break; case "1": i = n[2]; break; case "2": i = n[3]; break; case "5": i = n[4]; break; case "10": i = n[5]; break; } return i; } static coinInsertedBox(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["40", "41", "42", "43", "44", "45"], t = e.#f(i, s); e.#e(["2", "A0", t]); } static coinInsertedTube(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["50", "51", "52", "53", "54", "55"], t = e.#f(i, s); e.#e(["2", "A0", t]); } static #a(n, s = null) { let i = n[Math.floor(Math.random() * 4)]; if (s !== null && !isNaN(parseFloat(s))) switch (s.toString()) { case "20": i = n[0]; break; case "50": i = n[1]; break; case "100": i = n[2]; break; case "200": i = n[3]; break; case "500": i = n[4]; break; } return i; } static banknoteInsertedStacker(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["80", "81", "82", "83", "84"], t = e.#a(i, s); e.#e(["2", "B0", t]); } static banknoteInsertedEscrow(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["90", "91", "92", "93", "94"], t = e.#a(i, s); e.#e(["2", "B0", t]); } static banknoteEjected(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["A0", "A1", "A2", "A3", "A4"], t = e.#a(i, s); e.#e(["2", "B0", t]); } static banknoteInsertedRecycler(n = null, s = null) { if (!e.#n(n) || !e.#i()) return !1; const i = ["B0", "B1", "B2", "B3", "B4"], t = e.#a(i, s); e.#e(["2", "B0", t]); } static banknoteTaken(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "B0", "2a"]); } static coinPurseEnabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D0", "1"]); } static coinPurseDisabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D0", "0"]); } static billPurseDisabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D1", "0", "0"]); } static billPurseEnabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D1", "1", "1"]); } static readTubes(n = null) { if (!e.#n(n) || !e.#i()) return !1; const s = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f" ], [i, t, r, a, c, l] = [ s[Math.floor(Math.random() * 30)], s[Math.floor(Math.random() * 30)], s[Math.floor(Math.random() * 30)], s[Math.floor(Math.random() * 30)], s[Math.floor(Math.random() * 30)], s[Math.floor(Math.random() * 30)] ]; e.#e(["2", "D2", i, t, r, a, c, l]); } static readBillPurse(n = null, s = null) { if (!e.#n(n) || !e.#i() || e.instance === null) return !1; const i = e.instance, t = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c" ]; if (i.__banknote_purse.recycler.ict) { const r = t[Math.floor(Math.random() * 31)]; let a = "0", c = "0", l = "0", f = "0", h = "0"; if (s !== null && !isNaN(parseInt(s))) switch (s.toString()) { case "20": a = r; break; case "50": c = r; break; case "100": l = r; break; case "200": f = r; break; case "500": h = r; break; } else switch (i.__banknote_purse.recycler.banknote) { case 0: a = r; break; case 1: c = r; break; case 2: l = r; break; case 3: f = r; break; case 4: h = r; break; } e.#e(["2", "D3", a, c, l, f, h, "0"]); } else { const [r, a, c, l, f, h] = [ t[Math.floor(Math.random() * 30)], t[Math.floor(Math.random() * 30)], t[Math.floor(Math.random() * 30)], t[Math.floor(Math.random() * 2)], t[Math.floor(Math.random())], t[Math.floor(Math.random())] ]; e.#e(["2", "D3", r, a, c, l, f, h]); } } static banknoteAccepted(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D4", "1"]); } static banknoteRejected(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D4", "0"]); } static banknotesDispensed(n = null) { if (!e.#n(n) || !e.#i() || e.instance === null) return !1; const s = e.instance, i = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c" ]; if (s.__banknote_purse.recycler.ict) { const t = i[Math.floor(Math.random() * 30)]; let r = "0", a = "0", c = "0", l = "0", f = "0"; switch (s.__banknote_purse.recycler.banknote) { case 0: r = t; break; case 1: a = t; break; case 2: c = t; break; case 3: l = t; break; case 4: f = t; break; } e.#e(["2", "D5", r, a, c, l, f, "0"]); } else { const [t, r, a, c, l, f] = [ i[Math.floor(Math.random() * 30)], i[Math.floor(Math.random() * 30)], i[Math.floor(Math.random() * 30)], i[Math.floor(Math.random() * 2)], i[Math.floor(Math.random())], i[Math.floor(Math.random())] ]; e.#e(["2", "D5", t, r, a, c, l, f]); } } static coinsDispensed(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "D6"]); } static relayOn(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DA", "1"]); } static relayOff(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DA", "0"]); } static nayaxEnabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "1"]); } static nayaxDisabled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "0"]); } static nayaxPreCreditAuthorized(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "3"]); } static nayaxCancelRequest(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "4"]); } static nayaxSellApproved(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "5"]); } static nayaxSellDenied(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "6"]); } static nayaxEndSession(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "7"]); } static nayaxCancelled(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "8"]); } static nayaxDispensed(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "A", "0"]); } static nayaxNotDispensed(n = null) { if (!e.#n(n) || !e.#i()) return !1; e.#e(["2", "DD", "A", "1"]); } static fullTray(n = null) { if (!e.#n(n) || !e.#s()) return !1; e.#e(["6", "4F"]); } static setConnection(n = null) { if (!e.#n(n) || e.instance === null) return !1; e.instance.__internal__.serial.connected = !0; } } class v extends p { constructor(n = {}) { super(n), this.__internal__ = structuredClone(this.__internal__), this.getResponseAsArrayHex(), this.__internal__.device.door_open = !1, this.__internal__.time.response_engines = 2e3, this.__internal__.time.sense = 100, this.__internal__.interval.waiting_sense = 0, this.__internal__.dispense = { must_response: !1, dispensing: !1, status: null, counter: 0, limit_counter: 20, custom_limit_counter: null, backup_dispense: {} }; } get isDoorOpen() { return this.__internal__.device.door_open; } get isDispensing() { return this.__internal__.interval.waiting_sense || this.__internal__.dispense.dispensing; } async timeout(n, s) { await super.timeout(n, s), s === "dispense" && (this.__internal__.dispense.status = "no-response"); } internalClearSensing() { this.__internal__.interval.waiting_sense && clearInterval(this.__internal__.interval.waiting_sense), this.__internal__.interval.waiting_sense = 0, this.__internal__.dispense.status = null, this.__internal__.dispense.counter = 0, this.__internal__.dispense.dispensing = !1; } internalDispensingProcess() { let n = this.__internal__.dispense.limit_counter; return this.__internal__.dispense.custom_limit_counter && (n = this.__internal__.dispense.custom_limit_counter), n += Math.ceil(n * 0.6), this.__internal__.dispense.counter >= n ? (this.internalClearSensing(), this.__internal__.dispense.status = !1, this.__internal__.dispense.dispensing = !1, !1) : (this.__internal__.dispense.counter = parseFloat((0.1 + this.__internal__.dispense.counter).toFixed(1)), this.__internal__.dispense.counter % 1 === 0 && this.dispatch("dispensing", { status: this.__internal__.dispense.status, counter: this.__internal__.dispense.counter, limit: n }), null); } async internalDispenseStatus() { return this.__internal__.dispense.must_response && (await u(this.__internal__.time.response_engines + 10), this.__internal__.dispense.status === "no-response") ? (this.internalClearSensing(), this.__internal__.dispense.status = !1, this.dispatch("not-dispensed", { reason: "no-response" }), { status: !1, error: "no-response" }) : (this.__internal__.dispense.status = null, this.__internal__.dispense.dispensing = !0, this.dispatch("internal:dispense:running", {}), new Promise((n) => { this.__internal__.interval.waiting_sense = setInterval(() => { switch (this.__internal__.dispense.status) { case null: this.internalDispensingProcess() === !1 && (this.internalClearSensing(), this.dispatch("not-dispensed", { reason: "timeout" }), n({ status: !1, error: "timeout" })); break; case !0: this.internalClearSensing(), this.__internal__.dispense.status = !0, this.dispatch("dispensed", {}), n({ status: !0, error: null }); break; case !1: this.internalClearSensing(), this.__internal__.dispense.status = !1, this.dispatch("not-dispensed", { reason: "no-stock" }), n({ status: !1, error: null }); break; case "elevator-locked": this.internalClearSensing(), this.__internal__.dispense.status = !1, this.dispatch("not-dispensed", { reason: "elevator-locked" }), n({ status: !1, error: "elevator-locked" }); break; case "no-response": this.internalClearSensing(), this.__internal__.dispense.status = !1, this.dispatch("not-dispensed", { reason: "no-response" }), n({ status: !1, error: "no-response" }); break; } }, this.__internal__.time.sense); })); } async internalDispense(n) { if (this.isDispensing) throw new Error("Another dispensing process is running"); if (this.__internal__.dispense.dispensing = !0, !e.enable && !this.__internal__.serial.connected && (await this.serialConnect(), !this.__internal__.serial.connected)) throw this.__internal__.dispense.dispensing = !1, new Error("Serial device not connected"); return this.__internal__.serial.queue.length === 0 ? (await this.appendToQueue(n, "dispense"), await this.internalDispenseStatus()) : new Promise((s) => { const i = setInterval(async () => { if (this.__internal__.serial.queue.length > 0) return; clearInterval(i), await this.appendToQueue(n, "dispense"); const t = await this.internalDispenseStatus(); s(t); }, 100); }); } __emulate(n) { if (typeof n.code != "object") { console.error("Invalid data to make an emulation"); return; } this.__internal__.serial.connected || (this.__internal__.serial.connected = !0, this.dispatch("serial:connected"), o.instance.dispatch("change"), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0)), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0); const s = []; for (const i in n.code) s.push(n.code[i].toString().padStart(2, "0").toLowerCase()); this.serialMessage(s); } /** * @deprecated Use listenOnChannel instead * @param {string|number} channel */ set listenOnPort(n) { this.listenOnChannel = n; } /** * @deprecated Use listenOnChannel instead */ get listenOnPort() { return this.__internal__.device.listen_on_port ?? 1; } fixHexArray(n) { return n.map((s) => typeof s == "string" ? s.padStart(2, "0").toLowerCase() : s.toString(16).padStart(2, "0").toLowerCase()); } } export { e as E, v as K, g as a, D as b, M as g, y as i, w as s, u as w };