167 lines
6.7 KiB
Python
167 lines
6.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
import socket_protocol
|
|
try:
|
|
import piface_function
|
|
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
|
|
|
|
|
|
class my_server_protocol(my_base_protocol):
|
|
def __init__(self, comm_instance, secret=None):
|
|
socket_protocol.pure_json_protocol.__init__(self, comm_instance, 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)
|
|
|
|
def set_mode(self, msg):
|
|
if self.leyk.set_mode(msg.get_data()):
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_OPERATION_NOT_PERMITTED, None
|
|
|
|
def get_mode(self, msg):
|
|
return self.STATUS_OKAY, self.leyk.get_mode()
|
|
|
|
def get_state(self, msg):
|
|
return self.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
|
|
else:
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|
|
|
|
def get_bakery(self, msg):
|
|
return self.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
|
|
else:
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|
|
|
|
def get_bake_house(self, msg):
|
|
return self.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
|
|
else:
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|
|
|
|
def get_mill(self, msg):
|
|
return self.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
|
|
else:
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|
|
|
|
def get_reese_house(self, msg):
|
|
return self.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
|
|
else:
|
|
return self.STATUS_OKAY, None
|
|
else:
|
|
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|
|
|
|
def get_ploenlein(self, msg):
|
|
return self.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,
|
|
]
|
|
|
|
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)
|
|
|
|
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() == self.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:
|
|
print("Mode: %s" % repr(msg.get_data()))
|
|
return self.STATUS_OKAY, None
|
|
elif msg.get_data_id() == self.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]:
|
|
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',
|
|
}.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
|