diff --git a/app.py b/app.py index c37eef2..bd3705d 100755 --- a/app.py +++ b/app.py @@ -10,7 +10,6 @@ import json app = Flask(__name__) Bootstrap(app) app.config["BOOTSTRAP_SERVE_LOCAL"] = True -app.debug = False nav = Nav() nav.init_app(app) nav.register_element("frontend_top", Navbar(View("Tasmota SML Decoder", ".index"))) @@ -27,6 +26,7 @@ def decode(): return redirect("/") elif request.method == "POST": data = request.form["smldump"].splitlines() + data = [x.strip() for x in data] tas = TasmotaSMLParser() msgs = tas.decode_messages(data) @@ -43,9 +43,10 @@ def decode(): "decode.html", smldump=data, parse_errors=tas.parse_errors, + obis_errors=tas.obis_errors, messages=messages, ) if __name__ == "__main__": - app.run() + app.run(debug=True) diff --git a/sml_decoder.py b/sml_decoder.py index 9a4e66e..8b80123 100755 --- a/sml_decoder.py +++ b/sml_decoder.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 -from binascii import a2b_hex +from binascii import a2b_hex, b2a_hex from smllib.sml_frame import SmlFrame from smllib.const import OBIS_NAMES, UNITS +from pprint import pprint file = "test-data.txt" @@ -15,10 +16,10 @@ class TasmotaSMLParser: def parse_input(self, input): """Parse a line into a frame""" - if " : 77 " in input: + if " : 77 " in input.strip(): # Remove the spaces and return a bytestring frame = a2b_hex("".join((input.split(" : ", 1)[1]).split())) - elif input.startswith("77 0"): + elif input.startswith("77 "): frame = a2b_hex("".join(input.split())) else: self.parse_errors.append(input) @@ -32,8 +33,10 @@ class TasmotaSMLParser: msgs = f.get_obis() if len(msgs) == 0: return False - except: - self.obis_errors.append(frame) + except Exception as e: + self.obis_errors.append( + {"frame": frame, "hex": b2a_hex(frame, b" "), "msg": e.args[0]} + ) return None return msgs @@ -68,7 +71,9 @@ class TasmotaSMLParser: precision = 0 try: - human_readable = f"{round(msg.value * pow(10, msg.scaler), precision)}{unit} ({name})" + human_readable = ( + f"{round(msg.value * pow(10, msg.scaler), precision)}{unit} ({name})" + ) except TypeError: if msg.unit in UNITS and msg.name in OBIS_NAMES: human_readable = f"{msg.value}{unit} ({name})" @@ -117,6 +122,9 @@ def main(): print(tas.build_meter_def(msg)) print(80 * "#") + if len(tas.obis_errors) > 0: + pprint(tas.obis_errors) + if __name__ == "__main__": main() diff --git a/templates/decode.html b/templates/decode.html index bf02418..be54b86 100644 --- a/templates/decode.html +++ b/templates/decode.html @@ -27,7 +27,7 @@
| {{ msg.msg.obis_short }} | {{ msg.msg.name }} | {{ msg.msg.value}} | -{{ msg.msg.unitĀ }} | +{{ msg.msg.unit }} | {{ msg.msg.human_readable }} | {%- endfor %} @@ -62,13 +62,19 @@ {% endfor -%} {% endif %}