From 300dfa50e5b723da401b8dfe1b7730774c4a9336 Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Wed, 23 Oct 2024 22:13:31 +0200 Subject: [PATCH] chore: update project snippets to match new boilerplate (#7308) --- .vscode/typescript.code-snippets | 643 ++++++++++++++++++++----------- 1 file changed, 419 insertions(+), 224 deletions(-) diff --git a/.vscode/typescript.code-snippets b/.vscode/typescript.code-snippets index 5799ddeb1af3..e90d01cb4ee6 100644 --- a/.vscode/typescript.code-snippets +++ b/.vscode/typescript.code-snippets @@ -4,24 +4,24 @@ "prefix": "zwmsg", "body": [ "import {", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveApplicationHost, ZWaveHost } from \"@zwave-js/host\";", "import {", "\tFunctionType,", - "\tgotDeserializationOptions,", "\tMessage,", - "\tMessageBaseOptions,", - "\tMessageDeserializationOptions,", + "\ttype MessageBaseOptions,", + "\ttype MessageEncodingContext,", + "\ttype MessageParsingContext,", + "\ttype MessageRaw,", "\tMessageType,", "\tmessageTypes,", "\tpriority,", "} from \"@zwave-js/serial\";", "", - "export interface ${1}RequestOptions extends MessageBaseOptions {", + "export interface ${1}RequestOptions {", "\t${0:someProperty: number;}", "}", "", @@ -29,27 +29,33 @@ "@priority(MessagePriority.${2:Normal})", "export class ${1}Request extends Message {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions | ${1}RequestOptions,", + "\t\toptions: ${1}RequestOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "\t", + "\t\t// return new this({", + "\t\t// \t// ...", + "\t\t// });", + "\t}", + "", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -68,26 +74,26 @@ "prefix": "zwmsgres", "body": [ "import {", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveApplicationHost, ZWaveHost } from \"@zwave-js/host\";", "import {", "\texpectedResponse,", "\tFunctionType,", - "\tgotDeserializationOptions,", "\tMessage,", - "\tMessageBaseOptions,", - "\tMessageDeserializationOptions,", + "\ttype MessageBaseOptions,", + "\ttype MessageEncodingContext,", + "\ttype MessageParsingContext,", + "\ttype MessageRaw,", "\tMessageType,", "\tmessageTypes,", "\tpriority,", - "\tSuccessIndicator,", + "\ttype SuccessIndicator,", "} from \"@zwave-js/serial\";", "", - "export interface ${1}RequestOptions extends MessageBaseOptions {", + "export interface ${1}RequestOptions {", "\t${0:someProperty: number;}", "}", "", @@ -96,27 +102,33 @@ "@expectedResponse(FunctionType.${1})", "export class ${1}Request extends Message {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions | ${1}RequestOptions,", + "\t\toptions: ${1}RequestOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "\t", + "\t\t// return new this({", + "\t\t// \t// ...", + "\t\t// });", + "\t}", + "", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -129,11 +141,28 @@ "\t}", "}", "", + "export interface ${1}ResponseOptions {", + "\twasSent: boolean", + "}", + "", "@messageTypes(MessageType.Response, FunctionType.${1})", "export class ${1}Response extends Message implements SuccessIndicator {", - "\tpublic constructor(host: ZWaveHost, options: MessageDeserializationOptions) {", - "\t\tsuper(host, options);", - "\t\tthis.wasSent = this.payload[0] !== 0;", + "\tpublic constructor(", + "\t\toptions: ${1}ResponseOptions & MessageBaseOptions,", + "\t) {", + "\t\tsuper(options);", + "\t\tthis.wasSent = options.wasSent;", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Response {", + "\t\tconst wasSent = raw.payload[0] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\twasSent,", + "\t\t});", "\t}", "", "\tisOK(): boolean {", @@ -156,42 +185,43 @@ "prefix": "zwmsgrescb", "body": [ "import {", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveApplicationHost, ZWaveHost } from \"@zwave-js/host\";", "import {", "\texpectedCallback,", "\texpectedResponse,", "\tFunctionType,", - "\tgotDeserializationOptions,", "\tMessage,", - "\tMessageBaseOptions,", - "\tMessageDeserializationOptions,", - "\tMessageOptions,", + "\ttype MessageBaseOptions,", + "\ttype MessageEncodingContext,", + "\tMessageOrigin,", + "\ttype MessageParsingContext,", + "\ttype MessageRaw,", "\tMessageType,", "\tmessageTypes,", "\tpriority,", - "\tSuccessIndicator,", + "\ttype SuccessIndicator,", "} from \"@zwave-js/serial\";", "", "@messageTypes(MessageType.Request, FunctionType.${1:Dummy})", "@priority(MessagePriority.${2:Normal})", "export class ${1}RequestBase extends Message {", - "\tpublic constructor(host: ZWaveHost, options: MessageOptions) {", - "\t\tif (", - "\t\t\tgotDeserializationOptions(options) &&", - "\t\t\t(new.target as any) !== ${1}Callback", - "\t\t) {", - "\t\t\treturn new ${1}Callback(host, options);", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}RequestBase {", + "\t\tif (ctx.origin === MessageOrigin.Host) {", + "\t\t\treturn ${1}Request.from(raw, ctx);", + "\t\t} else {", + "\t\t\treturn ${1}Callback.from(raw, ctx);", "\t\t}", - "\t\tsuper(host, options);", "\t}", "}", "", - "export interface ${1}RequestOptions extends MessageBaseOptions {", + "export interface ${1}RequestOptions {", "\t${0:someProperty: number;}", "}", "", @@ -199,28 +229,35 @@ "@expectedCallback(FunctionType.${1})", "export class ${1}Request extends ${1}RequestBase {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions | ${1}RequestOptions,", + "\t\toptions: ${1}RequestOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "\t", + "\t\t// return new this({", + "\t\t// \t// ...", + "\t\t// });", + "\t}", + "", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", + "\t\tthis.assertCallbackId();", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t\tthis.callbackId,", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -233,11 +270,28 @@ "\t}", "}", "", + "export interface ${1}ResponseOptions {", + "\twasSent: boolean", + "}", + "", "@messageTypes(MessageType.Response, FunctionType.${1})", "export class ${1}Response extends Message {", - "\tpublic constructor(host: ZWaveHost, options: MessageDeserializationOptions) {", - "\t\tsuper(host, options);", - "\t\tthis.wasSent = this.payload[0] !== 0;", + "\tpublic constructor(", + "\t\toptions: ${1}ResponseOptions & MessageBaseOptions,", + "\t) {", + "\t\tsuper(options);", + "\t\tthis.wasSent = options.wasSent;", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Response {", + "\t\tconst wasSent = raw.payload[0] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\twasSent,", + "\t\t});", "\t}", "", "\tpublic readonly wasSent: boolean;", @@ -250,14 +304,33 @@ "\t}", "}", "", + "export interface ${1}CallbackOptions {", + "\tsuccess: boolean", + "}", + "", "export class ${1}Callback", "\textends ${1}RequestBase", "\timplements SuccessIndicator {", - "\tpublic constructor(host: ZWaveHost, options: MessageDeserializationOptions) {", - "\t\tsuper(host, options);", + "\tpublic constructor(", + "\t\toptions: ${1}CallbackOptions & MessageBaseOptions,", + "\t) {", + "\t\tsuper(options);", + "", + "\t\tthis.callbackId = options.callbackId;", + "\t\tthis.success = options.success;", + "\t}", "", - "\t\tthis.callbackId = this.payload[0];", - "\t\tthis.success = this.payload[1] !== 0;", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tconst callbackId = raw.payload[0];", + "\t\tconst success = raw.payload[1] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\tcallbackId,", + "\t\t\tsuccess,", + "\t\t});", "\t}", "", "\tisOK(): boolean {", @@ -283,69 +356,77 @@ "prefix": "zwmsgcb", "body": [ "import {", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveApplicationHost, ZWaveHost } from \"@zwave-js/host\";", "import {", - "\tFunctionType,", - "\tMessageType,", "\texpectedCallback,", - "\tgotDeserializationOptions,", + "\tFunctionType,", "\tMessage,", - "\tMessageBaseOptions,", - "\tMessageDeserializationOptions,", - "\tMessageOptions,", + "\ttype MessageBaseOptions,", + "\ttype MessageEncodingContext,", + "\tMessageOrigin,", + "\ttype MessageParsingContext,", + "\ttype MessageRaw,", + "\tMessageType,", "\tmessageTypes,", "\tpriority,", - "\tSuccessIndicator,", + "\ttype SuccessIndicator,", "} from \"@zwave-js/serial\";", "", "@messageTypes(MessageType.Request, FunctionType.${1:Dummy})", "@priority(MessagePriority.${2:Normal})", "export class ${1}RequestBase extends Message {", - "\tpublic constructor(host: ZWaveHost, options: MessageOptions) {", - "\t\tif (", - "\t\t\tgotDeserializationOptions(options) &&", - "\t\t\t(new.target as any) !== ${1}Callback", - "\t\t) {", - "\t\t\treturn new ${1}Callback(host, options);", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}RequestBase {", + "\t\tif (ctx.origin === MessageOrigin.Host) {", + "\t\t\treturn ${1}Request.from(raw, ctx);", + "\t\t} else {", + "\t\t\treturn ${1}Callback.from(raw, ctx);", "\t\t}", - "\t\tsuper(host, options);", "\t}", "}", "", - "export interface ${1}RequestOptions extends MessageBaseOptions {", + "export interface ${1}RequestOptions {", "\t${0:someProperty: number;}", "}", "", "@expectedCallback(FunctionType.${1})", "export class ${1}Request extends ${1}RequestBase {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions | ${1}RequestOptions,", + "\t\toptions: ${1}RequestOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "\t", + "\t\t// return new this({", + "\t\t// \t// ...", + "\t\t// });", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", + "\t\tthis.assertCallbackId();", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t\tthis.callbackId,", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -358,14 +439,33 @@ "\t}", "}", "", + "export interface ${1}CallbackOptions {", + "\tsuccess: boolean", + "}", + "", "export class ${1}Callback", "\textends ${1}RequestBase", "\timplements SuccessIndicator {", - "\tpublic constructor(host: ZWaveHost, options: MessageDeserializationOptions) {", - "\t\tsuper(host, options);", + "\tpublic constructor(", + "\t\toptions: ${1}CallbackOptions & MessageBaseOptions,", + "\t) {", + "\t\tsuper(options);", "", - "\t\tthis.callbackId = this.payload[0];", - "\t\tthis.success = this.payload[1] !== 0;", + "\t\tthis.callbackId = options.callbackId;", + "\t\tthis.success = options.success;", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tconst callbackId = raw.payload[0];", + "\t\tconst success = raw.payload[1] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\tcallbackId,", + "\t\t\tsuccess,", + "\t\t});", "\t}", "", "\tisOK(): boolean {", @@ -392,26 +492,22 @@ "body": [ "import {", "\tcreateSimpleReflectionDecorator,", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", - "\tMessageRecord,", + "\ttype MessageRecord,", "\tvalidatePayload,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveHost } from \"@zwave-js/host\";", - "import type {", - "\tDeserializingMessageConstructor,", - "\tSuccessIndicator,", - "} from \"@zwave-js/serial\";", "import {", "\texpectedResponse,", "\tFunctionType,", - "\tgotDeserializationOptions,", "\tMessage,", - "\tMessageBaseOptions,", - "\tMessageDeserializationOptions,", - "\tMessageOptions,", + "\ttype MessageBaseOptions,", + "\ttype MessageConstructor,", + "\ttype MessageEncodingContext,", + "\ttype MessageParsingContext,", + "\ttype MessageRaw,", "\tMessageType,", "\tmessageTypes,", "\tpriority,", @@ -425,12 +521,12 @@ "// We need to define the decorators for Requests and Responses separately", "const {", "\tdecorator: subCommandRequest,", - "\t// lookupConstructor: getSubCommandRequestConstructor,", + "\tlookupConstructor: getSubCommandRequestConstructor,", "\tlookupValue: getSubCommandForRequest,", "} = createSimpleReflectionDecorator<", "\t${1}Request,", "\t[command: ${1}Command],", - "\tDeserializingMessageConstructor<${1}Request>", + "\tMessageConstructor<${1}Request>", ">({", "\tname: \"subCommandRequest\",", "});", @@ -438,10 +534,11 @@ "const {", "\tdecorator: subCommandResponse,", "\tlookupConstructor: getSubCommandResponseConstructor,", + "\tlookupValue: getSubCommandForResponse,", "} = createSimpleReflectionDecorator<", "\t${1}Response,", "\t[command: ${1}Command],", - "\tDeserializingMessageConstructor<${1}Response>", + "\tMessageConstructor<${1}Response>", ">({", "\tname: \"subCommandResponse\",", "});", @@ -454,31 +551,53 @@ "\treturn (sent as ${1}Request).command === received.command;", "}", "", + "export interface ${1}RequestOptions {", + "\tcommand?: ${1}Command;", + "}", + "", "@messageTypes(MessageType.Request, FunctionType.${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}})", "@priority(MessagePriority.${2:Normal})", "@expectedResponse(testResponseFor${1}Request)", "export class ${1}Request extends Message {", - "\tpublic constructor(host: ZWaveHost, options: MessageOptions = {}) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\tthis.command = getSubCommandForRequest(this)!;", + "\tpublic constructor(", + "\t\toptions: ${1}RequestOptions & MessageBaseOptions,", + "\t) {", + "\t\tsuper(options);", + "\t\tthis.command = options.command ?? getSubCommandForRequest(this)!;", + "\t}", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Request {", + "\t\tconst command: ${1}Command = raw.payload[0];", + "\t\tconst payload = raw.payload.subarray(1);", + "", + "\t\tconst CommandConstructor = getSubCommandRequestConstructor(", + "\t\t\tcommand,", + "\t\t);", + "\t\tif (CommandConstructor) {", + "\t\t\treturn CommandConstructor.from(", + "\t\t\t\traw.withPayload(payload),", + "\t\t\t\tctx,", + "\t\t\t) as ${1}Request;", "\t\t}", + "", + "\t\tconst ret = new ${1}Request({", + "\t\t\tcommand,", + "\t\t});", + "\t\tret.payload = payload;", + "\t\treturn ret;", "\t}", "", "\tpublic command: ${1}Command;", "", - "\tpublic serialize(): Buffer {", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", "\t\tthis.payload = Buffer.concat([", "\t\t\tBuffer.from([this.command]),", "\t\t\tthis.payload,", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -495,23 +614,41 @@ "\t}", "}", "", + "export interface ${1}ResponseOptions {", + "\tcommand?: ${1}Command;", + "}", + "", "@messageTypes(MessageType.Response, FunctionType.${1})", "export class ${1}Response extends Message {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions,", + "\t\toptions: ${1}ResponseOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tthis.command = this.payload[0];", + "\t\tsuper(options);", + "\t\tthis.command = options.command ?? getSubCommandForResponse(this)!;", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}Response {", + "\t\tconst command: ${1}Command = raw.payload[0];", + "\t\tconst payload = raw.payload.subarray(1);", "", "\t\tconst CommandConstructor = getSubCommandResponseConstructor(", - "\t\t\tthis.command,", + "\t\t\tcommand,", "\t\t);", - "\t\tif (CommandConstructor && (new.target as any) !== CommandConstructor) {", - "\t\t\treturn new CommandConstructor(host, options);", + "\t\tif (CommandConstructor) {", + "\t\t\treturn CommandConstructor.from(", + "\t\t\t\traw.withPayload(payload),", + "\t\t\t\tctx,", + "\t\t\t) as ${1}Response;", "\t\t}", "", - "\t\tthis.payload = this.payload.slice(1);", + "\t\tconst ret = new ${1}Response({", + "\t\t\tcommand,", + "\t\t});", + "\t\tret.payload = payload;", + "\t\treturn ret;", "\t}", "", "\tpublic command: ${1}Command;", @@ -537,38 +674,40 @@ "body": [ "// =============================================================================", "", - "export interface ${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}}_${2:SomeCommand}RequestOptions extends MessageBaseOptions {", + "export interface ${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}}_${2:SomeCommand}RequestOptions {", "\t${0:someProperty: number;}", "}", "", "@subCommandRequest(${1}Command.${2})", "export class ${1}_${2}Request extends ${1}Request {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions:", - "\t\t\t| MessageDeserializationOptions", - "\t\t\t| ${1}_${2}RequestOptions,", + "\t\toptions: ${1}_${2}RequestOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tthis.command = ${1}Command.${2};", - "", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}_${2}Request {", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "\t", + "\t\t// return new this({", + "\t\t// \t// ...", + "\t\t// });", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic serialize(ctx: MessageEncodingContext): Buffer {", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t]);", "", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -580,17 +719,32 @@ "\t}", "}", "", + "export interface ${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}}_${2:SomeCommand}ResponseOptions {", + "\twasSent: boolean;", + "}", + "", "@subCommandResponse(${1}Command.${2})", "export class ${1}_${2}Response extends ${1}Response", "{", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions,", + "\t\toptions: ${1}_${2}ResponseOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tthis.wasSent = this.payload[0] !== 0;", + "\t\tsuper(options);", + "\t\tthis.wasSent = options.wasSent;", + "\t}", + "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}_${2}Response {", + "\t\tconst wasSent = raw.payload[0] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\twasSent,", + "\t\t});", "\t}", "", + "", "\tpublic readonly wasSent: boolean;", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -612,17 +766,32 @@ "@subCommandRequest(${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}}Command.${2:SomeCommand})", "export class ${1}_${2}Request extends ${1}Request {}", "", + "export interface ${1:${TM_FILENAME_BASE/(.*)Messages$/$1/}}_${2:SomeCommand}ResponseOptions {", + "\twasSent: boolean;", + "}", + "", "@subCommandResponse(${1}Command.${2})", "export class ${1}_${2}Response extends ${1}Response", "{", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: MessageDeserializationOptions,", + "\t\toptions: ${1}_${2}ResponseOptions & MessageBaseOptions,", "\t) {", - "\t\tsuper(host, options);", - "\t\tthis.wasSent = this.payload[0] !== 0;", + "\t\tsuper(options);", + "\t\tthis.wasSent = options.wasSent;", "\t}", "", + "\tpublic static from(", + "\t\traw: MessageRaw,", + "\t\tctx: MessageParsingContext,", + "\t): ${1}_${2}Response {", + "\t\tconst wasSent = raw.payload[0] !== 0;", + "\t", + "\t\treturn new this({", + "\t\t\twasSent,", + "\t\t});", + "\t}", + "", + "", "\tpublic readonly wasSent: boolean;", "", "\tpublic toLogEntry(): MessageOrCCLogEntry {", @@ -639,9 +808,9 @@ "scope": "typescript", "prefix": "zwcclog", "body": [ - "public toLogEntry(applHost: ZWaveApplicationHost): MessageOrCCLogEntry {", + "public toLogEntry(ctx?: GetValueDB): MessageOrCCLogEntry {", "\treturn {", - "\t\t...super.toLogEntry(applHost),", + "\t\t...super.toLogEntry(ctx),", "\t\tmessage: { ${1:someProp}: this.${1} },", "\t};", "}" @@ -651,9 +820,9 @@ "scope": "typescript", "prefix": "zwcclogempty", "body": [ - "public toLogEntry(applHost: ZWaveApplicationHost): MessageOrCCLogEntry {", + "public toLogEntry(ctx?: GetValueDB): MessageOrCCLogEntry {", "\treturn {", - "\t\t...super.toLogEntry(applHost),", + "\t\t...super.toLogEntry(ctx),", "\t\t// Hide the default payload line", "\t\tmessage: undefined,", "\t};", @@ -664,9 +833,9 @@ "scope": "typescript", "prefix": "zwccpv", "body": [ - "public persistValues(applHost: ZWaveApplicationHost): boolean {", - "\tif (!super.persistValues(applHost)) return false;", - "\tconst valueDB = this.getValueDB(applHost);", + "public persistValues(ctx: PersistValuesContext): boolean {", + "\tif (!super.persistValues(ctx)) return false;", + "\tconst valueDB = this.getValueDB(ctx);", "", "\t${0://TODO: Implementation}", "", @@ -679,29 +848,35 @@ "prefix": "zwcc", "body": [ "import {", + "\ttype CCRaw,", "\tCommandClass,", - "\tgotDeserializationOptions,", - "\ttype CCCommandOptions,", - "\ttype CCNode,", - "\ttype CommandClassDeserializationOptions,", + "\ttype InterviewContext,", + "\ttype PersistValuesContext,", + "\ttype RefreshValuesContext,", "} from \"../lib/CommandClass\";", "import {", + "\tAPI,", "\tCCCommand,", "\tccValue,", "\tccValues,", "\tcommandClass,", "\texpectedCCResponse,", "\timplementedVersion,", + "\tuseSupervision,", "} from \"../lib/CommandClassDecorators\";", "import {", "\tCommandClasses,", - "\tMessageOrCCLogEntry,", + "\ttype MessageOrCCLogEntry,", "\tMessagePriority,", + "\tValueMetadata,", + "\ttype WithAddress,", + "\tvalidatePayload,", "\tZWaveError,", "\tZWaveErrorCodes,", "} from \"@zwave-js/core\";", - "import type { ZWaveApplicationHost, ZWaveHost } from \"@zwave-js/host\";", + "import { CCEncodingContext, CCParsingContext } from \"@zwave-js/host\";", "import { CCAPI } from \"../lib/API\";", + "import { V } from \"../lib/Values\";", "", "// TODO: Move this enumeration into the src/lib/_Types.ts file", "// All additional type definitions (except CC constructor options) must be defined there too", @@ -809,7 +984,8 @@ "scope": "typescript", "prefix": "zwcccmd", "body": [ - "interface ${1:${TM_FILENAME_BASE/(.*)CC$/$1/}}CC${2:Get}Options extends CCCommandOptions {", + "// @publicAPI", + "export interface ${1:${TM_FILENAME_BASE/(.*)CC$/$1/}}CC${2:Get}Options {", "\tsomeProperty: number;", "}", "", @@ -817,27 +993,30 @@ "@expectedCCResponse(${1}CCReport)", "export class ${1}CC${2} extends ${1}CC {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: CommandClassDeserializationOptions | ${1}CC${2}Options,", + "\t\toptions: WithAddress<${1}CC${2}Options>,", "\t) {", - "\t\tsuper(host, options);", - "\t\tif (gotDeserializationOptions(options)) {", - "\t\t\t// TODO: Deserialize payload", - "\t\t\tthrow new ZWaveError(", - "\t\t\t\t`${this.constructor.name}: deserialization not implemented`,", - "\t\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", - "\t\t\t);", - "\t\t} else {", - "\t\t\t// TODO: Populate properties from options object", - "\t\t\tthrow new Error(\"not implemented\");", - "\t\t}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", + "\t}", + "\tpublic static from(raw: CCRaw, ctx: CCParsingContext): ${1}CC${2} {", + "\t\t// TODO: Deserialize payload", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "", + "\t\t// return new ${1}CC${2}({", + "\t\t// \tnodeId: ctx.sourceNodeId,", + "\t\t// \t// ...", + "\t\t// });", "\t}", "", - "\tpublic serialize(): Buffer {", + "\tpublic serialize(ctx: CCEncodingContext): Buffer {", "\t\tthis.payload = Buffer.from([", "\t\t\t/* TODO: serialize */", "\t\t]);", - "\t\treturn super.serialize();", + "\t\treturn super.serialize(ctx);", "\t}", "}", "${0}" @@ -848,14 +1027,32 @@ "scope": "typescript", "prefix": "zwccreport", "body": [ - "@CCCommand(${1:${TM_FILENAME_BASE/(.*)CC$/$1/}}Command.${2}Report)", - "export class ${1}CC${2}Report extends ${1}CC {", + "// @publicAPI", + "export interface ${1:${TM_FILENAME_BASE/(.*)CC$/$1/}}CC${2:Report}Options {", + "\tsomeProperty: number;", + "}", + "", + "@CCCommand(${1:${TM_FILENAME_BASE/(.*)CC$/$1/}}Command.${2})", + "export class ${1}CC${2} extends ${1}CC {", "\tpublic constructor(", - "\t\thost: ZWaveHost,", - "\t\toptions: CommandClassDeserializationOptions,", + "\t\toptions: WithAddress<${1}CC${2}Options>,", "\t) {", - "\t\tsuper(host, options);", - "\t\t${0:// TODO: Deserialize}", + "\t\tsuper(options);", + "\t\t// TODO: Populate properties from options object", + "\t\tthrow new Error(\"not implemented\");", + "\t}", + "", + "\tpublic static from(raw: CCRaw, ctx: CCParsingContext): ${1}CC${2} {", + "\t\t// TODO: Deserialize payload", + "\t\tthrow new ZWaveError(", + "\t\t\t`${this.name}: deserialization not implemented`,", + "\t\t\tZWaveErrorCodes.Deserialization_NotImplemented,", + "\t\t);", + "", + "\t\t// return new ${1}CC${2}({", + "\t\t// \tnodeId: ctx.sourceNodeId,", + "\t\t// \t// ...", + "\t\t// });", "\t}", "}" ], @@ -952,39 +1149,38 @@ "scope": "typescript", "prefix": "zwccinterview", "body": [ - "public async interview(applHost: ZWaveApplicationHost): Promise {", - "\tconst node = this.getNode(applHost)!;", - "\tconst endpoint = this.getEndpoint(applHost)!;", + "public async interview(ctx: InterviewContext): Promise {", + "\tconst node = this.getNode(ctx)!;", + "\tconst endpoint = this.getEndpoint(ctx)!;", "\tconst api = CCAPI.create(", "\t\tCommandClasses.${1:${TM_FILENAME_BASE/(.*)CC$/$1/}},", - "\t\tapplHost,", + "\t\tctx,", "\t\tendpoint", "\t).withOptions({", "\t\tpriority: MessagePriority.NodeQuery,", "\t});", - "\tconst valueDB = this.getValueDB(applHost);", "", - "\tapplHost.controllerLog.logNode(node.id, {", + "\tctx.logNode(node.id, {", "\t\tendpoint: this.endpointIndex,", "\t\tmessage: `Interviewing ${this.ccName}...`,", "\t\tdirection: \"none\",", "\t});", "", - "\tapplHost.controllerLog.logNode(node.id, {", + "\tctx.logNode(node.id, {", "\t\tendpoint: this.endpointIndex,", "\t\tmessage: \"doing something...\",", "\t\tdirection: \"outbound\",", "\t});", "\t${0:// TODO: Implementation}", "\tconst logMessage = `received response for something...`;", - "\tapplHost.controllerLog.logNode(node.id, {", + "\tctx.logNode(node.id, {", "\t\tendpoint: this.endpointIndex,", "\t\tmessage: logMessage,", "\t\tdirection: \"inbound\",", "\t});", "", "\t// Remember that the interview is complete", - "\tthis.setInterviewComplete(applHost, true);", + "\tthis.setInterviewComplete(ctx, true);", "}" ] }, @@ -1004,17 +1200,16 @@ "scope": "typescript", "prefix": "zwccrefval", "body": [ - "public async refreshValues(applHost: ZWaveApplicationHost): Promise {", - "\tconst node = this.getNode(applHost)!;", - "\tconst endpoint = this.getEndpoint(applHost)!;", + "public async refreshValues(ctx: RefreshValuesContext): Promise {", + "\tconst node = this.getNode(ctx)!;", + "\tconst endpoint = this.getEndpoint(ctx)!;", "\tconst api = CCAPI.create(", "\t\tCommandClasses.${1:${TM_FILENAME_BASE/(.*)CC$/$1/}},", - "\t\tapplHost,", + "\t\tctx,", "\t\tendpoint", "\t).withOptions({", "\t\tpriority: MessagePriority.NodeQuery,", "\t});", - "\tconst valueDB = this.getValueDB(applHost);", "", "\t${0:// TODO: Implementation}", "}"