#!/usr/bin/env python # -*- coding: UTF-8 -*- import socket_protocol try: import piface_function except ImportError: piface_function = None 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): my_base_protocol.__init__(self, comm_instance, secret=secret) self.leyk = piface_function.leyk() 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 socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None def get_mode(self, msg): return socket_protocol.STATUS_OKAY, self.leyk.get_mode() def get_state(self, msg): 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 socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: return socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_bakery(self, msg): 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 socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: return socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_bake_house(self, msg): 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 socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: return socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_mill(self, msg): 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 socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: return socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_reese_house(self, msg): 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 socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None else: return socket_protocol.STATUS_OKAY, None else: return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None def get_ploenlein(self, msg): return socket_protocol.STATUS_OKAY, self.leyk.get_ploenlein() class my_client_protocol(my_base_protocol): START_ROUTINE_DATA_IDS = [ DID_MODE, DID_BAKERY, DID_BAKE_HOUSE, DID_MILL, DID_REESE_HOUSE, DID_PLOENLEIN, ] def __init__(self, *args, **kwargs): 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: print("The client is authentificated.") else: print("AUTHENTIFICATION ERROR") return my_base_protocol.__authentificate_process_feedback__(self, msg) def start_routine_response(self, msg): if msg.get_status() == socket_protocol.STATUS_OKAY and msg.get_data_id() in self.START_ROUTINE_DATA_IDS: print("OKAY") else: print("FAILED") def read_data_main_state(self, msg): if msg.get_status() == socket_protocol.STATUS_OKAY: if msg.get_data_id() == DID_MODE: print("Mode: %s" % repr(msg.get_data())) elif msg.get_data_id() == DID_STATE: print("State: %s" % repr(msg.get_data())) 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 = { 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)) print('Error receiving data!')