예제 #1
0
    def get_stream(self, timeout=config.ZMQ_RECEIVE_TIMEOUT, data_change_callback=None):
        source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

        self.bsread_source = Source(host=source_host, port=source_port, mode=PULL,
                                    receive_timeout=timeout)
        self.bsread_source.handler = Handler(data_change_callback)
        return self.bsread_source
예제 #2
0
    def get_stream(self):

        self.verify_camera_online()
        self._collect_camera_settings()

        source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

        self.bsread_source = Source(host=source_host, port=source_port, mode=PULL,
                                    receive_timeout=config.ZMQ_RECEIVE_TIMEOUT)

        return self.bsread_source
예제 #3
0
파일: bsread_dal.py 프로젝트: lwwhsh/pyscan
    def _connect_bsread(self, host, port):
        # Configure the connection type.
        if config.bs_connection_mode.lower() == "sub":
            mode = mflow.SUB
        elif config.bs_connection_mode.lower() == "pull":
            mode = mflow.PULL

        if host and port:
            self.stream = Source(host=host,
                                 port=port,
                                 queue_size=config.bs_queue_size,
                                 receive_timeout=config.bs_receive_timeout,
                                 mode=mode)
        else:
            channels = [x.identifier for x in self.properties
                        ] + [x.identifier for x in self.conditions]
            self.stream = Source(channels=channels,
                                 queue_size=config.bs_queue_size,
                                 receive_timeout=config.bs_receive_timeout,
                                 mode=mode)
        self.stream.connect()
예제 #4
0
    def remove_source(self, source_id):
        """method to remove sources from the loop iterator"""
        
        self.source_ids.pop(self.source_ids.index(source_id))

        kwargs = self.source_default_keys.copy()
        kwargs['channels'] = self.source_ids    

        if self.source:
            self.source.disconnect()
        
        self.source = Source(**kwargs)
예제 #5
0
    def register_source(self, source_id):
        """method to register sources to be read in the loop iterator"""
        
        if not (source_id in self.source_ids):
            self.source_ids.append(source_id)

        kwargs = self.source_default_keys.copy()
        kwargs['channels'] = self.source_ids    

        if self.source:
            self.source.disconnect()
        
        self.source = Source(**kwargs)
예제 #6
0
def create_source(camera_stream_address,
                  receive_timeout=config.PIPELINE_RECEIVE_TIMEOUT,
                  mode=SUB):
    source_host, source_port = get_host_port_from_stream_address(
        camera_stream_address)
    if camera_stream_address.startswith("ipc"):
        return IpcSource(address=camera_stream_address,
                         receive_timeout=receive_timeout,
                         mode=mode)
    else:
        return Source(host=source_host,
                      port=source_port,
                      receive_timeout=receive_timeout,
                      mode=mode)
예제 #7
0
        def _collect_camera_settings(self):
            try:
                #self.width_raw, self.height_raw  = 659, 494
                source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

                stream = Source(host=source_host, port=source_port, mode=PULL,receive_timeout=3000)
                stream.connect()
                data = stream.receive()
                image = data.data.data[self.camera_config.get_source() + config.EPICS_PV_SUFFIX_IMAGE].value
                if image is None:
                    self.height_raw, self.width_raw = 0,0
                else:
                    image = transform_image(image, self.camera_config)
                    self.height_raw, self.width_raw = image.shape
            except:
                raise RuntimeError("Could not fetch camera settings cam_server:{}".format(self.camera_config.get_source()))
            finally:
                stream.disconnect()
예제 #8
0
 def __init__(self, host='localhost', port=9999):
     self.source = Source('localhost', 9999)
     self.s = self.source.connect()
예제 #9
0
 def __init__(self, host='localhost', port=9999):
     self.source = Source('localhost', 9999)
     self.read_Ids = []
channels_definitions = {}

for n_connection in range(n_connections):
    _logger.info("Starting connection number %d." % n_connection)

    stream_address = dispatcher.request_stream(channels)
    source_host, source_port = stream_address.rsplit(":", maxsplit=1)

    source_host = source_host.split("//")[1]
    source_port = int(source_port)

    _logger.info("Input stream host '%s' and port '%s'.", source_host,
                 source_port)

    stream = Source(host=source_host,
                    port=source_port,
                    mode=SUB,
                    receive_timeout=1000)
    stream.connect()

    n_received_messages = 0

    while n_received_messages < n_messages_per_connection:
        message = stream.receive(handler=handler.receive)

        # In case you set a receive timeout, the returned message can be None.
        if message is None:
            _logger.debug("Empty message.")
            continue

        n_received_messages += 1
예제 #11
0
 def __init__(self, host="localhost", port=9999):
     self.source = Source("localhost", 9999)
     self.s = self.source.connect()
예제 #12
0
def store_pipeline(stop_event, statistics, parameter_queue, cam_client,
                   pipeline_config, output_stream_port, background_manager):
    # TODO: Implement statistics: n_clients, input_throughput

    def no_client_timeout():
        _logger.warning(
            "No client connected to the pipeline stream for %d seconds. Closing instance."
            % config.MFLOW_NO_CLIENTS_TIMEOUT)
        stop_event.set()

    source = None
    sender = None

    try:

        camera_stream_address = cam_client.get_camera_stream(
            pipeline_config.get_camera_name())
        camera_name = pipeline_config.get_camera_name()
        _logger.debug("Connecting to camera %s on stream address %s.",
                      camera_name, camera_stream_address)

        source_host, source_port = get_host_port_from_stream_address(
            camera_stream_address)

        source = Source(host=source_host,
                        port=source_port,
                        receive_timeout=config.PIPELINE_RECEIVE_TIMEOUT,
                        mode=SUB)

        source.connect()

        _logger.debug("Opening output stream on port %d.", output_stream_port)

        sender = Sender(port=output_stream_port,
                        mode=PUSH,
                        data_header_compression=config.
                        CAMERA_BSREAD_DATA_HEADER_COMPRESSION,
                        block=False)

        sender.open(no_client_action=no_client_timeout,
                    no_client_timeout=config.MFLOW_NO_CLIENTS_TIMEOUT)
        # TODO: Register proper channels.

        # Indicate that the startup was successful.
        stop_event.clear()

        _logger.debug("Transceiver started.")

        while not stop_event.is_set():
            try:

                data = source.receive()

                # In case of receiving error or timeout, the returned data is None.
                if data is None:
                    continue

                forward_data = {camera_name: data.data.data["image"].value}

                pulse_id = data.data.pulse_id
                timestamp = (data.data.global_timestamp,
                             data.data.global_timestamp_offset)

                sender.send(data=forward_data,
                            pulse_id=pulse_id,
                            timestamp=timestamp)

            except:
                _logger.exception("Could not process message.")
                stop_event.set()

        _logger.info("Stopping transceiver.")

    except:
        _logger.exception(
            "Exception while trying to start the receive and process thread.")
        raise

    finally:
        if source:
            source.disconnect()

        if sender:
            sender.close()
예제 #13
0
def processing_pipeline(stop_event, statistics, parameter_queue, cam_client,
                        pipeline_config, output_stream_port,
                        background_manager):
    # TODO: Implement statistics: n_clients, input_throughput

    def no_client_timeout():
        _logger.warning(
            "No client connected to the pipeline stream for %d seconds. Closing instance."
            % config.MFLOW_NO_CLIENTS_TIMEOUT)
        stop_event.set()

    def process_pipeline_parameters():
        parameters = pipeline_config.get_configuration()
        _logger.debug("Processing pipeline parameters %s.", parameters)

        background_array = None
        if parameters.get("image_background_enable"):
            background_id = pipeline_config.get_background_id()
            _logger.debug("Image background enabled. Using background_id %s.",
                          background_id)

            background_array = background_manager.get_background(background_id)

        size_x, size_y = cam_client.get_camera_geometry(
            pipeline_config.get_camera_name())

        image_region_of_interest = parameters.get("image_region_of_interest")
        if image_region_of_interest:
            _, size_x, _, size_y = image_region_of_interest

        _logger.debug("Image width %d and height %d.", size_x, size_y)

        return parameters, background_array

    source = None
    sender = None

    try:
        pipeline_parameters, image_background_array = process_pipeline_parameters(
        )

        camera_stream_address = cam_client.get_camera_stream(
            pipeline_config.get_camera_name())
        _logger.debug("Connecting to camera stream address %s.",
                      camera_stream_address)

        source_host, source_port = get_host_port_from_stream_address(
            camera_stream_address)

        source = Source(host=source_host,
                        port=source_port,
                        receive_timeout=config.PIPELINE_RECEIVE_TIMEOUT,
                        mode=SUB)
        source.connect()

        _logger.debug("Opening output stream on port %d.", output_stream_port)

        sender = Sender(port=output_stream_port,
                        mode=PUB,
                        data_header_compression=config.
                        CAMERA_BSREAD_DATA_HEADER_COMPRESSION)

        sender.open(no_client_action=no_client_timeout,
                    no_client_timeout=config.MFLOW_NO_CLIENTS_TIMEOUT)
        # TODO: Register proper channels.

        # Indicate that the startup was successful.
        stop_event.clear()

        _logger.debug("Transceiver started.")

        while not stop_event.is_set():
            try:
                while not parameter_queue.empty():
                    new_parameters = parameter_queue.get()
                    pipeline_config.set_configuration(new_parameters)
                    pipeline_parameters, image_background_array = process_pipeline_parameters(
                    )

                data = source.receive()

                # In case of receiving error or timeout, the returned data is None.
                if data is None:
                    continue

                image = data.data.data["image"].value
                x_axis = data.data.data["x_axis"].value
                y_axis = data.data.data["y_axis"].value
                processing_timestamp = data.data.data["timestamp"].value

                processed_data = process_image(image, processing_timestamp,
                                               x_axis, y_axis,
                                               pipeline_parameters,
                                               image_background_array)

                processed_data["width"] = processed_data["image"].shape[1]
                processed_data["height"] = processed_data["image"].shape[0]

                pulse_id = data.data.pulse_id
                timestamp = (data.data.global_timestamp,
                             data.data.global_timestamp_offset)

                sender.send(data=processed_data,
                            timestamp=timestamp,
                            pulse_id=pulse_id)

            except:
                _logger.exception("Could not process message.")
                stop_event.set()

        _logger.info("Stopping transceiver.")

    except:
        _logger.exception(
            "Exception while trying to start the receive and process thread.")
        raise

    finally:
        if source:
            source.disconnect()

        if sender:
            sender.close()