Exemplo n.º 1
0
class EmbeddedApplication(object):

    LOGGING_DIRECTORY = "../data/logs"
    IMAGE_LOGGING_DIRECTORY = "../data/logs/images"
    SERVER_PORT = 8081
    REMOTE_SERVER_PORT = 8080
    STREAMING_SERVER_PORT = 5000
    STREAMING_SERVER_IP_ADDRESS = "0.0.0.0"
    DEFAULT_STREAM_IMAGE_FILE_NAME = "../resources/default-stream-image.png"
    DEFAULT_EMBEDDED_CAMERA_ID = 0
    EMBEDDED_CAMERA_FRAME_ROTATION_ANGLE = 90
    VIDEO_STREAM_BUFFER_SIZE = 1
    MAX_TCP_STREAM_IMAGE_SIZE = Vector2(360, 480)

    def initialize(self):
        #ptvsd.enable_attach(secret = 'd3sign3')
        #ptvsd.wait_for_attach()

        self._init_logging()
        self._init_drivers()
        self._init_services()
        self._publish_remoting_objects()
        self._init_web_app()
        self._logger.close()

    def _init_logging(self):
        self._logger = Logger()
        self._logger.logging_level = LogEntryType.INFORMATIONAL
        self._logger.save_blob_data = True
        self._csv_log_subscriber = CsvLogSubscriber(self.LOGGING_DIRECTORY)
        self._image_log_subscriber = ImageLogSubscriber(self.IMAGE_LOGGING_DIRECTORY)
        self._console_log_subscriber = ConsoleLogSubscriber()
        self._logger.subscribe(self._csv_log_subscriber)
        self._logger.subscribe(self._image_log_subscriber)
        self._logger.subscribe(self._console_log_subscriber)

    def _init_drivers(self):
        self._uart_driver = UartDriver()
        self._pololu_driver = PololuDriver(self._uart_driver)
        self._microcontroller_driver = MicrocontrollerDriver(self._uart_driver)

    def _init_services(self):
        self._init_remoting_services()
        self._camera_arm_control_service = CameraArmControlService(self._pololu_driver)
        self._hardware_status_collector_service = HardwareStatusCollectorService(self._microcontroller_driver)
        self._local_world_map_service = LocalWorldMapService(self._camera_arm_control_service)
        self._movement_executor = MovementExecutor(self._microcontroller_driver)
        self._secret_code_decoding_service = SecretCodeDecodingService(self._microcontroller_driver)
        self._treasure_handling_service = TreasureHandlingService(self._microcontroller_driver, self._local_world_map_service, self._movement_executor)
        self._init_video_streaming_service()

    def _init_video_streaming_service(self):
        self._tcp_stream_writer = TcpStreamWriter(self.STREAMING_SERVER_IP_ADDRESS, self.STREAMING_SERVER_PORT, self.VIDEO_STREAM_BUFFER_SIZE, self.MAX_TCP_STREAM_IMAGE_SIZE)
        self._local_world_map_stream_writer = LocalWorldMapStreamWriter(self._local_world_map_service)
        self._local_world_map_stream_writer.add_output_stream_writer(self._tcp_stream_writer)
        self._video_stream_dispatcher = VideoStreamDispatcher(Image.from_file(self.DEFAULT_STREAM_IMAGE_FILE_NAME))
        self._video_stream_dispatcher.add_stream_writer( self._local_world_map_stream_writer)
        self._video_streaming_service = VideoStreamingService(self._video_stream_dispatcher, None, self._tcp_stream_writer)
        self._video_streaming_service.set_camera_source(self.DEFAULT_EMBEDDED_CAMERA_ID, self.EMBEDDED_CAMERA_FRAME_ROTATION_ANGLE)

    def _init_remoting_services(self):
        self._remoting_server = RemotingServer(JsonSerializer())

    def _publish_remoting_objects(self):
        self._remoting_server.publish_object(self._movement_executor)
        self._remoting_server.publish_object(self._hardware_status_collector_service)
        self._remoting_server.publish_object(self._secret_code_decoding_service)
        self._remoting_server.publish_object(self._treasure_handling_service)
        self._remoting_server.publish_object(self._video_streaming_service)
        self._remoting_server.publish_object(self._camera_arm_control_service)
        self._remoting_server.publish_object(self._local_world_map_service)
        self._remoting_server.publish_object(self)

    def _init_web_app(self):
        self._web_server = EmbeddedWebServer(self.SERVER_PORT, self._logger, self._remoting_server)
        self._web_server.run()

    def restart(self):
        python = sys.executable
        script = os.path.realpath(__file__)
        subprocess.Popen([python, script])
        self.stop()

    def stop(self):
        self._web_server.shutdown()
        sys.exit(0)