leyk_txt_client/tcp_client.py

68 lines
2.6 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
2021-01-07 01:30:52 +01:00
import socket_protocol
2020-01-26 21:50:14 +01:00
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__':
2021-01-07 01:30:52 +01:00
report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), 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 = {
2021-01-07 01:30:52 +01:00
'bakehouse': [protocol.DID_BAKE_HOUSE, ],
'bakery': [protocol.DID_BAKERY, ],
'mill': [protocol.DID_MILL, ],
'ploenlein': [protocol.DID_PLOENLEIN, ],
'reesehouse': [protocol.DID_REESE_HOUSE, ],
'all': [protocol.DID_BAKE_HOUSE, protocol.DID_BAKERY, protocol.DID_MILL, protocol.DID_PLOENLEIN, protocol.DID_REESE_HOUSE]
2020-01-26 21:50:14 +01:00
}
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"]:
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_EXECUTE_REQUEST, protocol.DID_MODE, 'automatic')
2020-01-26 21:50:14 +01:00
elif cmd in ["man", "manual"]:
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_EXECUTE_REQUEST, protocol.DID_MODE, 'manual')
2020-01-26 21:50:14 +01:00
elif cmd == "mode":
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_READ_REQUEST, protocol.DID_MODE, None)
2020-01-26 21:50:14 +01:00
elif cmd == "state":
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_READ_REQUEST, protocol.DID_STATE, None)
2020-01-26 21:50:14 +01:00
elif cmd.startswith('get '):
target = target_dict.get(cmd.split(' ')[1])
if target is not None:
for t in target:
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_READ_REQUEST, t, None)
2020-01-26 21:50:14 +01:00
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:
2021-01-07 01:30:52 +01:00
prot.send(socket_protocol.SID_EXECUTE_REQUEST, t, state)
2020-01-26 21:50:14 +01:00
c.close()