Skip to content

Commit b79fc66

Browse files
committed
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 <[email protected]>
1 parent 4fa142e commit b79fc66

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

openhtf/output/servers/station_server.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import openhtf
2323
from openhtf.output.callbacks import mfg_inspector
24+
from openhtf.output.proto import mfg_event_converter
25+
from openhtf.output.proto import mfg_event_pb2
2426
from openhtf.output.servers import pub_sub
2527
from openhtf.output.servers import web_gui_server
2628
from openhtf.util import conf
@@ -461,10 +463,25 @@ class HistoryItemHandler(BaseHistoryHandler):
461463
"""GET endpoint for a test record from the history."""
462464

463465
def get(self, file_name):
464-
# TODO(Kenadia): Implement the history item handler. The implementation
465-
# depends on the format used to store test records on disk.
466-
self.write('Not implemented.')
467-
self.set_status(500)
466+
# The "Out of the box" disk format is fixed, subclasses/alternative
467+
# implementations need to be sure their implementation matches their
468+
# recorder
469+
470+
fn = os.path.join(self.history_path, file_name)
471+
if not os.path.isfile(fn):
472+
self.write("Not found")
473+
self.set_status(404)
474+
return
475+
476+
with open(fn, mode="rb") as f:
477+
me = mfg_event_pb2.MfgEvent()
478+
me.ParseFromString(f.read())
479+
tr = mfg_event_converter.test_record_from_mfg_event(me)
480+
test_record_dict = data.convert_to_base_types(tr)
481+
test_state_dict = _test_state_from_record(test_record_dict,
482+
StationPubSub._last_execution_uid)
483+
self.set_status(200)
484+
self.write(test_state_dict)
468485

469486

470487
class HistoryAttachmentsHandler(BaseHistoryHandler):
@@ -477,10 +494,25 @@ class HistoryAttachmentsHandler(BaseHistoryHandler):
477494
"""
478495

479496
def get(self, file_name, attachment_name):
480-
# TODO(Kenadia): Implement the history item handler. The implementation
481-
# depends on the format used to store test records on disk.
482-
self.write('Not implemented.')
483-
self.set_status(500)
497+
# The implementation depends on the format used to store
498+
# test records on disk.
499+
fn = os.path.join(self.history_path, file_name)
500+
if not os.path.isfile(fn):
501+
self.write('Not found')
502+
self.set_status(404)
503+
return
504+
505+
with open(fn, mode="rb") as f:
506+
me = mfg_event_pb2.MfgEvent()
507+
me.ParseFromString(f.read())
508+
# TODO: could use sha1 here to check?
509+
desired_real = [a for a in me.attachment if a.name == attachment_name]
510+
if len(desired_real) > 0:
511+
self.write(desired_real[0].value_binary)
512+
self.set_status(200)
513+
else:
514+
self.write('Attachment not found in test')
515+
self.set_status(404)
484516

485517

486518
class StationMulticast(multicast.MulticastListener):

0 commit comments

Comments
 (0)