homebridge-plugin-utils 1.34.0 → 2.0.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/README.md +130 -2
- package/build/eslint-plugin/README.md +164 -0
- package/build/eslint-plugin/config.mjs +308 -0
- package/build/eslint-plugin/index.mjs +7 -0
- package/build/eslint-plugin/plugin.mjs +46 -0
- package/build/eslint-plugin/rules/blank-line-after-open-brace.mjs +137 -0
- package/build/eslint-plugin/rules/blank-line-after-open-brace.test.mjs +112 -0
- package/build/eslint-plugin/rules/comment-style.mjs +190 -0
- package/build/eslint-plugin/rules/comment-style.test.mjs +190 -0
- package/build/eslint-plugin/rules/enforce-node-protocol.mjs +114 -0
- package/build/eslint-plugin/rules/enforce-node-protocol.test.mjs +116 -0
- package/build/eslint-plugin/rules/paren-comparisons-in-logical.mjs +94 -0
- package/build/eslint-plugin/rules/paren-comparisons-in-logical.test.mjs +107 -0
- package/build/eslint-plugin/rules/split-type-imports.mjs +354 -0
- package/build/eslint-plugin/rules/split-type-imports.test.mjs +268 -0
- package/build/eslint-plugin/test-setup.mjs +21 -0
- package/build/fs-ops.mjs +184 -0
- package/build/tsconfig.json +18 -3
- package/dist/backpressure.d.ts +128 -0
- package/dist/backpressure.js +273 -0
- package/dist/backpressure.js.map +1 -0
- package/dist/cli/index.d.ts +95 -0
- package/dist/cli/index.js +355 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/clock-double.d.ts +93 -0
- package/dist/clock-double.js +141 -0
- package/dist/clock-double.js.map +1 -0
- package/dist/clock.d.ts +39 -0
- package/dist/clock.js +34 -0
- package/dist/clock.js.map +1 -0
- package/dist/eslint-plugin/config.d.mts +193 -0
- package/dist/eslint-plugin/index.d.mts +2 -0
- package/dist/eslint-plugin/plugin.d.mts +87 -0
- package/dist/eslint-plugin/rules/blank-line-after-open-brace.d.mts +19 -0
- package/dist/eslint-plugin/rules/comment-style.d.mts +15 -0
- package/dist/eslint-plugin/rules/enforce-node-protocol.d.mts +18 -0
- package/dist/eslint-plugin/rules/paren-comparisons-in-logical.d.mts +15 -0
- package/dist/eslint-plugin/rules/split-type-imports.d.mts +16 -0
- package/dist/featureOptions-docs.d.ts +96 -0
- package/dist/featureOptions-docs.js +193 -0
- package/dist/featureOptions-docs.js.map +1 -0
- package/dist/featureOptions.d.ts +674 -0
- package/dist/featureOptions.js +870 -0
- package/dist/featureOptions.js.map +1 -0
- package/dist/ffmpeg/codecs.d.ts +256 -72
- package/dist/ffmpeg/codecs.js +477 -257
- package/dist/ffmpeg/codecs.js.map +1 -1
- package/dist/ffmpeg/dgram-util.d.ts +46 -0
- package/dist/ffmpeg/dgram-util.js +38 -0
- package/dist/ffmpeg/dgram-util.js.map +1 -0
- package/dist/ffmpeg/exec.d.ts +83 -64
- package/dist/ffmpeg/exec.js +77 -86
- package/dist/ffmpeg/exec.js.map +1 -1
- package/dist/ffmpeg/fmp4.d.ts +70 -4
- package/dist/ffmpeg/fmp4.js +100 -11
- package/dist/ffmpeg/fmp4.js.map +1 -1
- package/dist/ffmpeg/hap-enums.d.ts +214 -0
- package/dist/ffmpeg/hap-enums.js +92 -0
- package/dist/ffmpeg/hap-enums.js.map +1 -0
- package/dist/ffmpeg/index.d.ts +16 -9
- package/dist/ffmpeg/index.js +6 -0
- package/dist/ffmpeg/index.js.map +1 -1
- package/dist/ffmpeg/mp4-assembler.d.ts +120 -0
- package/dist/ffmpeg/mp4-assembler.js +387 -0
- package/dist/ffmpeg/mp4-assembler.js.map +1 -0
- package/dist/ffmpeg/mp4-parser.d.ts +94 -0
- package/dist/ffmpeg/mp4-parser.js +130 -0
- package/dist/ffmpeg/mp4-parser.js.map +1 -0
- package/dist/ffmpeg/options.d.ts +62 -149
- package/dist/ffmpeg/options.js +608 -499
- package/dist/ffmpeg/options.js.map +1 -1
- package/dist/ffmpeg/process.d.ts +143 -91
- package/dist/ffmpeg/process.js +406 -265
- package/dist/ffmpeg/process.js.map +1 -1
- package/dist/ffmpeg/record.d.ts +302 -188
- package/dist/ffmpeg/record.js +408 -560
- package/dist/ffmpeg/record.js.map +1 -1
- package/dist/ffmpeg/recording-process-double.d.ts +144 -0
- package/dist/ffmpeg/recording-process-double.js +168 -0
- package/dist/ffmpeg/recording-process-double.js.map +1 -0
- package/dist/ffmpeg/rtp-parser.d.ts +70 -0
- package/dist/ffmpeg/rtp-parser.js +77 -0
- package/dist/ffmpeg/rtp-parser.js.map +1 -0
- package/dist/ffmpeg/rtp.d.ts +198 -141
- package/dist/ffmpeg/rtp.js +473 -244
- package/dist/ffmpeg/rtp.js.map +1 -1
- package/dist/ffmpeg/settings.d.ts +5 -1
- package/dist/ffmpeg/settings.js +20 -2
- package/dist/ffmpeg/settings.js.map +1 -1
- package/dist/ffmpeg/stream.d.ts +57 -107
- package/dist/ffmpeg/stream.js +121 -148
- package/dist/ffmpeg/stream.js.map +1 -1
- package/dist/formatters.d.ts +106 -0
- package/dist/formatters.js +174 -0
- package/dist/formatters.js.map +1 -0
- package/dist/homebridge-enums.d.ts +30 -0
- package/dist/homebridge-enums.js +17 -0
- package/dist/homebridge-enums.js.map +1 -0
- package/dist/index.d.ts +12 -5
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/logclient/auth.d.ts +114 -0
- package/dist/logclient/auth.js +199 -0
- package/dist/logclient/auth.js.map +1 -0
- package/dist/logclient/cli-run.d.ts +76 -0
- package/dist/logclient/cli-run.js +639 -0
- package/dist/logclient/cli-run.js.map +1 -0
- package/dist/logclient/cli.d.ts +3 -0
- package/dist/logclient/cli.js +97 -0
- package/dist/logclient/cli.js.map +1 -0
- package/dist/logclient/client.d.ts +145 -0
- package/dist/logclient/client.js +600 -0
- package/dist/logclient/client.js.map +1 -0
- package/dist/logclient/config.d.ts +173 -0
- package/dist/logclient/config.js +199 -0
- package/dist/logclient/config.js.map +1 -0
- package/dist/logclient/endpoints.d.ts +54 -0
- package/dist/logclient/endpoints.js +73 -0
- package/dist/logclient/endpoints.js.map +1 -0
- package/dist/logclient/filter.d.ts +45 -0
- package/dist/logclient/filter.js +51 -0
- package/dist/logclient/filter.js.map +1 -0
- package/dist/logclient/frame.d.ts +93 -0
- package/dist/logclient/frame.js +203 -0
- package/dist/logclient/frame.js.map +1 -0
- package/dist/logclient/index.d.ts +31 -0
- package/dist/logclient/index.js +12 -0
- package/dist/logclient/index.js.map +1 -0
- package/dist/logclient/parser.d.ts +211 -0
- package/dist/logclient/parser.js +393 -0
- package/dist/logclient/parser.js.map +1 -0
- package/dist/logclient/rest.d.ts +41 -0
- package/dist/logclient/rest.js +111 -0
- package/dist/logclient/rest.js.map +1 -0
- package/dist/logclient/settings.d.ts +15 -0
- package/dist/logclient/settings.js +64 -0
- package/dist/logclient/settings.js.map +1 -0
- package/dist/logclient/socket-double.d.ts +201 -0
- package/dist/logclient/socket-double.js +384 -0
- package/dist/logclient/socket-double.js.map +1 -0
- package/dist/logclient/socket.d.ts +257 -0
- package/dist/logclient/socket.js +620 -0
- package/dist/logclient/socket.js.map +1 -0
- package/dist/logclient/stitch.d.ts +83 -0
- package/dist/logclient/stitch.js +146 -0
- package/dist/logclient/stitch.js.map +1 -0
- package/dist/logclient/time-expression.d.ts +42 -0
- package/dist/logclient/time-expression.js +181 -0
- package/dist/logclient/time-expression.js.map +1 -0
- package/dist/logclient/time-window.d.ts +38 -0
- package/dist/logclient/time-window.js +53 -0
- package/dist/logclient/time-window.js.map +1 -0
- package/dist/logclient/types.d.ts +107 -0
- package/dist/logclient/types.js +6 -0
- package/dist/logclient/types.js.map +1 -0
- package/dist/mqttClient.d.ts +287 -0
- package/dist/mqttClient.js +433 -0
- package/dist/mqttClient.js.map +1 -0
- package/dist/service.d.ts +64 -15
- package/dist/service.js +93 -66
- package/dist/service.js.map +1 -1
- package/dist/ui/featureOptions.js +870 -0
- package/dist/ui/featureOptions.js.map +1 -0
- package/dist/ui/formatters.js +174 -0
- package/dist/ui/formatters.js.map +1 -0
- package/dist/ui/pluginConfigSession.mjs +141 -0
- package/dist/ui/webUi-featureOptions/categoryState.mjs +135 -0
- package/dist/ui/webUi-featureOptions/effects/keyboard.mjs +61 -0
- package/dist/ui/webUi-featureOptions/effects/persist.mjs +226 -0
- package/dist/ui/webUi-featureOptions/effects/theme.mjs +398 -0
- package/dist/ui/webUi-featureOptions/effects/tokens.mjs +152 -0
- package/dist/ui/webUi-featureOptions/rendering.mjs +431 -0
- package/dist/ui/webUi-featureOptions/selectors.mjs +360 -0
- package/dist/ui/webUi-featureOptions/state.mjs +319 -0
- package/dist/ui/webUi-featureOptions/store.mjs +181 -0
- package/dist/ui/webUi-featureOptions/utils.mjs +200 -0
- package/dist/ui/webUi-featureOptions/views/connectionError.mjs +152 -0
- package/dist/ui/webUi-featureOptions/views/deviceInfo.mjs +80 -0
- package/dist/ui/webUi-featureOptions/views/header.mjs +77 -0
- package/dist/ui/webUi-featureOptions/views/nav.mjs +327 -0
- package/dist/ui/webUi-featureOptions/views/options.mjs +521 -0
- package/dist/ui/webUi-featureOptions/views/search.mjs +395 -0
- package/dist/ui/webUi-featureOptions.mjs +677 -0
- package/dist/ui/webUi.mjs +192 -87
- package/dist/util.d.ts +597 -45
- package/dist/util.js +787 -68
- package/dist/util.js.map +1 -1
- package/package.json +33 -15
- package/build/eslint-rules.mjs +0 -511
- package/dist/featureoptions.d.ts +0 -264
- package/dist/featureoptions.js +0 -480
- package/dist/featureoptions.js.map +0 -1
- package/dist/mqttclient.d.ts +0 -178
- package/dist/mqttclient.js +0 -310
- package/dist/mqttclient.js.map +0 -1
- package/dist/ui/featureoptions.js +0 -480
- package/dist/ui/featureoptions.js.map +0 -1
- package/dist/ui/webUi-featureoptions.mjs +0 -3765
package/dist/mqttclient.js
DELETED
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
/* Copyright(C) 2017-2026, HJD (https://github.com/hjdhjd). All rights reserved.
|
|
2
|
-
*
|
|
3
|
-
* mqttclient.ts: MQTT connectivity class for Homebridge plugins.
|
|
4
|
-
*/
|
|
5
|
-
import { connect } from "mqtt";
|
|
6
|
-
import util from "node:util";
|
|
7
|
-
const MQTT_DEFAULT_RECONNECT_INTERVAL = 60;
|
|
8
|
-
/**
|
|
9
|
-
* MQTT connectivity and topic management class for Homebridge plugins.
|
|
10
|
-
*
|
|
11
|
-
* This class manages connection, publishing, subscription, and message handling for an MQTT broker, and provides convenience methods for Homebridge accessories to
|
|
12
|
-
* interact with MQTT topics using a standard topic prefix.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* const mqtt = new MqttClient("mqtt://localhost:1883", "homebridge", log);
|
|
18
|
-
*
|
|
19
|
-
* // Publish a message to a topic.
|
|
20
|
-
* mqtt.publish("device1", "status", "on");
|
|
21
|
-
*
|
|
22
|
-
* // Subscribe to a topic.
|
|
23
|
-
* mqtt.subscribe("device1", "status", (msg) => {
|
|
24
|
-
*
|
|
25
|
-
* console.log(msg.toString());
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* // Subscribe to a 'get' topic and automatically publish a value in response.
|
|
29
|
-
* mqtt.subscribeGet("device1", "temperature", "Temperature", () => "21.5");
|
|
30
|
-
*
|
|
31
|
-
* // Subscribe to a 'set' topic and handle value changes.
|
|
32
|
-
* mqtt.subscribeSet("device1", "switch", "Switch", (value) => {
|
|
33
|
-
*
|
|
34
|
-
* console.log("Switch set to", value);
|
|
35
|
-
* });
|
|
36
|
-
*
|
|
37
|
-
* // Unsubscribe from a topic.
|
|
38
|
-
* mqtt.unsubscribe("device1", "status");
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export class MqttClient {
|
|
42
|
-
brokerUrl;
|
|
43
|
-
isConnected;
|
|
44
|
-
reconnectInterval;
|
|
45
|
-
log;
|
|
46
|
-
mqtt;
|
|
47
|
-
subscriptions;
|
|
48
|
-
topicPrefix;
|
|
49
|
-
/**
|
|
50
|
-
* Creates a new MQTT client for connecting to a broker and managing topics with a given prefix.
|
|
51
|
-
*
|
|
52
|
-
* @param brokerUrl - The MQTT broker URL (e.g., "mqtt://localhost:1883").
|
|
53
|
-
* @param topicPrefix - Prefix to use for all MQTT topics (e.g., "homebridge").
|
|
54
|
-
* @param log - Logger for debug and info messages.
|
|
55
|
-
* @param reconnectInterval - Optional. Interval (in seconds) to wait between reconnection attempts. Defaults to 60 seconds.
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
*
|
|
59
|
-
* ```ts
|
|
60
|
-
* const mqtt = new MqttClient("mqtt://localhost", "homebridge", log);
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* @remarks URL must conform to formats supported by {@link https://github.com/mqttjs/MQTT.js | MQTT.js}.
|
|
64
|
-
*/
|
|
65
|
-
constructor(brokerUrl, topicPrefix, log, reconnectInterval = MQTT_DEFAULT_RECONNECT_INTERVAL) {
|
|
66
|
-
this.brokerUrl = brokerUrl;
|
|
67
|
-
this.isConnected = false;
|
|
68
|
-
this.log = log;
|
|
69
|
-
this.mqtt = null;
|
|
70
|
-
this.reconnectInterval = reconnectInterval;
|
|
71
|
-
this.subscriptions = new Map();
|
|
72
|
-
this.topicPrefix = topicPrefix;
|
|
73
|
-
this.configure();
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Initializes and connects the MQTT client to the broker, setting up event handlers for connection, messages, and errors.
|
|
77
|
-
*
|
|
78
|
-
* Catches invalid broker URLs and logs errors. Handles all major MQTT client events internally.
|
|
79
|
-
*/
|
|
80
|
-
configure() {
|
|
81
|
-
// Try to connect to the MQTT broker and make sure we catch any URL errors.
|
|
82
|
-
try {
|
|
83
|
-
this.mqtt = connect(this.brokerUrl, { reconnectPeriod: this.reconnectInterval * 1000, rejectUnauthorized: false });
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
if (error instanceof Error) {
|
|
87
|
-
switch (error.message) {
|
|
88
|
-
case "Missing protocol":
|
|
89
|
-
this.log.error("MQTT Broker: Invalid URL provided: %s.", this.brokerUrl);
|
|
90
|
-
break;
|
|
91
|
-
default:
|
|
92
|
-
this.log.error("MQTT Broker: Error: %s.", error.message);
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// We've been unable to even attempt to connect. It's likely we have a configuration issue - we're done here.
|
|
98
|
-
if (!this.mqtt) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
// Notify the user when we connect to the broker.
|
|
102
|
-
this.mqtt.on("connect", () => {
|
|
103
|
-
this.isConnected = true;
|
|
104
|
-
// Inform users, while redacting authentication credentials.
|
|
105
|
-
this.log.info("MQTT Broker: Connected to %s (topic: %s).", this.brokerUrl.replace(/^(.*:\/\/.*:)(.*)(@.*)$/, "$1REDACTED$3"), this.topicPrefix);
|
|
106
|
-
});
|
|
107
|
-
// Notify the user when we've disconnected.
|
|
108
|
-
this.mqtt.on("close", () => {
|
|
109
|
-
// We only inform users if we're already connected. Otherwise, we're likely in an error state and that's logged elsewhere.
|
|
110
|
-
if (!this.isConnected) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
this.isConnected = false;
|
|
114
|
-
// Inform users.
|
|
115
|
-
this.log.info("MQTT Broker: Connection closed.");
|
|
116
|
-
});
|
|
117
|
-
// Process inbound messages and pass it to the right message handler.
|
|
118
|
-
this.mqtt.on("message", (topic, message) => {
|
|
119
|
-
void this.subscriptions.get(topic)?.(message);
|
|
120
|
-
});
|
|
121
|
-
// Notify the user when there's a connectivity error.
|
|
122
|
-
this.mqtt.on("error", (error) => {
|
|
123
|
-
const logError = (message) => {
|
|
124
|
-
this.log.error("MQTT Broker: %s. Will retry again in %s second%s.", message, this.reconnectInterval, this.reconnectInterval !== 1 ? "s" : "");
|
|
125
|
-
};
|
|
126
|
-
switch (error.code) {
|
|
127
|
-
case "ECONNREFUSED":
|
|
128
|
-
logError("Connection refused");
|
|
129
|
-
break;
|
|
130
|
-
case "ECONNRESET":
|
|
131
|
-
logError("Connection reset");
|
|
132
|
-
break;
|
|
133
|
-
case "ENOTFOUND":
|
|
134
|
-
this.mqtt?.end(true);
|
|
135
|
-
this.log.error("MQTT Broker: Hostname or IP address not found.");
|
|
136
|
-
break;
|
|
137
|
-
default:
|
|
138
|
-
logError(util.inspect(error, { sorted: true }));
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Publishes a message to a topic for a specific device.
|
|
145
|
-
*
|
|
146
|
-
* Expands the topic using the topic prefix and device ID, then publishes the provided message string.
|
|
147
|
-
*
|
|
148
|
-
* @param id - The device or accessory identifier.
|
|
149
|
-
* @param topic - The topic name to publish to.
|
|
150
|
-
* @param message - The message payload to publish.
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
*
|
|
154
|
-
* ```ts
|
|
155
|
-
* mqtt.publish("device1", "status", "on");
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
publish(id, topic, message) {
|
|
159
|
-
const expandedTopic = this.expandTopic(id, topic);
|
|
160
|
-
// No valid topic returned, we're done.
|
|
161
|
-
if (!expandedTopic) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
this.log.debug("MQTT publish: %s Message: %s.", expandedTopic, message);
|
|
165
|
-
// By default, we publish as: pluginTopicPrefix/id/topic
|
|
166
|
-
this.mqtt?.publish(expandedTopic, message);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Subscribes to a topic for a specific device and registers a handler for incoming messages.
|
|
170
|
-
*
|
|
171
|
-
* The topic is expanded using the prefix and device ID, and the callback will be called for each message received.
|
|
172
|
-
*
|
|
173
|
-
* @param id - The device or accessory identifier.
|
|
174
|
-
* @param topic - The topic name to subscribe to.
|
|
175
|
-
* @param callback - Handler function called with the message buffer.
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
*
|
|
179
|
-
* ```ts
|
|
180
|
-
* mqtt.subscribe("device1", "status", (msg) => {
|
|
181
|
-
*
|
|
182
|
-
* console.log(msg.toString());
|
|
183
|
-
* });
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
subscribe(id, topic, callback) {
|
|
187
|
-
const expandedTopic = this.expandTopic(id, topic);
|
|
188
|
-
// No valid topic returned, we're done.
|
|
189
|
-
if (!expandedTopic) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
this.log.debug("MQTT subscribe: %s.", expandedTopic);
|
|
193
|
-
// Add to our callback list.
|
|
194
|
-
this.subscriptions.set(expandedTopic, callback);
|
|
195
|
-
// Tell MQTT we're subscribing to this event.
|
|
196
|
-
// By default, we subscribe as: pluginTopicPrefix/id/topic
|
|
197
|
-
this.mqtt?.subscribe(expandedTopic);
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Subscribes to a '<topic>/get' topic and publishes a value in response to "true" messages.
|
|
201
|
-
*
|
|
202
|
-
* When a message "true" is received on the '<topic>/get' topic, this method will publish the result of `getValue()` on the main topic. The log will record each status
|
|
203
|
-
* publication event.
|
|
204
|
-
*
|
|
205
|
-
* @param id - The device or accessory identifier.
|
|
206
|
-
* @param topic - The topic name to use.
|
|
207
|
-
* @param type - A human-readable label for log messages (e.g., "Temperature").
|
|
208
|
-
* @param getValue - Function to get the value to publish as a string.
|
|
209
|
-
* @param log - Optional logger for status output. Defaults to the class logger.
|
|
210
|
-
*
|
|
211
|
-
* @example
|
|
212
|
-
*
|
|
213
|
-
* ```ts
|
|
214
|
-
* mqtt.subscribeGet("device1", "temperature", "Temperature", () => "21.5");
|
|
215
|
-
* ```
|
|
216
|
-
*/
|
|
217
|
-
subscribeGet(id, topic, type, getValue, log = this.log) {
|
|
218
|
-
// Subscribe to the get topic and publish the current value when requested.
|
|
219
|
-
this.subscribe(id, topic + "/get", (message) => {
|
|
220
|
-
const value = message.toString().toLowerCase();
|
|
221
|
-
// We only respond to "true" messages on the get topic.
|
|
222
|
-
if (value !== "true") {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
this.publish(id, topic, getValue());
|
|
226
|
-
log.info("MQTT: %s status published.", type);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Subscribes to a '<topic>/set' topic and calls a setter when a message is received.
|
|
231
|
-
*
|
|
232
|
-
* The `setValue` function is called with both a normalized value and the raw string. Handles both synchronous and promise-based setters. Logs when set messages are
|
|
233
|
-
* received and when errors occur.
|
|
234
|
-
*
|
|
235
|
-
* @param id - The device or accessory identifier.
|
|
236
|
-
* @param topic - The topic name to use.
|
|
237
|
-
* @param type - A human-readable label for log messages (e.g., "Switch").
|
|
238
|
-
* @param setValue - Function to call when a value is set. Can be synchronous or return a Promise.
|
|
239
|
-
* @param log - Optional logger for status output. Defaults to the class logger.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
*
|
|
243
|
-
* ```ts
|
|
244
|
-
* mqtt.subscribeSet("device1", "switch", "Switch", (value) => {
|
|
245
|
-
*
|
|
246
|
-
* console.log("Switch set to", value);
|
|
247
|
-
* });
|
|
248
|
-
* ```
|
|
249
|
-
*/
|
|
250
|
-
subscribeSet(id, topic, type, setValue, log = this.log) {
|
|
251
|
-
// Subscribe to the set topic and invoke the setter callback when a value is received.
|
|
252
|
-
this.subscribe(id, topic + "/set", async (message) => {
|
|
253
|
-
const value = message.toString().toLowerCase();
|
|
254
|
-
// Set our value and inform the user.
|
|
255
|
-
try {
|
|
256
|
-
await setValue(value, message.toString());
|
|
257
|
-
log.info("MQTT: set message received for %s: %s.", type, value);
|
|
258
|
-
}
|
|
259
|
-
catch (error) {
|
|
260
|
-
log.error("MQTT: error setting message received for %s: %s. %s", type, value, error);
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Unsubscribes from a topic for a specific device, removing its message handler.
|
|
266
|
-
*
|
|
267
|
-
* @param id - The device or accessory identifier.
|
|
268
|
-
* @param topic - The topic name to unsubscribe from.
|
|
269
|
-
*
|
|
270
|
-
* @example
|
|
271
|
-
*
|
|
272
|
-
* ```ts
|
|
273
|
-
* mqtt.unsubscribe("device1", "status");
|
|
274
|
-
* ```
|
|
275
|
-
*/
|
|
276
|
-
unsubscribe(id, topic) {
|
|
277
|
-
const expandedTopic = this.expandTopic(id, topic);
|
|
278
|
-
// No valid topic returned, we're done.
|
|
279
|
-
if (!expandedTopic) {
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
this.subscriptions.delete(expandedTopic);
|
|
283
|
-
this.mqtt?.unsubscribe(expandedTopic);
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Expands a topic string into a fully-formed topic path including the prefix and device ID.
|
|
287
|
-
*
|
|
288
|
-
* Returns `null` if the device ID is missing or empty.
|
|
289
|
-
*
|
|
290
|
-
* @param id - The device or accessory identifier.
|
|
291
|
-
* @param topic - The topic name to expand.
|
|
292
|
-
*
|
|
293
|
-
* @returns The expanded topic string, or `null` if the ID is missing.
|
|
294
|
-
*
|
|
295
|
-
* @example
|
|
296
|
-
*
|
|
297
|
-
* ```ts
|
|
298
|
-
* const topic = mqtt['expandTopic']("device1", "status");
|
|
299
|
-
* // topic = "homebridge/device1/status"
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
expandTopic(id, topic) {
|
|
303
|
-
// No id, we're done.
|
|
304
|
-
if (!id) {
|
|
305
|
-
return null;
|
|
306
|
-
}
|
|
307
|
-
return this.topicPrefix + "/" + id + "/" + topic;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
//# sourceMappingURL=mqttclient.js.map
|
package/dist/mqttclient.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mqttclient.js","sourceRoot":"","sources":["../src/mqttclient.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,EAAmC,OAAO,EAAE,MAAM,MAAM,CAAC;AAChE,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAA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