Test Smart Brain implementation
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

smart_brain_test.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import config
  2. import logging
  3. import mqtt
  4. import readline
  5. import report
  6. from simulation.rooms import house
  7. from tests import test_smarthome
  8. import time
  9. if __name__ == "__main__":
  10. report.stdoutLoggingConfigure(
  11. ((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)
  12. #
  13. mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
  14. password=config.MQTT_PASSWORD, name=config.APP_NAME + '_simulation')
  15. #
  16. COMMANDS = ['quit', 'help']
  17. #
  18. h = house(mc)
  19. for name in h.getmembers():
  20. d = h.getobjbyname(name)
  21. for c in d.capabilities():
  22. COMMANDS.append(name + '.' + c)
  23. #
  24. ts = test_smarthome(h)
  25. for name in ts.getmembers():
  26. d = ts.getobjbyname(name)
  27. for c in d.capabilities():
  28. COMMANDS.append('test.' + name + '.' + c)
  29. def reduced_list(text):
  30. """
  31. Create reduced completation list
  32. """
  33. reduced_list = {}
  34. for cmd in COMMANDS:
  35. next_dot = cmd[len(text):].find('.')
  36. if next_dot >= 0:
  37. reduced_list[cmd[:len(text) + next_dot + 1]] = None
  38. else:
  39. reduced_list[cmd] = None
  40. return reduced_list.keys()
  41. def completer(text, state):
  42. """
  43. Our custom completer function
  44. """
  45. options = [x for x in reduced_list(text) if x.startswith(text)]
  46. return options[state]
  47. def complete(text, state):
  48. for cmd in COMMANDS:
  49. if cmd.startswith(text):
  50. if not state:
  51. hit = ""
  52. index = 0
  53. sub_list = cmd.split('.')
  54. while len(text) >= len(hit):
  55. hit += sub_list[index] + '.'
  56. return hit # cmd
  57. else:
  58. state -= 1
  59. readline.parse_and_bind("tab: complete")
  60. readline.set_completer(completer)
  61. time.sleep(0.3)
  62. print("\nEnter command: ")
  63. while True:
  64. userfeedback = input('')
  65. command = userfeedback.split(' ')[0]
  66. if userfeedback == 'quit':
  67. break
  68. elif userfeedback == 'help':
  69. print("Help is not yet implemented!")
  70. elif userfeedback.startswith("test"):
  71. ts.command(userfeedback)
  72. elif userfeedback == 'test.smoke':
  73. ts.smoke()
  74. elif command in COMMANDS[2:]:
  75. h.command(userfeedback)
  76. elif userfeedback != "":
  77. print("Unknown command!")
  78. else:
  79. print()