123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #! /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 handle(self, data):
- data = data.strip()
- data = data.split(" ")
- cmd,*args = data
- kwargs = {"args":args,"command":cmd,"respond":self.respond}
- func = beewatch._commands.get(cmd,False)
- if func == False:
- self.respond("Unknown command")
- return
- try:
- retval = func(**kwargs)
- if hasattr(retval,"__await__"):
- lserver.debug("curio.spawn started")
- task = curio.spawn(retval)
- lserver.debug("curio.spawn finished")
- except Exception as e:
- lserver.error("Error while calling api func",exc_info=e)
- tb = traceback.format_exc()
- self.respond(tb)
- def respond(self,text,*args):
- self.send(text)
- def welcome_client(self):
- self.send("this is the server speaking, hello new client!")
- #
|