UNPKG

knxultimate

Version:

KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.

250 lines (249 loc) 9.04 kB
import dgram from 'dgram'; import net from 'net'; import { KnxResponse } from './protocol/KNXProtocol'; import { TunnelTypes } from './protocol/TunnelCRI'; import KNXAddress from './protocol/KNXAddress'; import { KNXLoggerOptions } from './KnxLog'; import { KNXDescriptionResponse, KNXPacket } from './protocol'; import KNXRoutingIndication from './protocol/KNXRoutingIndication'; import KNXTunnelingRequest from './protocol/KNXTunnelingRequest'; import { TypedEventEmitter } from './TypedEmitter'; import KNXHeader from './protocol/KNXHeader'; import KNXTunnelingAck from './protocol/KNXTunnelingAck'; import KNXSearchResponse from './protocol/KNXSearchResponse'; import { SerialFT12Options, SerialPortSummary } from './transports/SerialFT12'; export type DiscoveryInterface = { ip: string; port: number; name: string; ia: string; services: string[]; type: 'tunnelling' | 'routing'; transport?: 'UDP' | 'TCP' | 'Multicast'; }; export interface SecureConfig { tunnelInterfaceIndividualAddress?: string; knxkeys_file_path?: string; knxkeys_password?: string; tunnelUserPassword?: string; tunnelUserId?: number; } export declare enum ConncetionState { STARTED = "STARTED", CONNECTING = "CONNECTING", CONNECTED = "CONNECTED", DISCONNECTING = "DISCONNECTING", DISCONNECTED = "DISCONNECTED" } export declare enum SocketEvents { error = "error", message = "message", listening = "listening", data = "data", close = "close", connect = "connect" } export type KNXClientProtocol = 'TunnelUDP' | 'Multicast' | 'TunnelTCP' | 'SerialFT12'; export declare enum KNXClientEvents { error = "error", disconnected = "disconnected", discover = "discover", indication = "indication", connected = "connected", ready = "ready", response = "response", connecting = "connecting", ackReceived = "ackReceived", close = "close", descriptionResponse = "descriptionResponse" } export interface KNXClientEventCallbacks { error: (error: Error) => void; disconnected: (reason: string) => void; discover: (host: string, header: KNXHeader, message: KNXSearchResponse) => void; getGatewayDescription: (searchResponse: KNXDescriptionResponse) => void; indication: (packet: KNXRoutingIndication, echoed: boolean) => void; connected: (options: KNXClientOptions) => void; ready: () => void; response: (host: string, header: KNXHeader, message: KnxResponse) => void; connecting: (options: KNXClientOptions) => void; ackReceived: (packet: KNXTunnelingAck | KNXTunnelingRequest, ack: boolean) => void; close: () => void; descriptionResponse: (packet: KNXDescriptionResponse) => void; } export type KNXClientOptions = { physAddr?: string; connectionKeepAliveTimeout?: number; ipAddr?: string; ipPort?: number | string; hostProtocol?: KNXClientProtocol; isSecureKNXEnabled?: boolean; suppress_ack_ldatareq?: boolean; localIPAddress?: string; interface?: string; localSocketAddress?: string; KNXQueueSendIntervalMilliseconds?: number; sniffingMode?: boolean; theGatewayIsKNXVirtual?: boolean; secureTunnelConfig?: SecureConfig; secureRoutingWaitForTimer?: boolean; serialInterface?: SerialFT12Options; } & KNXLoggerOptions; export declare enum KNXTimer { ACK = "ack", HEARTBEAT = "heartbeat", CONNECTION_STATE = "connection_state", CONNECTION = "connection", CONNECT_REQUEST = "connect_request", DISCONNECT = "disconnect", DISCOVERY = "discovery", GATEWAYDESCRIPTION = "GatewayDescription" } export type SnifferPacket = { reqType?: string; request?: string; response?: string; resType?: string; deltaReq: number; deltaRes?: number; }; export default class KNXClient extends TypedEventEmitter<KNXClientEventCallbacks> { private _channelID; private _connectionState; private _numFailedTelegramACK; private _clientTunnelSeqNumber; private _options; private _peerHost; private _peerPort; private _heartbeatFailures; private _heartbeatRunning; private max_HeartbeatFailures; private _awaitingResponseType; private _clientSocket; private _serialDriver?; private sysLogger; private _clearToSend; private socketReady; private timers; physAddr: KNXAddress; theGatewayIsKNXVirtual: boolean; private commandQueue; private exitProcessingKNXQueueLoop; private currentItemHandledByTheQueue; private queueLock; private sniffingPackets; private lastSnifferRequest; private _tcpRxBuffer; private _secureSessionKey?; private _secureSessionId; private _secureWrapperSeq; private _secureTunnelSeq; private _securePrivateKey?; private _securePublicKey?; private _secureUserId; private _secureUserPasswordKey?; private _secureGroupKeys; private _secureSendSeq48; private _secureSerial; private _secureAssignedIa; private _secureKeyring?; private _secureCandidateIAs; private _secureCandidateIndex; private _secureSearchProbed; private _secureBackboneKey?; private _secureRoutingTimerOffsetMs; private _secureRoutingTimerAuthenticated; private _secureRoutingLatencyMs; private _secureHandshakeSessionTimer?; private _secureHandshakeAuthTimer?; private _secureHandshakeConnectTimer?; private _secureRoutingSyncTimer?; private _secureHandshakeState?; get udpSocket(): dgram.Socket; get tcpSocket(): net.Socket; private isSerialTransport; constructor(options: KNXClientOptions, createSocket?: (client: KNXClient) => void); private createSocket; private initTcpSocket; private connectSerialTransport; private closeSerialTransport; private handleSerialCemi; private extractCemiMessage; get channelID(): number; get clearToSend(): boolean; set clearToSend(val: boolean); private getKNXDataBuffer; private waitForEvent; private setTimer; private clearTimer; private clearAllTimers; private processKnxPacketQueueItem; private handleKNXQueue; send(_knxPacket: KNXPacket, _ACK: KNXTunnelingRequest, _priority: boolean, _expectedSeqNumberForACK: number): void; write(dstAddress: KNXAddress | string, data: any, dptid: string | number): void; respond(dstAddress: KNXAddress | string, data: Buffer, dptid: string | number): void; read(dstAddress: KNXAddress | string): void; writeRaw(dstAddress: KNXAddress | string, rawDataBuffer: Buffer, bitlength: number): void; private startHeartBeat; private stopHeartBeat; isDiscoveryRunning(): boolean; startDiscovery(): void; stopDiscovery(): void; static discover(eth?: string | number, timeout?: number): Promise<string[]>; static discoverDetailed(eth?: string | number, timeout?: number): Promise<string[]>; static discoverInterfaces(eth?: string | number, timeout?: number): Promise<DiscoveryInterface[]>; static listSerialInterfaces(): Promise<SerialPortSummary[]>; isGatewayDescriptionRunning(): boolean; startGatewayDescription(): void; stopGatewayDescription(): void; static getGatewayDescription(ipAddr: string, ipPort: string, eth?: string | number, timeout?: number): Promise<any[]>; private secureGetIaCandidatesByDiscovery; private secureConnectWithIaDiscovery; Connect(knxLayer?: TunnelTypes): void; private closeSocket; Disconnect(): Promise<void>; isConnected(): boolean; private setDisconnected; private runHeartbeat; private getSeqNumber; private getCurrentItemHandledByTheQueue; private secureGetTunnelSeq; private secureIncTunnelSeq; private incSeqNumber; private setTimerWaitingForACK; private getKNXConstantName; private processInboundMessage; private secureEnsureKeyring; private secureStartSession; private secureOnTcpData; private secureWrap; private secureDescribeSessionStatus; private secureRoutingTimerValue; private secureRoutingEmitConnectedIfPending; private secureSendTimerNotify; private secureWrapRouting; private secureDecryptRouting; private secureOnUdpData; private getLoggerLevel; private isLevelEnabled; private secureDecrypt; private secureBuildSessionRequest; private secureBuildSessionAuthenticate; private secureBuildConnectRequest; private secureBuildSecureApdu; private securePickSenderIaForGa; private secureBuildLDataReq; private secureParseGroupAddress; private secureFormatGroupAddress; private secureParseIndividualAddress; private maybeApplyDataSecure; private maybeDecryptDataSecure; private ensurePlainCEMI; private sendDescriptionRequestMessage; private sendSearchRequestMessage; private sendSecureSearchRequestTo; private sendConnectRequestMessage; private sendConnectionStateRequestMessage; private sendDisconnectRequestMessage; private sendDisconnectResponseMessage; }