12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import config
- import logging
- import mqtt
- import readline
- import report
- from simulation.rooms import house
- from tests import test_smarthome
- import time
-
- if __name__ == "__main__":
- report.stdoutLoggingConfigure(
- ((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)
- #
- mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
- password=config.MQTT_PASSWORD, name=config.APP_NAME + '_simulation')
- #
- COMMANDS = ['quit', 'help']
- #
- h = house(mc)
- for name in h.getmembers():
- d = h.getobjbyname(name)
- for c in d.capabilities():
- COMMANDS.append(name + '.' + c)
- #
- ts = test_smarthome(h)
- for name in ts.getmembers():
- d = ts.getobjbyname(name)
- for c in d.capabilities():
- COMMANDS.append('test.' + name + '.' + c)
-
- def reduced_list(text):
- """
- Create reduced completation list
- """
- reduced_list = {}
- for cmd in COMMANDS:
- next_dot = cmd[len(text):].find('.')
- if next_dot >= 0:
- reduced_list[cmd[:len(text) + next_dot + 1]] = None
- else:
- reduced_list[cmd] = None
- return reduced_list.keys()
-
- def completer(text, state):
- """
- Our custom completer function
- """
- options = [x for x in reduced_list(text) if x.startswith(text)]
- return options[state]
-
- def complete(text, state):
- for cmd in COMMANDS:
- if cmd.startswith(text):
- if not state:
- hit = ""
- index = 0
- sub_list = cmd.split('.')
- while len(text) >= len(hit):
- hit += sub_list[index] + '.'
- return hit # cmd
- else:
- state -= 1
-
- readline.parse_and_bind("tab: complete")
- readline.set_completer(completer)
- time.sleep(0.3)
- print("\nEnter command: ")
-
- while True:
- userfeedback = input('')
- command = userfeedback.split(' ')[0]
- if userfeedback == 'quit':
- break
- elif userfeedback == 'help':
- print("Help is not yet implemented!")
- elif userfeedback.startswith("test"):
- ts.command(userfeedback)
- elif userfeedback == 'test.smoke':
- ts.smoke()
- elif command in COMMANDS[2:]:
- h.command(userfeedback)
- elif userfeedback != "":
- print("Unknown command!")
- else:
- print()
|