Smarthome Functionen
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

house_n_gui_sim.py 2.1KB

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