inventoresed
Version:
Z-Wave driver written entirely in JavaScript/TypeScript
145 lines (128 loc) • 3.45 kB
text/typescript
import {
MessageOrCCLogEntry,
MessagePriority,
TransmitOptions,
ZWaveError,
ZWaveErrorCodes,
} from "@zwave-js/core";
import type { ZWaveHost } from "@zwave-js/host";
import type { SuccessIndicator } from "@zwave-js/serial";
import {
expectedCallback,
expectedResponse,
FunctionType,
gotDeserializationOptions,
Message,
MessageBaseOptions,
MessageDeserializationOptions,
MessageOptions,
MessageType,
messageTypes,
priority,
} from "@zwave-js/serial";
export enum SetSUCNodeIdStatus {
Succeeded = 0x05,
Failed = 0x06,
}
export interface SetSUCNodeIdRequestOptions extends MessageBaseOptions {
sucNodeId?: number;
enableSUC: boolean;
enableSIS: boolean;
transmitOptions?: TransmitOptions;
}
export class SetSUCNodeIdRequestBase extends Message {
public constructor(host: ZWaveHost, options: MessageOptions) {
if (
gotDeserializationOptions(options) &&
(new.target as any) !== SetSUCNodeIdRequestStatusReport
) {
return new SetSUCNodeIdRequestStatusReport(host, options);
}
super(host, options);
}
}
export class SetSUCNodeIdRequest extends SetSUCNodeIdRequestBase {
public constructor(
host: ZWaveHost,
options: MessageDeserializationOptions | SetSUCNodeIdRequestOptions,
) {
super(host, options);
if (gotDeserializationOptions(options)) {
throw new ZWaveError(
`${this.constructor.name}: deserialization not implemented`,
ZWaveErrorCodes.Deserialization_NotImplemented,
);
} else {
this.sucNodeId = options.sucNodeId ?? host.ownNodeId;
this.enableSUC = options.enableSUC;
this.enableSIS = options.enableSIS;
this.transmitOptions =
options.transmitOptions ?? TransmitOptions.DEFAULT;
}
}
public sucNodeId: number;
public enableSUC: boolean;
public enableSIS: boolean;
public transmitOptions: TransmitOptions;
public serialize(): Buffer {
this.payload = Buffer.from([
this.sucNodeId,
this.enableSUC ? 0x01 : 0x00,
this.transmitOptions,
this.enableSIS ? 0x01 : 0x00,
this.callbackId,
]);
return super.serialize();
}
public expectsCallback(): boolean {
if (this.sucNodeId === this.host.ownNodeId) return false;
return super.expectsCallback();
}
}
export class SetSUCNodeIdResponse extends Message implements SuccessIndicator {
public constructor(
host: ZWaveHost,
options: MessageDeserializationOptions,
) {
super(host, options);
this._wasExecuted = this.payload[0] !== 0;
}
isOK(): boolean {
return this._wasExecuted;
}
private _wasExecuted: boolean;
public get wasExecuted(): boolean {
return this._wasExecuted;
}
public toLogEntry(): MessageOrCCLogEntry {
return {
...super.toLogEntry(),
message: { "was executed": this.wasExecuted },
};
}
}
export class SetSUCNodeIdRequestStatusReport
extends SetSUCNodeIdRequestBase
implements SuccessIndicator
{
public constructor(
host: ZWaveHost,
options: MessageDeserializationOptions,
) {
super(host, options);
this.callbackId = this.payload[0];
this._status = this.payload[1];
}
private _status: SetSUCNodeIdStatus;
public get status(): SetSUCNodeIdStatus {
return this._status;
}
public isOK(): boolean {
return this._status === SetSUCNodeIdStatus.Succeeded;
}
}