client.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #! /usr/bin/python3
  2. #
  3. ## LICENSE
  4. # This file is part of Project BeeWatch.
  5. #
  6. # Project BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Project BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Project BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ## UNITS
  20. # speed: meter/second
  21. # temperature: kelvin
  22. # rotation: degree/second
  23. import argparse
  24. import logging
  25. import logging.config
  26. import logging.handlers
  27. import sys
  28. import threading
  29. import traceback
  30. import time
  31. import yaml
  32. import mylibnetwork
  33. lroot = logging.getLogger("root")
  34. def user_input():
  35. while True:
  36. data = input("type something:")
  37. client.send(data)
  38. def configure_logging():
  39. with open("../../bee_logging.yaml") as f:
  40. data = f.read()
  41. config = yaml.safe_load(data)
  42. logging.config.dictConfig(config)
  43. parser = argparse.ArgumentParser(description="BeeWatch Server",)
  44. parser.add_argument("host",type=str,
  45. help="hostname/ip address/filename (only if --use-file was specified)")
  46. parser.add_argument("port",type=int,default=80540,nargs="?",
  47. help="""the port on wich the connection will open up. ignored if --use-file
  48. is given""")
  49. parser.add_argument("-d","--debug",default=False,action="store_true",
  50. help="enables logging")
  51. parser.add_argument("-a","--async_client",default=False,action="store_true",
  52. help="Use an asynchronous client instead of a threaded one")
  53. group_af_family = parser.add_mutually_exclusive_group()
  54. group_af_family.add_argument(
  55. "-u","--af-unix",
  56. dest="af_family",
  57. action="store_const",
  58. const="AF_UNIX",
  59. help="""use an AF_UNIX socket (a file). can't be used in combination with
  60. --af-inet."""
  61. )
  62. group_af_family.add_argument(
  63. "-i","--af_inet",
  64. default="AF_INET",
  65. dest="af_family",
  66. action="store_const",
  67. const="AF_INET",
  68. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  69. with --af-unix"""
  70. )
  71. args = parser.parse_args()
  72. if args.debug:
  73. configure_logging()
  74. if args.async_client:
  75. client_class = mylibnetwork.AsyncClient
  76. else:
  77. client_class = mylibnetwork.Client
  78. client = client_class(
  79. host=args.host,
  80. port=args.port,
  81. af_family=args.af_family,
  82. )
  83. input_thread = threading.Thread(target=user_input,daemon=True)
  84. try:
  85. input_thread.start()
  86. if args.async_client:
  87. client.run(connect=True)
  88. else:
  89. client.start()
  90. client.connect()
  91. while True:
  92. client.join(1)
  93. if not client.is_running():
  94. break
  95. except KeyboardInterrupt:
  96. print("\r ", end="\r")
  97. lroot.warn("KeyboardInterrupt, aborting!")
  98. finally:
  99. client.stop()