kiosko 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/cli.js +500 -20
- package/package.json +1 -1
- package/sdk.js +1 -1
- package/web/assets/app.js +3 -3
- package/web/assets/styles.css +1 -1
package/cli.js
CHANGED
|
@@ -1110,7 +1110,7 @@ var CATEGORIES = [
|
|
|
1110
1110
|
{ id: "scene", label: { en: "Scenes", es: "Escenas" } },
|
|
1111
1111
|
{ id: "fun", label: { en: "Fun", es: "Diversi\xF3n" } }
|
|
1112
1112
|
];
|
|
1113
|
-
var SDK_VERSION = "0.2.
|
|
1113
|
+
var SDK_VERSION = "0.2.4";
|
|
1114
1114
|
function defineWidget(w) {
|
|
1115
1115
|
return w;
|
|
1116
1116
|
}
|
|
@@ -1460,9 +1460,21 @@ function normalize(raw) {
|
|
|
1460
1460
|
sources: c.sources ?? {},
|
|
1461
1461
|
plugins: { disabled: c.plugins?.disabled ?? [] },
|
|
1462
1462
|
server: { ...DEFAULT_CONFIG.server, ...c.server ?? {} },
|
|
1463
|
+
...c.homeassistant && typeof c.homeassistant === "object" ? { homeassistant: normalizeHa(c.homeassistant) } : {},
|
|
1463
1464
|
screens
|
|
1464
1465
|
};
|
|
1465
1466
|
}
|
|
1467
|
+
function normalizeHa(h) {
|
|
1468
|
+
const id = String(h.nodeId ?? "").toLowerCase().replace(/[^a-z0-9_-]+/g, "_").replace(/^_+|_+$/g, "");
|
|
1469
|
+
return {
|
|
1470
|
+
enabled: h.enabled ?? false,
|
|
1471
|
+
broker: typeof h.broker === "string" ? h.broker.trim() : "",
|
|
1472
|
+
...h.username ? { username: String(h.username) } : {},
|
|
1473
|
+
...h.password ? { password: String(h.password) } : {},
|
|
1474
|
+
discoveryPrefix: typeof h.discoveryPrefix === "string" && h.discoveryPrefix.trim() || "homeassistant",
|
|
1475
|
+
nodeId: id || "kiosko"
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1466
1478
|
function normalizeScreen(s, i) {
|
|
1467
1479
|
const width = s.width ?? 1920;
|
|
1468
1480
|
const height = s.height ?? 462;
|
|
@@ -1750,6 +1762,20 @@ function configSchema(plugins) {
|
|
|
1750
1762
|
host: { type: "string", default: DEFAULT_CONFIG.server.host, description: "Keep 127.0.0.1 unless you know why: the editor can install plugins." },
|
|
1751
1763
|
port: { type: "integer", minimum: 1, maximum: 65535, default: DEFAULT_CONFIG.server.port }
|
|
1752
1764
|
}
|
|
1765
|
+
},
|
|
1766
|
+
homeassistant: {
|
|
1767
|
+
type: "object",
|
|
1768
|
+
additionalProperties: false,
|
|
1769
|
+
description: "Kiosko as a device in Home Assistant, over MQTT discovery: each screen with its page to choose, a switch to turn it off and a button per interlude.",
|
|
1770
|
+
required: ["broker"],
|
|
1771
|
+
properties: {
|
|
1772
|
+
enabled: { type: "boolean", default: false },
|
|
1773
|
+
broker: { type: "string", description: "The MQTT broker Home Assistant uses: mqtt://host:1883, or mqtts:// for TLS." },
|
|
1774
|
+
username: { type: "string" },
|
|
1775
|
+
password: { type: "string", "x-secret": true },
|
|
1776
|
+
discoveryPrefix: { type: "string", default: "homeassistant", description: "Home Assistant's MQTT discovery prefix." },
|
|
1777
|
+
nodeId: { type: "string", pattern: "^[a-z0-9_-]+$", description: "This Kiosko's id in the topics, so several machines don't collide. Default: kiosko." }
|
|
1778
|
+
}
|
|
1753
1779
|
}
|
|
1754
1780
|
},
|
|
1755
1781
|
$defs: {
|
|
@@ -4087,17 +4113,17 @@ function lazyJpeg(c) {
|
|
|
4087
4113
|
return () => b ??= c.toBuffer("image/jpeg", 88);
|
|
4088
4114
|
}
|
|
4089
4115
|
function encode(c, kind, quality) {
|
|
4090
|
-
return kind === "jpeg" ? c.
|
|
4116
|
+
return kind === "jpeg" ? c.encode("jpeg", Math.max(1, Math.min(100, quality || 90))) : c.encode("png");
|
|
4091
4117
|
}
|
|
4092
4118
|
var MIN_FIT_QUALITY = 20;
|
|
4093
|
-
function encodeFit(c, kind, quality, maxBytes) {
|
|
4119
|
+
async function encodeFit(c, kind, quality, maxBytes) {
|
|
4094
4120
|
const q0 = Math.max(1, Math.min(100, quality || 90));
|
|
4095
|
-
let out = encode(c, kind, q0);
|
|
4121
|
+
let out = await encode(c, kind, q0);
|
|
4096
4122
|
if (kind !== "jpeg" || !(maxBytes > 0) || out.length <= maxBytes) return { out, quality: q0, over: false };
|
|
4097
4123
|
let q3 = q0;
|
|
4098
4124
|
while (q3 > MIN_FIT_QUALITY) {
|
|
4099
4125
|
q3 = Math.max(MIN_FIT_QUALITY, q3 - 10);
|
|
4100
|
-
out = encode(c, kind, q3);
|
|
4126
|
+
out = await encode(c, kind, q3);
|
|
4101
4127
|
if (out.length <= maxBytes) return { out, quality: q3, over: false };
|
|
4102
4128
|
}
|
|
4103
4129
|
return { out, quality: q3, over: true };
|
|
@@ -4114,6 +4140,10 @@ var ScreenRunner = class {
|
|
|
4114
4140
|
pinned = null;
|
|
4115
4141
|
playedSlot = /* @__PURE__ */ new Map();
|
|
4116
4142
|
interlude = null;
|
|
4143
|
+
/** Switched off from outside (Home Assistant, the API): black until switched on. Not saved. */
|
|
4144
|
+
off = false;
|
|
4145
|
+
/** An interlude asked for now (the editor, Home Assistant): played as soon as the screen is free. */
|
|
4146
|
+
wanted = null;
|
|
4117
4147
|
/** When `fit` last told the log that frames were shrunk, to say it now and then, not every frame. */
|
|
4118
4148
|
fitLoggedAt = 0;
|
|
4119
4149
|
/** Instance ids on the current page, to know if a data change concerns us. */
|
|
@@ -4139,8 +4169,8 @@ var ScreenRunner = class {
|
|
|
4139
4169
|
this.scheduler = new AnimationScheduler(() => rt.rnd(), (type, why) => rt.log(type, `animation ignored: ${why}`));
|
|
4140
4170
|
}
|
|
4141
4171
|
/** Encodes a frame within the screen's `maxBytes`, saying so in the log (at most every 10 min) when it had to shrink it. */
|
|
4142
|
-
fit(c, kind, quality, screen) {
|
|
4143
|
-
const r = encodeFit(c, kind, quality, screen.maxBytes);
|
|
4172
|
+
async fit(c, kind, quality, screen) {
|
|
4173
|
+
const r = await encodeFit(c, kind, quality, screen.maxBytes);
|
|
4144
4174
|
if ((r.quality !== Math.max(1, Math.min(100, quality || 90)) || r.over) && Date.now() - this.fitLoggedAt > 10 * 6e4) {
|
|
4145
4175
|
this.fitLoggedAt = Date.now();
|
|
4146
4176
|
this.rt.log(
|
|
@@ -4178,7 +4208,9 @@ var ScreenRunner = class {
|
|
|
4178
4208
|
if (this.busy || !this.screen.enabled) return;
|
|
4179
4209
|
this.busy = true;
|
|
4180
4210
|
try {
|
|
4181
|
-
|
|
4211
|
+
const asked = this.wanted;
|
|
4212
|
+
this.wanted = null;
|
|
4213
|
+
if (!this.off && await this.maybeInterlude(now, asked ?? void 0)) return;
|
|
4182
4214
|
const page = this.currentPage(now);
|
|
4183
4215
|
if (page?.id !== this.page?.id) {
|
|
4184
4216
|
this.page = page;
|
|
@@ -4203,7 +4235,7 @@ var ScreenRunner = class {
|
|
|
4203
4235
|
const theme = this.rt.theme();
|
|
4204
4236
|
let result = null;
|
|
4205
4237
|
let canvas;
|
|
4206
|
-
if (page) {
|
|
4238
|
+
if (page && !this.off) {
|
|
4207
4239
|
result = composePage({
|
|
4208
4240
|
cfg: this.rt.config,
|
|
4209
4241
|
screen,
|
|
@@ -4234,14 +4266,14 @@ var ScreenRunner = class {
|
|
|
4234
4266
|
this.animatable = [];
|
|
4235
4267
|
this.nextAt = now + 6e4;
|
|
4236
4268
|
}
|
|
4237
|
-
const sleeping = !!(screen.sleepWhenIdle && result?.idle);
|
|
4269
|
+
const sleeping = this.off || !!(screen.sleepWhenIdle && result?.idle);
|
|
4238
4270
|
if (sleeping) {
|
|
4239
4271
|
const g = canvas.getContext("2d");
|
|
4240
4272
|
g.fillStyle = "#000";
|
|
4241
4273
|
g.fillRect(0, 0, screen.width, screen.height);
|
|
4242
4274
|
}
|
|
4243
4275
|
const kind = encodeFor(screen.device, screen.jpegQuality);
|
|
4244
|
-
const out = this.fit(rotateCanvas(canvas, screen.rotate), kind, screen.jpegQuality, screen);
|
|
4276
|
+
const out = await this.fit(rotateCanvas(canvas, screen.rotate), kind, screen.jpegQuality, screen);
|
|
4245
4277
|
this.lastInfo = { screenId: screen.id, pageId: page?.id ?? null, at: now, image: lazyJpeg(canvas), tiles: result?.tiles ?? [], interlude: null, sleeping };
|
|
4246
4278
|
this.rt.emit("frame", this.lastInfo);
|
|
4247
4279
|
if (this.last?.equals(out)) return;
|
|
@@ -4277,7 +4309,7 @@ var ScreenRunner = class {
|
|
|
4277
4309
|
const kind = encodeFor(this.screen.device, this.screen.jpegQuality || 85);
|
|
4278
4310
|
const canvas = CANVAS.createCanvas(this.screen.width, this.screen.height);
|
|
4279
4311
|
const g = canvas.getContext("2d");
|
|
4280
|
-
|
|
4312
|
+
let start = Date.now();
|
|
4281
4313
|
for (let i = 0; i < frames && this.interlude; i++) {
|
|
4282
4314
|
g.save();
|
|
4283
4315
|
try {
|
|
@@ -4288,7 +4320,7 @@ var ScreenRunner = class {
|
|
|
4288
4320
|
} finally {
|
|
4289
4321
|
g.restore();
|
|
4290
4322
|
}
|
|
4291
|
-
const out = this.fit(rotateCanvas(canvas, this.screen.rotate), kind, this.screen.jpegQuality || 85, this.screen);
|
|
4323
|
+
const out = await this.fit(rotateCanvas(canvas, this.screen.rotate), kind, this.screen.jpegQuality || 85, this.screen);
|
|
4292
4324
|
await this.device.send(out);
|
|
4293
4325
|
if (i % 3 === 0) {
|
|
4294
4326
|
const snap = CANVAS.createCanvas(canvas.width, canvas.height);
|
|
@@ -4298,6 +4330,10 @@ var ScreenRunner = class {
|
|
|
4298
4330
|
}
|
|
4299
4331
|
const wait = start + (i + 1) * 1e3 / fps - Date.now();
|
|
4300
4332
|
if (wait > 0) await new Promise((r) => setTimeout(r, wait));
|
|
4333
|
+
else {
|
|
4334
|
+
start -= wait;
|
|
4335
|
+
await new Promise((r) => setImmediate(r));
|
|
4336
|
+
}
|
|
4301
4337
|
}
|
|
4302
4338
|
this.interlude = null;
|
|
4303
4339
|
this.last = null;
|
|
@@ -4383,6 +4419,7 @@ var ScreenRunner = class {
|
|
|
4383
4419
|
sent.push({ out: this.fit(rotateCanvas(result.canvas, screen.rotate), kind, screen.jpegQuality || 85, screen), snap: s % 3 === 1 ? result.canvas : null });
|
|
4384
4420
|
await new Promise((r) => setImmediate(r));
|
|
4385
4421
|
}
|
|
4422
|
+
const outs = await Promise.all(sent.map((x) => x.out));
|
|
4386
4423
|
const what = items.map((x) => `${x.animatable.type} ${x.play.plan.id}`).join(" + ");
|
|
4387
4424
|
this.rt.log(`screen:${screen.name}`, `animating ${what}: ${sent.length} frames at ${fps} fps (painted in ${Date.now() - t0} ms)`);
|
|
4388
4425
|
const holdAt = hold ? Math.max(0, Math.min(sent.length - 1, hold.index - 1)) : -1;
|
|
@@ -4391,7 +4428,7 @@ var ScreenRunner = class {
|
|
|
4391
4428
|
if (abort()) return false;
|
|
4392
4429
|
const due = Math.floor((Date.now() - start) * fps / 1e3);
|
|
4393
4430
|
if (k < due - 1 && k !== holdAt && k < sent.length - 1) continue;
|
|
4394
|
-
await this.device.send(
|
|
4431
|
+
await this.device.send(outs[k]);
|
|
4395
4432
|
const snap = sent[k].snap;
|
|
4396
4433
|
if (snap) {
|
|
4397
4434
|
this.lastInfo = { screenId: screen.id, pageId: page.id, at: Date.now(), image: lazyJpeg(snap), tiles: [], interlude: null, animation: items.map((x) => x.animatable.type).join(", "), sleeping: false };
|
|
@@ -4408,10 +4445,18 @@ var ScreenRunner = class {
|
|
|
4408
4445
|
}
|
|
4409
4446
|
return true;
|
|
4410
4447
|
}
|
|
4448
|
+
/**
|
|
4449
|
+
* Plays an interlude as soon as the screen is free: now if it is, or right
|
|
4450
|
+
* after the paint or animation under way (asked in the middle of one, it
|
|
4451
|
+
* used to be dropped without a word).
|
|
4452
|
+
*/
|
|
4411
4453
|
async playNow(slotId) {
|
|
4454
|
+
if (this.off) return;
|
|
4455
|
+
this.wanted = slotId;
|
|
4412
4456
|
if (this.busy) return;
|
|
4413
4457
|
this.busy = true;
|
|
4414
4458
|
try {
|
|
4459
|
+
this.wanted = null;
|
|
4415
4460
|
await this.maybeInterlude(Date.now(), slotId);
|
|
4416
4461
|
} finally {
|
|
4417
4462
|
this.busy = false;
|
|
@@ -4597,6 +4642,15 @@ var Runtime = class extends EventEmitter {
|
|
|
4597
4642
|
stopInterlude(screenId) {
|
|
4598
4643
|
this.runners.get(screenId)?.stopInterlude();
|
|
4599
4644
|
}
|
|
4645
|
+
/** Switches a screen's picture off (black) or back on, until the next restart. */
|
|
4646
|
+
setOff(screenId, off) {
|
|
4647
|
+
const r = this.runners.get(screenId);
|
|
4648
|
+
if (!r || r.off === off) return;
|
|
4649
|
+
r.off = off;
|
|
4650
|
+
if (off) r.stopInterlude();
|
|
4651
|
+
r.dirty = true;
|
|
4652
|
+
this.emit("screen", { screenId, off });
|
|
4653
|
+
}
|
|
4600
4654
|
refreshWidget(instanceId) {
|
|
4601
4655
|
this.fetches.refresh(instanceId);
|
|
4602
4656
|
}
|
|
@@ -4611,7 +4665,8 @@ var Runtime = class extends EventEmitter {
|
|
|
4611
4665
|
animation: r.animating,
|
|
4612
4666
|
device: r.device.status,
|
|
4613
4667
|
lastFrameAt: r.lastInfo?.at ?? null,
|
|
4614
|
-
sleeping: r.lastInfo?.sleeping ?? false
|
|
4668
|
+
sleeping: r.lastInfo?.sleeping ?? false,
|
|
4669
|
+
off: r.off
|
|
4615
4670
|
})),
|
|
4616
4671
|
sources: this.hub.status(),
|
|
4617
4672
|
fetches: this.fetches.status(),
|
|
@@ -5570,7 +5625,7 @@ var image = defineWidget({
|
|
|
5570
5625
|
var src_default = definePlugin({
|
|
5571
5626
|
id: "core",
|
|
5572
5627
|
name: { en: "Essentials", es: "B\xE1sicos" },
|
|
5573
|
-
version: "0.2.
|
|
5628
|
+
version: "0.2.4",
|
|
5574
5629
|
sdk: "^0.2.0",
|
|
5575
5630
|
// The picture widget reads the image (or folder, or URL) you give it.
|
|
5576
5631
|
permissions: { files: ["user"], network: ["user"] },
|
|
@@ -5665,7 +5720,7 @@ var module2 = defineWidget({
|
|
|
5665
5720
|
var src_default2 = definePlugin({
|
|
5666
5721
|
id: "legacy",
|
|
5667
5722
|
name: { en: "aimonitor compatibility", es: "Compatibilidad aimonitor" },
|
|
5668
|
-
version: "0.2.
|
|
5723
|
+
version: "0.2.4",
|
|
5669
5724
|
sdk: "^0.2.0",
|
|
5670
5725
|
// Runs the aimonitor modules you point it at, and reads their folder.
|
|
5671
5726
|
permissions: { exec: ["user"], files: ["user"] },
|
|
@@ -6792,6 +6847,428 @@ async function reloadPlugin(id, o) {
|
|
|
6792
6847
|
return problem;
|
|
6793
6848
|
}
|
|
6794
6849
|
|
|
6850
|
+
// packages/server/src/homeassistant.ts
|
|
6851
|
+
import { hostname } from "node:os";
|
|
6852
|
+
|
|
6853
|
+
// packages/server/src/mqtt.ts
|
|
6854
|
+
import { EventEmitter as EventEmitter2 } from "node:events";
|
|
6855
|
+
import { connect as tcp } from "node:net";
|
|
6856
|
+
import { connect as tls } from "node:tls";
|
|
6857
|
+
var RETRY_MS2 = [2e3, 5e3, 1e4, 3e4, 6e4];
|
|
6858
|
+
function encodeLength(n) {
|
|
6859
|
+
const out = [];
|
|
6860
|
+
do {
|
|
6861
|
+
let b = n % 128;
|
|
6862
|
+
n = Math.floor(n / 128);
|
|
6863
|
+
if (n > 0) b |= 128;
|
|
6864
|
+
out.push(b);
|
|
6865
|
+
} while (n > 0);
|
|
6866
|
+
return Buffer.from(out);
|
|
6867
|
+
}
|
|
6868
|
+
var str = (s) => {
|
|
6869
|
+
const b = Buffer.from(s, "utf8");
|
|
6870
|
+
const len = Buffer.alloc(2);
|
|
6871
|
+
len.writeUInt16BE(b.length);
|
|
6872
|
+
return Buffer.concat([len, b]);
|
|
6873
|
+
};
|
|
6874
|
+
var packet = (type, flags, body2) => Buffer.concat([Buffer.from([type << 4 | flags]), encodeLength(body2.length), body2]);
|
|
6875
|
+
function connectPacket(o) {
|
|
6876
|
+
let flags = 2;
|
|
6877
|
+
const payload = [str(o.clientId)];
|
|
6878
|
+
if (o.will) {
|
|
6879
|
+
flags |= 4 | (o.will.retain ? 32 : 0);
|
|
6880
|
+
payload.push(str(o.will.topic), str(o.will.payload));
|
|
6881
|
+
}
|
|
6882
|
+
if (o.username) {
|
|
6883
|
+
flags |= 128;
|
|
6884
|
+
payload.push(str(o.username));
|
|
6885
|
+
if (o.password) {
|
|
6886
|
+
flags |= 64;
|
|
6887
|
+
payload.push(str(o.password));
|
|
6888
|
+
}
|
|
6889
|
+
}
|
|
6890
|
+
const keep = Buffer.alloc(2);
|
|
6891
|
+
keep.writeUInt16BE(o.keepalive ?? 30);
|
|
6892
|
+
return packet(1, 0, Buffer.concat([str("MQTT"), Buffer.from([4, flags]), keep, ...payload]));
|
|
6893
|
+
}
|
|
6894
|
+
var publishPacket = (topic, payload, retain = false) => packet(3, retain ? 1 : 0, Buffer.concat([str(topic), Buffer.from(payload, "utf8")]));
|
|
6895
|
+
function subscribePacket(id, topics) {
|
|
6896
|
+
const pid = Buffer.alloc(2);
|
|
6897
|
+
pid.writeUInt16BE(id);
|
|
6898
|
+
return packet(8, 2, Buffer.concat([pid, ...topics.map((t2) => Buffer.concat([str(t2), Buffer.from([0])]))]));
|
|
6899
|
+
}
|
|
6900
|
+
function readPackets(buf) {
|
|
6901
|
+
const packets = [];
|
|
6902
|
+
let i = 0;
|
|
6903
|
+
for (; ; ) {
|
|
6904
|
+
if (buf.length - i < 2) break;
|
|
6905
|
+
let len = 0, mult = 1, j = i + 1, ok = false;
|
|
6906
|
+
for (; j < buf.length && j < i + 5; j++) {
|
|
6907
|
+
len += (buf[j] & 127) * mult;
|
|
6908
|
+
mult *= 128;
|
|
6909
|
+
if (!(buf[j] & 128)) {
|
|
6910
|
+
ok = true;
|
|
6911
|
+
break;
|
|
6912
|
+
}
|
|
6913
|
+
}
|
|
6914
|
+
if (!ok || buf.length < j + 1 + len) break;
|
|
6915
|
+
packets.push({ type: buf[i] >> 4, flags: buf[i] & 15, body: buf.subarray(j + 1, j + 1 + len) });
|
|
6916
|
+
i = j + 1 + len;
|
|
6917
|
+
}
|
|
6918
|
+
return { packets, rest: buf.subarray(i) };
|
|
6919
|
+
}
|
|
6920
|
+
var CONNACK_ERRORS = {
|
|
6921
|
+
1: "the broker refused the protocol version",
|
|
6922
|
+
2: "the broker refused the client id",
|
|
6923
|
+
3: "the broker is unavailable",
|
|
6924
|
+
4: "wrong user name or password",
|
|
6925
|
+
5: "not authorised"
|
|
6926
|
+
};
|
|
6927
|
+
var MqttClient = class extends EventEmitter2 {
|
|
6928
|
+
state = "connecting";
|
|
6929
|
+
error = null;
|
|
6930
|
+
sock = null;
|
|
6931
|
+
buf = Buffer.alloc(0);
|
|
6932
|
+
timers = [];
|
|
6933
|
+
attempt = 0;
|
|
6934
|
+
closed = false;
|
|
6935
|
+
nextId = 1;
|
|
6936
|
+
awaitingPong = false;
|
|
6937
|
+
o;
|
|
6938
|
+
constructor(o) {
|
|
6939
|
+
super();
|
|
6940
|
+
this.o = o;
|
|
6941
|
+
this.open();
|
|
6942
|
+
}
|
|
6943
|
+
get connected() {
|
|
6944
|
+
return this.state === "connected";
|
|
6945
|
+
}
|
|
6946
|
+
publish(topic, payload, retain = false) {
|
|
6947
|
+
if (this.connected) this.sock?.write(publishPacket(topic, payload, retain));
|
|
6948
|
+
}
|
|
6949
|
+
subscribe(topics) {
|
|
6950
|
+
if (this.connected && topics.length) this.sock?.write(subscribePacket(this.nextId++ % 65535 || 1, topics));
|
|
6951
|
+
}
|
|
6952
|
+
/** Goes away cleanly: the will is not published (publish your own "offline" first). */
|
|
6953
|
+
close() {
|
|
6954
|
+
this.closed = true;
|
|
6955
|
+
this.clear();
|
|
6956
|
+
if (this.sock && this.connected) this.sock.write(Buffer.from([224, 0]));
|
|
6957
|
+
this.sock?.end();
|
|
6958
|
+
this.sock?.destroy();
|
|
6959
|
+
this.sock = null;
|
|
6960
|
+
this.set("closed");
|
|
6961
|
+
}
|
|
6962
|
+
set(state, error = null) {
|
|
6963
|
+
this.state = state;
|
|
6964
|
+
this.error = error;
|
|
6965
|
+
this.emit("state", state, error);
|
|
6966
|
+
}
|
|
6967
|
+
clear() {
|
|
6968
|
+
for (const t2 of this.timers) clearTimeout(t2);
|
|
6969
|
+
this.timers = [];
|
|
6970
|
+
}
|
|
6971
|
+
later(fn, ms) {
|
|
6972
|
+
const t2 = setTimeout(fn, ms);
|
|
6973
|
+
t2.unref?.();
|
|
6974
|
+
this.timers.push(t2);
|
|
6975
|
+
}
|
|
6976
|
+
open() {
|
|
6977
|
+
if (this.closed) return;
|
|
6978
|
+
let u;
|
|
6979
|
+
try {
|
|
6980
|
+
u = new URL(this.o.url);
|
|
6981
|
+
} catch {
|
|
6982
|
+
this.set("error", `not a broker address: ${this.o.url}`);
|
|
6983
|
+
return;
|
|
6984
|
+
}
|
|
6985
|
+
const secure = u.protocol === "mqtts:";
|
|
6986
|
+
if (!secure && u.protocol !== "mqtt:") {
|
|
6987
|
+
this.set("error", `use mqtt:// or mqtts:// (got ${u.protocol})`);
|
|
6988
|
+
return;
|
|
6989
|
+
}
|
|
6990
|
+
const port = Number(u.port) || (secure ? 8883 : 1883);
|
|
6991
|
+
this.set("connecting", this.error);
|
|
6992
|
+
const s = this.o.socket ? this.o.socket(u.hostname, port, secure) : secure ? tls({ host: u.hostname, port, servername: u.hostname }) : tcp({ host: u.hostname, port });
|
|
6993
|
+
this.sock = s;
|
|
6994
|
+
this.buf = Buffer.alloc(0);
|
|
6995
|
+
const mine = (fn) => (...a) => this.sock === s && fn(...a);
|
|
6996
|
+
s.setNoDelay?.(true);
|
|
6997
|
+
s.on(secure ? "secureConnect" : "connect", mine(() => s.write(connectPacket(this.o))));
|
|
6998
|
+
s.on("data", mine((d) => this.onData(d)));
|
|
6999
|
+
s.on("error", mine((err) => this.drop(err.message)));
|
|
7000
|
+
s.on("close", mine(() => this.drop(this.error ?? "connection closed")));
|
|
7001
|
+
}
|
|
7002
|
+
onData(d) {
|
|
7003
|
+
const { packets, rest } = readPackets(Buffer.concat([this.buf, d]));
|
|
7004
|
+
this.buf = Buffer.from(rest);
|
|
7005
|
+
for (const p of packets) {
|
|
7006
|
+
if (p.type === 2) {
|
|
7007
|
+
const code = p.body[1] ?? 0;
|
|
7008
|
+
if (code) {
|
|
7009
|
+
this.drop(CONNACK_ERRORS[code] ?? `refused (${code})`, true);
|
|
7010
|
+
return;
|
|
7011
|
+
}
|
|
7012
|
+
this.attempt = 0;
|
|
7013
|
+
this.set("connected");
|
|
7014
|
+
this.ping();
|
|
7015
|
+
this.emit("connect");
|
|
7016
|
+
} else if (p.type === 3) {
|
|
7017
|
+
const len = p.body.readUInt16BE(0);
|
|
7018
|
+
const topic = p.body.subarray(2, 2 + len).toString("utf8");
|
|
7019
|
+
const qos = p.flags >> 1 & 3;
|
|
7020
|
+
const payload = p.body.subarray(2 + len + (qos ? 2 : 0)).toString("utf8");
|
|
7021
|
+
this.emit("message", topic, payload);
|
|
7022
|
+
} else if (p.type === 13) {
|
|
7023
|
+
this.awaitingPong = false;
|
|
7024
|
+
}
|
|
7025
|
+
}
|
|
7026
|
+
}
|
|
7027
|
+
ping() {
|
|
7028
|
+
const every = (this.o.keepalive ?? 30) * 1e3 * 0.8;
|
|
7029
|
+
this.later(() => {
|
|
7030
|
+
if (!this.connected) return;
|
|
7031
|
+
if (this.awaitingPong) {
|
|
7032
|
+
this.drop("no answer from the broker");
|
|
7033
|
+
return;
|
|
7034
|
+
}
|
|
7035
|
+
this.awaitingPong = true;
|
|
7036
|
+
this.sock?.write(Buffer.from([192, 0]));
|
|
7037
|
+
this.ping();
|
|
7038
|
+
}, every);
|
|
7039
|
+
}
|
|
7040
|
+
drop(why, refused = false) {
|
|
7041
|
+
if (this.closed) return;
|
|
7042
|
+
this.clear();
|
|
7043
|
+
const s = this.sock;
|
|
7044
|
+
this.sock = null;
|
|
7045
|
+
s?.destroy();
|
|
7046
|
+
this.awaitingPong = false;
|
|
7047
|
+
this.set("error", why);
|
|
7048
|
+
const ms = refused ? 5 * 6e4 : RETRY_MS2[Math.min(this.attempt++, RETRY_MS2.length - 1)];
|
|
7049
|
+
this.later(() => this.open(), ms);
|
|
7050
|
+
}
|
|
7051
|
+
};
|
|
7052
|
+
|
|
7053
|
+
// packages/server/src/homeassistant.ts
|
|
7054
|
+
var AUTO = { en: "Auto", es: "Autom\xE1tica" };
|
|
7055
|
+
var NAMES = {
|
|
7056
|
+
page: { en: "Page", es: "P\xE1gina" },
|
|
7057
|
+
shown: { en: "Page shown", es: "P\xE1gina en pantalla" },
|
|
7058
|
+
display: { en: "Display", es: "Pantalla" },
|
|
7059
|
+
interlude: { en: "Play", es: "Reproducir" }
|
|
7060
|
+
};
|
|
7061
|
+
var slug3 = (s) => s.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-z0-9_-]+/g, "_").replace(/^_+|_+$/g, "") || "x";
|
|
7062
|
+
function pageOptions(screen, auto) {
|
|
7063
|
+
const byName = /* @__PURE__ */ new Map([[auto, null]]);
|
|
7064
|
+
const nameOf = /* @__PURE__ */ new Map();
|
|
7065
|
+
for (const p of screen.pages.filter((x) => x.enabled)) {
|
|
7066
|
+
let name = p.name.trim() || p.id;
|
|
7067
|
+
for (let i = 2; byName.has(name); i++) name = `${p.name} (${i})`;
|
|
7068
|
+
byName.set(name, p.id);
|
|
7069
|
+
nameOf.set(p.id, name);
|
|
7070
|
+
}
|
|
7071
|
+
return { options: [...byName.keys()], byName, nameOf };
|
|
7072
|
+
}
|
|
7073
|
+
var HomeAssistantBridge = class {
|
|
7074
|
+
client = null;
|
|
7075
|
+
link = null;
|
|
7076
|
+
/** Discovery topics announced, to remove the ones that disappear. */
|
|
7077
|
+
announced = /* @__PURE__ */ new Set();
|
|
7078
|
+
/** Last state published per topic: only changes go out. */
|
|
7079
|
+
last = /* @__PURE__ */ new Map();
|
|
7080
|
+
onFrame = () => this.publishStates();
|
|
7081
|
+
onConfig = (cfg) => this.apply(cfg);
|
|
7082
|
+
onScreen = () => this.publishStates();
|
|
7083
|
+
runtime;
|
|
7084
|
+
version;
|
|
7085
|
+
makeClient;
|
|
7086
|
+
constructor(runtime, version, makeClient = (o) => new MqttClient(o)) {
|
|
7087
|
+
this.runtime = runtime;
|
|
7088
|
+
this.version = version;
|
|
7089
|
+
this.makeClient = makeClient;
|
|
7090
|
+
runtime.on("frame", this.onFrame);
|
|
7091
|
+
runtime.on("config", this.onConfig);
|
|
7092
|
+
runtime.on("screen", this.onScreen);
|
|
7093
|
+
this.apply(runtime.config);
|
|
7094
|
+
}
|
|
7095
|
+
status() {
|
|
7096
|
+
if (!this.link?.enabled) return { enabled: false, state: "off", error: null };
|
|
7097
|
+
return { enabled: true, state: this.client?.state ?? "connecting", error: this.client?.error ?? null, broker: this.link.broker };
|
|
7098
|
+
}
|
|
7099
|
+
stop() {
|
|
7100
|
+
this.runtime.off("frame", this.onFrame);
|
|
7101
|
+
this.runtime.off("config", this.onConfig);
|
|
7102
|
+
this.runtime.off("screen", this.onScreen);
|
|
7103
|
+
this.disconnect();
|
|
7104
|
+
}
|
|
7105
|
+
get base() {
|
|
7106
|
+
return `kiosko/${this.link.nodeId}`;
|
|
7107
|
+
}
|
|
7108
|
+
loc(l) {
|
|
7109
|
+
return resolveLocale(this.runtime.config).startsWith("es") ? l.es : l.en;
|
|
7110
|
+
}
|
|
7111
|
+
/** A new config: connect, reconnect (other broker or login) or just announce again. */
|
|
7112
|
+
apply(cfg) {
|
|
7113
|
+
const want = cfg.homeassistant?.enabled && cfg.homeassistant.broker ? cfg.homeassistant : null;
|
|
7114
|
+
const same3 = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
7115
|
+
if (!want) {
|
|
7116
|
+
this.disconnect();
|
|
7117
|
+
this.link = null;
|
|
7118
|
+
return;
|
|
7119
|
+
}
|
|
7120
|
+
if (this.client && same3(this.link, want)) {
|
|
7121
|
+
this.announce();
|
|
7122
|
+
return;
|
|
7123
|
+
}
|
|
7124
|
+
this.disconnect();
|
|
7125
|
+
this.link = want;
|
|
7126
|
+
const base3 = `kiosko/${want.nodeId}`;
|
|
7127
|
+
const c = this.makeClient({
|
|
7128
|
+
url: want.broker,
|
|
7129
|
+
clientId: `kiosko-${want.nodeId}-${hostname().slice(0, 20)}`.slice(0, 23),
|
|
7130
|
+
...want.username ? { username: want.username } : {},
|
|
7131
|
+
...want.password ? { password: want.password } : {},
|
|
7132
|
+
keepalive: 30,
|
|
7133
|
+
will: { topic: `${base3}/status`, payload: "offline", retain: true }
|
|
7134
|
+
});
|
|
7135
|
+
this.client = c;
|
|
7136
|
+
c.on("connect", () => {
|
|
7137
|
+
if (this.client !== c) return;
|
|
7138
|
+
this.last.clear();
|
|
7139
|
+
c.publish(`${base3}/status`, "online", true);
|
|
7140
|
+
c.subscribe([`${base3}/+/page/set`, `${base3}/+/display/set`, `${base3}/+/interlude/+/press`, `${this.link.discoveryPrefix}/status`]);
|
|
7141
|
+
this.announce();
|
|
7142
|
+
});
|
|
7143
|
+
c.on("message", (topic, payload) => this.client === c && this.onMessage(topic, payload));
|
|
7144
|
+
}
|
|
7145
|
+
disconnect() {
|
|
7146
|
+
const c = this.client;
|
|
7147
|
+
this.client = null;
|
|
7148
|
+
if (!c) return;
|
|
7149
|
+
if (c.connected && this.link) c.publish(`${this.base}/status`, "offline", true);
|
|
7150
|
+
c.close();
|
|
7151
|
+
this.announced.clear();
|
|
7152
|
+
this.last.clear();
|
|
7153
|
+
}
|
|
7154
|
+
/** Every entity's discovery config, and its state. */
|
|
7155
|
+
announce() {
|
|
7156
|
+
const c = this.client;
|
|
7157
|
+
if (!c?.connected || !this.link) return;
|
|
7158
|
+
const cfg = this.runtime.config;
|
|
7159
|
+
const prefix = this.link.discoveryPrefix;
|
|
7160
|
+
const node = this.link.nodeId;
|
|
7161
|
+
const now = /* @__PURE__ */ new Set();
|
|
7162
|
+
const put = (component, objectId, body2) => {
|
|
7163
|
+
const topic = `${prefix}/${component}/${node}/${objectId}/config`;
|
|
7164
|
+
now.add(topic);
|
|
7165
|
+
c.publish(topic, JSON.stringify(body2), true);
|
|
7166
|
+
};
|
|
7167
|
+
const availability = { availability_topic: `${this.base}/status` };
|
|
7168
|
+
for (const screen of cfg.screens) {
|
|
7169
|
+
const sid = slug3(screen.id);
|
|
7170
|
+
const device = {
|
|
7171
|
+
identifiers: [`kiosko_${node}_${sid}`],
|
|
7172
|
+
name: `Kiosko ${screen.name}`,
|
|
7173
|
+
manufacturer: "Kiosko",
|
|
7174
|
+
model: `${screen.width}\xD7${screen.height}`,
|
|
7175
|
+
sw_version: this.version
|
|
7176
|
+
};
|
|
7177
|
+
const id = (what) => `kiosko_${node}_${sid}_${what}`;
|
|
7178
|
+
const { options: options2 } = pageOptions(screen, this.loc(AUTO));
|
|
7179
|
+
put("select", `${sid}_page`, {
|
|
7180
|
+
name: this.loc(NAMES.page),
|
|
7181
|
+
unique_id: id("page"),
|
|
7182
|
+
icon: "mdi:book-open-page-variant",
|
|
7183
|
+
command_topic: `${this.base}/${sid}/page/set`,
|
|
7184
|
+
state_topic: `${this.base}/${sid}/page`,
|
|
7185
|
+
options: options2,
|
|
7186
|
+
device,
|
|
7187
|
+
...availability
|
|
7188
|
+
});
|
|
7189
|
+
put("sensor", `${sid}_shown`, {
|
|
7190
|
+
name: this.loc(NAMES.shown),
|
|
7191
|
+
unique_id: id("shown"),
|
|
7192
|
+
icon: "mdi:monitor-eye",
|
|
7193
|
+
state_topic: `${this.base}/${sid}/shown`,
|
|
7194
|
+
device,
|
|
7195
|
+
...availability
|
|
7196
|
+
});
|
|
7197
|
+
put("switch", `${sid}_display`, {
|
|
7198
|
+
name: this.loc(NAMES.display),
|
|
7199
|
+
unique_id: id("display"),
|
|
7200
|
+
icon: "mdi:monitor",
|
|
7201
|
+
command_topic: `${this.base}/${sid}/display/set`,
|
|
7202
|
+
state_topic: `${this.base}/${sid}/display`,
|
|
7203
|
+
payload_on: "ON",
|
|
7204
|
+
payload_off: "OFF",
|
|
7205
|
+
device,
|
|
7206
|
+
...availability
|
|
7207
|
+
});
|
|
7208
|
+
for (const slot of screen.interludes) {
|
|
7209
|
+
const def = this.runtime.registry.interlude(slot.type);
|
|
7210
|
+
const name = def ? typeof def.name === "string" ? def.name : this.loc({ en: def.name.en ?? slot.type, es: def.name.es ?? def.name.en ?? slot.type }) : slot.type;
|
|
7211
|
+
put("button", `${sid}_${slug3(slot.id)}`, {
|
|
7212
|
+
name: `${this.loc(NAMES.interlude)}: ${name}`,
|
|
7213
|
+
unique_id: id(slug3(slot.id)),
|
|
7214
|
+
icon: "mdi:play-circle-outline",
|
|
7215
|
+
command_topic: `${this.base}/${sid}/interlude/${slot.id}/press`,
|
|
7216
|
+
payload_press: "PRESS",
|
|
7217
|
+
device,
|
|
7218
|
+
...availability
|
|
7219
|
+
});
|
|
7220
|
+
}
|
|
7221
|
+
}
|
|
7222
|
+
for (const topic of this.announced) if (!now.has(topic)) c.publish(topic, "", true);
|
|
7223
|
+
this.announced = now;
|
|
7224
|
+
this.last.clear();
|
|
7225
|
+
this.publishStates();
|
|
7226
|
+
}
|
|
7227
|
+
/** Each screen's page (chosen and shown) and display, when they change. */
|
|
7228
|
+
publishStates() {
|
|
7229
|
+
const c = this.client;
|
|
7230
|
+
if (!c?.connected || !this.link) return;
|
|
7231
|
+
const status = this.runtime.status();
|
|
7232
|
+
for (const screen of this.runtime.config.screens) {
|
|
7233
|
+
const sid = slug3(screen.id);
|
|
7234
|
+
const st = status.screens.find((x) => x.id === screen.id);
|
|
7235
|
+
const { nameOf } = pageOptions(screen, this.loc(AUTO));
|
|
7236
|
+
const states = [
|
|
7237
|
+
[`${this.base}/${sid}/page`, st?.pinned ? nameOf.get(st.pinned) ?? this.loc(AUTO) : this.loc(AUTO)],
|
|
7238
|
+
[`${this.base}/${sid}/shown`, st?.off ? "\u2014" : st?.page ? nameOf.get(st.page) ?? st.page : "\u2014"],
|
|
7239
|
+
[`${this.base}/${sid}/display`, st?.off ? "OFF" : "ON"]
|
|
7240
|
+
];
|
|
7241
|
+
for (const [topic, value] of states) {
|
|
7242
|
+
if (this.last.get(topic) === value) continue;
|
|
7243
|
+
this.last.set(topic, value);
|
|
7244
|
+
c.publish(topic, value, true);
|
|
7245
|
+
}
|
|
7246
|
+
}
|
|
7247
|
+
}
|
|
7248
|
+
onMessage(topic, payload) {
|
|
7249
|
+
if (!this.link) return;
|
|
7250
|
+
if (topic === `${this.link.discoveryPrefix}/status`) {
|
|
7251
|
+
if (payload === "online") this.announce();
|
|
7252
|
+
return;
|
|
7253
|
+
}
|
|
7254
|
+
const m = new RegExp(`^${this.base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/([^/]+)/(page/set|display/set|interlude/([^/]+)/press)$`).exec(topic);
|
|
7255
|
+
if (!m) return;
|
|
7256
|
+
const screen = this.runtime.config.screens.find((s) => slug3(s.id) === m[1]);
|
|
7257
|
+
if (!screen) return;
|
|
7258
|
+
if (m[2] === "page/set") {
|
|
7259
|
+
const { byName } = pageOptions(screen, this.loc(AUTO));
|
|
7260
|
+
if (!byName.has(payload)) return;
|
|
7261
|
+
this.runtime.pin(screen.id, byName.get(payload) ?? null);
|
|
7262
|
+
} else if (m[2] === "display/set") {
|
|
7263
|
+
if (payload !== "ON" && payload !== "OFF") return;
|
|
7264
|
+
this.runtime.setOff(screen.id, payload === "OFF");
|
|
7265
|
+
} else if (m[3]) {
|
|
7266
|
+
if (screen.interludes.some((s) => s.id === m[3])) this.runtime.playInterlude(screen.id, m[3]);
|
|
7267
|
+
}
|
|
7268
|
+
this.publishStates();
|
|
7269
|
+
}
|
|
7270
|
+
};
|
|
7271
|
+
|
|
6795
7272
|
// packages/server/src/server.ts
|
|
6796
7273
|
var require3 = createRequire2(import.meta.url);
|
|
6797
7274
|
var MIME = {
|
|
@@ -6862,7 +7339,9 @@ data: ${JSON.stringify(data)}
|
|
|
6862
7339
|
runtime.on("frame", (f) => send("frame", { screenId: f.screenId, pageId: f.pageId, at: f.at, interlude: f.interlude, sleeping: f.sleeping, tiles: f.tiles }));
|
|
6863
7340
|
runtime.on("log", (e) => send("log", e));
|
|
6864
7341
|
runtime.on("config", () => thumbs.clear());
|
|
6865
|
-
const
|
|
7342
|
+
const bridge = o.homeassistant ?? !runtime.demoMode ? new HomeAssistantBridge(runtime, o.version) : null;
|
|
7343
|
+
const fullStatus = () => ({ ...runtime.status(), homeassistant: bridge?.status() ?? { enabled: false, state: "off", error: null } });
|
|
7344
|
+
const statusTimer = setInterval(() => send("status", fullStatus()), 3e3);
|
|
6866
7345
|
const allowedHosts = /* @__PURE__ */ new Set([`127.0.0.1:${o.port}`, `localhost:${o.port}`, `[::1]:${o.port}`, `${o.host}:${o.port}`]);
|
|
6867
7346
|
function state() {
|
|
6868
7347
|
const locale = resolveLocale(runtime.config);
|
|
@@ -6877,7 +7356,7 @@ data: ${JSON.stringify(data)}
|
|
|
6877
7356
|
categories: CATEGORIES,
|
|
6878
7357
|
themes: THEMES,
|
|
6879
7358
|
presets: PANEL_PRESETS,
|
|
6880
|
-
status:
|
|
7359
|
+
status: fullStatus(),
|
|
6881
7360
|
paths: { config: o.configPath, plugins: paths.plugins, home: paths.home },
|
|
6882
7361
|
restartNeeded,
|
|
6883
7362
|
official: OFFICIAL_PLUGINS,
|
|
@@ -7099,6 +7578,7 @@ data: ${JSON.stringify({ version: o.version })}
|
|
|
7099
7578
|
server,
|
|
7100
7579
|
close() {
|
|
7101
7580
|
clearInterval(statusTimer);
|
|
7581
|
+
bridge?.stop();
|
|
7102
7582
|
for (const t2 of updateTimers) clearTimeout(t2);
|
|
7103
7583
|
for (const c of clients) c.end();
|
|
7104
7584
|
server.close();
|
|
@@ -7143,7 +7623,7 @@ function starterConfig(registry2, o = {}) {
|
|
|
7143
7623
|
}
|
|
7144
7624
|
|
|
7145
7625
|
// packages/server/src/cli.ts
|
|
7146
|
-
var VERSION = "0.2.
|
|
7626
|
+
var VERSION = "0.2.4";
|
|
7147
7627
|
var USAGE2 = `Kiosko ${VERSION}: a widget mosaic for USB LCD panels
|
|
7148
7628
|
|
|
7149
7629
|
kiosko start [--port 7420] [--host 127.0.0.1] [--config file] [--open] [--demo]
|