123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import config
- import logging
- import mqtt
- import readline
- import report
- from __simulation__.rooms import house
- from __simulation__.test 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()
|