From 67aba95ef6dfd2a5cdd735e4a874538f30de1c4b Mon Sep 17 00:00:00 2001 From: marios8543 Date: Thu, 16 May 2024 15:25:10 +0300 Subject: [PATCH] Fix data logger --- pyren3/bus_monitor.py | 4 +--- pyren3/data_logger.py | 43 +++++++++++++++++++++++++++---------- pyren3/index.html | 4 ++++ pyren3/mod_ecu_parameter.py | 3 ++- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/pyren3/bus_monitor.py b/pyren3/bus_monitor.py index 4025978..cd9f8fb 100644 --- a/pyren3/bus_monitor.py +++ b/pyren3/bus_monitor.py @@ -76,7 +76,7 @@ class DDT_MON(): framefilter = '' def __init__(self, elm, xmlfile, outfile, infile ): - self.elm = elm + self.elm: ELM = elm clearScreen() print("Starting DDT process") @@ -390,8 +390,6 @@ def chooseXml(): choice = ChoiceLong(menu, "Choose :", choice[0]) return choice[0] - - def main(): diff --git a/pyren3/data_logger.py b/pyren3/data_logger.py index e25fec2..d32987c 100644 --- a/pyren3/data_logger.py +++ b/pyren3/data_logger.py @@ -5,6 +5,7 @@ from csv import writer, QUOTE_MINIMAL from datetime import datetime import mod_globals + mod_globals.opt_demo = True dash = RenDash("/dev/tty1") @@ -19,23 +20,33 @@ running = False bg_thread = None + def mainloop(): global running, watched_datarefs, watched_ecu, should_log, freeze if should_log: - log_filename = f'{datetime.now().strftime("%m/%d/%Y, %H:%M:%S")}.csv' + log_filename = f'../logs/{datetime.now().strftime("%m-%d-%Y %H:%M:%S")}.csv' print("Creating log on file: " + log_filename) - with open(log_filename, "w+") as file: - csv_writer = writer(file,delimiter=' ', quotechar='|', quoting=QUOTE_MINIMAL) - csv_writer.writerow(["freeze"].extend(watched_datarefs)) + file = open(log_filename, "w") + csv_writer = writer( + file, delimiter=" ", quotechar="|", quoting=QUOTE_MINIMAL + ) + csv_writer.writerow(["freeze"] + watched_datarefs) print("Starting data loop") while running: - frame = [dash.get_ecu_dataref(watched_ecu, dataref) for dataref in watched_datarefs] + frame = [ + dash.get_ecu_dataref(watched_ecu, dataref)[0] for dataref in watched_datarefs + ] socket.emit("frame", {"frame": frame}) if should_log: - csv_writer.writerow([freeze].extend(frame)) + csv_writer.writerow([freeze] + frame) freeze = 0 print(frame) + + if should_log: + file.flush() + file.close() + @socket.on("freeze") def freeze_event(): @@ -48,14 +59,17 @@ def get_ecus(): ecus = dash.get_ecu_names() return jsonify(ecus) + @web.get("/ecus//states") def get_ecu_states(ecu_doc): return jsonify(dash.get_ecu_states(ecu_doc)) + @web.get("/ecus//parameters") def get_ecu_parameters(ecu_doc): return jsonify(dash.get_ecu_parameters(ecu_doc)) + @web.post("/ecus//watch") def ecu_watch(ecu_doc): global watched_ecu, watched_datarefs @@ -63,8 +77,9 @@ def ecu_watch(ecu_doc): datarefs = request.get_json() watched_datarefs = [] for dataref in datarefs: - if dataref in dash.get_ecu_parameters(ecu_doc) \ - or dataref in dash.get_ecu_states(ecu_doc): + if dataref in dash.get_ecu_parameters( + ecu_doc + ) or dataref in dash.get_ecu_states(ecu_doc): watched_datarefs.append(dataref) else: watched_datarefs = [] @@ -73,20 +88,24 @@ def ecu_watch(ecu_doc): return jsonify({"ecu": watched_ecu, "datarefs": watched_datarefs}) + @web.get("/start") def start(): global should_log, bg_thread, running - + + if running: + return jsonify({"success": False}) log = request.args.get("log") if log == "true": should_log = True else: should_log = False - + running = True bg_thread = socket.start_background_task(mainloop) return jsonify({"success": True}) + @web.get("/stop") def stop(): global bg_thread, running @@ -95,9 +114,11 @@ def stop(): bg_thread.join() return jsonify({"success": True}) + @web.get("/") def index(): return send_file("index.html") + if __name__ == "__main__": - socket.run(web, "0.0.0.0", 5000) \ No newline at end of file + socket.run(web, "0.0.0.0", 5000) diff --git a/pyren3/index.html b/pyren3/index.html index 0612b4c..34c7558 100644 --- a/pyren3/index.html +++ b/pyren3/index.html @@ -31,6 +31,7 @@

Watching

+
{{ statesAndParams[item] }}: {{ itemValues[item] }}
@@ -112,6 +113,9 @@ method: "GET", }); }, + async sendFreeze() { + socket.emit("freeze"); + } }, mounted() { this.fetchEcuList(); diff --git a/pyren3/mod_ecu_parameter.py b/pyren3/mod_ecu_parameter.py index 7d6a39f..1e874b0 100755 --- a/pyren3/mod_ecu_parameter.py +++ b/pyren3/mod_ecu_parameter.py @@ -42,7 +42,8 @@ def get_parameter( pr, mn, se, elm, calc, dataids = {} ): tmpmin = '' tmpmax = '' - return "%5s %10s %-10s %-5s"%(tmpmin,pr.value,pr.unit,tmpmax), pr.helps, csv_data + #return "%5s %10s %-10s %-5s"%(tmpmin,pr.value,pr.unit,tmpmax), pr.helps, csv_data + return "%10s"%(pr.value), pr.helps, csv_data class ecu_parameter: