示例#1
0
def generate_real_stream(port, n_messages=None, interval=0.01):
    from bsread.sender import Sender

    generator = Sender(port=port)

    for channel in simulated_channels:
        generator.add_channel(**channel)

    generator.generate_stream(n_messages=n_messages, interval=interval)
示例#2
0
def start_sender():
    # Start a mock sender stream.
    generator = Sender(block=False)
    generator.add_channel('CAMERA1:X', lambda x: x, metadata={'type': 'int32'})
    generator.add_channel('CAMERA1:Y', lambda x: x, metadata={'type': 'int32'})
    generator.add_channel('CAMERA1:VALID',
                          lambda x: 10,
                          metadata={'type': 'int32'})

    generator.open()
    while bs_sending:
        generator.send()
        time.sleep(0.05)

    generator.close()
示例#3
0
def start_sender():
    # Start a mock sender stream.
    generator = Sender(block=False)
    generator.add_channel('CAMERA1:X', lambda x: x, metadata={'type': 'int32'})
    generator.add_channel('CAMERA1:Y', lambda x: x, metadata={'type': 'int32'})
    generator.add_channel('CAMERA1:VALID',
                          lambda x: 10,
                          metadata={'type': 'int32'})
    generator.add_channel('BEAM_OK',
                          lambda x: 1 if x % 20 == 0 else 0,
                          metadata={'type': 'int32'})

    generator.open()
    while bs_sending:
        generator.send()
        time.sleep(MOCK_SENDER_INTERVAL)

    generator.close()
示例#4
0
def createStream():
    s = TestData()
    generator = Sender()
    for par in pars:
        generator.add_channel(par, partial(s.getPar, parameter=par))
    generator.generate_stream()
示例#5
0
def image(pulse_id):
    image = []
    for i in range(2):
        # line = []
        # for index in range(0, 30, 1):
        #     grad = (3.1415*index/float(200))+pulse_id/float(100)
        #     line.append(math.sin(grad))
        # image.append(line)
        image.append([1.0, 2.0, 3.0, 4.0])
    return image


if __name__ == "__main__":

    generator = Sender()
    generator.add_channel('ABC', lambda x: x, metadata={'type': 'int32'})
    generator.add_channel('ABC_BIG',
                          lambda x: struct.pack('>i', x),
                          metadata={
                              'type': 'int32',
                              'encoding': 'big'
                          })
    generator.add_channel('ABCD', lambda x: x * 10.0)
    generator.add_channel('ABCDF', lambda x: x * 100.0)
    generator.add_channel('XYZ', lambda x: x * 200.0)
    generator.add_channel('XYZW',
                          lambda x: 'hello',
                          metadata={'type': 'string'})
    generator.add_channel('WWW',
                          lambda x: [1.0, 2.0, 3.0, 4.0],
                          metadata={
def main():
    parser = argparse.ArgumentParser()

    device_name = os.getenv("DEVICE_NAME", "test_device")
    parser.add_argument('--device_name',
                        type=str,
                        default=device_name,
                        help='Simulated device name')

    source_file = os.getenv("SOURCE_FILE", "sources.json")
    parser.add_argument('--source_file',
                        type=str,
                        default=source_file,
                        help='Simulation sources file')

    port = os.getenv("PORT", 9999)
    parser.add_argument('--port',
                        type=int,
                        default=port,
                        help='Simulation sources file')

    args = parser.parse_args()

    device_name = args.device_name
    source_file = args.source_file
    port = args.port

    _logger.info("Starting generator for device %s on port %s." %
                 (device_name, port))

    with open(source_file, 'r') as input_file:
        sources = json.load(input_file)

    if device_name not in sources:
        raise ValueError("device_name=%s not found in sources file." %
                         device_name)

    try:
        channels_metadata = sources[device_name]

        sender = Sender(port=port)

        for channel in channels_metadata:
            _logger.info("Adding channel %s with type %s and shape %s.." %
                         (channel["name"], channel["type"], channel["shape"]))

            sender.add_channel(name=channel["name"],
                               function=get_generator_function(
                                   channel["type"], channel["shape"]),
                               metadata={
                                   "type": channel["type"],
                                   "shape": channel["shape"]
                               })

        sender.generate_stream()

    except KeyboardInterrupt:
        _logger.info('Generator %s interupted (SIGINT)', device_name)

    except Exception as e:
        _logger.exception('Generator %s stopped', device_name)
示例#7
0
def process_epics_camera(stop_event, statistics, parameter_queue, camera,
                         port):
    """
    Start the camera stream and listen for image monitors. This function blocks until stop_event is set.
    :param stop_event: Event when to stop the process.
    :param statistics: Statistics namespace.
    :param parameter_queue: Parameters queue to be passed to the pipeline.
    :param camera: Camera instance to get the images from.
    :param port: Port to use to bind the output stream.
    """
    sender = None

    try:

        # If there is no client for some time, disconnect.
        def no_client_timeout():
            _logger.info(
                "No client connected to the '%s' stream for %d seconds. Closing instance."
                % (camera.get_name(), config.MFLOW_NO_CLIENTS_TIMEOUT))
            stop_event.set()

        def process_parameters():
            nonlocal x_size, y_size, x_axis, y_axis
            x_size, y_size = camera.get_geometry()
            x_axis, y_axis = camera.get_x_y_axis()
            sender.add_channel("image",
                               metadata={
                                   "compression":
                                   config.CAMERA_BSREAD_IMAGE_COMPRESSION,
                                   "shape": [x_size, y_size],
                                   "type": "uint16"
                               })

        x_size = y_size = x_axis = y_axis = None
        camera.connect()

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

        # Register the bsread channels - compress only the image.
        sender.add_channel("width",
                           metadata={
                               "compression":
                               config.CAMERA_BSREAD_SCALAR_COMPRESSION,
                               "type": "int64"
                           })

        sender.add_channel("height",
                           metadata={
                               "compression":
                               config.CAMERA_BSREAD_SCALAR_COMPRESSION,
                               "type": "int64"
                           })

        sender.add_channel("timestamp",
                           metadata={
                               "compression":
                               config.CAMERA_BSREAD_SCALAR_COMPRESSION,
                               "type": "float64"
                           })

        sender.add_channel("x_axis",
                           metadata={
                               "compression":
                               config.CAMERA_BSREAD_SCALAR_COMPRESSION,
                               "type": "float32"
                           })

        sender.add_channel("y_axis",
                           metadata={
                               "compression":
                               config.CAMERA_BSREAD_SCALAR_COMPRESSION,
                               "type": "float32"
                           })

        sender.open(no_client_action=no_client_timeout,
                    no_client_timeout=config.MFLOW_NO_CLIENTS_TIMEOUT)

        process_parameters()
        statistics.counter = 0

        def collect_and_send(image, timestamp):
            nonlocal x_size, y_size, x_axis, y_axis

            # Data to be sent over the stream.
            data = {
                "image": image,
                "timestamp": timestamp,
                "width": x_size,
                "height": y_size,
                "x_axis": x_axis,
                "y_axis": y_axis
            }

            try:
                sender.send(data=data, timestamp=timestamp, check_data=False)
            except Again:
                _logger.warning(
                    "Send timeout. Lost image with timestamp '%s'." %
                    timestamp)

            while not parameter_queue.empty():
                new_parameters = parameter_queue.get()
                camera.camera_config.set_configuration(new_parameters)

                process_parameters()

        camera.add_callback(collect_and_send)

        # This signals that the camera has successfully started.
        stop_event.clear()

    except:
        _logger.exception("Error while processing camera stream.")

    finally:

        # Wait for termination / update configuration / etc.
        stop_event.wait()

        camera.disconnect()

        if sender:
            sender.close()