bridge: subscribe to discover and +/alexaDirective only

The bridge subscribed to <root>/#, so the broker sent back every discover_r, alexaResponce and changeReport
the bridge published, and the backend's <endpointId>/alexaDirective_e. It now subscribes to <root>/discover
and <root>/+/alexaDirective, both named in src/topics.ts; a directive topic is recognised by the root and the
last segment instead of by counting three segments.
The test reads the subscriptions from the broker, publishes seven other topics under the root and one
directive for each of two endpoints: the bridge receives the two directives and the discovery request,
nothing else. Tests: 148 -> 150 (the count in the body of 6789a1a, 108 -> 113, should read 143 -> 148).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 19:36:09 +00:00
parent 6789a1a077
commit e0fe9d2812
11 changed files with 225 additions and 93 deletions

61
dist/cjs/Alex2Node.js vendored
View file

@ -57,7 +57,7 @@ const DESCRIBED = [
* directive dispatch.
*
* Events (all optional to listen to - since 1.5.1 a broker outage never throws out of the library):
* "connect" connected (or reconnected) and subscribed to <root>/#
* "connect" connected (or reconnected) and subscribed to <root>/discover and <root>/+/alexaDirective
* "offline" the connection dropped; mqtt.js reconnects on its own (reconnectPeriod, default 1 s)
* "reconnect" a reconnect attempt starts
* "close" the connection closed
@ -116,7 +116,7 @@ class Alex2MQTT extends events_1.EventEmitter {
this.client.on("connect", () => {
this.connected = true;
this.log("Connected to MQTT broker");
this.client.subscribe(this.rootTopic + "/#", (err) => {
this.client.subscribe(topics.subscriptions(this.rootTopic), (err) => {
if (err) {
this.fail(err);
return;
@ -131,7 +131,7 @@ class Alex2MQTT extends events_1.EventEmitter {
this.client.on("error", (err) => { this.connected = false; this.fail(err); });
this.client.on("message", (topic, message) => {
this.log(`MQTT Message Received`, { topic, payload: message.toString() });
if (topic == `${this.rootTopic}/discover`) {
if (topic === topics.discover(this.rootTopic)) {
this.log("Discovery request received, getting device json...");
const deviceArray = this.describeDevices();
this.lastDiscoveryAt = new Date().toISOString();
@ -143,35 +143,34 @@ class Alex2MQTT extends events_1.EventEmitter {
this.log(`Discovery payloads published to ${result.topic}`, deviceArray);
this.emit("discover", deviceArray.length);
});
return;
}
else if (topic.split("/").length == 3) {
const [, endpointId, directiveType] = topic.split("/");
if (directiveType != "alexaDirective") {
return;
}
const device = this.devices.find((d) => d.endpointId === endpointId);
if (!device) {
this.log(`No device found for endpointId: ${endpointId}`);
return;
}
let payload;
try {
payload = JSON.parse(message.toString());
}
catch (e) {
this.log(`Failed to parse payload JSON`, e);
return;
}
if (!payload || !payload.header)
return;
this.emit("directive", { endpointId, namespace: payload.header.namespace, name: payload.header.name });
if (payload.header.namespace === "Alexa" && payload.header.name === "ReportState") {
this.log(`ReportState directive for device ${endpointId}`);
device.emit("ReportState", payload);
}
else {
device.emit("Event", payload, payload.header.namespace);
}
// Another topic can still arrive: a broker may deliver what an earlier session of this client id subscribed to
const endpointId = topics.directiveEndpoint(this.rootTopic, topic);
if (endpointId === null)
return;
const device = this.devices.find((d) => d.endpointId === endpointId);
if (!device) {
this.log(`No device found for endpointId: ${endpointId}`);
return;
}
let payload;
try {
payload = JSON.parse(message.toString());
}
catch (e) {
this.log(`Failed to parse payload JSON`, e);
return;
}
if (!payload || !payload.header)
return;
this.emit("directive", { endpointId, namespace: payload.header.namespace, name: payload.header.name });
if (payload.header.namespace === "Alexa" && payload.header.name === "ReportState") {
this.log(`ReportState directive for device ${endpointId}`);
device.emit("ReportState", payload);
}
else {
device.emit("Event", payload, payload.header.namespace);
}
});
}

20
dist/cjs/topics.js vendored
View file

@ -2,10 +2,28 @@
// The topics of the Alex2MQTT contract, named once. The backend and Alex2ESP use the same names, so none of them
// can change here alone.
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeReport = exports.deferred = exports.response = exports.discoverReply = void 0;
exports.changeReport = exports.deferred = exports.response = exports.subscriptions = exports.directive = exports.discoverReply = exports.discover = void 0;
exports.directiveEndpoint = directiveEndpoint;
/** Where the backend asks for the endpoints of the root. */
const discover = (root) => `${root}/discover`;
exports.discover = discover;
/** Where the bridge answers a discovery request with its endpoints. */
const discoverReply = (root) => `${root}/discover_r`;
exports.discoverReply = discoverReply;
/** Where the backend publishes the directives for one endpoint. */
const directive = (root, endpointId) => `${root}/${endpointId}/alexaDirective`;
exports.directive = directive;
/**
* What the bridge subscribes to: the discovery requests and the directives of every endpoint. Not <root>/#, which
* also delivers what the bridge itself publishes and the backend's <endpointId>/alexaDirective_e.
*/
const subscriptions = (root) => [(0, exports.discover)(root), (0, exports.directive)(root, "+")];
exports.subscriptions = subscriptions;
/** The endpointId of a directive topic, null for any other topic. */
function directiveEndpoint(root, topic) {
const [, endpointId, ...rest] = topic.startsWith(`${root}/`) ? topic.slice(root.length).split("/") : [];
return endpointId && rest.length === 1 && rest[0] === "alexaDirective" ? endpointId : null;
}
/** Where the answer to a directive goes. "alexaResponce" is how the backend and Alex2ESP spell it. */
const response = (root, endpointId) => `${root}/${endpointId}/alexaResponce`;
exports.response = response;

View file

@ -23,7 +23,7 @@ export declare const DEFAULT_HOST = "mqtt://Alex2MQTT.stormysdream.club:1883";
* directive dispatch.
*
* Events (all optional to listen to - since 1.5.1 a broker outage never throws out of the library):
* "connect" connected (or reconnected) and subscribed to <root>/#
* "connect" connected (or reconnected) and subscribed to <root>/discover and <root>/+/alexaDirective
* "offline" the connection dropped; mqtt.js reconnects on its own (reconnectPeriod, default 1 s)
* "reconnect" a reconnect attempt starts
* "close" the connection closed

61
dist/esm/Alex2Node.js vendored
View file

@ -18,7 +18,7 @@ const DESCRIBED = [
* directive dispatch.
*
* Events (all optional to listen to - since 1.5.1 a broker outage never throws out of the library):
* "connect" connected (or reconnected) and subscribed to <root>/#
* "connect" connected (or reconnected) and subscribed to <root>/discover and <root>/+/alexaDirective
* "offline" the connection dropped; mqtt.js reconnects on its own (reconnectPeriod, default 1 s)
* "reconnect" a reconnect attempt starts
* "close" the connection closed
@ -77,7 +77,7 @@ class Alex2MQTT extends EventEmitter {
this.client.on("connect", () => {
this.connected = true;
this.log("Connected to MQTT broker");
this.client.subscribe(this.rootTopic + "/#", (err) => {
this.client.subscribe(topics.subscriptions(this.rootTopic), (err) => {
if (err) {
this.fail(err);
return;
@ -92,7 +92,7 @@ class Alex2MQTT extends EventEmitter {
this.client.on("error", (err) => { this.connected = false; this.fail(err); });
this.client.on("message", (topic, message) => {
this.log(`MQTT Message Received`, { topic, payload: message.toString() });
if (topic == `${this.rootTopic}/discover`) {
if (topic === topics.discover(this.rootTopic)) {
this.log("Discovery request received, getting device json...");
const deviceArray = this.describeDevices();
this.lastDiscoveryAt = new Date().toISOString();
@ -104,35 +104,34 @@ class Alex2MQTT extends EventEmitter {
this.log(`Discovery payloads published to ${result.topic}`, deviceArray);
this.emit("discover", deviceArray.length);
});
return;
}
else if (topic.split("/").length == 3) {
const [, endpointId, directiveType] = topic.split("/");
if (directiveType != "alexaDirective") {
return;
}
const device = this.devices.find((d) => d.endpointId === endpointId);
if (!device) {
this.log(`No device found for endpointId: ${endpointId}`);
return;
}
let payload;
try {
payload = JSON.parse(message.toString());
}
catch (e) {
this.log(`Failed to parse payload JSON`, e);
return;
}
if (!payload || !payload.header)
return;
this.emit("directive", { endpointId, namespace: payload.header.namespace, name: payload.header.name });
if (payload.header.namespace === "Alexa" && payload.header.name === "ReportState") {
this.log(`ReportState directive for device ${endpointId}`);
device.emit("ReportState", payload);
}
else {
device.emit("Event", payload, payload.header.namespace);
}
// Another topic can still arrive: a broker may deliver what an earlier session of this client id subscribed to
const endpointId = topics.directiveEndpoint(this.rootTopic, topic);
if (endpointId === null)
return;
const device = this.devices.find((d) => d.endpointId === endpointId);
if (!device) {
this.log(`No device found for endpointId: ${endpointId}`);
return;
}
let payload;
try {
payload = JSON.parse(message.toString());
}
catch (e) {
this.log(`Failed to parse payload JSON`, e);
return;
}
if (!payload || !payload.header)
return;
this.emit("directive", { endpointId, namespace: payload.header.namespace, name: payload.header.name });
if (payload.header.namespace === "Alexa" && payload.header.name === "ReportState") {
this.log(`ReportState directive for device ${endpointId}`);
device.emit("ReportState", payload);
}
else {
device.emit("Event", payload, payload.header.namespace);
}
});
}

11
dist/esm/topics.d.ts vendored
View file

@ -1,5 +1,16 @@
/** Where the backend asks for the endpoints of the root. */
export declare const discover: (root: string) => string;
/** Where the bridge answers a discovery request with its endpoints. */
export declare const discoverReply: (root: string) => string;
/** Where the backend publishes the directives for one endpoint. */
export declare const directive: (root: string, endpointId: string) => string;
/**
* What the bridge subscribes to: the discovery requests and the directives of every endpoint. Not <root>/#, which
* also delivers what the bridge itself publishes and the backend's <endpointId>/alexaDirective_e.
*/
export declare const subscriptions: (root: string) => string[];
/** The endpointId of a directive topic, null for any other topic. */
export declare function directiveEndpoint(root: string, topic: string): string | null;
/** Where the answer to a directive goes. "alexaResponce" is how the backend and Alex2ESP spell it. */
export declare const response: (root: string, endpointId: string) => string;
/** Where the answer goes once a DeferredResponse was sent for the directive. */

14
dist/esm/topics.js vendored
View file

@ -1,7 +1,21 @@
// The topics of the Alex2MQTT contract, named once. The backend and Alex2ESP use the same names, so none of them
// can change here alone.
/** Where the backend asks for the endpoints of the root. */
export const discover = (root) => `${root}/discover`;
/** Where the bridge answers a discovery request with its endpoints. */
export const discoverReply = (root) => `${root}/discover_r`;
/** Where the backend publishes the directives for one endpoint. */
export const directive = (root, endpointId) => `${root}/${endpointId}/alexaDirective`;
/**
* What the bridge subscribes to: the discovery requests and the directives of every endpoint. Not <root>/#, which
* also delivers what the bridge itself publishes and the backend's <endpointId>/alexaDirective_e.
*/
export const subscriptions = (root) => [discover(root), directive(root, "+")];
/** The endpointId of a directive topic, null for any other topic. */
export function directiveEndpoint(root, topic) {
const [, endpointId, ...rest] = topic.startsWith(`${root}/`) ? topic.slice(root.length).split("/") : [];
return endpointId && rest.length === 1 && rest[0] === "alexaDirective" ? endpointId : null;
}
/** Where the answer to a directive goes. "alexaResponce" is how the backend and Alex2ESP spell it. */
export const response = (root, endpointId) => `${root}/${endpointId}/alexaResponce`;
/** Where the answer goes once a DeferredResponse was sent for the directive. */

View file

@ -23,7 +23,7 @@ export declare const DEFAULT_HOST = "mqtt://Alex2MQTT.stormysdream.club:1883";
* directive dispatch.
*
* Events (all optional to listen to - since 1.5.1 a broker outage never throws out of the library):
* "connect" connected (or reconnected) and subscribed to <root>/#
* "connect" connected (or reconnected) and subscribed to <root>/discover and <root>/+/alexaDirective
* "offline" the connection dropped; mqtt.js reconnects on its own (reconnectPeriod, default 1 s)
* "reconnect" a reconnect attempt starts
* "close" the connection closed

View file

@ -1,5 +1,16 @@
/** Where the backend asks for the endpoints of the root. */
export declare const discover: (root: string) => string;
/** Where the bridge answers a discovery request with its endpoints. */
export declare const discoverReply: (root: string) => string;
/** Where the backend publishes the directives for one endpoint. */
export declare const directive: (root: string, endpointId: string) => string;
/**
* What the bridge subscribes to: the discovery requests and the directives of every endpoint. Not <root>/#, which
* also delivers what the bridge itself publishes and the backend's <endpointId>/alexaDirective_e.
*/
export declare const subscriptions: (root: string) => string[];
/** The endpointId of a directive topic, null for any other topic. */
export declare function directiveEndpoint(root: string, topic: string): string | null;
/** Where the answer to a directive goes. "alexaResponce" is how the backend and Alex2ESP spell it. */
export declare const response: (root: string, endpointId: string) => string;
/** Where the answer goes once a DeferredResponse was sent for the directive. */