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
|
|
@ -1,5 +1,4 @@
|
|||
import { EventEmitter } from "events";
|
||||
import type { MqttClient } from "mqtt";
|
||||
import { AlexaErrorResponse } from "../compat/AlexaErrorResponse.js";
|
||||
import { AlexaInterface } from "../compat/AlexaInterface.js";
|
||||
import { AlexaStatusMessage } from "../compat/AlexaStatusMessage.js";
|
||||
|
|
@ -13,6 +12,9 @@ import { EndpointHealth } from "../registry/interfaces/EndpointHealth.js";
|
|||
import { SchemaError } from "../registry/schema.js";
|
||||
import { DeclarationError } from "../registry/types.js";
|
||||
import type { Directives, EndpointView, InterfaceDescriptor, Label, Properties } from "../registry/types.js";
|
||||
import * as topics from "../topics.js";
|
||||
import { send } from "../transport.js";
|
||||
import type { Publisher } from "../transport.js";
|
||||
import { Capability, commonOptions } from "./Capability.js";
|
||||
import type { AnyCapability, CapabilityJson, Declaration } from "./Capability.js";
|
||||
import { checkCapability, checkCapabilityCount, checkEndpoint } from "./validate.js";
|
||||
|
|
@ -72,9 +74,16 @@ class Device extends EventEmitter {
|
|||
private capabilities: AnyCapability[] = [];
|
||||
/** Where a failed publish from this device or a message it built is reported; Alex2MQTT sets it (1.5.2). */
|
||||
public onPublishError?: (err: Error) => void;
|
||||
/**
|
||||
* What the device and the messages it builds publish through. The bridge sets it when the device is registered
|
||||
* and clears it when the device is unregistered; a MemoryPublisher here tests a device without a broker. While it
|
||||
* is null every send() resolves "" and reports why.
|
||||
*/
|
||||
public publisher: Publisher | null = null;
|
||||
|
||||
/** client is ignored: in 1.x it was the broker client, and a device could only be built after connect(). */
|
||||
constructor(
|
||||
private mqttClient: MqttClient,
|
||||
client: unknown,
|
||||
private rootTopic: string,
|
||||
public name: string,
|
||||
public endpointId: string,
|
||||
|
|
@ -87,11 +96,6 @@ class Device extends EventEmitter {
|
|||
super();
|
||||
}
|
||||
|
||||
/** Internal (1.5.2): Alex2MQTT.connect() re-binds every registered device to its new broker client after a disconnect(). */
|
||||
setMqttClient(client: MqttClient): void {
|
||||
this.mqttClient = client;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.name;
|
||||
}
|
||||
|
|
@ -124,7 +128,7 @@ class Device extends EventEmitter {
|
|||
correlationToken,
|
||||
this.rootTopic,
|
||||
this.endpointId,
|
||||
this.mqttClient
|
||||
this.publisher
|
||||
);
|
||||
msg.onPublishError = this.onPublishError;
|
||||
return msg;
|
||||
|
|
@ -138,7 +142,7 @@ class Device extends EventEmitter {
|
|||
correlationToken,
|
||||
this.rootTopic,
|
||||
this.endpointId,
|
||||
this.mqttClient,
|
||||
this.publisher,
|
||||
isResponse,
|
||||
isDeferred
|
||||
);
|
||||
|
|
@ -152,7 +156,7 @@ class Device extends EventEmitter {
|
|||
* Without a changed property send() publishes nothing and resolves with "": Alex2MQTT would drop the report.
|
||||
*/
|
||||
getChangeReport(cause: ChangeCause = "PHYSICAL_INTERACTION"): AlexaStatusMessage {
|
||||
const msg = new AlexaStatusMessage("", this.rootTopic, this.endpointId, this.mqttClient, false, false, cause);
|
||||
const msg = new AlexaStatusMessage("", this.rootTopic, this.endpointId, this.publisher, false, false, cause);
|
||||
msg.onPublishError = this.onPublishError;
|
||||
return msg;
|
||||
}
|
||||
|
|
@ -161,15 +165,9 @@ class Device extends EventEmitter {
|
|||
* (1.5.1). Resolves with the topic published to, or "" when the publish failed (never rejects, 1.5.2).
|
||||
*/
|
||||
sendSceneResponse(correlationToken: string, activated: boolean, cause: ChangeCause = "VOICE_INTERACTION", sendAsync = false): Promise<string> {
|
||||
const payload = sceneEvent({ endpointId: this.endpointId, correlationToken, activated, cause });
|
||||
const topic = `${this.rootTopic}/${this.endpointId}/${sendAsync ? "deferredResponse" : "alexaResponce"}`;
|
||||
return new Promise((resolve) => {
|
||||
this.mqttClient.publish(topic, JSON.stringify(payload), (err) => {
|
||||
if (!err) return resolve(topic);
|
||||
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
|
||||
});
|
||||
});
|
||||
const answer = sendAsync ? topics.deferred : topics.response;
|
||||
const build = () => sceneEvent({ endpointId: this.endpointId, correlationToken, activated, cause });
|
||||
return send(this.publisher, answer(this.rootTopic, this.endpointId), build, this.onPublishError);
|
||||
}
|
||||
/** The capabilities the device declared, in the order it declared them. */
|
||||
getCapabilities(): AnyCapability[] {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue