__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 logging
  20. import logging.handlers
  21. import digilib.network
  22. import os
  23. import queue
  24. import select
  25. import socket
  26. import sys
  27. import threading
  28. import time
  29. import traceback
  30. import digilib.pin
  31. import digilib.network
  32. import beewatch
  33. import beewatch.pinapi
  34. log = logging.getLogger(__name__+"")
  35. lapi = logging.getLogger(__name__+".api")
  36. lchat = logging.getLogger(__name__+".chat")
  37. class BeeWatchServer(digilib.network.Server):
  38. """docstring for Server."""
  39. def __init__(self,*args,**kwargs):
  40. super(BeeWatchServer, self).__init__(
  41. *args,
  42. **kwargs,
  43. handler=beewatch.server.ConnHandlerBeeWatch,
  44. )
  45. class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
  46. def __init__(self, socket, addr, server):
  47. super(ConnHandlerBeeWatch, self).__init__(socket, addr, server)
  48. self.engines_ctrl = beewatch._controllers["engines_ctrl"]
  49. # digilib.pin.EnginesController(
  50. # left=[4,17],
  51. # right=[27,22],
  52. # )
  53. self.text_to_func = {
  54. "turn":self.engines_ctrl.turn
  55. }
  56. def call(self,func_name,args,respond_method,kwargs):
  57. ttf = beewatch._commands
  58. if func_name in ttf.keys():
  59. try:
  60. task = curio.run(
  61. ttf[func_name](
  62. # self.bee_api,
  63. *args,
  64. respond_method=respond_method,
  65. **kwargs,
  66. )
  67. )
  68. except:
  69. tb = traceback.format_exc()
  70. print(tb)
  71. respond_method(tb)
  72. else:
  73. respond_method("Unknown command")
  74. def welcome_client(self):
  75. self.send("this is the server speaking, hello new client!")
  76. def handle(self, data_decoded):
  77. data_decoded=data_decoded.strip()
  78. # lchat.info("Client:"+data_decoded)
  79. if data_decoded.find(" ") != -1:
  80. cmd = data_decoded.split()[0]
  81. args = data_decoded.split()[1:]
  82. else:
  83. cmd=data_decoded
  84. args = ()
  85. kwargs = {}
  86. self.call(
  87. cmd,
  88. respond_method=self.respond,
  89. args=args,
  90. kwargs={},
  91. )
  92. def do(self,func_num,*args,respond_method):
  93. arg_to_func = {
  94. "1":self.bee_api.do_something_1,
  95. "2":self.bee_api.do_something_2,
  96. "3":self.bee_api.do_something_3,
  97. }
  98. arg_to_func[func_num](*args,respond_method=respond_method)
  99. def echo(self,*args,**kwargs):
  100. lchat.info(str(args))
  101. lchat.info(str(kwargs))
  102. def speedcheck(self,respond_method):
  103. left_speed=str(self.bee_api.c.movement_control.get_speed("left"))
  104. right_speed=str(self.bee_api.c.movement_control.get_speed("right"))
  105. result = "left: "+left_speed+", right: "+right_speed
  106. lchat.info(result)
  107. self.respond(result)
  108. def respond(self,text,*args):
  109. self.send(text)
  110. #