refactor mqtt client

This commit is contained in:
ari committed 2025-11-14 11:44:57 -05:00
1 parent 44c999437b
commit 5f18fd6315
17 files changed
+1987 -851

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
// globals.d.ts
// TypeScript declarations for Flic Hub Studio Global Functions & Classes
// https://studio.flic.io/static/documentation/#165_global_functions_and_classes
/**
* Global Buffer class (Node.js-like, partial support)
* Unsupported: poolSize, indexOf, lastIndexOf, swap16/32/64, BigInt methods
* Supported encodings: 'utf8', 'hex', 'base64'
*/
declare class Buffer extends Uint8Array {
/** Create Buffer from string, array, etc. */
constructor(
source: string | ArrayBuffer | ArrayBufferView | number | Array<number>,
encoding?: "utf8" | "hex" | "base64",
);
/** Length of the buffer */
readonly length: number;
/** Convert to string */
toString(
encoding?: "utf8" | "hex" | "base64",
start?: number,
end?: number,
): string;
/** Write string to buffer */
write(
string: string,
offset?: number,
length?: number,
encoding?: "utf8" | "hex" | "base64",
): number;
/** Copy between buffers */
copy(
target: Buffer,
targetStart?: number,
sourceStart?: number,
sourceEnd?: number,
): number;
/** Fill buffer with value */
fill(
value: string | number | Buffer,
offset?: number,
end?: number,
encoding?: "utf8" | "hex" | "base64",
): this;
/** Compare two buffers */
equals(otherBuffer: Buffer): boolean;
/** Slice buffer */
slice(start?: number, end?: number): Buffer;
/** Static: Allocate new buffer */
static alloc(
size: number,
fill?: string | number | Buffer,
encoding?: "utf8" | "hex" | "base64",
): Buffer;
/** Static: Concatenate buffers */
static concat(list: Buffer[], totalLength?: number): Buffer;
/** Static: From string or array */
static from(
source: string | ArrayBuffer | ArrayBufferView | Array<number>,
encoding?: "utf8" | "hex" | "base64",
): Buffer;
/** Static: Check if value is Buffer */
static isBuffer(obj: any): obj is Buffer;
/** Static: Byte length of string */
static byteLength(
string: string,
encoding?: "utf8" | "hex" | "base64",
): number;
}
/**
* TextEncoder (Web API)
* Only UTF-8 supported
* encodeInto() is NOT supported
*/
declare class TextEncoder {
readonly encoding: "utf-8";
encode(input?: string): Uint8Array;
}
/**
* TextDecoder (Web API)
* Only UTF-8 supported
*/
declare class TextDecoder {
constructor(
label?: "utf-8",
options?: { fatal?: boolean; ignoreBOM?: boolean },
);
readonly encoding: "utf-8";
readonly fatal: boolean;
readonly ignoreBOM: boolean;
decode(input?: BufferSource, options?: { stream?: boolean }): string;
}
/**
* Timer API (Node.js-like)
*/
declare function setTimeout(
callback: (...args: any[]) => void,
ms?: number,
...args: any[]
): number;
declare function setInterval(
callback: (...args: any[]) => void,
ms?: number,
...args: any[]
): number;
declare function clearTimeout(timeoutId: number): void;
declare function clearInterval(intervalId: number): void;
/**
* Debug printing (console.log alias)
*/
declare function print(...args: any[]): void;
+231
View File
@@ -0,0 +1,231 @@
// net.d.ts
// TypeScript declarations for Flic Hub Studio 'net' (TCP) module
// Based on: https://studio.flic.io/static/documentation/#63_tcp_module
declare module "net" {
import { EventEmitter } from "events";
/**
* Creates a TCP server.
*/
function createServer(
options?: ServerOptions,
connectionListener?: (socket: Socket) => void,
): Server;
/**
* Creates a TCP socket and connects it to the specified port and host.
*/
function createConnection(
options: ConnectOptions,
connectionListener?: () => void,
): Socket;
/**
* Alternative signature for createConnection.
*/
function createConnection(
port: number,
host?: string,
connectionListener?: () => void,
): Socket;
/**
* TCP Server class.
*/
class Server extends EventEmitter {
constructor(
options?: ServerOptions,
connectionListener?: (socket: Socket) => void,
);
/**
* Start listening for connections.
*/
listen(options: ListenOptions, callback?: () => void): this;
listen(
port?: number,
host?: string,
backlog?: number,
callback?: () => void,
): this;
listen(port?: number, host?: string, callback?: () => void): this;
listen(port?: number, callback?: () => void): this;
/**
* Stop the server from accepting new connections.
*/
close(callback?: (err?: Error) => void): this;
/**
* Returns the bound address, family, and port of the server.
*/
address(): AddressInfo | null;
/**
* Asynchronously get the number of concurrent connections.
*/
getConnections(callback: (err: Error | null, count: number) => void): void;
/**
* Whether the server is currently listening.
*/
readonly listening: boolean;
// Events
on(event: "close", listener: () => void): this;
on(event: "connection", listener: (socket: Socket) => void): this;
on(event: "error", listener: (err: Error) => void): this;
on(event: "listening", listener: () => void): this;
}
/**
* TCP Socket class.
*/
export class Socket extends EventEmitter {
constructor(options?: SocketOptions);
/**
* Initiate a connection.
*/
connect(options: ConnectOptions, connectionListener?: () => void): this;
connect(port: number, host?: string, connectionListener?: () => void): this;
/**
* Send data on the socket.
*/
write(
data: string | Buffer | Uint8Array,
encoding?: string,
callback?: (err?: Error) => void,
): boolean;
write(
data: string | Buffer | Uint8Array,
callback?: (err?: Error) => void,
): boolean;
/**
* Half-close the socket (send FIN).
*/
end(
data?: string | Buffer | Uint8Array,
encoding?: string,
callback?: () => void,
): this;
end(data?: string | Buffer | Uint8Array, callback?: () => void): this;
end(callback?: () => void): this;
/**
* Abruptly close the socket.
*/
destroy(exception?: Error): this;
/**
* Pause reading data.
*/
pause(): this;
/**
* Resume reading after pause().
*/
resume(): this;
/**
* Set encoding for incoming data.
*/
setEncoding(encoding?: "utf8" | "hex" | "base64" | null): this;
/**
* Disable Nagle's algorithm (enable no delay).
*/
setNoDelay(noDelay?: boolean): this;
/**
* Enable/disable TCP keep-alive.
*/
setKeepAlive(enable?: boolean, initialDelay?: number): this;
/**
* Get local address info.
*/
address(): AddressInfo | null;
// Read-only properties
readonly connecting: boolean;
readonly destroyed: boolean;
readonly localAddress?: string;
readonly localPort?: number;
readonly remoteAddress?: string;
readonly remotePort?: number;
readonly remoteFamily?: string;
readonly bytesRead: number;
readonly bytesWritten: number;
// Events
on(event: "close", listener: (hadError: boolean) => void): this;
on(event: "connect", listener: () => void): this;
on(event: "data", listener: (data: Buffer | string) => void): this;
on(event: "drain", listener: () => void): this;
on(event: "end", listener: () => void): this;
on(event: "error", listener: (err: Error) => void): this;
on(
event: "lookup",
listener: (
err: Error | null,
address: string | null,
family: string,
host: string,
) => void,
): this;
on(event: "ready", listener: () => void): this;
}
// Option Interfaces
interface ServerOptions {
/**
* Allow half-open TCP connections.
* Default: false
*/
allowHalfOpen?: boolean;
/**
* Pause incoming connections until resume() is called.
* Default: false
*/
pauseOnConnect?: boolean;
}
interface SocketOptions {
/**
* Allow half-open TCP connections.
* Default: false
*/
allowHalfOpen?: boolean;
}
interface ConnectOptions {
port: number;
host?: string;
}
interface ListenOptions {
port?: number;
host?: string;
backlog?: number;
}
interface AddressInfo {
address: string;
family: "IPv4";
port: number;
}
// Module export
const net: {
createServer: typeof createServer;
createConnection: typeof createConnection;
Server: typeof Server;
Socket: typeof Socket;
};
export default net;
}
+11 -7
View File
@@ -20,17 +20,21 @@ type FlicHubOptions = {
uniqueId: string;
debug: boolean;
};
type MQTTOpt = {
host: string;
type MQTTOptions = {
port: number;
client_id: string;
keep_alive: boolean;
client_id: string | number[];
keep_alive: number | boolean;
clean_session: boolean;
username: string;
password: string;
username?: string;
password?: string;
protocol_name: string;
protocol_level: number;
};
type Flic2HassMQTTOptions = MQTTOptions & {
host: string;
};
type Options = {
mqtt: Partial<MQTTOpt> & { host: string };
mqtt: Partial<Flic2HassMQTTOptions> & { host: string };
debug?: boolean;
ha?: Partial<HAmqttOptions>;
flicBtns?: Partial<ButtonControllerOpt> & { disabled?: boolean };