UNPKG

aes70

Version:

A controller library for the AES70 protocol.

1,799 lines 160 kB
!(function () { 'use strict'; function e(...e) { try { console.warn(...e); } catch (e) {} } function t(...e) { try { console.error(...e); } catch (e) {} } class n { constructor() { this.event_handlers = new Map(); } emit(e) { const t = this.event_handlers.get(e), n = Array.prototype.slice.call(arguments, 1); t && t.forEach((e) => { try { e.apply(this, n); } catch (t) { console.warn('ERROR when calling %o: %o', e, t); } }); } on(e, t) { let n = this.event_handlers.get(e); n || this.event_handlers.set(e, (n = new Set())), n.add(t); } addEventListener(e, t) { this.on(e, t); } removeEventListener(e, t) { const n = this.event_handlers.get(e); if (!n || !n.has(t)) throw new Error('removeEventListeners(): not installed.'); n.delete(t); } removeAllEventListeners() { this.event_handlers.clear(); } } class r { get messageType() { return this.constructor.messageType; } } class s { constructor(e, t) { (this.encoders = e), (this.data = t), (this.byteLength = (function (e, t) { let n = 0; for (let r = 0; r < e.length; r++) n += e[r].encodedLength(t[r]); return n; })(e, t)), (this.buffer = (function (e, t, n) { if (!n) return null; const r = new ArrayBuffer(n), s = new DataView(r); for (let n = 0, r = 0; n < e.length; n++) r = e[n].encodeTo(s, r, t[n]); return r; })(e, t, this.byteLength)); } encodeTo(e, t) { t |= 0; const { byteLength: n, buffer: r } = this; if (!n) return t; const s = new Uint8Array(r); return ( new Uint8Array(e.buffer, e.byteOffset, e.byteLength).set(s, t), t + n ); } } class o extends r { constructor(e, t, n, r, s) { super(), (this.target = +e), (this.method_level = 0 | t), (this.method_index = 0 | n), (this.param_count = 0 | r), (this.parameters = s || null), (this.handle = 0); } static get messageType() { return 0; } encode_to(e, t) { if ( ((t |= 0), e.setUint32(t, this.encoded_length()), (t += 4), e.setUint32(t, this.handle), (t += 4), e.setUint32(t, this.target), (t += 4), e.setUint16(t, this.method_level), (t += 2), e.setUint16(t, this.method_index), (t += 2), e.setUint8(t, this.param_count), t++, this.param_count) ) { const n = this.parameters; n instanceof s ? (t = n.encodeTo(e, t)) : (new Uint8Array(e.buffer).set(new Uint8Array(n), e.byteOffset + t), (t += n.byteLength)); } return t; } encoded_length() { return 17 + (this.param_count ? this.parameters.byteLength : 0); } decode_from(e, t, n) { let r = e.getUint32(t); if ( ((t += 4), (this.handle = e.getUint32(t)), (t += 4), (this.target = e.getUint32(t)), (t += 4), (this.method_level = e.getUint16(t)), (t += 2), (this.method_index = e.getUint16(t)), (t += 2), (this.param_count = e.getUint8(t)), t++, (r -= 17), r < 0) ) throw new Error('Bad Command Length.'); if (r > 0) { if (!this.param_count) throw new Error('Expected no parameter bytes.'); (this.parameters = e.buffer.slice( e.byteOffset + t, e.byteOffset + t + r )), (t += r); } return t; } response(e, t, n) { return new Response(this.handle, e, t, n); } } class i extends o { static get messageType() { return 1; } } function a(e) { if (!e.isConstantLength) return e; const t = e.encodedLength(), n = e.decode, r = e.encodeTo; return { isConstantLength: !0, encodedLength: e.encodedLength, encodeTo: r, decode: n, decodeFrom: function (e, r) { const s = n(e, r); return [r + t, s]; }, decodeLength: function (e, n) { return n + t; }, }; } const c = a({ isConstantLength: !0, encodedLength: function (e) { return 2; }, encodeTo: function (e, t, n) { return e.setUint16(t, 0 | n, !1), t + 2; }, decode: function (e, t) { return e.getUint16(t, !1); }, }); function l(e, t) { const n = Object.keys(e).length; return ( t || (t = class { constructor(...t) { if (t.length === n) { let n = 0; for (const r in e) Object.prototype.hasOwnProperty.call(e, r) && (this[r] = t[n++]); } else { if (1 !== t.length || 'object' != typeof t[0]) throw new TypeError('Unexpected arguments.'); { const n = t[0]; for (const t in e) Object.prototype.hasOwnProperty.call(e, t) && (this[t] = n[t]); } } } }), a({ type: t, isConstantLength: !1, encodedLength: function (t) { let n = 0; for (const r in e) { if (!Object.prototype.hasOwnProperty.call(e, r)) continue; n += e[r].encodedLength(t[r]); } return n; }, encodeTo: function (t, n, r) { for (const s in e) { if (!Object.prototype.hasOwnProperty.call(e, s)) continue; n = e[s].encodeTo(t, n, r[s]); } return n; }, decodeFrom: function (n, r) { const s = new Array(e.length); let o = 0; for (const t in e) { if (!Object.prototype.hasOwnProperty.call(e, t)) continue; const i = e[t]; let a; ([r, a] = i.decodeFrom(n, r)), (s[o++] = a); } return [r, new t(...s)]; }, decodeLength: function (t, n) { for (const r in e) { if (!Object.prototype.hasOwnProperty.call(e, r)) continue; n = e[r].decodeLength(t, n); } return n; }, }) ); } class u { constructor(e, t) { (this.DefLevel = e), (this.EventIndex = t); } } const h = l({ DefLevel: c, EventIndex: c }, u), d = a({ isConstantLength: !0, encodedLength: function (e) { return 4; }, encodeTo: function (e, t, n) { return e.setUint32(t, n, !1), t + 4; }, decode: function (e, t) { return e.getUint32(t, !1); }, }); class g { constructor(e, t) { (this.EmitterONo = e), (this.EventID = t); } } const f = l({ EmitterONo: d, EventID: h }, g); class p extends r { constructor(e, t, n, r, s, o, i) { super(), (this.target = e), (this.method_level = 0 | t), (this.method_index = 0 | n), (this.context = r), (this.event = s), (this.param_count = 0 | o), (this.parameters = i || null); } static get messageType() { return 2; } encode_to(e, t) { e.setUint32(t, this.encoded_length()), (t += 4), e.setUint32(t, this.target), (t += 4), e.setUint16(t, this.method_level), (t += 2), e.setUint16(t, this.method_index), (t += 2), e.setUint8(t, this.param_count), t++; const n = this.context; if (n) { const r = n.byteLength; e.setUint16(t, r), (t += 2), r > 0 && (new Uint8Array(e.buffer).set( new Uint8Array(this.context), e.byteOffset + t ), (t += r)); } else e.setUint16(t, 0), (t += 2); return ( e.setUint32(t, this.event.EmitterONo), (t += 4), e.setUint16(t, this.event.EventID.DefLevel), (t += 2), e.setUint16(t, this.event.EventID.EventIndex), (t += 2), this.param_count > 1 && (this.parameters instanceof s ? (t = this.parameters.encodeTo(e, t)) : (new Uint8Array(e.buffer).set( new Uint8Array(this.parameters), e.byteOffset + t ), (t += this.parameters.byteLength))), t ); } encoded_length() { return ( 23 + (this.param_count > 1 ? this.parameters.byteLength : 0) + (this.context ? this.context.byteLength : 0) ); } decode_from(e, t, n) { let r = e.getUint32(t); (t += 4), (this.target = e.getUint32(t)), (t += 4), (this.method_level = e.getUint16(t)), (t += 2), (this.method_index = e.getUint16(t)), (t += 2), (this.param_count = e.getUint8(t)), t++; const s = e.getUint16(t); let o; if ( ((t += 2), s ? ((this.context = e.buffer.slice( e.byteOffset + t, e.byteOffset + t + s )), (t += s)) : (this.context = null), ([t, o] = f.decodeFrom(e, t)), (this.event = o), (r -= 23 + s), r < 0) ) throw new Error('Bad Notification Length.'); return ( r > 0 && ((this.parameters = e.buffer.slice( e.byteOffset + t, e.byteOffset + t + r )), (t += r)), t ); } } class m extends r { constructor(e, t, n, r) { super(), (this.handle = e), (this.status_code = 0 | t), (this.param_count = 0 | n), (this.parameters = r || null); } static get messageType() { return 3; } encoded_length() { return 10 + (this.param_count ? this.parameters.byteLength : 0); } decode_from(e, t, n) { let r = e.getUint32(t); if ( ((t += 4), (this.handle = e.getUint32(t)), (t += 4), (this.status_code = e.getUint8(t)), t++, (this.param_count = e.getUint8(t)), t++, (r -= 10), r < 0) ) throw new Error('Bad Response length.'); if (r > 0) { if (!this.param_count) throw new Error( 'Decoding response with parameterCount=0 but %o bytes of parameters', r ); (this.parameters = e.buffer.slice( e.byteOffset + t, e.byteOffset + t + r )), (t += r); } return t; } encode_to(e, t) { return ( e.setUint32(t, this.encoded_length()), (t += 4), e.setUint32(t, this.handle), (t += 4), e.setUint8(t, this.status_code), t++, e.setUint8(t, this.param_count), t++, this.param_count && (this.parameters instanceof s ? (t = this.parameters.encodeTo(e, t)) : (new Uint8Array(e.buffer).set( new Uint8Array(this.parameters), e.byteOffset + t ), (t += this.parameters.byteLength))), t ); } } class S extends r { static get messageType() { return 4; } constructor(e) { super(), (this.time = e || 0); } decode_from(e, t, n) { if (4 == n) (this.time = e.getUint32(t)), (t += 4); else { if (2 != n) throw new Error('Bad keepalive timeout length.'); (this.time = 1e3 * e.getUint16(t)), (t += 2); } return t; } encode_to(e, t) { return ( this.time % 1e3 ? (e.setUint32(t, this.time), (t += 4)) : (e.setUint16(t, this.time / 1e3), (t += 2)), t ); } encoded_length() { return this.time % 1e3 ? 4 : 2; } } const y = [o, i, p, m, S]; function b(e, t, n) { if (e.byteLength < e.byteOffset + t + 10) return -1; if (((t |= 0), 59 != e.getUint8(t))) throw new Error('Bad sync value.'); t++, (t += 2); const r = e.getUint32(t); t += 4; const s = e.getUint8(t); t++; const o = e.getUint16(t); t += 2; const i = e.byteOffset + t - 9 + r; if (i > e.byteLength) return -1; n.length = o; const a = y[s]; if (void 0 === a) throw new Error('Bad Message Type'); if (a === S && 1 !== o) throw new Error('Bad KeepAlive message count.'); for (let r = 0; r < o; r++) (n[r] = new a()), (t = n[r].decode_from(e, t, i - e.byteOffset - t)); if (t != i) throw new Error('Decode error: ' + t + ' vs ' + i); return t; } function O(e, t, n, r, s) { r || (r = 0), s || (s = n.length); if (!(s - r <= 65535)) throw new Error('Too many PDUs.'); e.setUint8(t, 59); const o = (t += 1); e.setUint16(t, 1); const i = (t += 2); (t += 4), e.setUint8(t, n[r].messageType), t++, e.setUint16(t, s - r), (t += 2); for (let o = r; o < s; o++) t = n[o].encode_to(e, t); return e.setUint32(i, t - o), t; } function w(e) { Array.isArray(e) || (e = [e]); const t = (function (e) { let t = 10; const n = e[0].messageType; for (let r = 0; r < e.length; r++) { const s = e[r]; if (s.messageType != n) throw new Error('Cannot combine different types in one message.'); t += s.encoded_length(); } return t; })(e), n = new ArrayBuffer(t); if (O(new DataView(n), 0, e, 0, e.length) != t) throw new Error('Message length mismatch.'); return n; } class C { constructor(e, t) { if (!(e <= 4294967295)) throw new TypeError('Invalid batch size.'); (this._pdus = []), (this._batchSize = e), (this._resultCallback = t), (this._currentSize = 0), (this._currentCount = 0), (this._lastMessageType = -1), (this._flushScheduled = !1), (this._flushCb = () => { (this._flushScheduled = !1), null !== this._pdus && this.flush(); }); } add(e) { const t = this._currentSize, n = e.encoded_length(), r = e.messageType, s = this._lastMessageType === r && 4 !== r && this._currentCount < 65535; let o = n; s || (o += 10), t && t + o > this._batchSize && (this.flush(), (o = n + 10)), this._pdus.push(e), (this._currentSize += o), s ? this._currentCount++ : (this._currentCount = 1), (this._lastMessageType = r), this._currentSize > this._batchSize ? this.flush() : 1 === this._pdus.length && this.scheduleFlush(); } scheduleFlush() { this._flushScheduled || ((this._flushScheduled = !0), Promise.resolve() .then(this._flushCb) .catch((e) => { console.error(e); })); } flush() { if (!this._currentSize) return; const e = this._pdus, t = new ArrayBuffer(this._currentSize), n = new DataView(t), r = e.length; for (let t = 0, s = 0, o = 0; t < r; t++) { const i = e[t].messageType; (t !== r - 1 && t + 1 - s != 65535 && 4 !== i && i === e[t + 1].messageType) || ((o = O(n, o, e, s, t + 1)), (s = t + 1)); } (this._currentSize = 0), (this._lastMessageType = -1), (this._currentCount = 0), (this._pdus.length = 0), this._resultCallback(t); } dispose() { this._pdus = null; } } class _ extends n { constructor(e) { e || (e = {}), super(); const t = this._now(); this.options = e; const n = e.batch >= 0 ? e.batch : 65536; (this._message_generator = new C(n, (e) => this.write(e))), (this.inbuf = null), (this.inpos = 0), (this.last_rx_time = t), (this.last_tx_time = t), (this.rx_bytes = 0), (this.tx_bytes = 0), (this.keepalive_interval = -1), (this._keepalive_interval_id = null); const r = () => { this.removeEventListener('close', r), this.removeEventListener('error', r), this.cleanup(); }; this.on('close', r), this.on('error', r); } get is_reliable() { return !0; } send(e) { if (this.is_closed()) throw new Error('Connection is closed.'); this._message_generator.add(e); } tx_idle_time() { return this._now() - this.last_tx_time; } rx_idle_time() { return this._now() - this.last_rx_time; } read(e) { if ( ((this.rx_bytes += e.byteLength), (this.last_rx_time = this._now()), this.inbuf) ) { const t = this.inbuf.byteLength - this.inpos, n = new Uint8Array(new ArrayBuffer(t + e.byteLength)); n.set(new Uint8Array(this.inbuf, this.inpos)), n.set(new Uint8Array(e), t), (this.inbuf = null), (this.inpos = 0), (e = n.buffer); } let t = 0; const n = new DataView(e); try { do { const r = [], s = b(n, t, r); if (-1 == s) { (this.inbuf = e), (this.inpos = t); break; } (t = s), this.incoming(r); } while (t < e.byteLength); } catch (e) { if (this.is_reliable) return void this.emit('error', e); console.error(e); } this._check_keepalive(); } incoming(e) {} write(e) { (this.last_tx_time = this._now()), (this.tx_bytes += e.byteLength); } is_closed() { return null === this._message_generator; } close() { this.is_closed() || this.emit('close'); } error(e) { this.is_closed() || this.emit('error', e); } cleanup() { if (this.is_closed()) throw new Error('cleanup() called twice.'); this.set_keepalive_interval(0), this._message_generator.dispose(), (this._message_generator = null), this.removeAllEventListeners(); } _check_keepalive() { if (!(this.keepalive_interval > 0)) return; const e = this.keepalive_interval; this.rx_idle_time() > 3 * e ? (this.emit('timeout'), this.error(new Error('Keepalive timeout.'))) : this.tx_idle_time() > 0.75 * e && (this.flush(), this.tx_idle_time() > 0.75 * e && this.send(new S(e))); } flush() { this._message_generator.flush(); } set_keepalive_interval(e) { const t = 1e3 * e; null !== this._keepalive_interval_id && (clearInterval(this._keepalive_interval_id), (this._keepalive_interval_id = null)), (this.keepalive_interval = t), this.is_closed() || (this.send(new S(t)), t > 0 && (this._keepalive_interval_id = setInterval(() => { this._check_keepalive(); }, t / 2))); } } class P extends Error { constructor(e, t) { super('Call failed with OcaStatus ' + e.name), (this.name = 'aes70.RemoteError'), (this.status = e), (this.cmd = t); } static check_status(e, t) { return e instanceof this && e.status === t; } } class v { constructor(e) { this.values = e; } item(e) { return this.values[e]; } get length() { return this.values.length; } [Symbol.iterator]() { return this.values[Symbol.iterator](); } } function G(e, t) { return Object.prototype.hasOwnProperty.call(e, t); } function D(e) { let t = null; function n(n) { if (null === t) { t = new Map(); for (const n in e) G(e, n) && t.set(e[n], n); } return t.get(n); } let r = null; const s = class { get isEnum() { return !0; } constructor(t) { if ('string' == typeof t) { if (!G(e, t)) throw new Error('No such enum value.'); return this.constructor[t]; } if (null !== r && r.has(t)) return r.get(t); (this.value = t), (function (e, t) { null === r && (r = new Map()), r.set(e, t); })(t, this); } get name() { return n(this.value); } valueOf() { return this.value; } toString() { return this.name; } static getName(e) { const t = n(e); if (void 0 === t) throw new Error('No such enum value.'); return t; } static getValue(t) { const n = (function (t) { if (G(e, t)) return e[t]; })(t); if (void 0 === n) throw new Error('No such enum value.'); return n; } static values() { return e; } }; for (const t in e) G(e, t) && Object.defineProperty(s, t, { get: function () { return new this(e[t]); }, enumerable: !1, configurable: !0, }); return s; } class T extends D({ OK: 0, ProtocolVersionError: 1, DeviceError: 2, Locked: 3, BadFormat: 4, BadONo: 5, ParameterError: 6, ParameterOutOfRange: 7, NotImplemented: 8, InvalidRequest: 9, ProcessingFailed: 10, BadMethod: 11, PartiallySucceeded: 12, Timeout: 13, BufferOverflow: 14, }) {} class M { get handle() { return this.command.handle; } constructor(e, t, n, r, s) { (this.resolve = e), (this.reject = t), (this.returnTypes = n), (this.command = r), (this.stack = s), (this.lastSent = 0), (this.retries = 0); } handleError(e) { this.stack && e instanceof Error && (e.stack = this.stack), this.reject(e); } response(e) { const { resolve: t, reject: n, returnTypes: r, command: s } = this; if (0 !== e.status_code) { const t = new P(new T(e.status_code), s); this.handleError(t); } else if (r) try { const n = Math.min(e.param_count, r.length); if (0 === n) t(); else { const s = new Array(n), o = new DataView(e.parameters); for (let e = 0, t = 0; e < n; e++) { let n; ([t, n] = r[e].decodeFrom(o, t)), (s[e] = n); } t(1 === n ? s[0] : new v(s)); } } catch (e) { n(e); } else t(e); } } function I(e) { const t = e.EmitterONo, n = e.EventID; return [t, n.DefLevel, n.EventIndex].join(','); } class A extends _ { constructor(e) { super(e), (this._pendingCommands = new Map()), (this._nextCommandHandle = 0), (this._subscribers = new Map()); } cleanup() { super.cleanup(), (this._subscribers = null); const e = this._pendingCommands; this._pendingCommands = null; const t = new Error('closed'); e.forEach((e, n) => { e.handleError(structuredClone(t)); }); } _addSubscriber(e, t) { const n = I(e), r = this._subscribers; if (r.has(n)) throw new Error('Subscriber already exists.'); r.set(n, t); } _removeSubscriber(e) { const t = I(e), n = this._subscribers; if (!n.has(t)) throw new Error('Unknown subscriber.'); n.delete(t); } _getNextCommandHandle() { let e; const t = this._pendingCommands; if (null === t) throw new Error('Connection not open.'); do { (e = this._nextCommandHandle), (this._nextCommandHandle = (e + 1) | 0); } while (t.has(e)); return e; } _estimate_next_tx_time() { return this._now(); } send_command(e, t, n, r) { const s = (n, s) => { const o = this._getNextCommandHandle(); e.handle = o; const i = new M(n, s, t, e, r); this._pendingCommands.set(o, i), (i.lastSent = this._estimate_next_tx_time()), this.send(e); }; if (!n) return new Promise(s); s( (e) => n(!0, e), (e) => n(!1, e) ); } _removePendingCommand(e) { const t = this._pendingCommands, n = t.get(e); return n ? (t.delete(e), n) : null; } incoming(e) { for (let t = 0; t < e.length; t++) { if (null === this._pendingCommands) return; const n = e[t]; if (n instanceof m) { const e = this._removePendingCommand(n.handle); if (null === e) { if (this.is_reliable) return void this.error(new Error('Unknown handle.')); continue; } e.response(n); } else if (n instanceof p) { const e = this._subscribers, t = I(n.event), r = e.get(t); if (!r) continue; r(n); } else { if (!(n instanceof S)) throw new Error('Unexpected PDU'); if (!(n.time > 0)) throw new Error('Bad keepalive timeout.'); } } } } const L = a({ isConstantLength: !1, encodedLength: function (e) { if ( (e instanceof ArrayBuffer && (e = new Uint8Array(e)), !(Array.isArray(e) || e instanceof Uint8Array)) ) throw new TypeError('Expected Array or Uint8Array'); const t = e.length; if (t > 65535) throw new Error('Array too long for OcaBlob OCP.1 encoding.'); return 2 + t; }, encodeTo: function (e, t, n) { n instanceof ArrayBuffer && (n = new Uint8Array(n)); const r = n.length; e.setUint16(t, r), (t += 2); return new Uint8Array(e.buffer, e.byteOffset).set(n, t), t + r; }, decodeFrom: function (e, t) { const n = e.getUint16(t); return [(t += 2) + n, new Uint8Array(e.buffer, e.byteOffset + t, n)]; }, decodeLength: function (e, t) { return t + 2 + e.getUint16(t); }, }); function E(e) { return a({ isConstantLength: !0, encodedLength: function (t) { return e; }, encodeTo: function (t, n, r) { if (!(Array.isArray(r) || r instanceof Uint8Array)) throw new TypeError('Expected Array or Uint8Array'); if (r.length !== e) throw new Error('Length mismatch.'); return new Uint8Array(t.buffer, t.byteOffset).set(r, n), n + e; }, decode: function (t, n) { return new Uint8Array(t.buffer, t.byteOffset + n, e); }, }); } const k = a({ isConstantLength: !0, encodedLength: function (e) { return 1; }, encodeTo: function (e, t, n) { return e.setUint8(t, n ? 1 : 0), t + 1; }, decode: function (e, t) { return 0 !== e.getUint8(t); }, }), x = c, N = x; function U(e) { return ArrayBuffer.isView(e) && !(e instanceof DataView); } function R(e) { return e.isConstantLength ? (function (e) { const t = e.encodedLength(void 0), n = e.encodeTo, r = e.decode; return a({ isConstantLength: !1, encodedLength: function (e) { if (!Array.isArray(e) && !U(e)) throw new TypeError('Expected array.'); const n = e.length; if (n > 65535) throw new Error('Array too long for OcaList OCP.1 encoding'); return 2 + n * t; }, encodeTo: function (e, t, r) { const s = r.length; e.setUint16(t, s), (t += 2); for (let o = 0; o < s; o++) t = n(e, t, r[o]); return t; }, decodeFrom: function (e, n) { const s = e.getUint16(n); n += 2; const o = new Array(s); for (let i = 0; i < s; i++) (o[i] = r(e, n)), (n += t); return [n, o]; }, decodeLength: function (e, n) { return n + (2 + e.getUint16(n) * t); }, }); })(e) : (function (e) { const t = e.encodedLength, n = e.encodeTo, r = e.decodeFrom, s = e.decodeLength; return a({ isConstantLength: !1, encodedLength: function (e) { if (!Array.isArray(e) && !U(e)) throw new TypeError('Expected array.'); const n = e.length; if (n > 65535) throw new Error('Array too long for OcaList OCP.1 encoding'); let r = 2; for (let s = 0; s < n; s++) r += t(e[s]); return r; }, encodeTo: function (e, t, r) { const s = r.length; e.setUint16(t, s), (t += 2); for (let o = 0; o < s; o++) t = n(e, t, r[o]); return t; }, decodeFrom: function (e, t) { const n = e.getUint16(t); t += 2; const s = new Array(n); for (let o = 0; o < n; o++) { let n; ([t, n] = r(e, t)), (s[o] = n); } return [t, s]; }, decodeLength: function (e, t) { const n = e.getUint16(t); t += 2; for (let r = 0; r < n; r++) t = s(e, t); return t; }, }); })(e); } const F = new TextEncoder(), B = new TextDecoder(); function V(e) { return F.encode(e); } function j(e, t, n) { const r = t; for (; n--; ) { const n = e.getUint8(t); if ((t++, !(n <= 127))) { if (n < 194) throw new Error('Invalid UTF8 sequence.'); if ((t++, !(n <= 223 || (t++, n <= 239 || (t++, n <= 244))))) throw new Error('Invalid UTF8 sequence.'); } } return t - r; } const z = a({ isConstantLength: !1, encodedLength: function (e) { if ('string' != typeof e) throw new TypeError('Expected string.'); return 2 + V(e).byteLength; }, encodeTo: function (e, t, n) { const r = (function (e) { let t = 0; for (let n = 0; n < e.length; n++, t++) { const t = e.charCodeAt(n); if (t >= 55296 && t <= 56319) { n++; const t = e.charCodeAt(n); if (t < 56320 || t > 57343) throw new TypeError('Expected valid unicode string.'); } } return t; })(n); if (r > 65535) throw new Error('String too long for OcaString OCP.1 encoding.'); e.setUint16(t, r), (t += 2); const s = new Uint8Array(V(n)); return new Uint8Array(e.buffer, e.byteOffset).set(s, t), t + s.length; }, decodeFrom: function (e, t) { const n = e.getUint16(t), r = j(e, (t += 2), n), s = new Uint8Array(e.buffer, e.byteOffset + t, r); return [t + r, ((o = s), B.decode(o))]; var o; }, decodeLength: function (e, t) { const n = e.getUint16(t); return (t += 2) + j(e, t, n); }, }), q = a({ isConstantLength: !1, encodedLength: function (e) { if ('string' != typeof e) throw new TypeError('Expected string.'); const t = e.length; if (t > 65535) throw new Error('Array too long for String16 OCP.1 encoding.'); return 2 + 2 * t; }, encodeTo: function (e, t, n) { const r = n.length; e.setUint16(t, r, !1), (t += 2); for (let s = 0; s < r; s++, t += 2) e.setUint16(t, n.charCodeAt(s), !1); return t; }, decodeFrom: function (e, t) { const n = e.getUint16(t, !1); t += 2; const r = new Array(n); for (let s = 0; s < n; s++, t += 2) r[s] = e.getUint16(t, !1); return [t, String.fromCharCode.apply(String, r)]; }, decodeLength: function (e, t) { return t + 2 + 2 * e.getUint16(t); }, }); class H { constructor(e, t, n, r) { (this.ObjectNumber = e), (this.Name = t), (this.ClassID = n), (this.ClassVersion = r); } } const W = l({ ObjectNumber: d, Name: z, ClassID: q, ClassVersion: c }, H); class K { constructor(e, t, n) { (this.Manufacturer = e), (this.Name = t), (this.Version = n); } } const $ = l({ Manufacturer: z, Name: z, Version: z }, K); class X { constructor(e, t, n) { (this.Reserved = e), (this.MfrCode = t), (this.ModelCode = n); } } const Y = l({ Reserved: E(1), MfrCode: E(3), ModelCode: E(4) }, X), Q = a({ isConstantLength: !0, encodedLength: function (e) { return 1; }, encodeTo: function (e, t, n) { return e.setUint8(t, 0 | n), t + 1; }, decode: function (e, t) { return e.getUint8(t); }, }); function J(e, t) { const n = t.encodeTo, r = t.decode, s = a({ isConstantLength: !0, encodedLength: t.encodedLength, encodeTo: function (t, r, s) { if ('object' == typeof s && s instanceof e) s = s.value; else if ('string' == typeof s) s = e.getValue(s); else { if ('number' != typeof s) throw new TypeError('Unsupported type.'); e.getName(s); } return n(t, r, s); }, decode: function (t, n) { const s = r(t, n); return new e(s); }, }); for (const t in e.values()) Object.defineProperty(s, t, { get: function () { return e[t]; }, enumerable: !1, configurable: !0, }); return s; } function Z(e) { return J(e, Q); } class ee extends D({ PowerOn: 0, InternalError: 1, Upgrade: 2, ExternalRequest: 3, }) {} const te = Z(ee); class ne { constructor(e, t, n) { (this.object = e), (this.id = t), (this.handlers = new Set()), (this.result = null), (this.argumentTypes = n); } GetOcaEvent() { return new g(this.object.ObjectNumber, this.id); } do_subscribe() {} do_unsubscribe() {} subscribe(e) { return ( this.handlers.add(e), 1 === this.handlers.size ? (this.result = this.do_subscribe().then( () => ((this.result = null), !0) )) : null !== this.result ? this.result : Promise.resolve(!0) ); } unsubscribe(e) { return ( this.handlers.delete(e), this.handlers.size || this.do_unsubscribe().catch(function () {}), Promise.resolve(!0) ); } Dipose() { this.handlers.size && this.do_unsubscribe().catch(function () {}), this.handlers.clear(); } } const re = new ArrayBuffer(); class se extends ne { constructor(e, n, r) { super(e, n, r), (this.callback = (e) => { if (!this.handlers.size) return; const n = new Array(r.length), s = new DataView(e.parameters || re); for (let e = 0, t = 0; t < r.length; t++) { let o; ([e, o] = r[t].decodeFrom(s, e)), (n[t] = o); } const o = this.object; this.handlers.forEach(function (e) { try { e.apply(o, n); } catch (e) { t(e); } }); }); } do_subscribe() { return this.object.device.add_subscription( this.GetOcaEvent(), this.callback ); } do_unsubscribe(e) { return this.object.device.remove_subscription( this.GetOcaEvent(), this.callback ); } } class oe extends ne { constructor(e, n, r) { super(e, n, r), (this.callback = ([n, s, o]) => { if ( n.DefLevel !== this.id.DefLevel || n.PropertyIndex !== this.id.PropertyIndex ) return; const i = r[0].decodeFrom(s, 0)[1]; this.handlers.forEach(function (r) { try { r.call(e, i, o, n); } catch (e) { t(e); } }); }); } do_subscribe() { return this.object.OnPropertyChanged.subscribe(this.callback); } do_unsubscribe(e) { return this.object.OnPropertyChanged.unsubscribe(this.callback); } } class ie extends D({ CurrentChanged: 1, MinChanged: 2, MaxChanged: 3, ItemAdded: 4, ItemChanged: 5, ItemDeleted: 6, }) {} class ae { init(e) { (this.o = e), (this.values = []), (this.synchronized = !1), (this.subscriptions = []); } sync() { if (this.synchronized) return Promise.resolve(); let e = 0; const t = []; return ( this.o.get_properties().forEach((n) => { const r = n.getter(this.o); if (!r) return; const s = n.event(this.o); if (s) { const n = function (e, t, n) { n === ie.CurrentChanged && (this.values[e] = t); }.bind(this, e); this.subscriptions.push(s.unsubscribe.bind(s, n)), t.push(s.subscribe(n).catch(function () {})); } t.push( r().then( function (e, t) { t instanceof v && (t = t.item(0)), (this.values[e] = t); }.bind(this, e), function () {} ) ), e++; }), Promise.all(t) ); } forEach(e, t) { let n = 0; t || (t = this), this.o.get_properties().forEach((r) => { r.getter(this.o) && (e.call(t, this.values[n], r.name), n++); }); } Dispose() { (this.o = null), this.subscriptions.forEach((e) => e()), (this.subscriptions = null); } } class ce { constructor(e, t) { (this.DefLevel = e), (this.PropertyIndex = t); } } class le { constructor(e, t, n) { const r = new Map(), s = []; (this.by_name = r), (this.parent = n), (this.properties = s), (this.level = t); for (let t = 0; t < e.length; t++) { const n = e[t]; if ((r.set(n.name, n), (s[n.index] = n), n.aliases)) { const e = n.aliases; for (let t = 0; t < e.length; t++) r.set(e[t], n); } } } find_property(e) { if (e instanceof ce) { if (e.DefLevel == this.level) return this.properties[e.PropertyIndex]; if (this.parent) return this.parent.find_property(e); } else { if ('string' != typeof e) throw new Error('Expected PropertyID'); { const t = this.by_name.get(e); if (t) return t; if (this.parent) return this.parent.find_property(e); } } } find_name(e) { const t = this.find_property(e); if (t) return t.name; } forEach(e, t) { const n = this.parent ? this.parent.forEach(e, t) : [], r = this.properties; for (let s = 0; s < r.length; s++) { const o = r[s]; void 0 !== o && n.push(e.call(t, o)); } return n; } } class ue { constructor(e, t, n, r, s, o, i, a) { (this.name = e), (this.type = t), (this.level = n), (this.index = r), (this.readonly = s), (this.static = o), (this.aliases = i), i || a || o || (a = { get: 'Get' + e }), (this.accessors = a); } GetPropertyID() { return new ce(this.level, this.index); } GetName() { return this.name; } getter(e, t) { let n = this.name, r = 0; const s = this.aliases, o = this.accessors; if (o) { const n = o.get; if (!n) return null; let r; if ('string' == typeof n) r = e[n]; else { if ('object' != typeof n) throw new Error('Unexpected accessor.'); { const { name: t, index: s } = n; r = s >= 0 ? function (e) { if ('function' != typeof e) return this[t]().then((e) => e.item(s)); this[t]((t, n) => { t && (n = n.item(s)), e(t, n); }); } : e[t]; } } return r ? (t ? r : r.bind(e)) : null; } if (this.static) { const t = e.constructor[n]; if (void 0 !== t) return function () { return Promise.resolve(t); }; } else { const r = e['Get' + n]; if (r) return t ? r : r.bind(e); } return s && r < s.length && (n = s[r++]), null; } setter(e, t) { if (this.readonly || this.static) return null; let n = this.name, r = 0; const s = this.aliases; { const o = e['Set' + n]; if (o) return t ? o : o.bind(e); s && r < s.length && (n = s[r++]); } return null; } event(e) { let t = this.name, n = 0; const r = this.aliases; { const s = e['On' + t + 'Changed']; if (s) return s; r && n < r.length && (t = r[n++]); } return null; } subscribe(e, n) { const r = this.event(e), s = this.getter(e); return r && r.subscribe(n).catch(t), s && s().then(n, t), r || !!s; } } function he(e, t) { if (!t || !t.length) return; const [n, r, o, a, c, l] = t; (e.prototype[n] = function (...e) { const t = a.length; let n = null; t < e.length && t + 1 === e.length && 'function' == typeof e[t] && ((n = e[t]), (e.length = t)); const l = new i(this.ono, r, o, t, new s(a, e)); return this.device.send_command(l, c, n); }), l && l.forEach((t) => { e.prototype[t] = function (...e) { return this[n](...e); }; }); } function de(e, t) { const [n, r, s, o] = t; Object.defineProperty(e.prototype, 'On' + n, { get: function () { const e = '_On' + n, t = this[e]; return t || (this[e] = new se(this, new u(r, s), o)); }, }); } function ge(e) { return 'On' + e + 'Changed'; } function fe(e, t) { if (t.static) return; if ('ObjectNumber' === t.name) return; const n = ge(t.name); Object.defineProperty(e.prototype, n, { get: function () { const e = '_' + n, r = this[e]; return r || (this[e] = new oe(this, new ce(t.level, t.index), t.type)); }, }), t.aliases && t.aliases.forEach((t) => { const r = ge(t); e.prototype[r] || Object.defineProperty(e.prototype, r, { get: function () { return this[n]; }, }); }); } function pe(e) { if ('object' == typeof e && e instanceof ue) return e; if (Array.isArray(e)) return ( 'object' != typeof e[1] && (e[1] = (function (e) { throw new Error('Not implemented.'); })(e[1])), new ue(...e) ); throw new Error('Bad property.'); } function me(e, t, n, r, s, o, i, a) { let c = null, l = null; i = i.map((e) => pe(e)); const u = class extends s { static get ClassID() { return n; } static get ClassVersion() { return r; } static get ClassName() { return e; } static get_properties() { return null === l && (l = new le(i, t, s.get_properties())), l; } static GetPropertySync() { return ( null === c && (c = (function (e) { const t = Object.create(ae.prototype), n = Object.create(e.prototype); let r = 0; e.get_properties().forEach((e) => { const s = !!e.setter(n, !0); if (!e.getter(n, !0)) return; const o = { enumerable: !0, get: ((i = r), function () { return this.values[i]; }), }; var i, a; s && (o.set = ((a = e.setter(n, !0)), function (e) { return a.call(this.o, e), e; })), Object.defineProperty(t, e.name, o), r++; }); const s = function (e) { this.init(e); }; return (s.prototype = t), s; })(this)), c ); } constructor(e, t) { super(e, t); for (let e = 0; e < i.length; e++) { this['_On' + i[e].name + 'Changed'] = null; } for (let e = 0; e < a.length; e++) { this['_On' + a[e][0]] = null; } } Dispose() { super.Dispose(); for (let e = 0; e < i.length; e++) { const t = this['_On' + i[e].name + 'Changed']; t && t.Dispose(); } for (let e = 0; e < a.length; e++) { const t = this['_On' + a[e][0]]; t && t.Dispose(); } } }; for (let e = 0; e < o.length; e++) he(u, o[e]); for (let e = 0; e < i.length; e++) fe(u, i[e]); for (let e = 0; e < a.length; e++) de(u, a[e]); return u; } class Se { constructor(e, t) { (this.ClassID = e), (this.ClassVersion = t); } } const ye = l({ ClassID: q, ClassVersion: c }, Se), be = l({ DefLevel: c, PropertyIndex: c }, ce), Oe = Z(ie); function we(...e) { const t = e.length; return a({ isConstantLength: !1, encodedLength: function (n) { if (!Array.isArray(n) && !U(n)) throw new TypeError('Expected array.'); if (n.length !== t) throw new Error('Length mismatch.'); let r = 0; for (let s = 0; s < t; s++) r += e[s].encodedLength(n[s]); return r; }, encodeTo: function (n, r, s) { for (let o = 0; o < t; o++) r = e[o].encodeTo(n, r, s[o]); return r; }, decodeFrom: function (n, r) { const s = new Array(t); for (let o = 0; o < t; o++) { let t; ([r, t] = e[o].decodeFrom(n, r)), (s[o] = t); } return [r, s]; }, decodeLength: function (n, r) { for (let s = 0; s < t; s++) r = e[s].decodeLength(n, r); return r; }, }); } const Ce = we( be, ((_e = 1), a({ isConstantLength: !1, encodedLength: function (e) { if (!('object' == typeof e && e instanceof DataView)) throw new TypeError('Expected DataView.'); return e.byteLength - _e; }, encodeTo: function (e, t, n) { const r = n.byteLength, s = new Uint8Array(n.buffer, n.byteOffset, r); return new Uint8Array(e.buffer, e.byteOffset + t).set(s), t + r; }, decodeFrom: function (e, t) { const n = e.byteLength - t - _e; return [t + n, new DataView(e.buffer, e.byteOffset + t, n)]; }, decodeLength: function (e, t) { return t + (e.byteLength - t - _e); }, })), Oe ); var _e; const Pe = me( 'OcaRoot', 1, '', 2, class { constructor(e, t) { (this.ono = e), (this.device = t); } get ObjectNumber() { return this.ono; } get ClassVersion() { return this.constructor.ClassVersion; } get ClassID() { return this.constructor.ClassID; } get ClassName() { return this.constructor.ClassName; } sendCommandRrq(e, t, n, r, s) { const o = new i(this.ono, e, t, n, r); return this.device.send_command(o, s); } GetPropertyName(e) { return this.get_properties().find_name(e); } GetPropertyID(e) { const t = this.get_properties().find_property(e); if (t) return t.GetPropertyID(); } static get_properties() { return null; } get_properties() { return this.constructor.get_properties(); } get __oca_properties__() { return this.get_properties(); } GetPropertySync() { return new (this.constructor.GetPropertySync())(this); } Dispose() {} }, [ ['GetClassIdentification', 1, 1, [], [ye]], ['GetLockable', 1, 2, [], [k]], ['LockTotal', 1, 3, [], []], ['Unlock', 1, 4, [], []], ['GetRole', 1, 5, [], [z]], ['LockReadonly', 1, 6, [], []], ], [ ['ClassID', [q], 1, 1, !0, !0, null], ['ClassVersion', [c], 1, 2, !0, !0, null], ['ObjectNumber', [d], 1, 3, !0, !1, null], ['Lockable', [k], 1, 4, !0, !1, null], ['Role', [z], 1, 5, !0, !1, null], ], [['PropertyChanged', 1, 1, [Ce]]] ), ve = me('OcaManager', 2, '', 2, Pe, [], [], []), Ge = me( 'OcaDeviceManager', 3, '', 2, ve, [ ['GetOcaVersion', 3, 1, [], [c]], ['GetModelGUID', 3, 2, [], [Y]], ['GetSerialNumber', 3, 3, [], [z]], ['GetDeviceName', 3, 4, [], [z]], ['SetDeviceNa