#! /usr/bin/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 . 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.pinapi 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() 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, } 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) 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, } 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={} # if cmd in self.command_to_function.keys(): if cmd in self.bee_api.text_to_func.keys(): try: # self.respond(str(args)) self.bee_api.text_to_func[cmd]( self.bee_api, *args, respond_method=self.respond, **kwargs, ) 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() def do(self,func_num,*args,respond_method): 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_method=respond_method) def echo(self,*args,**kwargs): lchat.info(str(args)) lchat.info(str(kwargs)) def speedcheck(self,respond_method): 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)