コード例 #1
0
    async def run_client():
        # Some user function to call when subscriptions receive data.

        def user_callback(command):
            print("Subscription has received data.")
            called.append(True)

        broadcaster = client.SharedBroadcaster(log_level='DEBUG')
        await broadcaster.register()
        ctx = client.Context(broadcaster, log_level='DEBUG')
        await ctx.search('pi')
        print('done searching')
        chan1 = await ctx.create_channel('pi')
        chan1.register_user_callback(user_callback)
        # ...and then wait for all the responses.
        await chan1.wait_for_connection()
        reading = await chan1.read()
        print('reading:', reading)
        sub_id = await chan1.subscribe()
        await chan1.unsubscribe(sub_id)
        await chan1.write((5, ))
        reading = await chan1.read()
        print('reading:', reading)
        await chan1.write((6, ))
        reading = await chan1.read()
        print('reading:', reading)
        await chan1.disconnect()
        await chan1.circuit.socket.close()
コード例 #2
0
def test_curio_server_example(prefix, run_client):
    import caproto.curio.client as client
    from caproto.ioc_examples.type_varieties import (pvdb)
    from caproto.curio.server import ServerExit, start_server as server_main

    pvdb = {prefix + key: value for key, value in pvdb.items()}
    pi_pv = prefix + 'int'
    broadcaster = client.SharedBroadcaster()
    ctx = client.Context(broadcaster)

    async def connect():
        await broadcaster.register()
        await ctx.search(pi_pv)
        print('done searching')
        chan1 = await ctx.create_channel(pi_pv)
        # ...and then wait for all the responses.
        await chan1.wait_for_connection()
        return chan1

    async def task():
        async def server_wrapper():
            try:
                await server_main(pvdb)
            except ServerExit:
                print('Server exited normally')

        try:
            server_task = await curio.spawn(server_wrapper)
            await curio.sleep(1)  # Give server some time to start up.
            chan1 = await connect()
            await run_client(chan1, pvdb, ctx)
            await chan1.disconnect()
            print('client is done')
        finally:
            try:
                await server_task.cancel()
                await server_task.join()
            except curio.KernelExit:
                print('Server exited normally')

    with curio.Kernel() as kernel:
        kernel.run(task)
    print('done')
コード例 #3
0
ファイル: test_examples.py プロジェクト: ZLLentz/caproto
    async def run_client():
        # Some user function to call when subscriptions receive data.

        def user_callback(command):
            print("Subscription has received data: {}".format(command))
            commands.append(command)

        broadcaster = client.SharedBroadcaster(log_level='DEBUG')
        await broadcaster.register()
        ctx = client.Context(broadcaster, log_level='DEBUG')
        await ctx.search('pi')
        print('done searching')
        chan1 = await ctx.create_channel('pi')
        chan1.register_user_callback(user_callback)
        # ...and then wait for all the responses.
        await chan1.wait_for_connection()
        reading = await chan1.read()
        print('reading:', reading)
        sub_id = await chan1.subscribe()
        await curio.sleep(0.2)
        await chan1.unsubscribe(sub_id)
        await chan1.write((5,))
        reading = await chan1.read()
        expected = 5
        actual, = reading.data
        assert actual == expected
        print('reading:', reading)
        await chan1.write((6,))
        reading = await chan1.read()
        expected = 6
        actual, = reading.data
        assert actual == expected
        print('reading:', reading)

        # Test updating metadata...
        # _fields_ = [
        #     ('status', short_t),
        #     ('severity', short_t),
        #     ('secondsSinceEpoch', ctypes.c_uint32),
        #     ('nanoSeconds', ctypes.c_uint32),
        #     ('RISC_Pad', long_t),
        # ]
        metadata = (0, 0, ca.TimeStamp(4, 0), 0)  # set timestamp to 4 seconds
        await chan1.write((7,), data_type=ca.ChannelType.TIME_DOUBLE,
                          metadata=metadata)
        reading = await chan1.read(data_type=20)
        # check reading
        expected = 7
        actual, = reading.data
        assert actual == expected
        # check timestamp
        expected = 4

        print('timestamp is', reading.metadata.stamp.as_datetime())

        actual = reading.metadata.secondsSinceEpoch
        assert actual == expected
        print('reading:', reading)

        status, severity = ca.AlarmStatus.SCAN, ca.AlarmSeverity.MAJOR_ALARM
        server_alarm = pvdb['pi'].alarm

        await server_alarm.write(status=status, severity=severity)

        # test acknowledge alarm status/severity
        reading = await chan1.read(data_type=ca.ChannelType.TIME_DOUBLE)
        assert reading.metadata.status == status
        assert reading.metadata.severity == severity

        # acknowledge the severity
        metadata = (severity + 1, )
        await chan1.write((),
                          data_type=ca.ChannelType.PUT_ACKS,
                          metadata=metadata)

        assert server_alarm.severity_to_acknowledge == 0

        # now make a transient alarm and toggle the severity
        # now require transients to be acknowledged
        metadata = (1, )
        await chan1.write((),
                          data_type=ca.ChannelType.PUT_ACKT,
                          metadata=metadata)

        assert server_alarm.must_acknowledge_transient

        await server_alarm.write(severity=severity)
        await server_alarm.write(severity=ca.AlarmSeverity.NO_ALARM)

        assert server_alarm.severity_to_acknowledge == severity

        # acknowledge the severity
        metadata = (severity + 1, )
        await chan1.write((),
                          data_type=ca.ChannelType.PUT_ACKS,
                          metadata=metadata)

        assert server_alarm.severity_to_acknowledge == 0

        severity = ca.AlarmSeverity.NO_ALARM

        reading = await chan1.read(data_type=ca.ChannelType.TIME_DOUBLE)
        # check reading (unchanged since last time)
        expected = 7
        actual, = reading.data
        assert actual == expected
        # check status
        actual = reading.metadata.status
        assert actual == status
        # check severity
        actual = reading.metadata.severity
        assert actual == severity

        await chan1.disconnect()
        assert commands, 'subscription not called in client'
        # await chan1.circuit.socket.close()

        commands.clear()
        await ctx.search('str')
        await ctx.search('str2')
        print('done searching')
        chan2 = await ctx.create_channel('str')
        chan3 = await ctx.create_channel('str2')
        chan2.register_user_callback(user_callback)
        chan3.register_user_callback(user_callback)
        await chan2.wait_for_connection()
        await chan3.wait_for_connection()
        sub_id2 = await chan2.subscribe()
        sub_id3 = await chan3.subscribe()
        print('write...')
        await chan2.write(b'hell')
        await chan3.write(b'good')

        print('setting alarm status...')
        await pvdb['str'].alarm.write(severity=ca.AlarmSeverity.MAJOR_ALARM)

        await curio.sleep(0.5)

        await chan2.unsubscribe(sub_id2)
        await chan3.unsubscribe(sub_id3)
        # expecting that the subscription callback should get called:
        #   1. on connection (2)
        #   2. when chan2 is written to (1)
        #   3. when chan3 is written to (1)
        #   4. when alarm status is updated for both channels (2)
        # for a total of 6
        assert len(commands) == 2 + 2 + 2