71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
|
import config
|
||
|
import logging
|
||
|
import mqtt
|
||
|
import readline
|
||
|
import report
|
||
|
from __simulation__.rooms import house
|
||
|
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)
|
||
|
#
|
||
|
|
||
|
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)
|
||
|
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 command in COMMANDS[2:]:
|
||
|
h.command(userfeedback)
|
||
|
else:
|
||
|
print("Unknown command!")
|