123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #! /usr/bin/env python3.5
- # Copyright 2017 Digital
- #
- # This file is part of BeeWatch.
- #
- # BeeWatch is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # BeeWatch is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
- import curio
- import blinker
- import logging
- import logging.handlers
- import digilib.network
- import os
- import queue
- import select
- import socket
- import sys
- import threading
- import time
- import traceback
- import digilib.pin
- import digilib.network
- import beewatch
- import beewatch.pinapi
- log = logging.getLogger(__name__+"")
- lapi = logging.getLogger(__name__+".api")
- lchat = logging.getLogger(__name__+".chat")
- lserver = logging.getLogger(__name__+".server")
- class BeeWatchServer(digilib.network.Server):
- """docstring for Server."""
- def __init__(self,*args,**kwargs):
- super(BeeWatchServer, self).__init__(
- *args,
- **kwargs,
- handler=beewatch.server.ConnHandlerBeeWatch,
- )
- def make_socket(self):
- s = super(BeeWatchServer,self).make_socket()
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- return s
- class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
- def __init__(self, socket, addr, server):
- super(ConnHandlerBeeWatch, self).__init__(socket, addr, server)
- self.engines_ctrl = beewatch._controllers["engines_ctrl"]
- # digilib.pin.EnginesController(
- # left=[4,17],
- # right=[27,22],
- # )
- self.text_to_func = {
- "turn":self.engines_ctrl.turn
- }
- def call(self,func_name,args,respond,kwargs):
- ttf = beewatch._commands
- if func_name in ttf.keys():
- try:
- task = curio.run(
- ttf[func_name](
- # self.bee_api,
- *args,
- respond=respond,
- **kwargs,
- )
- )
- except:
- tb = traceback.format_exc()
- print(tb)
- respond(tb)
- else:
- respond("Unknown command")
- def welcome_client(self):
- self.send("this is the server speaking, hello new client!")
- def handle(self, data_decoded):
- data_decoded=data_decoded.strip()
- # lchat.info("Client:"+data_decoded)
- if data_decoded.find(" ") != -1:
- cmd = data_decoded.split()[0]
- args = data_decoded.split()[1:]
- else:
- cmd=data_decoded
- args = ()
- kwargs = {}
- ttf = beewatch._commands
- if cmd in ttf.keys():
- try:
- task = curio.run(
- ttf[cmd](
- # self.bee_api,
- args=args,
- command=cmd,
- respond=self.respond,
- )
- )
- except Exception as e:
- lserver.error("Error while calling api func",exc_info=e)
- tb = traceback.format_exc()
- self.respond(tb)
- else:
- self.respond("Unknown command")
- # self.call(
- # cmd,
- # respond=self.respond,
- # args=args,
- # kwargs={},
- # )
- def do(self,func_num,*args,respond):
- arg_to_func = {
- "1":self.bee_api.do_something_1,
- "2":self.bee_api.do_something_2,
- "3":self.bee_api.do_something_3,
- }
- arg_to_func[func_num](*args,respond=respond)
- def echo(self,*args,**kwargs):
- lchat.info(str(args))
- lchat.info(str(kwargs))
- def speedcheck(self,respond):
- left_speed=str(self.bee_api.c.movement_control.get_speed("left"))
- right_speed=str(self.bee_api.c.movement_control.get_speed("right"))
- result = "left: "+left_speed+", right: "+right_speed
- lchat.info(result)
- self.respond(result)
- def respond(self,text,*args):
- self.send(text)
- #
|