Protocol definition for Leyk lightener
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import socket_protocol
  4. try:
  5. import piface_function
  6. except ImportError:
  7. piface_function = None
  8. DID_MODE = 0
  9. DID_STATE = 1
  10. #
  11. DID_BAKERY = 2
  12. DID_BAKE_HOUSE = 3
  13. DID_MILL = 4
  14. DID_REESE_HOUSE = 5
  15. DID_PLOENLEIN = 6
  16. class my_base_protocol(socket_protocol.pure_json_protocol):
  17. def __init__(self, *args, **kwargs):
  18. socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
  19. self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_MODE, 'mode')
  20. self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_STATE, 'state')
  21. self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_BAKERY, 'on_off_bakery')
  22. self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_BAKE_HOUSE, 'on_off_bake_house')
  23. self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_MILL, 'on_off_mill')
  24. self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_REESE_HOUSE, 'on_off_reese_house')
  25. self.add_data((socket_protocol.SID_EXECUTE_REQUEST, socket_protocol.SID_EXECUTE_RESPONSE), DID_PLOENLEIN, 'on_off_ploenlein')
  26. class my_server_protocol(my_base_protocol):
  27. def __init__(self, comm_instance, secret=None):
  28. my_base_protocol.__init__(self, comm_instance, secret=secret)
  29. self.leyk = piface_function.leyk()
  30. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MODE, self.set_mode)
  31. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKERY, self.set_bakery)
  32. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_BAKE_HOUSE, self.set_bake_house)
  33. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_MILL, self.set_mill)
  34. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_REESE_HOUSE, self.set_reese_house)
  35. self.register_callback(socket_protocol.SID_EXECUTE_REQUEST, DID_PLOENLEIN, self.set_ploenlein)
  36. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MODE, self.get_mode)
  37. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_STATE, self.get_state)
  38. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKERY, self.get_bakery)
  39. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BAKE_HOUSE, self.get_bake_house)
  40. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_MILL, self.get_mill)
  41. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_REESE_HOUSE, self.get_reese_house)
  42. self.register_callback(socket_protocol.SID_READ_REQUEST, DID_PLOENLEIN, self.get_ploenlein)
  43. def set_mode(self, msg):
  44. if self.leyk.set_mode(msg.get_data()):
  45. return socket_protocol.STATUS_OKAY, None
  46. else:
  47. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  48. def get_mode(self, msg):
  49. return socket_protocol.STATUS_OKAY, self.leyk.get_mode()
  50. def get_state(self, msg):
  51. return socket_protocol.STATUS_OKAY, self.leyk.get_state()
  52. def set_bakery(self, msg):
  53. if msg.get_data() is True or msg.get_data() is False:
  54. if self.leyk.set_bakery(msg.get_data()) is not True:
  55. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  56. else:
  57. return socket_protocol.STATUS_OKAY, None
  58. else:
  59. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  60. def get_bakery(self, msg):
  61. return socket_protocol.STATUS_OKAY, self.leyk.get_bakery()
  62. def set_bake_house(self, msg):
  63. if msg.get_data() is True or msg.get_data() is False:
  64. if self.leyk.set_bake_house(msg.get_data()) is not True:
  65. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  66. else:
  67. return socket_protocol.STATUS_OKAY, None
  68. else:
  69. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  70. def get_bake_house(self, msg):
  71. return socket_protocol.STATUS_OKAY, self.leyk.get_bake_house()
  72. def set_mill(self, msg):
  73. if msg.get_data() is True or msg.get_data() is False:
  74. if self.leyk.set_mill(msg.get_data()) is not True:
  75. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  76. else:
  77. return socket_protocol.STATUS_OKAY, None
  78. else:
  79. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  80. def get_mill(self, msg):
  81. return socket_protocol.STATUS_OKAY, self.leyk.get_mill()
  82. def set_reese_house(self, msg):
  83. if msg.get_data() is True or msg.get_data() is False:
  84. if self.leyk.set_reese_house(msg.get_data()) is not True:
  85. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  86. else:
  87. return socket_protocol.STATUS_OKAY, None
  88. else:
  89. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  90. def get_reese_house(self, msg):
  91. return socket_protocol.STATUS_OKAY, self.leyk.get_reese_house()
  92. def set_ploenlein(self, msg):
  93. if msg.get_data() is True or msg.get_data() is False:
  94. if self.leyk.set_ploenlein(msg.get_data()) is not True:
  95. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  96. else:
  97. return socket_protocol.STATUS_OKAY, None
  98. else:
  99. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  100. def get_ploenlein(self, msg):
  101. return socket_protocol.STATUS_OKAY, self.leyk.get_ploenlein()
  102. class my_client_protocol(my_base_protocol):
  103. START_ROUTINE_DATA_IDS = [
  104. DID_MODE,
  105. DID_BAKERY,
  106. DID_BAKE_HOUSE,
  107. DID_MILL,
  108. DID_REESE_HOUSE,
  109. DID_PLOENLEIN,
  110. ]
  111. def __init__(self, *args, **kwargs):
  112. my_base_protocol.__init__(self, *args, **kwargs)
  113. self.register_callback(socket_protocol.SID_EXECUTE_RESPONSE, None, self.start_routine_response)
  114. self.register_callback(socket_protocol.SID_READ_RESPONSE, None, self.read_data_main_state)
  115. def __authentificate_process_feedback__(self, msg):
  116. if msg.get_data() is True:
  117. print("The client is authentificated.")
  118. else:
  119. print("AUTHENTIFICATION ERROR")
  120. return my_base_protocol.__authentificate_process_feedback__(self, msg)
  121. def start_routine_response(self, msg):
  122. if msg.get_status() == socket_protocol.STATUS_OKAY and msg.get_data_id() in self.START_ROUTINE_DATA_IDS:
  123. print("OKAY")
  124. else:
  125. print("FAILED")
  126. def read_data_main_state(self, msg):
  127. if msg.get_status() == socket_protocol.STATUS_OKAY:
  128. if msg.get_data_id() == DID_MODE:
  129. print("Mode: %s" % repr(msg.get_data()))
  130. elif msg.get_data_id() == DID_STATE:
  131. print("State: %s" % repr(msg.get_data()))
  132. elif msg.get_data_id() in [DID_BAKE_HOUSE, DID_BAKERY, DID_MILL, DID_PLOENLEIN, DID_REESE_HOUSE]:
  133. state = {
  134. 0: 'Off',
  135. 1: 'On',
  136. }.get(msg.get_data())
  137. target = {
  138. DID_BAKE_HOUSE: 'Bakehouse',
  139. DID_BAKERY: 'Bakery',
  140. DID_MILL: 'Mill',
  141. DID_REESE_HOUSE: 'Reesehouse',
  142. DID_PLOENLEIN: 'Ploenlein',
  143. }.get(msg.get_data_id())
  144. print('The light in {target} is {state}'.format(state=state, target=target))
  145. print('Error receiving data!')