Leyk remote control text client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcp_client.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import config
  4. import os
  5. import protocol
  6. import report
  7. import sys
  8. import tcp_socket
  9. def help_msg():
  10. print(""""Possible commands are:
  11. * q[uit]
  12. * auto[matic]
  13. * man[ual]
  14. * mode
  15. * state
  16. * get [all,bakehose,bakery,mill,reesehouse,ploenlein]
  17. * set [all,bakehose,bakery,mill,reesehouse,ploenlein] [on,off]""")
  18. if __name__ == '__main__':
  19. report.appLoggingConfigure(os.path.dirname(__file__), 'logfile', config.loggers)
  20. c = tcp_socket.tcp_client_stp(config.server_ip, config.server_port)
  21. prot = protocol.my_client_protocol(c, secret=config.secret)
  22. target_dict = {
  23. 'bakehouse': [prot.DID_BAKE_HOUSE, ],
  24. 'bakery': [prot.DID_BAKERY, ],
  25. 'mill': [prot.DID_MILL, ],
  26. 'ploenlein': [prot.DID_PLOENLEIN, ],
  27. 'reesehouse': [prot.DID_REESE_HOUSE, ],
  28. 'all': [prot.DID_BAKE_HOUSE, prot.DID_BAKERY, prot.DID_MILL, prot.DID_PLOENLEIN, prot.DID_REESE_HOUSE]
  29. }
  30. if prot.authentificate():
  31. help_msg()
  32. cmd = None
  33. while cmd not in ["q", "quit"]:
  34. cmd = sys.stdin.readline()
  35. cmd = cmd.lower().strip('\n')
  36. if cmd == 'help':
  37. help_msg()
  38. elif cmd in ["auto", "automatic"]:
  39. prot.send(prot.SID_EXECUTE_REQUEST, prot.DID_MODE, 'automatic')
  40. elif cmd in ["man", "manual"]:
  41. prot.send(prot.SID_EXECUTE_REQUEST, prot.DID_MODE, 'manual')
  42. elif cmd == "mode":
  43. prot.send(prot.SID_READ_REQUEST, prot.DID_MODE, None)
  44. elif cmd == "state":
  45. prot.send(prot.SID_READ_REQUEST, prot.DID_STATE, None)
  46. elif cmd.startswith('get '):
  47. target = target_dict.get(cmd.split(' ')[1])
  48. if target is not None:
  49. for t in target:
  50. prot.send(prot.SID_READ_REQUEST, t, None)
  51. elif cmd.startswith('set '):
  52. target = target_dict.get(cmd.split(' ')[1])
  53. try:
  54. state = {'on': True, 'off': False}.get(cmd.split(' ')[2])
  55. except IndexError:
  56. state = None
  57. if target is not None and state is not None:
  58. for t in target:
  59. prot.send(prot.SID_EXECUTE_REQUEST, t, state)
  60. c.close()