From a4e894cbd017a15c01d1ad8e8fdcb0090d55a646 Mon Sep 17 00:00:00 2001 From: Thanasi Pantazides Date: Fri, 3 Jan 2025 22:19:47 -0600 Subject: [PATCH] adding PingCollection --- collections/PingCollection.py | 50 +++++++++++++++++++++++++++++++++++ parsers/Pingparser.py | 8 +++--- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 collections/PingCollection.py diff --git a/collections/PingCollection.py b/collections/PingCollection.py new file mode 100644 index 0000000..f1c4648 --- /dev/null +++ b/collections/PingCollection.py @@ -0,0 +1,50 @@ +""" +Ping collection to handle the read-in Formatter ping data. +""" + + +class PingCollection: + """ + A container for ping data after being parsed. + + Parameters + --------- + parsed_data : `tuple`, length 2 + Contains `error`, `data`, as returned from the parser. + + old_data_time : `int`, `float` + The last time of the last data point previously extracted and + used. Default is `0` and so should take all data. + Default: 0 + + Example + ------- + with BackwardsReader(file=self.data_file, blksize=self.buffer_size, forward=True) as f: + data = f.read_block() + error, data = Pingparser(raw_data) + + ping_data = PingCollection((error, data)) + + plt.figure() + ping_data.plot_trace() + plt.show() + """ + + def __init__(self, parsed_data, old_data_time=0): + # bring in the parsed data + self.output, self.error_flag = parsed_data + + # used in the filter to only consider data with times > than this + self.last_data_time = old_data_time + + def get_unixtime(self): + return self.output["unixtime"] + + def get_global_errors_int(self): + return self.output["global_errors_int"] + + def get_all_systems_state(self): + return {k: self.output[k]["system_state_str"] for k in self.output.keys() if type(k) is int} + + def get_all_systems_error(self): + return {k: self.output[k]["system_error_str"] for k in self.output.keys() if type(k) is int} \ No newline at end of file diff --git a/parsers/Pingparser.py b/parsers/Pingparser.py index ad0aa4a..353832b 100644 --- a/parsers/Pingparser.py +++ b/parsers/Pingparser.py @@ -84,10 +84,10 @@ def pingparser(data: bytes): return {}, error_flag unixtime_raw = int.from_bytes(data[0:4], "big") - global_errors_raw = int.from_bytes(data[5:6], "big") + global_errors_int = int.from_bytes(data[5:6], "big") nsys = (len(data) - 6) // 4 # count systems in the rest of the packet - result = {"unixtime": unixtime_raw, "global_errors_int": global_errors_raw} + result = {"unixtime": unixtime_raw, "global_errors_int": global_errors_int} for k in range(nsys): # k indexes systems i = 6 + 4*k # i indexes the data array @@ -133,8 +133,8 @@ def chunks(lst, n): data = f.read() for chunk in chunks(data, 46): - p = pingparser(chunk) - pprint(p[0][9]['system_error_str']['reading_packet']) + p, e = pingparser(chunk) + pprint(p) else: print("use like this:\n\t> python parsers/Pingparser.py\n\t\tor\n\t>python parsers/Pingparser.py path/to/ping/file.log") \ No newline at end of file