import { getNextSessionID } from "./GW_COMMAND" ; "use strict" ; export const KLF200_PORT = 51200 ; export enum GatewayCommand { GW_ERROR_NTF = 0x0000 , GW_REBOOT_REQ = 0x0001 , GW_REBOOT_CFM = 0x0002 , GW_SET_FACTORY_DEFAULT_REQ = 0x0003 , GW_SET_FACTORY_DEFAULT_CFM = 0x0004 , GW_GET_VERSION_REQ = 0x0008 , GW_GET_VERSION_CFM = 0x0009 , GW_GET_PROTOCOL_VERSION_REQ = 0x000A , GW_GET_PROTOCOL_VERSION_CFM = 0x000B , GW_GET_STATE_REQ = 0x000C , GW_GET_STATE_CFM = 0x000D , GW_LEAVE_LEARN_STATE_REQ = 0x000E , GW_LEAVE_LEARN_STATE_CFM = 0x000F , GW_GET_NETWORK_SETUP_REQ = 0x00E0 , GW_GET_NETWORK_SETUP_CFM = 0x00E1 , GW_SET_NETWORK_SETUP_REQ = 0x00E2 , GW_SET_NETWORK_SETUP_CFM = 0x00E3 , GW_CS_GET_SYSTEMTABLE_DATA_REQ = 0x0100 , GW_CS_GET_SYSTEMTABLE_DATA_CFM = 0x0101 , GW_CS_GET_SYSTEMTABLE_DATA_NTF = 0x0102 , GW_CS_DISCOVER_NODES_REQ = 0x0103 , GW_CS_DISCOVER_NODES_CFM = 0x0104 , GW_CS_DISCOVER_NODES_NTF = 0x0105 , GW_CS_REMOVE_NODES_REQ = 0x0106 , GW_CS_REMOVE_NODES_CFM = 0x0107 , GW_CS_VIRGIN_STATE_REQ = 0x0108 , GW_CS_VIRGIN_STATE_CFM = 0x0109 , GW_CS_CONTROLLER_COPY_REQ = 0x010A , GW_CS_CONTROLLER_COPY_CFM = 0x010B , GW_CS_CONTROLLER_COPY_NTF = 0x010C , GW_CS_CONTROLLER_COPY_CANCEL_NTF = 0x010D , GW_CS_RECEIVE_KEY_REQ = 0x010E , GW_CS_RECEIVE_KEY_CFM = 0x010F , GW_CS_RECEIVE_KEY_NTF = 0x0110 , GW_CS_PGC_JOB_NTF = 0x0111 , GW_CS_SYSTEM_TABLE_UPDATE_NTF = 0x0112 , GW_CS_GENERATE_NEW_KEY_REQ = 0x0113 , GW_CS_GENERATE_NEW_KEY_CFM = 0x0114 , GW_CS_GENERATE_NEW_KEY_NTF = 0x0115 , GW_CS_REPAIR_KEY_REQ = 0x0116 , GW_CS_REPAIR_KEY_CFM = 0x0117 , GW_CS_REPAIR_KEY_NTF = 0x0118 , GW_CS_ACTIVATE_CONFIGURATION_MODE_REQ = 0x0119 , GW_CS_ACTIVATE_CONFIGURATION_MODE_CFM = 0x011A , GW_GET_NODE_INFORMATION_REQ = 0x0200 , GW_GET_NODE_INFORMATION_CFM = 0x0201 , GW_GET_NODE_INFORMATION_NTF = 0x0210 , GW_GET_ALL_NODES_INFORMATION_REQ = 0x0202 , GW_GET_ALL_NODES_INFORMATION_CFM = 0x0203 , GW_GET_ALL_NODES_INFORMATION_NTF = 0x0204 , GW_GET_ALL_NODES_INFORMATION_FINISHED_NTF = 0x0205 , GW_SET_NODE_VARIATION_REQ = 0x0206 , GW_SET_NODE_VARIATION_CFM = 0x0207 , GW_SET_NODE_NAME_REQ = 0x0208 , GW_SET_NODE_NAME_CFM = 0x0209 , GW_SET_NODE_VELOCITY_REQ = 0x020A , GW_SET_NODE_VELOCITY_CFM = 0x020B , GW_NODE_INFORMATION_CHANGED_NTF = 0x020C , GW_NODE_STATE_POSITION_CHANGED_NTF = 0x0211 , GW_SET_NODE_ORDER_AND_PLACEMENT_REQ = 0x020D , GW_SET_NODE_ORDER_AND_PLACEMENT_CFM = 0x020E , GW_GET_GROUP_INFORMATION_REQ = 0x0220 , GW_GET_GROUP_INFORMATION_CFM = 0x0221 , GW_GET_GROUP_INFORMATION_NTF = 0x0230 , GW_SET_GROUP_INFORMATION_REQ = 0x0222 , GW_SET_GROUP_INFORMATION_CFM = 0x0223 , GW_GROUP_INFORMATION_CHANGED_NTF = 0x0224 , GW_DELETE_GROUP_REQ = 0x0225 , GW_DELETE_GROUP_CFM = 0x0226 , GW_NEW_GROUP_REQ = 0x0227 , GW_NEW_GROUP_CFM = 0x0228 , GW_GET_ALL_GROUPS_INFORMATION_REQ = 0x0229 , GW_GET_ALL_GROUPS_INFORMATION_CFM = 0x022A , GW_GET_ALL_GROUPS_INFORMATION_NTF = 0x022B , GW_GET_ALL_GROUPS_INFORMATION_FINISHED_NTF = 0x022C , GW_GROUP_DELETED_NTF = 0x022D , GW_HOUSE_STATUS_MONITOR_ENABLE_REQ = 0x0240 , GW_HOUSE_STATUS_MONITOR_ENABLE_CFM = 0x0241 , GW_HOUSE_STATUS_MONITOR_DISABLE_REQ = 0x0242 , GW_HOUSE_STATUS_MONITOR_DISABLE_CFM = 0x0243 , GW_COMMAND_SEND_REQ = 0x0300 , GW_COMMAND_SEND_CFM = 0x0301 , GW_COMMAND_RUN_STATUS_NTF = 0x0302 , GW_COMMAND_REMAINING_TIME_NTF = 0x0303 , GW_SESSION_FINISHED_NTF = 0x0304 , GW_STATUS_REQUEST_REQ = 0x0305 , GW_STATUS_REQUEST_CFM = 0x0306 , GW_STATUS_REQUEST_NTF = 0x0307 , GW_WINK_SEND_REQ = 0x0308 , GW_WINK_SEND_CFM = 0x0309 , GW_WINK_SEND_NTF = 0x030A , GW_SET_LIMITATION_REQ = 0x0310 , GW_SET_LIMITATION_CFM = 0x0311 , GW_GET_LIMITATION_STATUS_REQ = 0x0312 , GW_GET_LIMITATION_STATUS_CFM = 0x0313 , GW_LIMITATION_STATUS_NTF = 0x0314 , GW_MODE_SEND_REQ = 0x0320 , GW_MODE_SEND_CFM = 0x0321 , GW_MODE_SEND_NTF = 0x0322 , GW_INITIALIZE_SCENE_REQ = 0x0400 , GW_INITIALIZE_SCENE_CFM = 0x0401 , GW_INITIALIZE_SCENE_NTF = 0x0402 , GW_INITIALIZE_SCENE_CANCEL_REQ = 0x0403 , GW_INITIALIZE_SCENE_CANCEL_CFM = 0x0404 , GW_RECORD_SCENE_REQ = 0x0405 , GW_RECORD_SCENE_CFM = 0x0406 , GW_RECORD_SCENE_NTF = 0x0407 , GW_DELETE_SCENE_REQ = 0x0408 , GW_DELETE_SCENE_CFM = 0x0409 , GW_RENAME_SCENE_REQ = 0x040A , GW_RENAME_SCENE_CFM = 0x040B , GW_GET_SCENE_LIST_REQ = 0x040C , GW_GET_SCENE_LIST_CFM = 0x040D , GW_GET_SCENE_LIST_NTF = 0x040E , GW_GET_SCENE_INFORMATION_REQ = 0x040F , GW_GET_SCENE_INFORMATION_CFM = 0x0410 , GW_GET_SCENE_INFORMATION_NTF = 0x0411 , GW_ACTIVATE_SCENE_REQ = 0x0412 , GW_ACTIVATE_SCENE_CFM = 0x0413 , GW_STOP_SCENE_REQ = 0x0415 , GW_STOP_SCENE_CFM = 0x0416 , GW_SCENE_INFORMATION_CHANGED_NTF = 0x0419 , GW_ACTIVATE_PRODUCTGROUP_REQ = 0x0447 , GW_ACTIVATE_PRODUCTGROUP_CFM = 0x0448 , GW_ACTIVATE_PRODUCTGROUP_NTF = 0x0449 , GW_GET_CONTACT_INPUT_LINK_LIST_REQ = 0x0460 , GW_GET_CONTACT_INPUT_LINK_LIST_CFM = 0x0461 , GW_SET_CONTACT_INPUT_LINK_REQ = 0x0462 , GW_SET_CONTACT_INPUT_LINK_CFM = 0x0463 , GW_REMOVE_CONTACT_INPUT_LINK_REQ = 0x0464 , GW_REMOVE_CONTACT_INPUT_LINK_CFM = 0x0465 , GW_GET_ACTIVATION_LOG_HEADER_REQ = 0x0500 , GW_GET_ACTIVATION_LOG_HEADER_CFM = 0x0501 , GW_CLEAR_ACTIVATION_LOG_REQ = 0x0502 , GW_CLEAR_ACTIVATION_LOG_CFM = 0x0503 , GW_GET_ACTIVATION_LOG_LINE_REQ = 0x0504 , GW_GET_ACTIVATION_LOG_LINE_CFM = 0x0505 , GW_ACTIVATION_LOG_UPDATED_NTF = 0x0506 , GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_REQ = 0x0507 , GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_NTF = 0x0508 , GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_CFM = 0x0509 , GW_SET_UTC_REQ = 0x2000 , GW_SET_UTC_CFM = 0x2001 , GW_RTC_SET_TIME_ZONE_REQ = 0x2002 , GW_RTC_SET_TIME_ZONE_CFM = 0x2003 , GW_GET_LOCAL_TIME_REQ = 0x2004 , GW_GET_LOCAL_TIME_CFM = 0x2005 , GW_PASSWORD_ENTER_REQ = 0x3000 , GW_PASSWORD_ENTER_CFM = 0x3001 , GW_PASSWORD_CHANGE_REQ = 0x3002 , GW_PASSWORD_CHANGE_CFM = 0x3003 , GW_PASSWORD_CHANGE_NTF = 0x3004 } export type GatewayCommand_Request = GatewayCommand . GW_REBOOT_REQ | GatewayCommand . GW_SET_FACTORY_DEFAULT_REQ | GatewayCommand . GW_GET_VERSION_REQ | GatewayCommand . GW_GET_PROTOCOL_VERSION_REQ | GatewayCommand . GW_GET_STATE_REQ | GatewayCommand . GW_LEAVE_LEARN_STATE_REQ | GatewayCommand . GW_GET_NETWORK_SETUP_REQ | GatewayCommand . GW_SET_NETWORK_SETUP_REQ | GatewayCommand . GW_CS_GET_SYSTEMTABLE_DATA_REQ | GatewayCommand . GW_CS_DISCOVER_NODES_REQ | GatewayCommand . GW_CS_REMOVE_NODES_REQ | GatewayCommand . GW_CS_VIRGIN_STATE_REQ | GatewayCommand . GW_CS_CONTROLLER_COPY_REQ | GatewayCommand . GW_CS_RECEIVE_KEY_REQ | GatewayCommand . GW_CS_GENERATE_NEW_KEY_REQ | GatewayCommand . GW_CS_REPAIR_KEY_REQ | GatewayCommand . GW_CS_ACTIVATE_CONFIGURATION_MODE_REQ | GatewayCommand . GW_GET_NODE_INFORMATION_REQ | GatewayCommand . GW_GET_ALL_NODES_INFORMATION_REQ | GatewayCommand . GW_SET_NODE_VARIATION_REQ | GatewayCommand . GW_SET_NODE_NAME_REQ | GatewayCommand . GW_SET_NODE_VELOCITY_REQ | GatewayCommand . GW_SET_NODE_ORDER_AND_PLACEMENT_REQ | GatewayCommand . GW_GET_GROUP_INFORMATION_REQ | GatewayCommand . GW_SET_GROUP_INFORMATION_REQ | GatewayCommand . GW_DELETE_GROUP_REQ | GatewayCommand . GW_NEW_GROUP_REQ | GatewayCommand . GW_GET_ALL_GROUPS_INFORMATION_REQ | GatewayCommand . GW_HOUSE_STATUS_MONITOR_ENABLE_REQ | GatewayCommand . GW_HOUSE_STATUS_MONITOR_DISABLE_REQ | GatewayCommand . GW_COMMAND_SEND_REQ | GatewayCommand . GW_STATUS_REQUEST_REQ | GatewayCommand . GW_WINK_SEND_REQ | GatewayCommand . GW_SET_LIMITATION_REQ | GatewayCommand . GW_GET_LIMITATION_STATUS_REQ | GatewayCommand . GW_MODE_SEND_REQ | GatewayCommand . GW_INITIALIZE_SCENE_REQ | GatewayCommand . GW_INITIALIZE_SCENE_CANCEL_REQ | GatewayCommand . GW_RECORD_SCENE_REQ | GatewayCommand . GW_DELETE_SCENE_REQ | GatewayCommand . GW_RENAME_SCENE_REQ | GatewayCommand . GW_GET_SCENE_LIST_REQ | GatewayCommand . GW_GET_SCENE_INFORMATION_REQ | GatewayCommand . GW_ACTIVATE_SCENE_REQ | GatewayCommand . GW_STOP_SCENE_REQ | GatewayCommand . GW_ACTIVATE_PRODUCTGROUP_REQ | GatewayCommand . GW_GET_CONTACT_INPUT_LINK_LIST_REQ | GatewayCommand . GW_SET_CONTACT_INPUT_LINK_REQ | GatewayCommand . GW_REMOVE_CONTACT_INPUT_LINK_REQ | GatewayCommand . GW_GET_ACTIVATION_LOG_HEADER_REQ | GatewayCommand . GW_CLEAR_ACTIVATION_LOG_REQ | GatewayCommand . GW_GET_ACTIVATION_LOG_LINE_REQ | GatewayCommand . GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_REQ | GatewayCommand . GW_SET_UTC_REQ | GatewayCommand . GW_RTC_SET_TIME_ZONE_REQ | GatewayCommand . GW_GET_LOCAL_TIME_REQ | GatewayCommand . GW_PASSWORD_ENTER_REQ | GatewayCommand . GW_PASSWORD_CHANGE_REQ ; export type GatewayCommand_Confirmation = GatewayCommand . GW_REBOOT_CFM | GatewayCommand . GW_SET_FACTORY_DEFAULT_CFM | GatewayCommand . GW_GET_VERSION_CFM | GatewayCommand . GW_GET_PROTOCOL_VERSION_CFM | GatewayCommand . GW_GET_STATE_CFM | GatewayCommand . GW_LEAVE_LEARN_STATE_CFM | GatewayCommand . GW_GET_NETWORK_SETUP_CFM | GatewayCommand . GW_SET_NETWORK_SETUP_CFM | GatewayCommand . GW_CS_GET_SYSTEMTABLE_DATA_CFM | GatewayCommand . GW_CS_DISCOVER_NODES_CFM | GatewayCommand . GW_CS_REMOVE_NODES_CFM | GatewayCommand . GW_CS_VIRGIN_STATE_CFM | GatewayCommand . GW_CS_CONTROLLER_COPY_CFM | GatewayCommand . GW_CS_RECEIVE_KEY_CFM | GatewayCommand . GW_CS_GENERATE_NEW_KEY_CFM | GatewayCommand . GW_CS_REPAIR_KEY_CFM | GatewayCommand . GW_CS_ACTIVATE_CONFIGURATION_MODE_CFM | GatewayCommand . GW_GET_NODE_INFORMATION_CFM | GatewayCommand . GW_GET_ALL_NODES_INFORMATION_CFM | GatewayCommand . GW_SET_NODE_VARIATION_CFM | GatewayCommand . GW_SET_NODE_NAME_CFM | GatewayCommand . GW_SET_NODE_VELOCITY_CFM | GatewayCommand . GW_SET_NODE_ORDER_AND_PLACEMENT_CFM | GatewayCommand . GW_GET_GROUP_INFORMATION_CFM | GatewayCommand . GW_SET_GROUP_INFORMATION_CFM | GatewayCommand . GW_DELETE_GROUP_CFM | GatewayCommand . GW_NEW_GROUP_CFM | GatewayCommand . GW_GET_ALL_GROUPS_INFORMATION_CFM | GatewayCommand . GW_HOUSE_STATUS_MONITOR_ENABLE_CFM | GatewayCommand . GW_HOUSE_STATUS_MONITOR_DISABLE_CFM | GatewayCommand . GW_COMMAND_SEND_CFM | GatewayCommand . GW_STATUS_REQUEST_CFM | GatewayCommand . GW_WINK_SEND_CFM | GatewayCommand . GW_SET_LIMITATION_CFM | GatewayCommand . GW_GET_LIMITATION_STATUS_CFM | GatewayCommand . GW_MODE_SEND_CFM | GatewayCommand . GW_INITIALIZE_SCENE_CFM | GatewayCommand . GW_INITIALIZE_SCENE_CANCEL_CFM | GatewayCommand . GW_RECORD_SCENE_CFM | GatewayCommand . GW_DELETE_SCENE_CFM | GatewayCommand . GW_RENAME_SCENE_CFM | GatewayCommand . GW_GET_SCENE_LIST_CFM | GatewayCommand . GW_GET_SCENE_INFORMATION_CFM | GatewayCommand . GW_ACTIVATE_SCENE_CFM | GatewayCommand . GW_STOP_SCENE_CFM | GatewayCommand . GW_ACTIVATE_PRODUCTGROUP_CFM | GatewayCommand . GW_GET_CONTACT_INPUT_LINK_LIST_CFM | GatewayCommand . GW_SET_CONTACT_INPUT_LINK_CFM | GatewayCommand . GW_REMOVE_CONTACT_INPUT_LINK_CFM | GatewayCommand . GW_GET_ACTIVATION_LOG_HEADER_CFM | GatewayCommand . GW_CLEAR_ACTIVATION_LOG_CFM | GatewayCommand . GW_GET_ACTIVATION_LOG_LINE_CFM | GatewayCommand . GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_CFM | GatewayCommand . GW_SET_UTC_CFM | GatewayCommand . GW_RTC_SET_TIME_ZONE_CFM | GatewayCommand . GW_GET_LOCAL_TIME_CFM | GatewayCommand . GW_PASSWORD_ENTER_CFM | GatewayCommand . GW_PASSWORD_CHANGE_CFM ; export type GatewayCommand_Notification = GatewayCommand . GW_ERROR_NTF | GatewayCommand . GW_CS_GET_SYSTEMTABLE_DATA_NTF | GatewayCommand . GW_CS_DISCOVER_NODES_NTF | GatewayCommand . GW_CS_CONTROLLER_COPY_NTF | GatewayCommand . GW_CS_CONTROLLER_COPY_CANCEL_NTF | GatewayCommand . GW_CS_RECEIVE_KEY_NTF | GatewayCommand . GW_CS_PGC_JOB_NTF | GatewayCommand . GW_CS_SYSTEM_TABLE_UPDATE_NTF | GatewayCommand . GW_CS_GENERATE_NEW_KEY_NTF | GatewayCommand . GW_CS_REPAIR_KEY_NTF | GatewayCommand . GW_GET_NODE_INFORMATION_NTF | GatewayCommand . GW_GET_ALL_NODES_INFORMATION_NTF | GatewayCommand . GW_GET_ALL_NODES_INFORMATION_FINISHED_NTF | GatewayCommand . GW_NODE_INFORMATION_CHANGED_NTF | GatewayCommand . GW_NODE_STATE_POSITION_CHANGED_NTF | GatewayCommand . GW_GET_GROUP_INFORMATION_NTF | GatewayCommand . GW_GROUP_INFORMATION_CHANGED_NTF | GatewayCommand . GW_GET_ALL_GROUPS_INFORMATION_NTF | GatewayCommand . GW_GET_ALL_GROUPS_INFORMATION_FINISHED_NTF | GatewayCommand . GW_GROUP_DELETED_NTF | GatewayCommand . GW_COMMAND_RUN_STATUS_NTF | GatewayCommand . GW_COMMAND_REMAINING_TIME_NTF | GatewayCommand . GW_SESSION_FINISHED_NTF | GatewayCommand . GW_STATUS_REQUEST_NTF | GatewayCommand . GW_WINK_SEND_NTF | GatewayCommand . GW_LIMITATION_STATUS_NTF | GatewayCommand . GW_MODE_SEND_NTF | GatewayCommand . GW_INITIALIZE_SCENE_NTF | GatewayCommand . GW_RECORD_SCENE_NTF | GatewayCommand . GW_GET_SCENE_LIST_NTF | GatewayCommand . GW_GET_SCENE_INFORMATION_NTF | GatewayCommand . GW_SCENE_INFORMATION_CHANGED_NTF | GatewayCommand . GW_ACTIVATE_PRODUCTGROUP_NTF | GatewayCommand . GW_ACTIVATION_LOG_UPDATED_NTF | GatewayCommand . GW_GET_MULTIPLE_ACTIVATION_LOG_LINES_NTF | GatewayCommand . GW_PASSWORD_CHANGE_NTF ; export type GatewayCommand_Receive = GatewayCommand_Confirmation | GatewayCommand_Notification ; export enum GW_COMMON_STATUS { SUCCESS = 0 , ERROR = 1 , INVALID_NODE_ID = 2 } export enum GW_INVERSE_STATUS { ERROR = 0 , SUCCESS = 1 } const C_COMMAND_SIZE = 2 ; const C_BUFFERLEN_SIZE = 1 ; export const C_MAX_PWD_LENGTH = 32 ; export interface IGW_FRAME { readonly Command : GatewayCommand ; } export interface IGW_FRAME_REQ extends IGW_FRAME { readonly Data : Buffer ; } export interface IGW_FRAME_RCV extends IGW_FRAME { } export interface IGW_FRAME_RCV_CTOR { new ( Data ) : IGW_FRAME_RCV ; } export interface IGW_FRAME_CFM extends IGW_FRAME_RCV { } export interface IGW_FRAME_NTF extends IGW_FRAME_RCV { } export interface IGW_FRAME_COMMAND extends IGW_FRAME { readonly SessionID : number ; } export abstract class GW_FRAME implements IGW_FRAME { readonly Command : GatewayCommand = GatewayCommand [ < keyof typeof GatewayCommand > this . constructor . name ] ; protected readonly offset = C_BUFFERLEN_SIZE + C_COMMAND_SIZE ; protected constructor ( ) { } } export abstract class GW_FRAME_REQ extends GW_FRAME implements IGW_FRAME_REQ { param { number } BufferSize memberof constructor ( readonly BufferSize ) { super ( ) ; this . data = Buffer . alloc ( BufferSize + this . offset ) ; this . data . writeUInt16BE ( this . Command , C_BUFFERLEN_SIZE ) ; this . data . writeUInt8 ( this . data . byteLength , 0 ) ; } protected abstract param { number } BufferSize param { boolean } CopyData memberof protected AllocBuffer ( BufferSize , CopyData = true ) { const oldData = this . data ; this . data = Buffer . alloc ( BufferSize + this . offset ) ; this . data . writeUInt16BE ( this . Command , C_BUFFERLEN_SIZE ) ; this . data . writeUInt8 ( this . data . byteLength , 0 ) ; if ( typeof oldData !== "undefined" && CopyData === true ) { const copyBufferLength = Math . min ( oldData . byteLength , this . data . byteLength ) - this . offset ; if ( copyBufferLength > 0 ) { oldData . copy ( this . data , this . offset , this . offset , this . offset + copyBufferLength ) ; } } } private data : Buffer ; public get Data ( ) : Buffer { return this . data ; } } export abstract class GW_FRAME_COMMAND_REQ extends GW_FRAME_REQ implements IGW_FRAME_COMMAND { public readonly SessionID : number ; constructor ( BufferSize ) { super ( BufferSize ) ; this . SessionID = getNextSessionID ( ) ; } } export abstract class GW_FRAME_RCV extends GW_FRAME implements IGW_FRAME_RCV { public constructor ( readonly Data ) { super ( ) ; const command = < GatewayCommand_Receive > Data . readUInt16BE ( C_BUFFERLEN_SIZE ) ; this . CheckCommand ( command ) ; this . Data = Data . slice ( C_BUFFERLEN_SIZE + C_COMMAND_SIZE ) ; } private CheckCommand ( command ) { if ( command !== this . Command ) throw new Error ( `Command from buffer (${ command }) doesn't match command of frame (${ this . Command }).` ) ; } } export abstract class GW_FRAME_CFM extends GW_FRAME_RCV implements IGW_FRAME_CFM { } export abstract class GW_FRAME_NTF extends GW_FRAME_RCV implements IGW_FRAME_NTF { } export param { Buffer } data returns { string } export function readZString ( data ) { return data . toString ( "utf8" ) . split ( "\0" , 1 ) [ 0 ] ; } export class KLF200Protocol { static readonly ProtocolID = 0 ; static Encode ( data ) { const result = Buffer . alloc ( data . byteLength + 2 ) ; result . writeUInt8 ( this . ProtocolID , 0 ) ; data . copy ( result , 1 ) ; let CRC = 0 ; for ( let index = 0 ; index < result . byteLength - 1 ; index ++ ) { CRC ^= result [ index ] ; } result . writeUInt8 ( CRC , result . byteLength - 1 ) ; return result ; } static Decode ( data ) { if ( data [ 0 ] !== this . ProtocolID ) { throw new Error ( "Invalid ProtocolID." ) ; } let CRC = 0 ; for ( let index = 0 ; index < data . byteLength - 1 ; index ++ ) { CRC ^= data [ index ] ; } if ( CRC !== data [ data . byteLength - 1 ] ) { throw new Error ( "CRC error" ) ; } const result = Buffer . alloc ( data . byteLength - 2 ) ; data . copy ( result , 0 , 1 , data . byteLength - 1 ) ; return result ; } } export const SLIP_END = 0xC0 ; const SLIP_ESC = 0xDB ; const SLIP_ESC_END = 0xDC ; const SLIP_ESC_ESC = 0xDD ; export class SLIPProtocol { static Encode ( data ) { const resultBuffer = Buffer . alloc ( data . byteLength * 2 + 2 ) ; let resultLength = 0 ; resultBuffer [ resultLength ++ ] = SLIP_END ; for ( let i = 0 ; i < data . byteLength ; i ++ ) { const dataByte = data [ i ] ; switch ( dataByte ) { case SLIP_END : resultBuffer . writeUInt8 ( SLIP_ESC , resultLength ++ ) ; resultBuffer . writeUInt8 ( SLIP_ESC_END , resultLength ++ ) ; break ; case SLIP_ESC : resultBuffer . writeUInt8 ( SLIP_ESC , resultLength ++ ) ; resultBuffer . writeUInt8 ( SLIP_ESC_ESC , resultLength ++ ) ; break ; default : resultBuffer . writeUInt8 ( dataByte , resultLength ++ ) ; break ; } } resultBuffer [ resultLength ++ ] = SLIP_END ; return resultBuffer . slice ( 0 , resultLength ) ; } static Decode ( data ) { if ( data [ 0 ] !== SLIP_END || data [ data . byteLength - 1 ] !== SLIP_END ) throw new Error ( "Missing END mark." ) ; const resultBuffer = Buffer . alloc ( data . byteLength - 2 ) ; let resultLength = 0 ; for ( let i = 1 ; i < data . byteLength - 1 ; i ++ ) { const dataByte = data [ i ] ; switch ( dataByte ) { case SLIP_ESC : const nextDataByte = data [ ++ i ] ; switch ( nextDataByte ) { case SLIP_ESC_ESC : resultBuffer . writeUInt8 ( SLIP_ESC , resultLength ++ ) ; break ; case SLIP_ESC_END : resultBuffer . writeUInt8 ( SLIP_END , resultLength ++ ) ; break ; default : throw new Error ( "Invalid SLIP special character." ) ; } break ; default : resultBuffer . writeUInt8 ( dataByte , resultLength ++ ) ; break ; } } return resultBuffer . slice ( 0 , resultLength ) ; } }