mqtt2matter 0.0.0-stage → 0.2.0
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/CHANGELOG.md +76 -0
- package/LICENSE +202 -0
- package/NOTICE +23 -0
- package/README.md +172 -2
- package/dist/app.d.ts +34 -0
- package/dist/app.js +157 -0
- package/dist/app.js.map +1 -0
- package/dist/commands/import-z2m.d.ts +20 -0
- package/dist/commands/import-z2m.js +173 -0
- package/dist/commands/import-z2m.js.map +1 -0
- package/dist/commands/install.d.ts +41 -0
- package/dist/commands/install.js +250 -0
- package/dist/commands/install.js.map +1 -0
- package/dist/commands/update.d.ts +14 -0
- package/dist/commands/update.js +109 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.js +3 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/load.d.ts +57 -0
- package/dist/config/load.js +137 -0
- package/dist/config/load.js.map +1 -0
- package/dist/config/schema.d.ts +496 -0
- package/dist/config/schema.js +115 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/engine/payload.d.ts +94 -0
- package/dist/engine/payload.js +515 -0
- package/dist/engine/payload.js.map +1 -0
- package/dist/engine/runtime.d.ts +79 -0
- package/dist/engine/runtime.js +438 -0
- package/dist/engine/runtime.js.map +1 -0
- package/dist/engine/template.d.ts +20 -0
- package/dist/engine/template.js +264 -0
- package/dist/engine/template.js.map +1 -0
- package/dist/engine/units.d.ts +9 -0
- package/dist/engine/units.js +42 -0
- package/dist/engine/units.js.map +1 -0
- package/dist/families/climate.d.ts +11 -0
- package/dist/families/climate.js +68 -0
- package/dist/families/climate.js.map +1 -0
- package/dist/families/color.d.ts +30 -0
- package/dist/families/color.js +152 -0
- package/dist/families/color.js.map +1 -0
- package/dist/families/cover.d.ts +17 -0
- package/dist/families/cover.js +308 -0
- package/dist/families/cover.js.map +1 -0
- package/dist/families/fan.d.ts +16 -0
- package/dist/families/fan.js +154 -0
- package/dist/families/fan.js.map +1 -0
- package/dist/families/index.d.ts +32 -0
- package/dist/families/index.js +162 -0
- package/dist/families/index.js.map +1 -0
- package/dist/families/light.d.ts +36 -0
- package/dist/families/light.js +258 -0
- package/dist/families/light.js.map +1 -0
- package/dist/families/lock.d.ts +10 -0
- package/dist/families/lock.js +31 -0
- package/dist/families/lock.js.map +1 -0
- package/dist/families/on-off.d.ts +7 -0
- package/dist/families/on-off.js +56 -0
- package/dist/families/on-off.js.map +1 -0
- package/dist/families/power-source.d.ts +12 -0
- package/dist/families/power-source.js +60 -0
- package/dist/families/power-source.js.map +1 -0
- package/dist/families/registry.d.ts +40 -0
- package/dist/families/registry.js +72 -0
- package/dist/families/registry.js.map +1 -0
- package/dist/families/rvc.d.ts +40 -0
- package/dist/families/rvc.js +83 -0
- package/dist/families/rvc.js.map +1 -0
- package/dist/families/switch.d.ts +19 -0
- package/dist/families/switch.js +94 -0
- package/dist/families/switch.js.map +1 -0
- package/dist/importers/actions.d.ts +19 -0
- package/dist/importers/actions.js +98 -0
- package/dist/importers/actions.js.map +1 -0
- package/dist/importers/ha/areas.d.ts +6 -0
- package/dist/importers/ha/areas.js +279 -0
- package/dist/importers/ha/areas.js.map +1 -0
- package/dist/importers/ha/generate.d.ts +51 -0
- package/dist/importers/ha/generate.js +1376 -0
- package/dist/importers/ha/generate.js.map +1 -0
- package/dist/importers/ha/index.d.ts +6 -0
- package/dist/importers/ha/index.js +7 -0
- package/dist/importers/ha/index.js.map +1 -0
- package/dist/importers/ha/source.d.ts +36 -0
- package/dist/importers/ha/source.js +95 -0
- package/dist/importers/ha/source.js.map +1 -0
- package/dist/importers/ha/sync.d.ts +29 -0
- package/dist/importers/ha/sync.js +132 -0
- package/dist/importers/ha/sync.js.map +1 -0
- package/dist/importers/ha/templates.d.ts +18 -0
- package/dist/importers/ha/templates.js +285 -0
- package/dist/importers/ha/templates.js.map +1 -0
- package/dist/importers/ha/types.d.ts +78 -0
- package/dist/importers/ha/types.js +479 -0
- package/dist/importers/ha/types.js.map +1 -0
- package/dist/importers/index.d.ts +5 -0
- package/dist/importers/index.js +8 -0
- package/dist/importers/index.js.map +1 -0
- package/dist/importers/patch.d.ts +50 -0
- package/dist/importers/patch.js +224 -0
- package/dist/importers/patch.js.map +1 -0
- package/dist/importers/sync.d.ts +112 -0
- package/dist/importers/sync.js +220 -0
- package/dist/importers/sync.js.map +1 -0
- package/dist/importers/twins.d.ts +10 -0
- package/dist/importers/twins.js +34 -0
- package/dist/importers/twins.js.map +1 -0
- package/dist/importers/z2m/generate.d.ts +81 -0
- package/dist/importers/z2m/generate.js +1171 -0
- package/dist/importers/z2m/generate.js.map +1 -0
- package/dist/importers/z2m/index.d.ts +5 -0
- package/dist/importers/z2m/index.js +6 -0
- package/dist/importers/z2m/index.js.map +1 -0
- package/dist/importers/z2m/source.d.ts +18 -0
- package/dist/importers/z2m/source.js +46 -0
- package/dist/importers/z2m/source.js.map +1 -0
- package/dist/importers/z2m/sync.d.ts +29 -0
- package/dist/importers/z2m/sync.js +92 -0
- package/dist/importers/z2m/sync.js.map +1 -0
- package/dist/importers/z2m/types.d.ts +72 -0
- package/dist/importers/z2m/types.js +46 -0
- package/dist/importers/z2m/types.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +17 -0
- package/dist/log.js +54 -0
- package/dist/log.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +202 -0
- package/dist/main.js.map +1 -0
- package/dist/matter/bridge.d.ts +113 -0
- package/dist/matter/bridge.js +467 -0
- package/dist/matter/bridge.js.map +1 -0
- package/dist/matter/check.d.ts +11 -0
- package/dist/matter/check.js +116 -0
- package/dist/matter/check.js.map +1 -0
- package/dist/matter/cluster-defaults.d.ts +6 -0
- package/dist/matter/cluster-defaults.js +63 -0
- package/dist/matter/cluster-defaults.js.map +1 -0
- package/dist/matter/commissioning.d.ts +21 -0
- package/dist/matter/commissioning.js +88 -0
- package/dist/matter/commissioning.js.map +1 -0
- package/dist/matter/device-types.d.ts +44 -0
- package/dist/matter/device-types.js +230 -0
- package/dist/matter/device-types.js.map +1 -0
- package/dist/matter/ids.d.ts +31 -0
- package/dist/matter/ids.js +83 -0
- package/dist/matter/ids.js.map +1 -0
- package/dist/matter/index.d.ts +7 -0
- package/dist/matter/index.js +8 -0
- package/dist/matter/index.js.map +1 -0
- package/dist/matter/node-manager.d.ts +39 -0
- package/dist/matter/node-manager.js +161 -0
- package/dist/matter/node-manager.js.map +1 -0
- package/dist/matter/shards.d.ts +26 -0
- package/dist/matter/shards.js +93 -0
- package/dist/matter/shards.js.map +1 -0
- package/dist/model/attribute.d.ts +18 -0
- package/dist/model/attribute.js +28 -0
- package/dist/model/attribute.js.map +1 -0
- package/dist/model/catalog.d.ts +79 -0
- package/dist/model/catalog.js +223 -0
- package/dist/model/catalog.js.map +1 -0
- package/dist/model/index.d.ts +7 -0
- package/dist/model/index.js +8 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model/migrate.d.ts +4 -0
- package/dist/model/migrate.js +39 -0
- package/dist/model/migrate.js.map +1 -0
- package/dist/model/options.d.ts +102 -0
- package/dist/model/options.js +88 -0
- package/dist/model/options.js.map +1 -0
- package/dist/model/presets.d.ts +37 -0
- package/dist/model/presets.js +105 -0
- package/dist/model/presets.js.map +1 -0
- package/dist/model/schema.d.ts +1747 -0
- package/dist/model/schema.js +243 -0
- package/dist/model/schema.js.map +1 -0
- package/dist/model/validate.d.ts +21 -0
- package/dist/model/validate.js +484 -0
- package/dist/model/validate.js.map +1 -0
- package/dist/mqtt/client.d.ts +38 -0
- package/dist/mqtt/client.js +94 -0
- package/dist/mqtt/client.js.map +1 -0
- package/dist/mqtt/index.d.ts +2 -0
- package/dist/mqtt/index.js +3 -0
- package/dist/mqtt/index.js.map +1 -0
- package/dist/mqtt/topic-cache.d.ts +20 -0
- package/dist/mqtt/topic-cache.js +58 -0
- package/dist/mqtt/topic-cache.js.map +1 -0
- package/dist/server/auth.d.ts +59 -0
- package/dist/server/auth.js +185 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/catalog.d.ts +52 -0
- package/dist/server/catalog.js +96 -0
- package/dist/server/catalog.js.map +1 -0
- package/dist/server/compact.d.ts +3 -0
- package/dist/server/compact.js +105 -0
- package/dist/server/compact.js.map +1 -0
- package/dist/server/config-store.d.ts +36 -0
- package/dist/server/config-store.js +155 -0
- package/dist/server/config-store.js.map +1 -0
- package/dist/server/events.d.ts +50 -0
- package/dist/server/events.js +71 -0
- package/dist/server/events.js.map +1 -0
- package/dist/server/http.d.ts +27 -0
- package/dist/server/http.js +168 -0
- package/dist/server/http.js.map +1 -0
- package/dist/server/importers.d.ts +40 -0
- package/dist/server/importers.js +75 -0
- package/dist/server/importers.js.map +1 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +8 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/log-buffer.d.ts +22 -0
- package/dist/server/log-buffer.js +55 -0
- package/dist/server/log-buffer.js.map +1 -0
- package/dist/server/presets.d.ts +17 -0
- package/dist/server/presets.js +55 -0
- package/dist/server/presets.js.map +1 -0
- package/dist/server/preview.d.ts +19 -0
- package/dist/server/preview.js +121 -0
- package/dist/server/preview.js.map +1 -0
- package/dist/server/server.d.ts +107 -0
- package/dist/server/server.js +810 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/update.d.ts +60 -0
- package/dist/server/update.js +191 -0
- package/dist/server/update.js.map +1 -0
- package/docs/api.md +128 -0
- package/docs/architecture.md +121 -0
- package/docs/config.md +538 -0
- package/docs/importers.md +241 -0
- package/example/mqtt2matter.json +276 -0
- package/package.json +90 -4
- package/presets/mqtt-smarthome/button.json +47 -0
- package/presets/mqtt-smarthome/contact.json +37 -0
- package/presets/mqtt-smarthome/cover.json +50 -0
- package/presets/mqtt-smarthome/dimmer.json +57 -0
- package/presets/mqtt-smarthome/humidity.json +38 -0
- package/presets/mqtt-smarthome/light.json +47 -0
- package/presets/mqtt-smarthome/occupancy.json +46 -0
- package/presets/mqtt-smarthome/switch.json +47 -0
- package/presets/mqtt-smarthome/temperature.json +38 -0
- package/presets/openbeken/color-light.json +64 -0
- package/presets/openbeken/ct-light.json +51 -0
- package/presets/openbeken/dimmer.json +47 -0
- package/presets/openbeken/humidity.json +37 -0
- package/presets/openbeken/relay.json +40 -0
- package/presets/openbeken/temperature.json +41 -0
- package/presets/shelly-gen1/dimmer.json +48 -0
- package/presets/shelly-gen1/door-window.json +43 -0
- package/presets/shelly-gen1/ht.json +43 -0
- package/presets/shelly-gen1/relay-pm.json +60 -0
- package/presets/shelly-gen1/relay.json +36 -0
- package/presets/shelly-gen1/roller.json +52 -0
- package/presets/shelly-gen2/cover.json +68 -0
- package/presets/shelly-gen2/dimmer.json +61 -0
- package/presets/shelly-gen2/ht.json +47 -0
- package/presets/shelly-gen2/i4.json +82 -0
- package/presets/shelly-gen2/switch-pm.json +101 -0
- package/presets/shelly-gen2/switch.json +47 -0
- package/presets/tasmota/climate.json +52 -0
- package/presets/tasmota/color-light.json +63 -0
- package/presets/tasmota/ct-light.json +50 -0
- package/presets/tasmota/dimmer.json +45 -0
- package/presets/tasmota/relay.json +39 -0
- package/presets/tasmota/shutter.json +46 -0
- package/presets/tasmota/temperature-humidity.json +42 -0
- package/presets/tasmota/temperature.json +32 -0
- package/presets/valetudo/vacuum.json +103 -0
- package/presets/wled/dimmer.json +46 -0
- package/presets/wled/light.json +59 -0
- package/ui/dist/assets/index-BDtT6bdH.js +10 -0
- package/ui/dist/assets/index-CHiG-aoU.css +1 -0
- package/ui/dist/index.html +18 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/* The configuration file as the UI edits it. The store keeps two views: the raw file content (what the
|
|
2
|
+
user wrote, without defaults and without the environment's overrides — what is written back) and the
|
|
3
|
+
parsed, running configuration the application holds. A device is saved raw and handed to the
|
|
4
|
+
application parsed; the paths the environment sets are refused, because the file would not win. */
|
|
5
|
+
import { mkdir, rename, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { ConfigSchema } from "../config/schema.js";
|
|
9
|
+
import { DEVICE_KEY_PATTERN, DeviceSchema } from "../model/schema.js";
|
|
10
|
+
import { HttpError } from "./http.js";
|
|
11
|
+
function isRecord(value) {
|
|
12
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
/** the top-level settings the UI edits (everything but the devices) */
|
|
15
|
+
export const SETTINGS_KEYS = [
|
|
16
|
+
'mqtt',
|
|
17
|
+
'storage',
|
|
18
|
+
'log',
|
|
19
|
+
'vendorId',
|
|
20
|
+
'productId',
|
|
21
|
+
'bridges',
|
|
22
|
+
'server',
|
|
23
|
+
'presets',
|
|
24
|
+
'importers',
|
|
25
|
+
];
|
|
26
|
+
export class ConfigStore {
|
|
27
|
+
path;
|
|
28
|
+
raw;
|
|
29
|
+
config;
|
|
30
|
+
overridden;
|
|
31
|
+
writing = Promise.resolve();
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.path = options.path;
|
|
34
|
+
this.raw = options.raw;
|
|
35
|
+
this.config = options.config;
|
|
36
|
+
this.overridden = options.overridden ?? [];
|
|
37
|
+
}
|
|
38
|
+
/** the raw devices, as the file has them */
|
|
39
|
+
get rawDevices() {
|
|
40
|
+
const devices = this.raw.devices;
|
|
41
|
+
return isRecord(devices) ? devices : {};
|
|
42
|
+
}
|
|
43
|
+
rawDevice(key) {
|
|
44
|
+
return this.rawDevices[key];
|
|
45
|
+
}
|
|
46
|
+
/** the raw settings (no devices), for the settings editor */
|
|
47
|
+
rawSettings() {
|
|
48
|
+
const settings = {};
|
|
49
|
+
for (const key of SETTINGS_KEYS) {
|
|
50
|
+
if (this.raw[key] !== undefined) {
|
|
51
|
+
settings[key] = this.raw[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return settings;
|
|
55
|
+
}
|
|
56
|
+
/** validate a raw device entry; the error names every problem with its path */
|
|
57
|
+
static parseDevice(raw) {
|
|
58
|
+
const result = DeviceSchema.safeParse(raw);
|
|
59
|
+
if (!result.success) {
|
|
60
|
+
throw new HttpError(400, 'the device is invalid', {
|
|
61
|
+
issues: result.error.issues.map((issue) => ({
|
|
62
|
+
level: 'error',
|
|
63
|
+
path: issue.path.map(String).join('.'),
|
|
64
|
+
message: issue.message,
|
|
65
|
+
})),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return result.data;
|
|
69
|
+
}
|
|
70
|
+
static checkKey(key) {
|
|
71
|
+
if (!DEVICE_KEY_PATTERN.test(key)) {
|
|
72
|
+
throw new HttpError(400, 'a device key is 1–64 characters of A–Z a–z 0–9 : . _ -');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** store a device raw and return it parsed; the file is written */
|
|
76
|
+
async saveDevice(key, raw) {
|
|
77
|
+
ConfigStore.checkKey(key);
|
|
78
|
+
if (!isRecord(raw)) {
|
|
79
|
+
throw new HttpError(400, 'a device is a JSON object');
|
|
80
|
+
}
|
|
81
|
+
const device = ConfigStore.parseDevice(raw);
|
|
82
|
+
const devices = this.rawDevices;
|
|
83
|
+
devices[key] = structuredClone(raw);
|
|
84
|
+
this.raw.devices = devices;
|
|
85
|
+
await this.write();
|
|
86
|
+
return device;
|
|
87
|
+
}
|
|
88
|
+
async deleteDevice(key) {
|
|
89
|
+
const devices = this.rawDevices;
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
91
|
+
delete devices[key];
|
|
92
|
+
this.raw.devices = devices;
|
|
93
|
+
await this.write();
|
|
94
|
+
}
|
|
95
|
+
/** replace the top-level settings; the environment's paths must not change */
|
|
96
|
+
async saveSettings(raw) {
|
|
97
|
+
if (!isRecord(raw)) {
|
|
98
|
+
throw new HttpError(400, 'the settings are a JSON object');
|
|
99
|
+
}
|
|
100
|
+
const next = { ...this.raw };
|
|
101
|
+
for (const key of SETTINGS_KEYS) {
|
|
102
|
+
if (raw[key] === undefined) {
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
104
|
+
delete next[key];
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
next[key] = structuredClone(raw[key]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const result = ConfigSchema.safeParse({ ...next, devices: {} });
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
throw new HttpError(400, `the settings are invalid:\n${z.prettifyError(result.error)}`);
|
|
113
|
+
}
|
|
114
|
+
const at = (source, path) => path.split('.').reduce((value, key) => (isRecord(value) ? value[key] : undefined), source);
|
|
115
|
+
for (const path of this.overridden) {
|
|
116
|
+
if (JSON.stringify(at(this.raw, path)) !== JSON.stringify(at(next, path))) {
|
|
117
|
+
throw new HttpError(409, `${path} is set by the environment and cannot be changed here`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const key of SETTINGS_KEYS) {
|
|
121
|
+
if (next[key] === undefined) {
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
123
|
+
delete this.raw[key];
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
this.raw[key] = next[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
await this.write();
|
|
130
|
+
return result.data;
|
|
131
|
+
}
|
|
132
|
+
/** replace every device (an import with `replace`), raw; returns the parsed entries */
|
|
133
|
+
async replaceDevices(raw) {
|
|
134
|
+
const parsed = new Map();
|
|
135
|
+
for (const [key, entry] of Object.entries(raw)) {
|
|
136
|
+
ConfigStore.checkKey(key);
|
|
137
|
+
parsed.set(key, ConfigStore.parseDevice(entry));
|
|
138
|
+
}
|
|
139
|
+
this.raw.devices = structuredClone(raw);
|
|
140
|
+
await this.write();
|
|
141
|
+
return parsed;
|
|
142
|
+
}
|
|
143
|
+
/** write the file atomically: a temporary file next to it, then a rename */
|
|
144
|
+
async write() {
|
|
145
|
+
const run = async () => {
|
|
146
|
+
await mkdir(dirname(this.path), { recursive: true });
|
|
147
|
+
const tmp = `${this.path}.${process.pid}.tmp`;
|
|
148
|
+
await writeFile(tmp, `${JSON.stringify(this.raw, null, 2)}\n`, 'utf8');
|
|
149
|
+
await rename(tmp, this.path);
|
|
150
|
+
};
|
|
151
|
+
this.writing = this.writing.then(run, run);
|
|
152
|
+
await this.writing;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=config-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-store.js","sourceRoot":"","sources":["../../src/server/config-store.ts"],"names":[],"mappings":"AAAA;;;qGAGqG;AAErG,OAAO,EAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAElC,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAC,YAAY,EAAc,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAC,kBAAkB,EAAE,YAAY,EAAoB,MAAM,oBAAoB,CAAC;AACvF,OAAO,EAAC,SAAS,EAAC,MAAM,WAAW,CAAC;AAEpC,SAAS,QAAQ,CAAC,KAAc;IAC5B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChF,CAAC;AAUD,uEAAuE;AACvE,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,MAAM;IACN,SAAS;IACT,KAAK;IACL,UAAU;IACV,WAAW;IACX,SAAS;IACT,QAAQ;IACR,SAAS;IACT,WAAW;CACL,CAAC;AAEX,MAAM,OAAO,WAAW;IACX,IAAI,CAAS;IACb,GAAG,CAA0B;IAC7B,MAAM,CAAS;IACf,UAAU,CAAoB;IAC/B,OAAO,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;IAEnD,YAAY,OAA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,CAAC,IAAI,CAAC,EAAE,EAAC,SAAS,EAAE,IAAI,EAAC,CAAC,CAAC;YACnD,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,MAAM,CAAC;YAC9C,MAAM,SAAS,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACvE,MAAM,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC,CAAC;QACF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAC3C,MAAM,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;CACJ"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
|
+
import type { Logger } from '../log.ts';
|
|
4
|
+
export type ServerEvent = {
|
|
5
|
+
type: 'hello';
|
|
6
|
+
version: string;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'mqtt';
|
|
9
|
+
topic: string;
|
|
10
|
+
payload: string;
|
|
11
|
+
retain: boolean;
|
|
12
|
+
ts: number;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'attribute';
|
|
15
|
+
key: string;
|
|
16
|
+
endpoint: string;
|
|
17
|
+
cluster: string;
|
|
18
|
+
attribute: string;
|
|
19
|
+
value: unknown;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'log';
|
|
22
|
+
entry: {
|
|
23
|
+
id: number;
|
|
24
|
+
ts: string;
|
|
25
|
+
level: string;
|
|
26
|
+
scope: string;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
29
|
+
} | {
|
|
30
|
+
type: 'devices';
|
|
31
|
+
} | {
|
|
32
|
+
type: 'bridges';
|
|
33
|
+
} | {
|
|
34
|
+
type: 'discovered';
|
|
35
|
+
} | {
|
|
36
|
+
type: 'status';
|
|
37
|
+
mqtt: boolean;
|
|
38
|
+
};
|
|
39
|
+
export declare class EventHub {
|
|
40
|
+
private readonly server;
|
|
41
|
+
private readonly clients;
|
|
42
|
+
private readonly log;
|
|
43
|
+
private readonly hello;
|
|
44
|
+
constructor(log: Logger, hello: () => ServerEvent);
|
|
45
|
+
get size(): number;
|
|
46
|
+
/** take over an HTTP upgrade for `/api/events` */
|
|
47
|
+
upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
48
|
+
broadcast(event: ServerEvent): void;
|
|
49
|
+
close(): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* The event stream: one WebSocket per browser tab, every event to every tab. The events are the
|
|
2
|
+
ones docs/api.md lists; the hub knows nothing about where they come from. A client may ask to
|
|
3
|
+
mute the MQTT events (a tab that is not on the live view does not need a busy broker's traffic). */
|
|
4
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
5
|
+
import { toJson } from "./http.js";
|
|
6
|
+
export class EventHub {
|
|
7
|
+
server = new WebSocketServer({ noServer: true });
|
|
8
|
+
clients = new Set();
|
|
9
|
+
log;
|
|
10
|
+
hello;
|
|
11
|
+
constructor(log, hello) {
|
|
12
|
+
this.log = log;
|
|
13
|
+
this.hello = hello;
|
|
14
|
+
}
|
|
15
|
+
get size() {
|
|
16
|
+
return this.clients.size;
|
|
17
|
+
}
|
|
18
|
+
/** take over an HTTP upgrade for `/api/events` */
|
|
19
|
+
upgrade(request, socket, head) {
|
|
20
|
+
this.server.handleUpgrade(request, socket, head, (ws) => {
|
|
21
|
+
const client = { socket: ws, mqtt: true };
|
|
22
|
+
this.clients.add(client);
|
|
23
|
+
ws.on('message', (data) => {
|
|
24
|
+
try {
|
|
25
|
+
const text = Array.isArray(data)
|
|
26
|
+
? Buffer.concat(data).toString('utf8')
|
|
27
|
+
: data instanceof ArrayBuffer
|
|
28
|
+
? Buffer.from(data).toString('utf8')
|
|
29
|
+
: data.toString('utf8');
|
|
30
|
+
const message = JSON.parse(text);
|
|
31
|
+
if (message.type === 'subscribe' && typeof message.mqtt === 'boolean') {
|
|
32
|
+
client.mqtt = message.mqtt;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// a client that talks nonsense is ignored
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
ws.on('close', () => {
|
|
40
|
+
this.clients.delete(client);
|
|
41
|
+
});
|
|
42
|
+
ws.on('error', (error) => {
|
|
43
|
+
this.log.debug(`event client: ${error.message}`);
|
|
44
|
+
this.clients.delete(client);
|
|
45
|
+
});
|
|
46
|
+
ws.send(toJson(this.hello()));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
broadcast(event) {
|
|
50
|
+
if (this.clients.size === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const text = toJson(event);
|
|
54
|
+
for (const client of this.clients) {
|
|
55
|
+
if (event.type === 'mqtt' && !client.mqtt) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (client.socket.readyState === WebSocket.OPEN) {
|
|
59
|
+
client.socket.send(text);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
close() {
|
|
64
|
+
for (const client of this.clients) {
|
|
65
|
+
client.socket.close();
|
|
66
|
+
}
|
|
67
|
+
this.clients.clear();
|
|
68
|
+
this.server.close();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/server/events.ts"],"names":[],"mappings":"AAAA;;sGAEsG;AAKtG,OAAO,EAAC,SAAS,EAAE,eAAe,EAAC,MAAM,IAAI,CAAC;AAG9C,OAAO,EAAC,MAAM,EAAC,MAAM,WAAW,CAAC;AAiBjC,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,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAChC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC1B,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;CACJ"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
export declare class HttpError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly details: unknown;
|
|
5
|
+
constructor(status: number, message: string, details?: unknown);
|
|
6
|
+
}
|
|
7
|
+
export interface Request {
|
|
8
|
+
method: string;
|
|
9
|
+
path: string;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
query: URLSearchParams;
|
|
12
|
+
/** the parsed JSON body, undefined without one */
|
|
13
|
+
body: unknown;
|
|
14
|
+
raw: IncomingMessage;
|
|
15
|
+
}
|
|
16
|
+
export type Handler = (request: Request, response: ServerResponse) => unknown;
|
|
17
|
+
/** JSON with matter.js' values made serialisable: bigints as strings, byte arrays as hex */
|
|
18
|
+
export declare function toJson(value: unknown): string;
|
|
19
|
+
export declare function sendJson(response: ServerResponse, status: number, value: unknown): void;
|
|
20
|
+
export declare class Router {
|
|
21
|
+
private readonly routes;
|
|
22
|
+
add(method: string, path: string, handler: Handler): this;
|
|
23
|
+
/** dispatch; false when no route matches the path at all */
|
|
24
|
+
dispatch(request: IncomingMessage, response: ServerResponse): Promise<boolean>;
|
|
25
|
+
}
|
|
26
|
+
/** serve the files of `root`; any path that is not a file gets `index.html` (the UI routes by hash) */
|
|
27
|
+
export declare function serveStatic(root: string, path: string, response: ServerResponse): Promise<void>;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/* The small HTTP toolkit the API is built on: a method + path router with `:params`, JSON in and out
|
|
2
|
+
with the errors the UI understands, and the static files of the built UI. No framework: the API is
|
|
3
|
+
thirty routes, and node:http does the rest. */
|
|
4
|
+
import { createReadStream } from 'node:fs';
|
|
5
|
+
import { stat } from 'node:fs/promises';
|
|
6
|
+
import { extname, join, normalize, resolve, sep } from 'node:path';
|
|
7
|
+
export class HttpError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
details;
|
|
10
|
+
constructor(status, message, details) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'HttpError';
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.details = details;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const MAX_BODY = 8 * 1024 * 1024;
|
|
18
|
+
/** JSON with matter.js' values made serialisable: bigints as strings, byte arrays as hex */
|
|
19
|
+
export function toJson(value) {
|
|
20
|
+
return JSON.stringify(value, (_key, v) => {
|
|
21
|
+
if (typeof v === 'bigint') {
|
|
22
|
+
return v.toString();
|
|
23
|
+
}
|
|
24
|
+
if (v instanceof Uint8Array) {
|
|
25
|
+
return Buffer.from(v).toString('hex');
|
|
26
|
+
}
|
|
27
|
+
return v;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export function sendJson(response, status, value) {
|
|
31
|
+
const text = value === undefined ? 'null' : toJson(value);
|
|
32
|
+
response.writeHead(status, {
|
|
33
|
+
'content-type': 'application/json; charset=utf-8',
|
|
34
|
+
'cache-control': 'no-store',
|
|
35
|
+
});
|
|
36
|
+
response.end(text);
|
|
37
|
+
}
|
|
38
|
+
async function readBody(request) {
|
|
39
|
+
const type = request.headers['content-type'] ?? '';
|
|
40
|
+
const chunks = [];
|
|
41
|
+
let size = 0;
|
|
42
|
+
for await (const chunk of request) {
|
|
43
|
+
const buffer = chunk;
|
|
44
|
+
size += buffer.length;
|
|
45
|
+
if (size > MAX_BODY) {
|
|
46
|
+
throw new HttpError(413, 'the request body is too large');
|
|
47
|
+
}
|
|
48
|
+
chunks.push(buffer);
|
|
49
|
+
}
|
|
50
|
+
if (chunks.length === 0) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
54
|
+
if (!type.includes('json')) {
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(text);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw new HttpError(400, `the request body is not valid JSON: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export class Router {
|
|
65
|
+
routes = [];
|
|
66
|
+
add(method, path, handler) {
|
|
67
|
+
const names = [];
|
|
68
|
+
const source = path
|
|
69
|
+
.split('/')
|
|
70
|
+
.map((segment) => {
|
|
71
|
+
if (segment.startsWith(':')) {
|
|
72
|
+
names.push(segment.slice(1));
|
|
73
|
+
return '([^/]+)';
|
|
74
|
+
}
|
|
75
|
+
return segment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
76
|
+
})
|
|
77
|
+
.join('/');
|
|
78
|
+
this.routes.push({ method, pattern: new RegExp(`^${source}$`), names, handler });
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/** dispatch; false when no route matches the path at all */
|
|
82
|
+
async dispatch(request, response) {
|
|
83
|
+
const url = new URL(request.url ?? '/', 'http://localhost');
|
|
84
|
+
const path = url.pathname;
|
|
85
|
+
let pathMatched = false;
|
|
86
|
+
for (const route of this.routes) {
|
|
87
|
+
const match = route.pattern.exec(path);
|
|
88
|
+
if (match === null) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
pathMatched = true;
|
|
92
|
+
if (route.method !== request.method) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const params = {};
|
|
96
|
+
route.names.forEach((name, index) => {
|
|
97
|
+
params[name] = decodeURIComponent(match[index + 1] ?? '');
|
|
98
|
+
});
|
|
99
|
+
try {
|
|
100
|
+
const body = await readBody(request);
|
|
101
|
+
const result = await route.handler({ method: route.method, path, params, query: url.searchParams, body, raw: request }, response);
|
|
102
|
+
if (!response.headersSent) {
|
|
103
|
+
sendJson(response, result === undefined ? 204 : 200, result ?? null);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (response.headersSent) {
|
|
108
|
+
response.end();
|
|
109
|
+
}
|
|
110
|
+
else if (error instanceof HttpError) {
|
|
111
|
+
sendJson(response, error.status, { error: error.message, details: error.details });
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
sendJson(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
if (pathMatched) {
|
|
120
|
+
sendJson(response, 405, { error: `${request.method ?? ''} is not allowed on ${path}` });
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const TYPES = {
|
|
127
|
+
'.html': 'text/html; charset=utf-8',
|
|
128
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
129
|
+
'.css': 'text/css; charset=utf-8',
|
|
130
|
+
'.json': 'application/json; charset=utf-8',
|
|
131
|
+
'.svg': 'image/svg+xml',
|
|
132
|
+
'.png': 'image/png',
|
|
133
|
+
'.ico': 'image/x-icon',
|
|
134
|
+
'.woff2': 'font/woff2',
|
|
135
|
+
'.map': 'application/json',
|
|
136
|
+
};
|
|
137
|
+
/** serve the files of `root`; any path that is not a file gets `index.html` (the UI routes by hash) */
|
|
138
|
+
export async function serveStatic(root, path, response) {
|
|
139
|
+
const base = resolve(root);
|
|
140
|
+
const wanted = normalize(join(base, path === '/' ? 'index.html' : path));
|
|
141
|
+
let file = wanted.startsWith(base + sep) ? wanted : join(base, 'index.html');
|
|
142
|
+
try {
|
|
143
|
+
const info = await stat(file);
|
|
144
|
+
if (!info.isFile()) {
|
|
145
|
+
file = join(base, 'index.html');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
file = join(base, 'index.html');
|
|
150
|
+
}
|
|
151
|
+
let size;
|
|
152
|
+
try {
|
|
153
|
+
size = (await stat(file)).size;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
157
|
+
response.end('the UI is not built: run `npm run build`');
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const hashed = /-[A-Za-z0-9_-]{8}\.[a-z0-9]+$/.test(file) && file.includes(`${sep}assets${sep}`);
|
|
161
|
+
response.writeHead(200, {
|
|
162
|
+
'content-type': TYPES[extname(file)] ?? 'application/octet-stream',
|
|
163
|
+
'content-length': size,
|
|
164
|
+
'cache-control': hashed ? 'public, max-age=31536000, immutable' : 'no-cache',
|
|
165
|
+
});
|
|
166
|
+
createReadStream(file).pipe(response);
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/server/http.ts"],"names":[],"mappings":"AAAA;;iDAEiD;AAEjD,OAAO,EAAC,gBAAgB,EAAC,MAAM,SAAS,CAAC;AACzC,OAAO,EAAC,IAAI,EAAC,MAAM,kBAAkB,CAAC;AAEtC,OAAO,EAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,EAAC,MAAM,WAAW,CAAC;AAEjE,MAAM,OAAO,SAAU,SAAQ,KAAK;IACvB,MAAM,CAAS;IACf,OAAO,CAAU;IAE1B,YAAY,MAAc,EAAE,OAAe,EAAE,OAAiB;QAC1D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AAqBD,MAAM,QAAQ,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;A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;IACjG,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE;QACpB,cAAc,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,0BAA0B;QAClE,gBAAgB,EAAE,IAAI;QACtB,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC,qCAAqC,CAAC,CAAC,CAAC,UAAU;KAC/E,CAAC,CAAC;IACH,gBAAgB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC1C,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type AdoptOptions, type AdoptResult, ha, type SyncEntry, z2m } from '../importers/index.ts';
|
|
2
|
+
import type { Logger } from '../log.ts';
|
|
3
|
+
import type { DevicesInput } from '../model/schema.ts';
|
|
4
|
+
import type { TopicCache } from '../mqtt/topic-cache.ts';
|
|
5
|
+
export type { AdoptOptions, AdoptResult, SyncEntry };
|
|
6
|
+
export interface Importer {
|
|
7
|
+
readonly kind: 'z2m' | 'ha-discovery';
|
|
8
|
+
/** the topics to subscribe for the source */
|
|
9
|
+
readonly topics: readonly string[];
|
|
10
|
+
/** take a message; true when it was one of the source's (the view may have changed) */
|
|
11
|
+
handle(topic: string, payload: Buffer): boolean;
|
|
12
|
+
/** take what the cache already holds */
|
|
13
|
+
load(cache: TopicCache): void;
|
|
14
|
+
/** what adopting would do to `devices`; nothing is touched */
|
|
15
|
+
diff(devices: DevicesInput): SyncEntry[];
|
|
16
|
+
/** the devices record after adopting, with the report */
|
|
17
|
+
adopt(devices: DevicesInput, options?: AdoptOptions): AdoptResult;
|
|
18
|
+
}
|
|
19
|
+
export declare class Z2mImporter implements Importer {
|
|
20
|
+
readonly kind: "z2m";
|
|
21
|
+
readonly source: z2m.Z2mSource;
|
|
22
|
+
private readonly log;
|
|
23
|
+
constructor(baseTopic?: string, log?: Logger);
|
|
24
|
+
get topics(): readonly string[];
|
|
25
|
+
handle(topic: string, payload: Buffer): boolean;
|
|
26
|
+
load(cache: TopicCache): void;
|
|
27
|
+
diff(devices: DevicesInput): SyncEntry[];
|
|
28
|
+
adopt(devices: DevicesInput, options?: AdoptOptions): AdoptResult;
|
|
29
|
+
}
|
|
30
|
+
export declare class HaImporter implements Importer {
|
|
31
|
+
readonly kind: "ha-discovery";
|
|
32
|
+
readonly source: ha.HaSource;
|
|
33
|
+
private readonly log;
|
|
34
|
+
constructor(prefix?: string, log?: Logger);
|
|
35
|
+
get topics(): readonly string[];
|
|
36
|
+
handle(topic: string, payload: Buffer): boolean;
|
|
37
|
+
load(cache: TopicCache): void;
|
|
38
|
+
diff(devices: DevicesInput): SyncEntry[];
|
|
39
|
+
adopt(devices: DevicesInput, options?: AdoptOptions): AdoptResult;
|
|
40
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* The importers as the server runs them: each one subscribes its source topics through the server,
|
|
2
|
+
takes the messages, and answers the adopt page with the importer's own report (`diff`) or applies an
|
|
3
|
+
adoption (`adopt`) over the devices record as it stands in the file. The logic — baseline, the
|
|
4
|
+
user's edits, conflicts, orphaning — is the importer's (docs/importers.md); the server only moves the
|
|
5
|
+
results into the file and the running bridge. */
|
|
6
|
+
import { ha, z2m } from "../importers/index.js";
|
|
7
|
+
export class Z2mImporter {
|
|
8
|
+
kind = 'z2m';
|
|
9
|
+
source;
|
|
10
|
+
log;
|
|
11
|
+
constructor(baseTopic = 'zigbee2mqtt', log) {
|
|
12
|
+
this.source = new z2m.Z2mSource(baseTopic);
|
|
13
|
+
this.log = log;
|
|
14
|
+
}
|
|
15
|
+
get topics() {
|
|
16
|
+
return this.source.topics;
|
|
17
|
+
}
|
|
18
|
+
handle(topic, payload) {
|
|
19
|
+
try {
|
|
20
|
+
return this.source.handle(topic, payload) !== undefined;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
this.log?.warn(`${topic}: ${error instanceof Error ? error.message : String(error)}`);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
load(cache) {
|
|
28
|
+
try {
|
|
29
|
+
this.source.load(cache);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
this.log?.warn(`loading the retained bridge topics failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
diff(devices) {
|
|
36
|
+
return z2m.diff(this.source.discover(), devices);
|
|
37
|
+
}
|
|
38
|
+
adopt(devices, options = {}) {
|
|
39
|
+
return z2m.adopt(devices, this.source.discover(), options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class HaImporter {
|
|
43
|
+
kind = 'ha-discovery';
|
|
44
|
+
source;
|
|
45
|
+
log;
|
|
46
|
+
constructor(prefix = 'homeassistant', log) {
|
|
47
|
+
this.source = new ha.HaSource(prefix);
|
|
48
|
+
this.log = log;
|
|
49
|
+
}
|
|
50
|
+
get topics() {
|
|
51
|
+
return this.source.topics;
|
|
52
|
+
}
|
|
53
|
+
handle(topic, payload) {
|
|
54
|
+
try {
|
|
55
|
+
return this.source.handle(topic, payload) !== undefined;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
this.log?.warn(`${topic}: ${error instanceof Error ? error.message : String(error)}`);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
load(cache) {
|
|
63
|
+
const failed = this.source.load(cache);
|
|
64
|
+
for (const topic of failed) {
|
|
65
|
+
this.log?.warn(`${topic}: the retained discovery payload does not parse`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
diff(devices) {
|
|
69
|
+
return ha.diff(this.source.discover(), devices);
|
|
70
|
+
}
|
|
71
|
+
adopt(devices, options = {}) {
|
|
72
|
+
return ha.adopt(devices, this.source.discover(), options);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=importers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"importers.js","sourceRoot":"","sources":["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CAAC,CAAC;QAC9E,CAAC;IACL,CAAC;IAED,IAAI,CAAC,OAAqB;QACtB,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;IACpD,CAAC;IAED,KAAK,CAAC,OAAqB,EAAE,UAAwB,EAAE;QACnD,OAAO,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;IAC9D,CAAC;CACJ"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Server, type ServerOptions, UI_DIST } from './server.ts';
|
|
2
|
+
export { ConfigStore } from './config-store.ts';
|
|
3
|
+
export { LogBuffer } from './log-buffer.ts';
|
|
4
|
+
export { HaImporter, type Importer, type SyncEntry, Z2mImporter } from './importers.ts';
|
|
5
|
+
export { Auth, hashPassword } from './auth.ts';
|
|
6
|
+
export { RESTART_EXIT_CODE, Updater } from './update.ts';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* The web server of docs/api.md: REST, the event stream, the built UI. */
|
|
2
|
+
export { Server, UI_DIST } from "./server.js";
|
|
3
|
+
export { ConfigStore } from "./config-store.js";
|
|
4
|
+
export { LogBuffer } from "./log-buffer.js";
|
|
5
|
+
export { HaImporter, Z2mImporter } from "./importers.js";
|
|
6
|
+
export { Auth, hashPassword } from "./auth.js";
|
|
7
|
+
export { RESTART_EXIT_CODE, Updater } from "./update.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAE1E,OAAO,EAAC,MAAM,EAAsB,OAAO,EAAC,MAAM,aAAa,CAAC;AAChE,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAC,UAAU,EAAiC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AACtF,OAAO,EAAC,IAAI,EAAE,YAAY,EAAC,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAC,iBAAiB,EAAE,OAAO,EAAC,MAAM,aAAa,CAAC"}
|