protocol adaption to module update

This commit is contained in:
Dirk Alders 2021-01-07 01:07:53 +01:00
parent 70c458d349
commit 2ca538c49f

View File

@ -8,122 +8,131 @@ except ImportError:
piface_function = None piface_function = None
class my_base_protocol(socket_protocol.pure_json_protocol): DID_MODE = 0
DID_MODE = 0 DID_STATE = 1
DID_STATE = 1 #
# DID_BAKERY = 2
DID_BAKERY = 2 DID_BAKE_HOUSE = 3
DID_BAKE_HOUSE = 3 DID_MILL = 4
DID_MILL = 4 DID_REESE_HOUSE = 5
DID_REESE_HOUSE = 5 DID_PLOENLEIN = 6
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): class my_server_protocol(my_base_protocol):
def __init__(self, comm_instance, secret=None): 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.leyk = piface_function.leyk()
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MODE, self.set_mode) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MODE, self.set_mode)
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKERY, self.set_bakery) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKERY, self.set_bakery)
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKE_HOUSE, self.set_bake_house) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKE_HOUSE, self.set_bake_house)
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MILL, self.set_mill) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MILL, self.set_mill)
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_REESE_HOUSE, self.set_reese_house) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_REESE_HOUSE, self.set_reese_house)
self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_PLOENLEIN, self.set_ploenlein) self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_PLOENLEIN, self.set_ploenlein)
self.register_callback(self.SID_READ_REQUEST, self.DID_MODE, self.get_mode) self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MODE, self.get_mode)
self.register_callback(self.SID_READ_REQUEST, self.DID_STATE, self.get_state) self.register_callback(socket_protocol.SID_READ_REQUEST, DID_STATE, self.get_state)
self.register_callback(self.SID_READ_REQUEST, self.DID_BAKERY, self.get_bakery) self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKERY, self.get_bakery)
self.register_callback(self.SID_READ_REQUEST, self.DID_BAKE_HOUSE, self.get_bake_house) self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKE_HOUSE, self.get_bake_house)
self.register_callback(self.SID_READ_REQUEST, self.DID_MILL, self.get_mill) self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MILL, self.get_mill)
self.register_callback(self.SID_READ_REQUEST, self.DID_REESE_HOUSE, self.get_reese_house) self.register_callback(socket_protocol.SID_READ_REQUEST, 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_READ_REQUEST, DID_PLOENLEIN, self.get_ploenlein)
def set_mode(self, msg): def set_mode(self, msg):
if self.leyk.set_mode(msg.get_data()): if self.leyk.set_mode(msg.get_data()):
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_OPERATION_NOT_PERMITTED, None return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
def get_mode(self, msg): 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): 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): def set_bakery(self, msg):
if msg.get_data() is True or msg.get_data() is False: if msg.get_data() is True or msg.get_data() is False:
if self.leyk.set_bakery(msg.get_data()) is not True: 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: else:
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def get_bakery(self, msg): 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): def set_bake_house(self, msg):
if msg.get_data() is True or msg.get_data() is False: if msg.get_data() is True or msg.get_data() is False:
if self.leyk.set_bake_house(msg.get_data()) is not True: 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: else:
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def get_bake_house(self, msg): 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): def set_mill(self, msg):
if msg.get_data() is True or msg.get_data() is False: if msg.get_data() is True or msg.get_data() is False:
if self.leyk.set_mill(msg.get_data()) is not True: 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: else:
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def get_mill(self, msg): 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): def set_reese_house(self, msg):
if msg.get_data() is True or msg.get_data() is False: if msg.get_data() is True or msg.get_data() is False:
if self.leyk.set_reese_house(msg.get_data()) is not True: 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: else:
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def get_reese_house(self, msg): 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): def set_ploenlein(self, msg):
if msg.get_data() is True or msg.get_data() is False: if msg.get_data() is True or msg.get_data() is False:
if self.leyk.set_ploenlein(msg.get_data()) is not True: 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: else:
return self.STATUS_OKAY, None return socket_protocol.STATUS_OKAY, None
else: else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def get_ploenlein(self, msg): 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): class my_client_protocol(my_base_protocol):
START_ROUTINE_DATA_IDS = [ START_ROUTINE_DATA_IDS = [
my_base_protocol.DID_MODE, DID_MODE,
my_base_protocol.DID_BAKERY, DID_BAKERY,
my_base_protocol.DID_BAKE_HOUSE, DID_BAKE_HOUSE,
my_base_protocol.DID_MILL, DID_MILL,
my_base_protocol.DID_REESE_HOUSE, DID_REESE_HOUSE,
my_base_protocol.DID_PLOENLEIN, DID_PLOENLEIN,
] ]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs) my_base_protocol.__init__(self, *args, **kwargs)
self.register_callback(self.SID_EXECUTE_RESPONSE, None, self.start_routine_response) self.register_callback(socket_protocol.SID_EXECUTE_RESPONSE, None, self.start_routine_response)
self.register_callback(self.SID_READ_RESPONSE, None, self.read_data_main_state) self.register_callback(socket_protocol.SID_READ_RESPONSE, None, self.read_data_main_state)
def __authentificate_process_feedback__(self, msg): def __authentificate_process_feedback__(self, msg):
if msg.get_data() is True: 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) return my_base_protocol.__authentificate_process_feedback__(self, msg)
def start_routine_response(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") print("OKAY")
return self.STATUS_OKAY, None
else: else:
print("FAILED") print("FAILED")
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
def read_data_main_state(self, msg): def read_data_main_state(self, msg):
if msg.get_status() == self.STATUS_OKAY: if msg.get_status() == socket_protocol.STATUS_OKAY:
if msg.get_data_id() == self.DID_MODE: if msg.get_data_id() == DID_MODE:
print("Mode: %s" % repr(msg.get_data())) print("Mode: %s" % repr(msg.get_data()))
return self.STATUS_OKAY, None elif msg.get_data_id() == DID_STATE:
elif msg.get_data_id() == self.DID_STATE:
print("State: %s" % repr(msg.get_data())) print("State: %s" % repr(msg.get_data()))
return self.STATUS_OKAY, None elif msg.get_data_id() in [DID_BAKE_HOUSE, DID_BAKERY, DID_MILL, DID_PLOENLEIN, DID_REESE_HOUSE]:
elif msg.get_data_id() in [self.DID_BAKE_HOUSE, self.DID_BAKERY, self.DID_MILL, self.DID_PLOENLEIN, self.DID_REESE_HOUSE]:
state = { state = {
0: 'Off', 0: 'Off',
1: 'On', 1: 'On',
}.get(msg.get_data()) }.get(msg.get_data())
target = { target = {
self.DID_BAKE_HOUSE: 'Bakehouse', DID_BAKE_HOUSE: 'Bakehouse',
self.DID_BAKERY: 'Bakery', DID_BAKERY: 'Bakery',
self.DID_MILL: 'Mill', DID_MILL: 'Mill',
self.DID_REESE_HOUSE: 'Reesehouse', DID_REESE_HOUSE: 'Reesehouse',
self.DID_PLOENLEIN: 'Ploenlein', DID_PLOENLEIN: 'Ploenlein',
}.get(msg.get_data_id()) }.get(msg.get_data_id())
print('The light in {target} is {state}'.format(state=state, target=target)) print('The light in {target} is {state}'.format(state=state, target=target))
return self.STATUS_OKAY, None
print('Error receiving data!') print('Error receiving data!')
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None