Compare commits

...
7 Commits
Author SHA1 Message Date
Zachary Wander 2935827f60 Properly reference flicIR.debug (#5) 2025-03-22 18:55:35 -04:00
Zachary Wander bf4e69e2ce Fix StackOverflow avoidance (#4) 2025-03-17 19:07:55 -04:00
Ari Sosnovsky df74989fdd Merge pull request #1 from zacharee/fix-stackoverflow
Avoid a StackOverflow when re-emitting packets
2025-01-01 13:58:39 -05:00
Zachary Wander 969b8bd0e2 Avoid a StackOverflow when re-emitting packets 2024-09-16 16:36:58 -04:00
Ari Sosnovsky ec295d3f8a Update README.md 2024-07-03 16:07:15 -04:00
Ariel Sosnovsky c97c9c494c images 2023-11-02 16:50:07 -04:00
Ariel Sosnovsky ecc223d6e0 images 2023-11-02 16:48:32 -04:00
9 changed files with 61 additions and 28 deletions

No files matched your search

+5 -2
View File
@@ -1,12 +1,15 @@
# Flic2Hass ![GitHub package.json version (subfolder of monorepo)](https://img.shields.io/github/package-json/v/asosnovsky/flic2hass) # Flic2Hass ![GitHub package.json version (subfolder of monorepo)](https://img.shields.io/github/package-json/v/asosnovsky/flic2hass)
> <a href='https://ko-fi.com/J3J8C4RB7' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
This is a complete refactor of various previous versions of flic2hass. This version provides more features and even supports the onboard IR module! This is a complete refactor of various previous versions of flic2hass. This version provides more features and even supports the onboard IR module!
## Features ## Features
### Complete Overview of Buttons ### Complete Overview of Buttons
![Alt text](images/btn.png) ![Alt text](images/button-eg.png)
* Show connectivity state * Show connectivity state
* Show click action types: `click` `hold` `double_click` * Show click action types: `click` `hold` `double_click`
@@ -20,7 +23,7 @@ This is a complete refactor of various previous versions of flic2hass. This vers
* Record remote signals * Record remote signals
* Send remote signals * Send remote signals
![Alt text](images/ir.png) ![Alt text](images/ir-eg.png)
Requirements: Requirements:
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

+5
View File
@@ -926,6 +926,11 @@ MQTT.prototype.packetHandler = function(data) {
this.partData = data; this.partData = data;
return; return;
} }
// Allow zero-size pData, but detect if its indexes
// will go out of bounds.
if (1 + dLen.lenBy >= data.length) {
return;
}
// Get the data for this packet // Get the data for this packet
var pData = data.slice(1 + dLen.lenBy, pLen); var pData = data.slice(1 + dLen.lenBy, pLen);
// more than one packet? re-emit it so we handle it later // more than one packet? re-emit it so we handle it later
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "flic2hass", "name": "flic2hass",
"version": "1.0.0", "version": "1.1.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "flic2hass", "name": "flic2hass",
"version": "1.0.0", "version": "1.1.0",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@swc/cli": "^0.1.62", "@swc/cli": "^0.1.62",
+14 -13
View File
@@ -4,6 +4,7 @@ import { IRControllerOpt, makeIRController } from "./IRController";
import {makeLogger} from "./Logger"; import {makeLogger} from "./Logger";
import {ButtonModule, IRModule} from './flicTypes'; import {ButtonModule, IRModule} from './flicTypes';
import * as mqtt from './mqtt'; import * as mqtt from './mqtt';
export type MQTTOpt = { export type MQTTOpt = {
host: string; host: string;
port: number; port: number;
@@ -30,15 +31,15 @@ export const start = (
{...options.mqtt, keep_alive: true}, {...options.mqtt, keep_alive: true},
); );
const logger = makeLogger('root', options.debug ?? false); const logger = makeLogger('root', options.debug ?? false);
options.ha = options.ha ?? {} options.ha = options.ha ?? {};
options.flicBtns = options.flicBtns ?? {} options.flicBtns = options.flicBtns ?? {};
options.flicIR = options.flicIR ?? {} options.flicIR = options.flicIR ?? {};
options.ha.debug = options.ha.debug ?? options.debug ?? false options.ha.debug = options.ha.debug ?? options.debug ?? false;
options.flicBtns.debug = options.flicBtns.debug ?? options.debug ?? false options.flicBtns.debug = options.flicBtns.debug ?? options.debug ?? false;
options.flicIR.debug = options.flicBtns.debug ?? options.debug ?? false options.flicIR.debug = options.flicIR.debug ?? options.debug ?? false;
const ha = makeHAmqtt(mqttServer, options.ha); const ha = makeHAmqtt(mqttServer, options.ha);
mqttServer.on('connected', () => { mqttServer.on('connected', () => {
logger.info("connected to mqtt") logger.info("connected to mqtt");
if (!options.flicBtns?.disabled) { if (!options.flicBtns?.disabled) {
makeButtonController( makeButtonController(
ha, buttonModule, options.flicBtns, ha, buttonModule, options.flicBtns,
@@ -52,25 +53,25 @@ export const start = (
options.flicIR, options.flicIR,
).start(); ).start();
} }
logger.info("all services up!") logger.info("all services up!");
}); });
mqttServer.on('error', function (err) { mqttServer.on('error', function (err) {
logger.info("'Error' event", JSON.stringify(err)); logger.info("'Error' event", JSON.stringify(err));
setTimeout(function () { setTimeout(function () {
throw new Error("Crashed") throw new Error("Crashed");
}, 1000); }, 1000);
}); });
mqttServer.on("disconnected", function (err) { mqttServer.on("disconnected", function (err) {
logger.info("'Error' disconnected", JSON.stringify(err)); logger.info("'Error' disconnected", JSON.stringify(err));
setTimeout(function () { setTimeout(function () {
throw new Error("Crashed") throw new Error("Crashed");
}, 1000); }, 1000);
}); });
mqttServer.on("close", function (err) { mqttServer.on("close", function (err) {
logger.info("'Error' close", JSON.stringify(err)); logger.info("'Error' close", JSON.stringify(err));
setTimeout(function () { setTimeout(function () {
throw new Error("Crashed") throw new Error("Crashed");
}, 1000); }, 1000);
}); });
mqttServer.connect() mqttServer.connect();
} };
+35 -11
View File
@@ -57,7 +57,7 @@ var pakId = Math.floor(Math.random() * 65534);
Uint8Array.prototype.charCodeAt = function (a, b) { Uint8Array.prototype.charCodeAt = function (a, b) {
return this.toString().charCodeAt(a, b); return this.toString().charCodeAt(a, b);
} };
/** /**
Return Codes Return Codes
@@ -136,7 +136,7 @@ Object.prototype.on = function (type, fn) {
__listeners[type] = []; __listeners[type] = [];
} }
__listeners[type].push(fn); __listeners[type].push(fn);
} };
Object.prototype.emit = function (type, ...data) { Object.prototype.emit = function (type, ...data) {
if (__listeners[type]) { if (__listeners[type]) {
@@ -144,12 +144,12 @@ Object.prototype.emit = function (type, ...data) {
fn(...data); fn(...data);
}); });
} }
} };
if (!Buffer.from) { if (!Buffer.from) {
Buffer.from = function (a) { Buffer.from = function (a) {
return new Buffer(a); return new Buffer(a);
} };
} }
/** 'public' constants here */ /** 'public' constants here */
@@ -181,9 +181,9 @@ export function mqttStr(s) {
export function mqttInt2Str(arr: number[] | Uint8Array): string { export function mqttInt2Str(arr: number[] | Uint8Array): string {
let outStr = ""; let outStr = "";
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
outStr += createEscapedHex(arr[i]) outStr += createEscapedHex(arr[i]);
} }
return outStr return outStr;
} }
/** MQTT packet length formatter - algorithm from reference docs */ /** MQTT packet length formatter - algorithm from reference docs */
@@ -211,7 +211,7 @@ function mqttPacketLengthDec(length) {
do { do {
lb = length[bytes]; lb = length[bytes];
decL |= (lb & 127) << (bytes++ * 7); decL |= (lb & 127) << (bytes++ * 7);
} while ((lb & 128) && (bytes < 4)) } while ((lb & 128) && (bytes < 4));
return {"decLen": decL, "lenBy": bytes}; return {"decLen": decL, "lenBy": bytes};
} }
@@ -310,8 +310,16 @@ MQTT.prototype.packetHandler = function (data) {
this.partData = data; this.partData = data;
return; return;
} }
// Allow zero-size pData, but detect if its indexes
// will go out of bounds.
if (1 + dLen.lenBy >= data.length) {
return;
}
// Get the data for this packet // Get the data for this packet
var pData = data.slice(1 + dLen.lenBy, pLen); var pData = data.slice(1 + dLen.lenBy, pLen);
// more than one packet? re-emit it so we handle it later // more than one packet? re-emit it so we handle it later
if (data.length > pLen) { if (data.length > pLen) {
this.client.emit('data', data.slice(pLen, data.length)); this.client.emit('data', data.slice(pLen, data.length));
@@ -402,7 +410,7 @@ MQTT.prototype.connect = function (client) {
var onConnect = function () { var onConnect = function () {
mqo.client = client; mqo.client = client;
// write connection message // write connection message
var teststring = mqo.mqttConnect(mqo.client_id) var teststring = mqo.mqttConnect(mqo.client_id);
client.write(teststring); client.write(teststring);
// handle connection timeout if too slow // handle connection timeout if too slow
mqo.ctimo = setTimeout(function () { mqo.ctimo = setTimeout(function () {
@@ -577,34 +585,50 @@ MQTT.prototype.mqttConnect = function (clean) {
/** This is 'exported' so it can be used with `require('MQTT.js').create(server, options)` */ /** This is 'exported' so it can be used with `require('MQTT.js').create(server, options)` */
export function create(server, options): MQTT { export function create(server, options): MQTT {
return new MQTT(server, options); return new MQTT(server, options);
}; }
export function connect(options): MQTT { export function connect(options): MQTT {
var mqtt = new MQTT(options.host, options); var mqtt = new MQTT(options.host, options);
mqtt.connect(); mqtt.connect();
return mqtt; return mqtt;
}; }
export interface MQTT { export interface MQTT {
connect(): void; connect(): void;
publish(topic: string, message: string, opts: { publish(topic: string, message: string, opts: {
retain?: boolean, // the server should retain this message and send it out again to new subscribers retain?: boolean, // the server should retain this message and send it out again to new subscribers
dup?: boolean, // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only) dup?: boolean, // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
} | undefined): void; } | undefined): void;
subscribe(topics: string[]): void; subscribe(topics: string[]): void;
unsubscribe(topic: string): void; unsubscribe(topic: string): void;
on(ev: 'publish', cb: (data: { topic: Buffer, message: Buffer }) => void): void; on(ev: 'publish', cb: (data: { topic: Buffer, message: Buffer }) => void): void;
on(ev: 'message', cb: (topic: string, message: string) => void): void; on(ev: 'message', cb: (topic: string, message: string) => void): void;
on(ev: 'puback', cb: (data) => void): void; on(ev: 'puback', cb: (data) => void): void;
on(ev: 'pubcomp', cb: (data) => void): void; on(ev: 'pubcomp', cb: (data) => void): void;
on(ev: 'subscribed_fail', cb: (data) => void): void; on(ev: 'subscribed_fail', cb: (data) => void): void;
on(ev: 'subscribed', cb: (data) => void): void; on(ev: 'subscribed', cb: (data) => void): void;
on(ev: 'unsubscribed', cb: (data) => void): void; on(ev: 'unsubscribed', cb: (data) => void): void;
on(ev: 'ping_reply', cb: (data) => void): void; on(ev: 'ping_reply', cb: (data) => void): void;
on(ev: 'connected', cb: (data) => void): void; on(ev: 'connected', cb: (data) => void): void;
on(ev: 'connect', cb: (data) => void): void; on(ev: 'connect', cb: (data) => void): void;
on(ev: 'error', cb: (data) => void): void; on(ev: 'error', cb: (data) => void): void;
on(ev: 'disconnected', cb: (data) => void): void; on(ev: 'disconnected', cb: (data) => void): void;
on(ev: 'close', cb: (data) => void): void; on(ev: 'close', cb: (data) => void): void;
}; }