bridge: publish through a Publisher; devices no longer hold the broker client
src/transport.ts has the Publisher interface, MqttPublisher (the client the bridge has at the time) and MemoryPublisher (tests without a broker); src/topics.ts names the four topics the library publishes to. Device, AlexaStatusMessage, AlexaErrorResponse and sendSceneResponse shared three copies of the publish-and-report code: they now call one send() that resolves the topic or "" and never rejects. registerDevice() and addDevice() work before connect(); a send() without a connection resolves "" and the "error" event says to call connect(). unregisterDevice() and clearDevices() take the publisher from the device. Device.setMqttClient() is gone, the first constructor argument of Device is ignored, and the message classes take a Publisher where they took the client. Tests: 108 -> 113. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
46fa06728c
commit
6789a1a077
39 changed files with 839 additions and 246 deletions
27
dist/esm/compat/AlexaStatusMessage.js
vendored
27
dist/esm/compat/AlexaStatusMessage.js
vendored
|
|
@ -1,6 +1,8 @@
|
|||
import { randomUUID } from "crypto";
|
||||
import { changeReport, deferredResponse, response } from "../messages/build.js";
|
||||
import { StateBuilder } from "../messages/StateBuilder.js";
|
||||
import * as topics from "../topics.js";
|
||||
import { send } from "../transport.js";
|
||||
import { AlexaInterfaceType, TemperatureSensorScale } from "./enums.js";
|
||||
// The 1.x helpers report every temperature in Celsius, whatever scale the device works in
|
||||
function celsius(scale, value) {
|
||||
|
|
@ -11,11 +13,11 @@ function celsius(scale, value) {
|
|||
* device.getChangeReport(), the add...Prop() calls, send(). The values are not checked.
|
||||
*/
|
||||
export class AlexaStatusMessage {
|
||||
constructor(correlationToken, rootTopic, endpointId, mqttClient, isResponse = false, isDeferred = false, changeCause = null) {
|
||||
constructor(correlationToken, rootTopic, endpointId, publisher, isResponse = false, isDeferred = false, changeCause = null) {
|
||||
this.correlationToken = correlationToken;
|
||||
this.rootTopic = rootTopic;
|
||||
this.endpointId = endpointId;
|
||||
this.mqttClient = mqttClient;
|
||||
this.publisher = publisher;
|
||||
this.isResponse = isResponse;
|
||||
this.isDeferred = isDeferred;
|
||||
this.changeCause = changeCause;
|
||||
|
|
@ -106,23 +108,8 @@ export class AlexaStatusMessage {
|
|||
* to). Never rejects (1.5.1 did, so an un-caught send() could kill the host).
|
||||
*/
|
||||
send(sendAsync = false) {
|
||||
const topic = this.changeCause
|
||||
? `${this.rootTopic}/changeReport`
|
||||
: `${this.rootTopic}/${this.endpointId}/${sendAsync ? "deferredResponse" : "alexaResponce"}`; // "alexaResponce" is how Alex2MQTT and Alex2ESP spell the topic
|
||||
return new Promise((resolve) => {
|
||||
const failed = (err) => {
|
||||
if (this.onPublishError)
|
||||
this.onPublishError(err); // -> the bridge's "error" event (when somebody listens)
|
||||
resolve(""); // 1.5.1 rejected here, and an un-caught send() then killed the host on any broker hiccup
|
||||
};
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.stringify(this.toJSON());
|
||||
}
|
||||
catch (err) {
|
||||
return failed(err);
|
||||
}
|
||||
this.mqttClient.publish(topic, payload, (err) => (err ? failed(err) : resolve(topic)));
|
||||
});
|
||||
const answer = sendAsync ? topics.deferred : topics.response;
|
||||
const topic = this.changeCause ? topics.changeReport(this.rootTopic) : answer(this.rootTopic, this.endpointId);
|
||||
return send(this.publisher, topic, () => this.toJSON(), this.onPublishError);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue