leyk_txt_client/tcp_client.py

67 lines
2.4 KiB
Python
Raw Normal View History

2020-01-26 21:50:14 +01:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import config
import os
import protocol
import report
import sys
import tcp_socket
def help_msg():
print(""""Possible commands are:
* q[uit]
* auto[matic]
* man[ual]
* mode
* state
* get [all,bakehose,bakery,mill,reesehouse,ploenlein]
* set [all,bakehose,bakery,mill,reesehouse,ploenlein] [on,off]""")
if __name__ == '__main__':
2020-12-26 16:32:59 +01:00
report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, ((config.APP_NAME, 'DEBUG'), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
2020-01-26 21:50:14 +01:00
c = tcp_socket.tcp_client_stp(config.server_ip, config.server_port)
2020-12-26 16:32:59 +01:00
prot = protocol.my_client_protocol(c, secret=config.secret, channel_name='leyk')
2020-01-26 21:50:14 +01:00
target_dict = {
'bakehouse': [prot.DID_BAKE_HOUSE, ],
'bakery': [prot.DID_BAKERY, ],
'mill': [prot.DID_MILL, ],
'ploenlein': [prot.DID_PLOENLEIN, ],
'reesehouse': [prot.DID_REESE_HOUSE, ],
'all': [prot.DID_BAKE_HOUSE, prot.DID_BAKERY, prot.DID_MILL, prot.DID_PLOENLEIN, prot.DID_REESE_HOUSE]
}
if prot.authentificate():
help_msg()
cmd = None
while cmd not in ["q", "quit"]:
cmd = sys.stdin.readline()
cmd = cmd.lower().strip('\n')
if cmd == 'help':
help_msg()
elif cmd in ["auto", "automatic"]:
prot.send(prot.SID_EXECUTE_REQUEST, prot.DID_MODE, 'automatic')
elif cmd in ["man", "manual"]:
prot.send(prot.SID_EXECUTE_REQUEST, prot.DID_MODE, 'manual')
elif cmd == "mode":
prot.send(prot.SID_READ_REQUEST, prot.DID_MODE, None)
elif cmd == "state":
prot.send(prot.SID_READ_REQUEST, prot.DID_STATE, None)
elif cmd.startswith('get '):
target = target_dict.get(cmd.split(' ')[1])
if target is not None:
for t in target:
prot.send(prot.SID_READ_REQUEST, t, None)
elif cmd.startswith('set '):
target = target_dict.get(cmd.split(' ')[1])
try:
state = {'on': True, 'off': False}.get(cmd.split(' ')[2])
except IndexError:
state = None
if target is not None and state is not None:
for t in target:
prot.send(prot.SID_EXECUTE_REQUEST, t, state)
c.close()