diff --git a/.vscode/typescript.code-snippets b/.vscode/typescript.code-snippets index 5799ddeb1af3..ddade71af84b 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): DummyRequest {", + "\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 = 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 {", @@ -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 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 {", @@ -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 = 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 {",