1.5.1: broker errors as events that never crash the host (error only when listened to; connect/offline/reconnect/close; connected flag), configurable broker host + mqtt options + log hook, Alexa.ChangeReport via Device.getChangeReport (<root>/changeReport), SceneController discovery + sendSceneResponse, addCapability options (proactivelyReported), unregisterDevice/clearDevices/getDevices/disconnect, discover/directive events, promise-returning quiet send(); tests on an in-process broker (aedes); readme
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
e96850bdd6
commit
717af632de
16 changed files with 2080 additions and 225 deletions
|
|
@ -47,8 +47,15 @@ interface ContextProperty {
|
|||
instance?: string;
|
||||
}
|
||||
|
||||
/** Why a ChangeReport is sent (Alexa.ChangeReport payload.change.cause.type). */
|
||||
export type ChangeCause = "APP_INTERACTION" | "PHYSICAL_INTERACTION" | "PERIODIC_POLL" | "RULE_TRIGGER" | "VOICE_INTERACTION";
|
||||
|
||||
export class AlexaStatusMessage {
|
||||
private context: { properties: ContextProperty[] } = { properties: [] };
|
||||
/** ChangeReport only: the properties that changed (payload.change.properties); the rest go to context. */
|
||||
private changeProps: ContextProperty[] = [];
|
||||
private changeCause: ChangeCause | null = null;
|
||||
private target: "context" | "change" = "context";
|
||||
private event: {
|
||||
header: DirectiveHeader;
|
||||
endpoint: DirectiveEndpoint;
|
||||
|
|
@ -65,16 +72,21 @@ export class AlexaStatusMessage {
|
|||
endpointId: string,
|
||||
mqttClient: MqttClient,
|
||||
isResponse: boolean = false,
|
||||
isDeferred: boolean = false
|
||||
isDeferred: boolean = false,
|
||||
changeCause: ChangeCause | null = null
|
||||
) {
|
||||
this.rootTopic = rootTopic;
|
||||
this.endpointId = endpointId;
|
||||
this.mqttClient = mqttClient;
|
||||
this.isDeferred = isDeferred;
|
||||
this.changeCause = changeCause;
|
||||
if (changeCause) this.target = "change";
|
||||
this.event = {
|
||||
header: {
|
||||
namespace: "Alexa",
|
||||
name: isDeferred
|
||||
name: changeCause
|
||||
? "ChangeReport"
|
||||
: isDeferred
|
||||
? "DeferredResponse"
|
||||
: isResponse
|
||||
? "Response"
|
||||
|
|
@ -112,9 +124,33 @@ export class AlexaStatusMessage {
|
|||
uncertaintyInMilliseconds,
|
||||
};
|
||||
if (instance) prop.instance = instance;
|
||||
this.context.properties.push(prop);
|
||||
(this.target === "change" ? this.changeProps : this.context.properties).push(prop);
|
||||
return this;
|
||||
}
|
||||
/** ChangeReport: the add*Prop calls that follow describe what CHANGED (the default for a change report). */
|
||||
public changed(): this {
|
||||
this.target = "change";
|
||||
return this;
|
||||
}
|
||||
/** ChangeReport: the add*Prop calls that follow describe the other, unchanged properties (context). */
|
||||
public unchanged(): this {
|
||||
this.target = "context";
|
||||
return this;
|
||||
}
|
||||
/** True for a ChangeReport (Device.getChangeReport). */
|
||||
public isChangeReport(): boolean {
|
||||
return this.changeCause !== null;
|
||||
}
|
||||
/** The message as it will be published (for tests and logging). */
|
||||
public toJSON(): { event: any; context: { properties: ContextProperty[] } | null } {
|
||||
if (this.changeCause) {
|
||||
return {
|
||||
event: { ...this.event, payload: { change: { cause: { type: this.changeCause }, properties: this.changeProps } } },
|
||||
context: this.context,
|
||||
};
|
||||
}
|
||||
return { context: this.isDeferred ? null : this.context, event: this.event };
|
||||
}
|
||||
public addModeControllerProp(instance: string, value: string, uncertaintyInMs = 0): this {
|
||||
return this.addProperty(
|
||||
AlexaInterfaceType.MODE_CONTROLLER,
|
||||
|
|
@ -250,23 +286,18 @@ export class AlexaStatusMessage {
|
|||
return this;
|
||||
}
|
||||
|
||||
public send(sendAsync: boolean): void {
|
||||
const payload = {
|
||||
context: this.isDeferred ? null : this.context,
|
||||
event: this.event,
|
||||
};
|
||||
const topic = `${this.rootTopic}/${this.endpointId}/${sendAsync?"deferredResponse":"alexaResponce"}`; //Yes this should be response but it is incorrect in both Alex2MQTT and Alex2ESP so for consistency is is wrong here too
|
||||
const payloadStr = JSON.stringify(payload);
|
||||
|
||||
this.mqttClient.publish(topic, payloadStr, (err) => {
|
||||
if (err) {
|
||||
console.error(
|
||||
"[AlexaStatusMessage] Failed to publish status message:",
|
||||
err
|
||||
);
|
||||
} else {
|
||||
console.log(`[AlexaStatusMessage] Sent message to ${topic}`);
|
||||
}
|
||||
/**
|
||||
* Publish: a Response/StateReport to <root>/<endpoint>/alexaResponce (sendAsync: deferredResponse), a ChangeReport
|
||||
* to <root>/changeReport (Alex2MQTT adds the user's token and posts it to the Alexa event gateway). Resolves with
|
||||
* the topic; rejects on a publish error (1.4.0 only logged).
|
||||
*/
|
||||
public send(sendAsync: boolean = false): Promise<string> {
|
||||
const payloadStr = JSON.stringify(this.toJSON());
|
||||
const topic = this.changeCause
|
||||
? `${this.rootTopic}/changeReport`
|
||||
: `${this.rootTopic}/${this.endpointId}/${sendAsync ? "deferredResponse" : "alexaResponce"}`; //Yes this should be response but it is incorrect in both Alex2MQTT and Alex2ESP so for consistency is is wrong here too
|
||||
return new Promise((resolve, reject) => {
|
||||
this.mqttClient.publish(topic, payloadStr, (err) => (err ? reject(err) : resolve(topic)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue