From 2ca538c49fe141ed356f28bb019c81b1b2d8890b Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Thu, 7 Jan 2021 01:07:53 +0100 Subject: [PATCH] protocol adaption to module update --- __init__.py | 147 +++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 72 deletions(-) diff --git a/__init__.py b/__init__.py index ce8596f..1dde508 100644 --- a/__init__.py +++ b/__init__.py @@ -8,122 +8,131 @@ except ImportError: piface_function = None -class my_base_protocol(socket_protocol.pure_json_protocol): - DID_MODE = 0 - DID_STATE = 1 - # - DID_BAKERY = 2 - DID_BAKE_HOUSE = 3 - DID_MILL = 4 - DID_REESE_HOUSE = 5 - DID_PLOENLEIN = 6 +DID_MODE = 0 +DID_STATE = 1 +# +DID_BAKERY = 2 +DID_BAKE_HOUSE = 3 +DID_MILL = 4 +DID_REESE_HOUSE = 5 +DID_PLOENLEIN = 6 +class my_base_protocol(socket_protocol.pure_json_protocol): + def __init__(self, *args, **kwargs): + socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs) + self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_MODE, 'mode') + self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_STATE, 'state') + self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_BAKERY, 'on_off_bakery') + self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_BAKE_HOUSE, 'on_off_bake_house') + self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_MILL, 'on_off_mill') + self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_REESE_HOUSE, 'on_off_reese_house') + self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_PLOENLEIN, 'on_off_ploenlein') class my_server_protocol(my_base_protocol): def __init__(self, comm_instance, secret=None): - socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret) + my_base_protocol.__init__(self, comm_instance, secret=secret) self.leyk = piface_function.leyk() - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MODE, self.set_mode) - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKERY, self.set_bakery) - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKE_HOUSE, self.set_bake_house) - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MILL, self.set_mill) - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_REESE_HOUSE, self.set_reese_house) - self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_PLOENLEIN, self.set_ploenlein) - self.register_callback(self.SID_READ_REQUEST, self.DID_MODE, self.get_mode) - self.register_callback(self.SID_READ_REQUEST, self.DID_STATE, self.get_state) - self.register_callback(self.SID_READ_REQUEST, self.DID_BAKERY, self.get_bakery) - self.register_callback(self.SID_READ_REQUEST, self.DID_BAKE_HOUSE, self.get_bake_house) - self.register_callback(self.SID_READ_REQUEST, self.DID_MILL, self.get_mill) - self.register_callback(self.SID_READ_REQUEST, self.DID_REESE_HOUSE, self.get_reese_house) - self.register_callback(self.SID_READ_REQUEST, self.DID_PLOENLEIN, self.get_ploenlein) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MODE, self.set_mode) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKERY, self.set_bakery) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKE_HOUSE, self.set_bake_house) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MILL, self.set_mill) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_REESE_HOUSE, self.set_reese_house) + self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_PLOENLEIN, self.set_ploenlein) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MODE, self.get_mode) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_STATE, self.get_state) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKERY, self.get_bakery) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKE_HOUSE, self.get_bake_house) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MILL, self.get_mill) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_REESE_HOUSE, self.get_reese_house) + self.register_callback(socket_protocol.SID_READ_REQUEST, DID_PLOENLEIN, self.get_ploenlein) def set_mode(self, msg): if self.leyk.set_mode(msg.get_data()): - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None def get_mode(self, msg): - return self.STATUS_OKAY, self.leyk.get_mode() + return socket_protocol.STATUS_OKAY, self.leyk.get_mode() def get_state(self, msg): - return self.STATUS_OKAY, self.leyk.get_state() + return socket_protocol.STATUS_OKAY, self.leyk.get_state() def set_bakery(self, msg): if msg.get_data() is True or msg.get_data() is False: if self.leyk.set_bakery(msg.get_data()) is not True: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_bakery(self, msg): - return self.STATUS_OKAY, self.leyk.get_bakery() + return socket_protocol.STATUS_OKAY, self.leyk.get_bakery() def set_bake_house(self, msg): if msg.get_data() is True or msg.get_data() is False: if self.leyk.set_bake_house(msg.get_data()) is not True: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_bake_house(self, msg): - return self.STATUS_OKAY, self.leyk.get_bake_house() + return socket_protocol.STATUS_OKAY, self.leyk.get_bake_house() def set_mill(self, msg): if msg.get_data() is True or msg.get_data() is False: if self.leyk.set_mill(msg.get_data()) is not True: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_mill(self, msg): - return self.STATUS_OKAY, self.leyk.get_mill() + return socket_protocol.STATUS_OKAY, self.leyk.get_mill() def set_reese_house(self, msg): if msg.get_data() is True or msg.get_data() is False: if self.leyk.set_reese_house(msg.get_data()) is not True: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_reese_house(self, msg): - return self.STATUS_OKAY, self.leyk.get_reese_house() + return socket_protocol.STATUS_OKAY, self.leyk.get_reese_house() def set_ploenlein(self, msg): if msg.get_data() is True or msg.get_data() is False: if self.leyk.set_ploenlein(msg.get_data()) is not True: - return self.STATUS_OPERATION_NOT_PERMITTED, None + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: - return self.STATUS_OKAY, None + return socket_protocol.STATUS_OKAY, None else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_ploenlein(self, msg): - return self.STATUS_OKAY, self.leyk.get_ploenlein() + return socket_protocol.STATUS_OKAY, self.leyk.get_ploenlein() class my_client_protocol(my_base_protocol): START_ROUTINE_DATA_IDS = [ - my_base_protocol.DID_MODE, - my_base_protocol.DID_BAKERY, - my_base_protocol.DID_BAKE_HOUSE, - my_base_protocol.DID_MILL, - my_base_protocol.DID_REESE_HOUSE, - my_base_protocol.DID_PLOENLEIN, + DID_MODE, + DID_BAKERY, + DID_BAKE_HOUSE, + DID_MILL, + DID_REESE_HOUSE, + DID_PLOENLEIN, ] def __init__(self, *args, **kwargs): - socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs) - self.register_callback(self.SID_EXECUTE_RESPONSE, None, self.start_routine_response) - self.register_callback(self.SID_READ_RESPONSE, None, self.read_data_main_state) + my_base_protocol.__init__(self, *args, **kwargs) + self.register_callback(socket_protocol.SID_EXECUTE_RESPONSE, None, self.start_routine_response) + self.register_callback(socket_protocol.SID_READ_RESPONSE, None, self.read_data_main_state) def __authentificate_process_feedback__(self, msg): if msg.get_data() is True: @@ -133,34 +142,28 @@ class my_client_protocol(my_base_protocol): return my_base_protocol.__authentificate_process_feedback__(self, msg) def start_routine_response(self, msg): - if msg.get_status() == self.STATUS_OKAY and msg.get_data_id() in self.START_ROUTINE_DATA_IDS: + if msg.get_status() == socket_protocol.STATUS_OKAY and msg.get_data_id() in self.START_ROUTINE_DATA_IDS: print("OKAY") - return self.STATUS_OKAY, None else: print("FAILED") - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None def read_data_main_state(self, msg): - if msg.get_status() == self.STATUS_OKAY: - if msg.get_data_id() == self.DID_MODE: + if msg.get_status() == socket_protocol.STATUS_OKAY: + if msg.get_data_id() == DID_MODE: print("Mode: %s" % repr(msg.get_data())) - return self.STATUS_OKAY, None - elif msg.get_data_id() == self.DID_STATE: + elif msg.get_data_id() == DID_STATE: print("State: %s" % repr(msg.get_data())) - return self.STATUS_OKAY, None - elif msg.get_data_id() in [self.DID_BAKE_HOUSE, self.DID_BAKERY, self.DID_MILL, self.DID_PLOENLEIN, self.DID_REESE_HOUSE]: + elif msg.get_data_id() in [DID_BAKE_HOUSE, DID_BAKERY, DID_MILL, DID_PLOENLEIN, DID_REESE_HOUSE]: state = { 0: 'Off', 1: 'On', }.get(msg.get_data()) target = { - self.DID_BAKE_HOUSE: 'Bakehouse', - self.DID_BAKERY: 'Bakery', - self.DID_MILL: 'Mill', - self.DID_REESE_HOUSE: 'Reesehouse', - self.DID_PLOENLEIN: 'Ploenlein', + DID_BAKE_HOUSE: 'Bakehouse', + DID_BAKERY: 'Bakery', + DID_MILL: 'Mill', + DID_REESE_HOUSE: 'Reesehouse', + DID_PLOENLEIN: 'Ploenlein', }.get(msg.get_data_id()) print('The light in {target} is {state}'.format(state=state, target=target)) - return self.STATUS_OKAY, None print('Error receiving data!') - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None