Error reporting of parsing errors

This commit is contained in:
Andreas Thienemann 2022-02-26 19:32:39 +01:00
parent ab6d472510
commit a7c619c057
3 changed files with 27 additions and 12 deletions

5
app.py
View File

@ -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)

View File

@ -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()

View File

@ -27,7 +27,7 @@
<p><p>
</div>
<div>
{% if messages | length > 0 %}
{% if messages | length > 0 %}
<h2>Dekodierte SML Nachrichten</h2>
<table class="table table-striped">
<thead class="thead-light">
@ -47,7 +47,7 @@
<td>{{ msg.msg.obis_short }}</td>
<td>{{ msg.msg.name }}</td>
<td>{{ msg.msg.value}}</td>
<td>{{ msg.msg.unit }}</td>
<td>{{ msg.msg.unit }}</td>
<td>{{ msg.msg.human_readable }}</td>
</tr>
{%- endfor %}
@ -62,13 +62,19 @@
{% endfor -%}</pre>
{% endif %}
<hr>
{% if obis_errors | length > 0 %}
<h2>Parsing Fehler</h2>
<pre>{% for error in obis_errors -%}
<span class="parser-error">{{ error.hex.decode('utf-8') }}</span>: {{ error.msg }}
{% endfor -%}</pre>
{% endif %}
<h2>Empfangene Daten</h2>
Folgende Daten wurden empfangen.
<pre>{% for line in smldump -%}
{% if line in parse_errors -%}
<span class="parser-error">{{ line }}</span>
<span class="parser-error">{{ line | e }}</span>
{% else -%}
<span class="parser-success">{{ line }}</span>
<span class="parser-success">{{ line | e }}</span>
{% endif -%}
{% endfor -%}</pre>
</div>