def testItCanStop(self): '''Spy ''' class SpyingElectronics(Electronics): def pushBrakes(self, brakingPower): self.brakingPower = brakingPower def getBrakingPower(self): return self.brakingPower class SpyingStatusPanel(StatusPanel): def __init__(self): self.speedWasRequested = False self.currentSpeed = 1 def getSpeed(self): if self.speedWasRequested: self.currentSpeed = 0 self.speedWasRequested = True return self.currentSpeed def hasSpeedRequested(self): return self.speedWasRequested def spyOnSpeed(self): return self.currentSpeed halfBrakingPower = 50 spyingElectronics = SpyingElectronics() spyingStatusPanel = SpyingStatusPanel() carController = CarController() carController.stop(halfBrakingPower, spyingElectronics, spyingStatusPanel) self.assertEquals(halfBrakingPower, spyingElectronics.getBrakingPower()) self.assertTrue(spyingStatusPanel.hasSpeedRequested()) self.assertEquals(0, spyingStatusPanel.spyOnSpeed(), 'Expected speed to be zero after stopping but it actually was %d' % spyingStatusPanel.spyOnSpeed())
def testItCanGetReadyTheCar(self): '''Dummy ''' carController = CarController() engine = Engine() gearBox = GearBox() electronics = Electronics() dummyLights = MagicMock('Lights') self.assertTrue(carController.getReadyToGo(engine, gearBox, electronics, dummyLights))
def testItCanAccelerate(self): '''Stub with Mock ''' carController = CarController() mockElectronics = Mock('Electronics') mockElectronics.accelerate = Mock() stubStatusPanel = Mock('StatusPanel') stubStatusPanel.thereIsEnoughFuel = Mock(return_value=True) stubStatusPanel.engineIsRunning = Mock(return_value=True) carController.goForward(mockElectronics, stubStatusPanel) stubStatusPanel.thereIsEnoughFuel.assert_called_with() stubStatusPanel.engineIsRunning.assert_called_with() mockElectronics.accelerate.assert_called_once_with()
def testItCanStop_2(self): '''Spy with Mock ''' halfBrakingPower = 50 carController = CarController() mockElectronics = Mock('Electronics') mockElectronics.pushBrakes = Mock() mockStatusPanel = Mock('StatusPanel') returns = [1, 0, Exception()] def side_effect(*args): result = returns.pop(0) if isinstance(result, Exception): raise result return result mockStatusPanel.getSpeed = Mock(side_effect=side_effect) carController.stop(halfBrakingPower, mockElectronics, mockStatusPanel) mockElectronics.pushBrakes.assert_called_with(halfBrakingPower) self.assertEquals(2, mockStatusPanel.getSpeed.call_count)
def POST(self): logging.info('connect function : connecting...') _status = False _frequency = 0 data = web.input() if data.get('frequency'): _status = True _frequency = int(data.get('frequency')) CarController.BasicConfig(_frequency) # 设置命令发送频率 logging.info('connect & setting : set frequency is %s', _frequency) web.header('Content-Type', 'text/json; charset=utf-8', unique=True) return render.connect(_status, _frequency)
def handle_client (self, client, addr): self.handshake(client) try: while 1: delayTime = 1 #the delay between the packages for making the car move data = self.recv_data(client) print("received [%s]" % (data,)) self.broadcast_resp(data) switch = data print (switch) if switch == "forward": print("Forward command received") #code here so motors go forward carCon.runCar(delayTime,"forward") elif switch == "back": print("Backward command received") #code here so motors go backward carCon.runCar(delayTime,"back") elif switch == "right": print("Right command received") #code here so motors go right carCon.runCar(delayTime,"right") elif switch == "left": print("Left command received") #code here so motors go left carCon.runCar(delayTime,"left") elif switch == "stop": print("stop command received") #code here so motors go stop carCon.runCar(delayTime,"stop") else: client.send('Error') #returns "Error" if the command DON'T exists except Exception as e: print("Exception %s" % (str(e))) print('Client closed: ' + str(addr)) self.LOCK.acquire() self.clients.remove(client) self.LOCK.release() client.close()
s.bind((HOST, PORT)) s.listen(1) c, addr = s.accept() c.sendall('connected to server') print "Connected by", addr while True: c, addr = s.accept() # Establish connection with client. #print 'Got connection from', addr inData = c.recv(1024) if(len(inData) > 0): switch = inData inData = None if switch == "forward": c.sendall('ok') #returns "ok" if the command exists carCon.runCar(delayTime,"forward") elif switch == "back": c.sendall('ok') #returns "ok" if the command exists carCon.runCar(delayTime,"back") elif switch == "right": c.sendall('ok') #returns "ok" if the command exists carCon.runCar(delayTime,"right") elif switch == "left": c.sendall('ok') #returns "ok" if the command exists carCon.runCar(delayTime,"left") elif switch == "reset": c.sendall("ok") carCon.reset(); elif switch == "exit": c.sendall("exiting the server") exit(0)
CAR_SPEED = 255 pi = pigpio.pi() pi = pi if not pi.connected: print('Pi not connected. Exiting...') exit() pygame.init() gameDisplay = pygame.display.set_mode((800, 600)) pygame.display.set_caption('Control the car') car = CarController.CarModule(pi, ENABLE_SPEED_PIN, SPEED_PIN_1, SPEED_PIN_2, ENABLE_DIRECTION_PIN, DIRECTION_PIN_1, DIRECTION_PIN_2, CAR_SPEED) while True: try: events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: car.forward() elif event.key == pygame.K_DOWN: car.backward() elif event.key == pygame.K_RIGHT: car.right_turn() elif event.key == pygame.K_LEFT:
logging.config.fileConfig("logger.conf") logger = logging.getLogger("example01") #controller = GPIOControl.GPIOController() urls = ("/", "hello", "/connect", "connect", "/action", "action", "/getstatus", "getstatus", "/stop", "stop") # 指定任何url都指向hello类 web.config.debug = True app_root = os.path.dirname(__file__) templates_root = os.path.join(app_root, 'templates') render = web.template.render(templates_root) modeArray = [1, 2, 3] # 自由、预设、轨迹 at = CarController.ActionTranslate() def notfound(): return web.notfound("Sorry, the page you were looking for was not found.") # 定义相应类 class hello: def GET(self): logging.info('web service started.') return "web service started" class connect: def POST(self):