__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #! /usr/bin/python3
  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 logging
  19. import logging.handlers
  20. import digilib.network
  21. import os
  22. import queue
  23. import select
  24. import socket
  25. import sys
  26. import threading
  27. import time
  28. import traceback
  29. import digilib.pin
  30. import digilib.network
  31. import beewatch.pinapi
  32. log = logging.getLogger(__name__+"")
  33. lapi = logging.getLogger(__name__+".api")
  34. lchat = logging.getLogger(__name__+".chat")
  35. def log_something():
  36. lapi.info("logging works")
  37. class BeeWatchServer(digilib.network.Server):
  38. """docstring for Server."""
  39. def __init__(self,*args,**kwargs):
  40. print(kwargs)
  41. self.bee_api = beewatch.server.BeeAPI()
  42. super(BeeWatchServer, self).__init__(
  43. *args,
  44. **kwargs,
  45. handler=beewatch.server.ConnHandlerBeeWatch,
  46. handler_kwargs={"bee_api":self.bee_api}
  47. )
  48. class BeeAPI(object):
  49. def __init__(self, config_file=None):
  50. super(BeeAPI, self).__init__()
  51. lapi.info("BeeAPI")
  52. self.pin_api = beewatch.pinapi.PinAPIBee()
  53. def turn_left(self,respond_method):
  54. self.pin_api.turn_left()
  55. respond_method("heyooo")
  56. def do_something_1(self,*args,respond_method):
  57. respond_string ="doing something 1 with '{}'".format("', '".join(args))
  58. respond_method(respond_string)
  59. lapi.debug(respond_string)
  60. def do_something_2(self,*args,respond_method):
  61. lapi.debug("doing something 2 with '"+"','".join(args)+"'")
  62. respond_method("doing secondary stuff")
  63. def do_something_3(self,*args,respond_method):
  64. lapi.debug("doing something 3 with '"+"','".join(args)+"'")
  65. respond_method("doing three things at once")
  66. def log(self,*args):
  67. lapi.debug(args)
  68. text_to_func = {
  69. "turn_left":turn_left,
  70. }
  71. class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
  72. def __init__(self, socket, addr, server, bee_api=None):
  73. self.status = "init"
  74. self.super_class = super(ConnHandlerBeeWatch, self)
  75. self.super_class.__init__(socket, addr, server)
  76. lchat.info('hey')
  77. self.server = server
  78. self.address = addr
  79. self.bee_api = bee_api
  80. self.command_to_function={
  81. "do":self.do,
  82. "get_speed":self.speedcheck,
  83. "print":self.echo,
  84. "None":None,
  85. }
  86. def welcome_client(self):
  87. self.send("this is the server speaking, hello new client!")
  88. def handle(self, data_decoded):
  89. data_decoded=data_decoded.strip()
  90. lchat.info("Client:"+data_decoded)
  91. if data_decoded.find(" ") != -1:
  92. cmd=data_decoded.split()[0]
  93. args=data_decoded.split()[1:]
  94. else:
  95. cmd=data_decoded
  96. args=()
  97. kwargs={}
  98. # if cmd in self.command_to_function.keys():
  99. if cmd in self.bee_api.text_to_func.keys():
  100. try:
  101. # self.respond(str(args))
  102. self.bee_api.text_to_func[cmd](
  103. self.bee_api,
  104. *args,
  105. respond_method=self.respond,
  106. **kwargs,
  107. )
  108. except:
  109. tb = traceback.format_exc()
  110. print(tb)
  111. self.respond(tb)
  112. # for line in tb.split('\n'):
  113. # self.respond(line)
  114. # # print(line)
  115. # # time.sleep(0.001)
  116. # time.sleep(0.1)
  117. # # if line != "\n"
  118. else:
  119. self.respond("Unknown command")
  120. # Notify.Notification.new(
  121. # self.address[0]+" ("+self.server.host+":"+str(self.server.port)+")",
  122. # data_decoded
  123. # ).show()
  124. def do(self,func_num,*args,respond_method):
  125. arg_to_func = {
  126. "1":self.bee_api.do_something_1,
  127. "2":self.bee_api.do_something_2,
  128. "3":self.bee_api.do_something_3,
  129. }
  130. arg_to_func[func_num](*args,respond_method=respond_method)
  131. def echo(self,*args,**kwargs):
  132. lchat.info(str(args))
  133. lchat.info(str(kwargs))
  134. def speedcheck(self,respond_method):
  135. left_speed=str(self.bee_api.c.movement_control.get_speed("left"))
  136. right_speed=str(self.bee_api.c.movement_control.get_speed("right"))
  137. result = "left: "+left_speed+", right: "+right_speed
  138. lchat.info(result)
  139. self.respond(result)
  140. def respond(self,text,*args):
  141. self.send(text)