async def update_metadata(chan1, pvdb, ctx): # 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, ), notify=True, 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 # work around pypy bug in _anonymous_ actual = reading.metadata.stamp.secondsSinceEpoch assert actual == expected print('reading:', reading)
(ca.ChannelType.FLOAT, 3, (7, 21.1, 3.1), None), (ca.ChannelType.FLOAT, 3, float_arr, None), (ca.ChannelType.FLOAT, 3, bytes(float_arr), None), (ca.ChannelType.FLOAT, 3, numpy.array([7, 21.1, 3.1], dtype='f4'), None), (ca.ChannelType.LONG, 1, (7, ), None), (ca.ChannelType.LONG, 2, (7, 21), None), (ca.ChannelType.LONG, 5, numpy.array([7, 21, 2, 4, 5], dtype='i4'), None), (ca.ChannelType.LONG, 5, long_arr, None), (ca.ChannelType.LONG, 5, bytes(long_arr), None), (ca.ChannelType.DOUBLE, 1, (7, ), None), (ca.ChannelType.DOUBLE, 6, (7, 21.1, 7, 7, 2.1, 1.1), None), (ca.ChannelType.DOUBLE, 3, numpy.array([7, 21.1, 3.1], dtype='f8'), None), (ca.ChannelType.DOUBLE, 3, double_arr, None), (ca.ChannelType.DOUBLE, 3, bytes(double_arr), None), (ca.ChannelType.TIME_DOUBLE, 1, (7, ), ca.DBR_TIME_DOUBLE(1, 0, ca.TimeStamp(3, 5))), (ca.ChannelType.TIME_DOUBLE, 1, (7, ), (1, 0, ca.TimeStamp(3, 5))), (ca.ChannelType.TIME_DOUBLE, 2, (7, 3.4), (1, 0, ca.TimeStamp(3, 5))), (ca.ChannelType.STRING, 1, b'abc'.ljust(40, b'\x00'), None), (ca.ChannelType.STRING, 3, 3 * b'abc'.ljust(40, b'\x00'), None), (ca.ChannelType.STRING, 3, numpy.array(['abc', 'def'], '>S40'), None), (ca.ChannelType.STRING, 3, numpy.array(['abc', 'def'], 'S40'), None), (ca.ChannelType.CHAR, 1, b'z', None), (ca.ChannelType.CHAR, 3, b'abc', None), ] @pytest.mark.parametrize('data_type, data_count, data, metadata', payloads) def test_reads(circuit_pair, data_type, data_count, data, metadata): cli_circuit, srv_circuit = circuit_pair
def test_timestamp_flexible_epics_copy(): ts = ca.TimeStamp.from_flexible_value(ca.TimeStamp(2, 3)) assert ts.secondsSinceEpoch == 2 assert ts.nanoSeconds == 3
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