From 5ba152a0e63322098e78fc8c819691ddc9d2ce51 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Fri, 12 Jun 2020 17:22:28 +0000 Subject: [PATCH] station_server: provide history handlers While the history handlers do need to match what has been saved on disk, the project provides MfgEvent protobuffer writers out of the box, and no-others, so at least let the out of box experience function as expected, even if you would want to change this in your own implementations. To enable the (built in) writers, something like this is required in your station server. ``` if __name__ == '__main__': openhtf.util.conf.load(station_server_port='4444') interface = mfg_inspector.MfgInspector() interface.set_converter(mfg_event_from_test_record) with station_server.StationServer(history_path="somepath") as server: while 1: test = .... #your tests here test.add_output_callbacks(server.publish_final_state) # explicitly match hardcoded pattern in HistoryListHandler test.add_output_callbacks(interface.save_to_disk("somepath/mfg_event_{dut_id}_{start_time_millis}.pb")) test.execute(test_start=user_input.prompt_for_test_start()) ``` Signed-off-by: Karl Palsson --- openhtf/output/servers/station_server.py | 48 ++++++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/openhtf/output/servers/station_server.py b/openhtf/output/servers/station_server.py index fe20bb2bb..be773ab5b 100644 --- a/openhtf/output/servers/station_server.py +++ b/openhtf/output/servers/station_server.py @@ -21,6 +21,8 @@ import openhtf from openhtf.output.callbacks import mfg_inspector +from openhtf.output.proto import mfg_event_converter +from openhtf.output.proto import mfg_event_pb2 from openhtf.output.servers import pub_sub from openhtf.output.servers import web_gui_server from openhtf.util import conf @@ -461,10 +463,25 @@ class HistoryItemHandler(BaseHistoryHandler): """GET endpoint for a test record from the history.""" def get(self, file_name): - # TODO(Kenadia): Implement the history item handler. The implementation - # depends on the format used to store test records on disk. - self.write('Not implemented.') - self.set_status(500) + # The "Out of the box" disk format is fixed, subclasses/alternative + # implementations need to be sure their implementation matches their + # recorder + + fn = os.path.join(self.history_path, file_name) + if not os.path.isfile(fn): + self.write('Not found') + self.set_status(404) + return + + with open(fn, mode='rb') as f: + me = mfg_event_pb2.MfgEvent() + me.ParseFromString(f.read()) + tr = mfg_event_converter.test_record_from_mfg_event(me) + test_record_dict = data.convert_to_base_types(tr) + test_state_dict = _test_state_from_record(test_record_dict, + StationPubSub._last_execution_uid) + self.set_status(200) + self.write(test_state_dict) class HistoryAttachmentsHandler(BaseHistoryHandler): @@ -477,10 +494,25 @@ class HistoryAttachmentsHandler(BaseHistoryHandler): """ def get(self, file_name, attachment_name): - # TODO(Kenadia): Implement the history item handler. The implementation - # depends on the format used to store test records on disk. - self.write('Not implemented.') - self.set_status(500) + # The implementation depends on the format used to store + # test records on disk. + fn = os.path.join(self.history_path, file_name) + if not os.path.isfile(fn): + self.write('Not found') + self.set_status(404) + return + + with open(fn, mode='rb') as f: + me = mfg_event_pb2.MfgEvent() + me.ParseFromString(f.read()) + # TODO: could use sha1 here to check? + desired_real = [a for a in me.attachment if a.name == attachment_name] + if len(desired_real) > 0: + self.write(desired_real[0].value_binary) + self.set_status(200) + else: + self.write('Attachment not found in test') + self.set_status(404) class StationMulticast(multicast.MulticastListener):