|
@@ -38,70 +38,47 @@ log = logging.getLogger(__name__+"")
|
|
|
lapi = logging.getLogger(__name__+".api")
|
|
|
lchat = logging.getLogger(__name__+".chat")
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
def log_something():
|
|
|
lapi.info("logging works")
|
|
|
|
|
|
class BeeWatchServer(digilib.network.Server):
|
|
|
"""docstring for Server."""
|
|
|
def __init__(self,*args,**kwargs):
|
|
|
- print(kwargs)
|
|
|
- self.bee_api = beewatch.server.BeeAPI()
|
|
|
super(BeeWatchServer, self).__init__(
|
|
|
*args,
|
|
|
**kwargs,
|
|
|
handler=beewatch.server.ConnHandlerBeeWatch,
|
|
|
- handler_kwargs={"bee_api":self.bee_api}
|
|
|
)
|
|
|
|
|
|
-class BeeAPI(object):
|
|
|
- def __init__(self, config_file=None):
|
|
|
- super(BeeAPI, self).__init__()
|
|
|
- lapi.info("BeeAPI")
|
|
|
- self.pin_api = beewatch.pinapi.PinAPIBee()
|
|
|
- self.text_to_func = {
|
|
|
- "turn_left":self.pin_api.turn_left,
|
|
|
- "turn_left":self.pin_api.turn_right,
|
|
|
- "turn":self.pin_api.turn,
|
|
|
- }
|
|
|
- def turn_left(self,respond_method):
|
|
|
- self.pin_api.turn_left()
|
|
|
- respond_method("heyooo")
|
|
|
- def do_something_1(self,*args,respond_method):
|
|
|
- respond_string ="doing something 1 with '{}'".format("', '".join(args))
|
|
|
- respond_method(respond_string)
|
|
|
- lapi.debug(respond_string)
|
|
|
- def do_something_2(self,*args,respond_method):
|
|
|
- lapi.debug("doing something 2 with '"+"','".join(args)+"'")
|
|
|
- respond_method("doing secondary stuff")
|
|
|
- def do_something_3(self,*args,respond_method):
|
|
|
- lapi.debug("doing something 3 with '"+"','".join(args)+"'")
|
|
|
- respond_method("doing three things at once")
|
|
|
- def log(self,*args):
|
|
|
- lapi.debug(args)
|
|
|
- # text_to_func = {
|
|
|
- # "turn_left":turn_left,
|
|
|
- # "turn_left":turn_right,
|
|
|
- # "turn":turn,
|
|
|
- # }
|
|
|
-
|
|
|
class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
|
|
|
- def __init__(self, socket, addr, server, bee_api=None):
|
|
|
- self.status = "init"
|
|
|
- self.super_class = super(ConnHandlerBeeWatch, self)
|
|
|
- self.super_class.__init__(socket, addr, server)
|
|
|
+ def __init__(self, socket, addr, server):
|
|
|
+ super(ConnHandlerBeeWatch, self).__init__(socket, addr, server)
|
|
|
lchat.info('hey')
|
|
|
- self.server = server
|
|
|
- self.address = addr
|
|
|
- self.bee_api = bee_api
|
|
|
- self.command_to_function={
|
|
|
- "do":self.do,
|
|
|
- "get_speed":self.speedcheck,
|
|
|
- "print":self.echo,
|
|
|
- "None":None,
|
|
|
+ self.engine_controller = digilib.pin.EnginesController(
|
|
|
+ left=[4,17],
|
|
|
+ right=[27,22],
|
|
|
+ )
|
|
|
+ self.text_to_func = {
|
|
|
+ "turn":self.engine_controller.turn
|
|
|
}
|
|
|
+ def call(self,func_name,args,respond_method,kwargs):
|
|
|
+ ttf = self.text_to_func
|
|
|
+ if func_name in ttf.keys():
|
|
|
+ try:
|
|
|
+ task = curio.run(
|
|
|
+ ttf[func_name](
|
|
|
+ # self.bee_api,
|
|
|
+ *args,
|
|
|
+ respond_method=respond_method,
|
|
|
+ **kwargs,
|
|
|
+ )
|
|
|
+ )
|
|
|
+ except:
|
|
|
+ tb = traceback.format_exc()
|
|
|
+ print(tb)
|
|
|
+ respond_method(tb)
|
|
|
+ else:
|
|
|
+ respond_method("Unknown command")
|
|
|
def welcome_client(self):
|
|
|
self.send("this is the server speaking, hello new client!")
|
|
|
def handle(self, data_decoded):
|
|
@@ -114,35 +91,12 @@ class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
|
|
|
cmd=data_decoded
|
|
|
args = ()
|
|
|
kwargs = {}
|
|
|
- # if cmd in self.command_to_function.keys():
|
|
|
- if cmd in self.bee_api.text_to_func.keys():
|
|
|
- try:
|
|
|
- # self.respond(str(args))
|
|
|
- task = curio.run(
|
|
|
- self.bee_api.text_to_func[cmd],
|
|
|
- self.bee_api,
|
|
|
- *args,
|
|
|
- self.respond
|
|
|
- # respond_method=self.respond,
|
|
|
- # **kwargs,
|
|
|
- )
|
|
|
- task.join()
|
|
|
- except:
|
|
|
- tb = traceback.format_exc()
|
|
|
- print(tb)
|
|
|
- self.respond(tb)
|
|
|
- # for line in tb.split('\n'):
|
|
|
- # self.respond(line)
|
|
|
- # # print(line)
|
|
|
- # # time.sleep(0.001)
|
|
|
- # time.sleep(0.1)
|
|
|
- # # if line != "\n"
|
|
|
- else:
|
|
|
- self.respond("Unknown command")
|
|
|
- # Notify.Notification.new(
|
|
|
- # self.address[0]+" ("+self.server.host+":"+str(self.server.port)+")",
|
|
|
- # data_decoded
|
|
|
- # ).show()
|
|
|
+ self.call(
|
|
|
+ cmd,
|
|
|
+ respond_method=self.respond,
|
|
|
+ args=args,
|
|
|
+ kwargs={},
|
|
|
+ )
|
|
|
def do(self,func_num,*args,respond_method):
|
|
|
arg_to_func = {
|
|
|
"1":self.bee_api.do_something_1,
|
|
@@ -161,3 +115,71 @@ class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
|
|
|
self.respond(result)
|
|
|
def respond(self,text,*args):
|
|
|
self.send(text)
|
|
|
+
|
|
|
+
|
|
|
+# class BeeAPI(object):
|
|
|
+# def __init__(self, config_file=None):
|
|
|
+# super(BeeAPI, self).__init__()
|
|
|
+# lapi.info("BeeAPI")
|
|
|
+# self.engine_controller = digilib.pin.EnginesController(
|
|
|
+# left=[4,17],
|
|
|
+# right=[27,22],
|
|
|
+# )
|
|
|
+# self.text_to_func = {
|
|
|
+# "turn":self.engine_controller.turn
|
|
|
+# }
|
|
|
+# def call(self,func_name,args,respond_method,kwargs):
|
|
|
+# ttf = self.text_to_func
|
|
|
+# if func_name in ttf.keys():
|
|
|
+# try:
|
|
|
+# task = curio.run(
|
|
|
+# ttf[func_name](
|
|
|
+# # self.bee_api,
|
|
|
+# *args,
|
|
|
+# respond_method=respond_method,
|
|
|
+# **kwargs,
|
|
|
+# )
|
|
|
+# )
|
|
|
+# except:
|
|
|
+# tb = traceback.format_exc()
|
|
|
+# print(tb)
|
|
|
+# respond_method(tb)
|
|
|
+# else:
|
|
|
+# respond_method("Unknown command")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#
|