Smarthome Functionen
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

house_n_gui_sim.py 2.5KB

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