Smarthome Functionen
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

house_n_gui_sim.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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', 'test.smoke', 'test.full']
  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. def reduced_list(text):
  25. """
  26. Create reduced completation list
  27. """
  28. reduced_list = {}
  29. for cmd in COMMANDS:
  30. next_dot = cmd[len(text):].find('.')
  31. if next_dot >= 0:
  32. reduced_list[cmd[:len(text) + next_dot + 1]] = None
  33. else:
  34. reduced_list[cmd] = None
  35. return reduced_list.keys()
  36. def completer(text, state):
  37. """
  38. Our custom completer function
  39. """
  40. options = [x for x in reduced_list(text) if x.startswith(text)]
  41. return options[state]
  42. def complete(text, state):
  43. for cmd in COMMANDS:
  44. if cmd.startswith(text):
  45. if not state:
  46. hit = ""
  47. index = 0
  48. sub_list = cmd.split('.')
  49. while len(text) >= len(hit):
  50. hit += sub_list[index] + '.'
  51. return hit # cmd
  52. else:
  53. state -= 1
  54. readline.parse_and_bind("tab: complete")
  55. readline.set_completer(completer)
  56. time.sleep(0.3)
  57. print("\nEnter command: ")
  58. while True:
  59. userfeedback = input('')
  60. command = userfeedback.split(' ')[0]
  61. if userfeedback == 'quit':
  62. break
  63. elif userfeedback == 'help':
  64. print("Help is not yet implemented!")
  65. elif userfeedback == 'test.full':
  66. ts.full()
  67. elif userfeedback == 'test.smoke':
  68. ts.smoke()
  69. elif command in COMMANDS[2:]:
  70. h.command(userfeedback)
  71. elif userfeedback != "":
  72. print("Unknown command!")
  73. else:
  74. print()