def start_server(): init_state() set_power_switch(False) def handle_message(msg): global last_client_message_time last_client_message_time = time.time() message_queue.put(msg) # wait for a connection class _Server(socketserver.StreamRequestHandler): def handle(self): debug("_Server.handle called") global server server = Server(handle_message, on_connection_open, on_connection_close) self.server = server self.server.request = self.request self.server.open() self.server.wait() def finish(self): if on_connection_close: on_connection_close() _server = socketserver.TCPServer((settings['HOST'], settings['PORT']), _Server) global server_thread server_thread = make_thread(_server.serve_forever, "socket server") server_thread.start() start_message_loop() debug("serving on {0} {1}".format(settings['HOST'], settings['PORT']))
def initialize_hardware(): set_power_switch(on=True) global motors_thread motors_thread = make_thread(run_motors, "motors") motors_thread.start() global init_camera_thread init_camera_thread = make_thread(run_init_camera, "camera") init_camera_thread.start()
def on_connection_close(): debug("connection closing") # send motors home if motors is not None: # leave A alone # send B to its min so that it's upright motor_b = motors['B'] motor_b_home = admin.motor_bounds()[1][0] debug("sending motor " + repr(motor_b.name) + " home to " + repr(motor_b_home)) # send X, Y, Z to 0 for char in "XYZ": motor = motors[char] motor.stoppedMovingHandlers.remove(motorStoppedMovingHandler) debug("sending motor " + repr(motor.name) + " home to 0") move_motor(motor, 0) # clean up global finished finished = True if live_view_thread is not None: debug("waiting for live view thread to join") live_view_thread.join() if motors_thread is not None: debug("waiting for motors thread to join") motor_movement_queue.put({}) motors_thread.join() if monitor_thread is not None: debug("waiting for monitor thread to join") monitor_thread.join() if message_thread is not None: debug("waiting for message thread to join") message_queue.put(DummyCloseConnection()) message_thread.join() if motors is not None: debug("shutting down motors") motor_disposer_threads = [] def shutdown_motor(motor): debug("waiting for motor " + repr(motor.name) + " to stop moving") for _ in range(settings['MOTOR_MOVEMENT_TIMEOUT']): if motor.isReady(): break time.sleep(1) else: error("timed out waiting for motor " + repr(motor.name) + " to stop moving") debug("disposing motor " + repr(motor.name)) motor.dispose() for motor in motors.values(): thread = make_thread(shutdown_motor, "motor " + repr(motor.name) + " disposer", args=[motor]) thread.start() motor_disposer_threads.append(thread) for thread in motor_disposer_threads: thread.join() set_power_switch(on=False) debug("done with life. comitting seppuku") os._exit(0)