__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #! /usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # 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. # 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 BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. import curio
  19. import blinker
  20. import logging
  21. import logging.handlers
  22. import digilib.network
  23. import os
  24. import queue
  25. import select
  26. import socket
  27. import sys
  28. import threading
  29. import time
  30. import traceback
  31. import digilib.pin
  32. import digilib.network
  33. import beewatch
  34. import beewatch.pinapi
  35. log = logging.getLogger(__name__+"")
  36. lapi = logging.getLogger(__name__+".api")
  37. lchat = logging.getLogger(__name__+".chat")
  38. lserver = logging.getLogger(__name__+".server")
  39. class BeeWatchServer(digilib.network.Server):
  40. """docstring for Server."""
  41. def __init__(self,*args,**kwargs):
  42. super(BeeWatchServer, self).__init__(
  43. *args,
  44. **kwargs,
  45. handler=beewatch.server.ConnHandlerBeeWatch,
  46. )
  47. def make_socket(self):
  48. s = super(BeeWatchServer,self).make_socket()
  49. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  50. return s
  51. class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
  52. def __init__(self, socket, addr, server):
  53. super(ConnHandlerBeeWatch, self).__init__(socket, addr, server)
  54. self.engines_ctrl = beewatch._controllers["engines_ctrl"]
  55. # digilib.pin.EnginesController(
  56. # left=[4,17],
  57. # right=[27,22],
  58. # )
  59. self.text_to_func = {
  60. "turn":self.engines_ctrl.turn
  61. }
  62. def call(self,func_name,args,respond,kwargs):
  63. ttf = beewatch._commands
  64. if func_name in ttf.keys():
  65. try:
  66. task = curio.run(
  67. ttf[func_name](
  68. # self.bee_api,
  69. *args,
  70. respond=respond,
  71. **kwargs,
  72. )
  73. )
  74. except:
  75. tb = traceback.format_exc()
  76. print(tb)
  77. respond(tb)
  78. else:
  79. respond("Unknown command")
  80. def welcome_client(self):
  81. self.send("this is the server speaking, hello new client!")
  82. def handle(self, data_decoded):
  83. data_decoded=data_decoded.strip()
  84. # lchat.info("Client:"+data_decoded)
  85. if data_decoded.find(" ") != -1:
  86. cmd = data_decoded.split()[0]
  87. args = data_decoded.split()[1:]
  88. else:
  89. cmd=data_decoded
  90. args = ()
  91. kwargs = {}
  92. ttf = beewatch._commands
  93. if cmd in ttf.keys():
  94. try:
  95. task = curio.run(
  96. ttf[cmd](
  97. # self.bee_api,
  98. args=args,
  99. command=cmd,
  100. respond=self.respond,
  101. )
  102. )
  103. except Exception as e:
  104. lserver.error("Error while calling api func",exc_info=e)
  105. tb = traceback.format_exc()
  106. self.respond(tb)
  107. else:
  108. self.respond("Unknown command")
  109. # self.call(
  110. # cmd,
  111. # respond=self.respond,
  112. # args=args,
  113. # kwargs={},
  114. # )
  115. def do(self,func_num,*args,respond):
  116. arg_to_func = {
  117. "1":self.bee_api.do_something_1,
  118. "2":self.bee_api.do_something_2,
  119. "3":self.bee_api.do_something_3,
  120. }
  121. arg_to_func[func_num](*args,respond=respond)
  122. def echo(self,*args,**kwargs):
  123. lchat.info(str(args))
  124. lchat.info(str(kwargs))
  125. def speedcheck(self,respond):
  126. left_speed=str(self.bee_api.c.movement_control.get_speed("left"))
  127. right_speed=str(self.bee_api.c.movement_control.get_speed("right"))
  128. result = "left: "+left_speed+", right: "+right_speed
  129. lchat.info(result)
  130. self.respond(result)
  131. def respond(self,text,*args):
  132. self.send(text)
  133. #