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__) app = Flask(__name__)
Bootstrap(app) Bootstrap(app)
app.config["BOOTSTRAP_SERVE_LOCAL"] = True app.config["BOOTSTRAP_SERVE_LOCAL"] = True
app.debug = False
nav = Nav() nav = Nav()
nav.init_app(app) nav.init_app(app)
nav.register_element("frontend_top", Navbar(View("Tasmota SML Decoder", ".index"))) nav.register_element("frontend_top", Navbar(View("Tasmota SML Decoder", ".index")))
@ -27,6 +26,7 @@ def decode():
return redirect("/") return redirect("/")
elif request.method == "POST": elif request.method == "POST":
data = request.form["smldump"].splitlines() data = request.form["smldump"].splitlines()
data = [x.strip() for x in data]
tas = TasmotaSMLParser() tas = TasmotaSMLParser()
msgs = tas.decode_messages(data) msgs = tas.decode_messages(data)
@ -43,9 +43,10 @@ def decode():
"decode.html", "decode.html",
smldump=data, smldump=data,
parse_errors=tas.parse_errors, parse_errors=tas.parse_errors,
obis_errors=tas.obis_errors,
messages=messages, messages=messages,
) )
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run(debug=True)

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from binascii import a2b_hex from binascii import a2b_hex, b2a_hex
from smllib.sml_frame import SmlFrame from smllib.sml_frame import SmlFrame
from smllib.const import OBIS_NAMES, UNITS from smllib.const import OBIS_NAMES, UNITS
from pprint import pprint
file = "test-data.txt" file = "test-data.txt"
@ -15,10 +16,10 @@ class TasmotaSMLParser:
def parse_input(self, input): def parse_input(self, input):
"""Parse a line into a frame""" """Parse a line into a frame"""
if " : 77 " in input: if " : 77 " in input.strip():
# Remove the spaces and return a bytestring # Remove the spaces and return a bytestring
frame = a2b_hex("".join((input.split(" : ", 1)[1]).split())) frame = a2b_hex("".join((input.split(" : ", 1)[1]).split()))
elif input.startswith("77 0"): elif input.startswith("77 "):
frame = a2b_hex("".join(input.split())) frame = a2b_hex("".join(input.split()))
else: else:
self.parse_errors.append(input) self.parse_errors.append(input)
@ -32,8 +33,10 @@ class TasmotaSMLParser:
msgs = f.get_obis() msgs = f.get_obis()
if len(msgs) == 0: if len(msgs) == 0:
return False return False
except: except Exception as e:
self.obis_errors.append(frame) self.obis_errors.append(
{"frame": frame, "hex": b2a_hex(frame, b" "), "msg": e.args[0]}
)
return None return None
return msgs return msgs
@ -68,7 +71,9 @@ class TasmotaSMLParser:
precision = 0 precision = 0
try: 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: except TypeError:
if msg.unit in UNITS and msg.name in OBIS_NAMES: if msg.unit in UNITS and msg.name in OBIS_NAMES:
human_readable = f"{msg.value}{unit} ({name})" human_readable = f"{msg.value}{unit} ({name})"
@ -117,6 +122,9 @@ def main():
print(tas.build_meter_def(msg)) print(tas.build_meter_def(msg))
print(80 * "#") print(80 * "#")
if len(tas.obis_errors) > 0:
pprint(tas.obis_errors)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

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