From e6556e5eb243c0d33d3dc418bf3f52921360f7f2 Mon Sep 17 00:00:00 2001 From: Moritz Graf Date: Sun, 5 Jul 2026 07:45:37 +0200 Subject: [PATCH] Recent fixes to the TAUPI script + upload logic --- terraform_shelly/scripts/Taupi-4.0.js | 47 +++++++++++++-------------- terraform_shelly/scripts/upload.py | 9 +++++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/terraform_shelly/scripts/Taupi-4.0.js b/terraform_shelly/scripts/Taupi-4.0.js index 905d351..c83d4b1 100644 --- a/terraform_shelly/scripts/Taupi-4.0.js +++ b/terraform_shelly/scripts/Taupi-4.0.js @@ -193,7 +193,10 @@ const BTH = { 0x01: { n: "battery", t: uint8 }, 0x02: { n: "temperature", t: int16, f: 0.01 }, 0x03: { n: "humidity", t: uint16, f: 0.01 }, + 0x1e: { n: "light", t: uint8 }, + 0x2d: { n: "window", t: uint8 }, 0x2e: { n: "humidity", t: uint8 }, + 0x3f: { n: "rotation", t: int16, f: 0.1 }, 0x45: { n: "temperature", t: int16, f: 0.1 } }; @@ -209,24 +212,24 @@ const BTHomeDecoder = { const mask = 1 << (bitsz - 1); return num & mask ? num - (1 << bitsz) : num; }, - getUInt8: function (buffer) { - return buffer.at(0); + getUInt8: function (buffer, offset) { + return buffer.at(offset); }, - getInt8: function (buffer) { - return this.utoi(this.getUInt8(buffer), 8); + getInt8: function (buffer, offset) { + return this.utoi(this.getUInt8(buffer, offset), 8); }, - getUInt16LE: function (buffer) { - return 0xffff & ((buffer.at(1) << 8) | buffer.at(0)); + getUInt16LE: function (buffer, offset) { + return 0xffff & ((buffer.at(offset + 1) << 8) | buffer.at(offset)); }, - getInt16LE: function (buffer) { - return this.utoi(this.getUInt16LE(buffer), 16); + getInt16LE: function (buffer, offset) { + return this.utoi(this.getUInt16LE(buffer, offset), 16); }, - getBufValue: function (type, buffer) { - if (buffer.length < getByteSize(type)) return null; - if (type === uint8) return this.getUInt8(buffer); - if (type === int8) return this.getInt8(buffer); - if (type === uint16) return this.getUInt16LE(buffer); - if (type === int16) return this.getInt16LE(buffer); + getBufValue: function (type, buffer, offset) { + if (buffer.length - offset < getByteSize(type)) return null; + if (type === uint8) return this.getUInt8(buffer, offset); + if (type === int8) return this.getInt8(buffer, offset); + if (type === uint16) return this.getUInt16LE(buffer, offset); + if (type === int16) return this.getInt16LE(buffer, offset); return null; }, @@ -238,17 +241,17 @@ const BTHomeDecoder = { result["BTHome_version"] = _dib >> 5; if (result["BTHome_version"] !== 2) return null; if (result["encryption"]) return result; - buffer = buffer.slice(1); + let offset = 1; let _bth; let _value; - while (buffer.length > 0) { - _bth = BTH[buffer.at(0)]; + while (offset < buffer.length) { + _bth = BTH[buffer.at(offset)]; if (typeof _bth === "undefined") { break; } - buffer = buffer.slice(1); - _value = this.getBufValue(_bth.t, buffer); + offset++; + _value = this.getBufValue(_bth.t, buffer, offset); if (_value === null) break; if (typeof _bth.f !== "undefined") _value = _value * _bth.f; @@ -268,7 +271,7 @@ const BTHomeDecoder = { } } - buffer = buffer.slice(getByteSize(_bth.t)); + offset += getByteSize(_bth.t); } return result; }, @@ -347,10 +350,6 @@ if (typeof HTTPServer !== "undefined" && typeof HTTPServer.registerEndpoint === function addGauge(name, val, help) { if ((typeof val === "number" && !isNaN(val)) || typeof val === "boolean") { var numVal = (typeof val === "boolean") ? (val ? 1 : 0) : val; - if (help) { - body += "# HELP " + name + " " + help + "\n"; - } - body += "# TYPE " + name + " gauge\n"; body += name + " " + numVal + "\n"; } } diff --git a/terraform_shelly/scripts/upload.py b/terraform_shelly/scripts/upload.py index 6bb1f4f..41e7e37 100644 --- a/terraform_shelly/scripts/upload.py +++ b/terraform_shelly/scripts/upload.py @@ -7,6 +7,15 @@ def upload(ip, script_id, code_file): with open(code_file, 'r', encoding='utf-8') as f: code = f.read() + import re + # Remove comments and whitespace to save Shelly RAM + # Remove multi-line comments + code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL) + # Remove single-line comments (ignoring http:// or https://) + code = re.sub(r'(?