Leyk remote control text client
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tcp_client.py 2.6KB

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