|
| 1 | +"""Simulated IOC for CLS field-change callback tests. |
| 2 | +
|
| 3 | +Creates a small set of records and registers ``on_field_change`` callbacks via |
| 4 | +the CLS extension. Counter PVs are incremented each time a callback fires, |
| 5 | +allowing the test client to verify behaviour over CA or PVA. |
| 6 | +
|
| 7 | +Records created |
| 8 | +--------------- |
| 9 | +``{prefix}:AO`` |
| 10 | + The record whose fields the test client writes to. |
| 11 | +``{prefix}:SCAN-CB-CNT`` |
| 12 | + Incremented by the SCAN callback. |
| 13 | +``{prefix}:DISA-CB-CNT`` |
| 14 | + Incremented by the DISA callback. |
| 15 | +``{prefix}:VAL-CB-CNT`` |
| 16 | + Incremented by the VAL callback. |
| 17 | +``{prefix}:HIHI-CB-CNT`` |
| 18 | + Incremented by the HIHI callback (alarm field, DBF_DOUBLE). |
| 19 | +``{prefix}:DESC-CB-CNT`` |
| 20 | + Incremented by the DESC callback (string field, DBF_STRING). |
| 21 | +``{prefix}:ANY-CB-CNT`` |
| 22 | + Incremented by a wildcard ``"*"`` callback (fires on **every** field |
| 23 | + write). |
| 24 | +
|
| 25 | +Expected behaviour |
| 26 | +------------------ |
| 27 | +- Original (upstream) pythonSoftIOC: ``on_field_change`` does not exist, so |
| 28 | + this script raises ``AttributeError`` before printing READY. |
| 29 | +- CLS fork: all callbacks register successfully and READY is printed. |
| 30 | +""" |
| 31 | + |
| 32 | +import sys |
| 33 | +from argparse import ArgumentParser |
| 34 | +from multiprocessing.connection import Client |
| 35 | + |
| 36 | +from softioc import softioc, builder, asyncio_dispatcher |
| 37 | + |
| 38 | +from conftest import ADDRESS, select_and_recv |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + with Client(ADDRESS) as conn: |
| 43 | + parser = ArgumentParser() |
| 44 | + parser.add_argument("prefix", help="PV prefix for the records") |
| 45 | + parsed_args = parser.parse_args() |
| 46 | + builder.SetDeviceName(parsed_args.prefix) |
| 47 | + |
| 48 | + # Main record whose fields the test client writes to. |
| 49 | + ao = builder.aOut("AO", initial_value=0.0, HIHI=90.0, HIGH=70.0) |
| 50 | + |
| 51 | + # Counter PVs — incremented by the corresponding callback. |
| 52 | + scan_cnt = builder.longOut("SCAN-CB-CNT", initial_value=0) |
| 53 | + disa_cnt = builder.longOut("DISA-CB-CNT", initial_value=0) |
| 54 | + val_cnt = builder.longOut("VAL-CB-CNT", initial_value=0) |
| 55 | + hihi_cnt = builder.longOut("HIHI-CB-CNT", initial_value=0) |
| 56 | + desc_cnt = builder.longOut("DESC-CB-CNT", initial_value=0) |
| 57 | + any_cnt = builder.longOut("ANY-CB-CNT", initial_value=0) |
| 58 | + |
| 59 | + # Boot the IOC. |
| 60 | + dispatcher = asyncio_dispatcher.AsyncioDispatcher() |
| 61 | + builder.LoadDatabase() |
| 62 | + softioc.iocInit(dispatcher) |
| 63 | + |
| 64 | + # ---- Register on_field_change callbacks (CLS extension) ---------- |
| 65 | + # With upstream pythonSoftIOC this raises AttributeError. |
| 66 | + |
| 67 | + def _make_increment(counter): |
| 68 | + """Return a callback that increments *counter* by one.""" |
| 69 | + def _cb(rec_name, field, value): |
| 70 | + counter.set(counter.get() + 1) |
| 71 | + return _cb |
| 72 | + |
| 73 | + # Per-field callbacks. |
| 74 | + ao.on_field_change("SCAN", _make_increment(scan_cnt)) |
| 75 | + ao.on_field_change("DISA", _make_increment(disa_cnt)) |
| 76 | + ao.on_field_change("VAL", _make_increment(val_cnt)) |
| 77 | + # DBF_DOUBLE alarm field |
| 78 | + ao.on_field_change("HIHI", _make_increment(hihi_cnt)) |
| 79 | + # DBF_STRING metadata field |
| 80 | + ao.on_field_change("DESC", _make_increment(desc_cnt)) |
| 81 | + |
| 82 | + # Wildcard callback — fires for every field write on this record. |
| 83 | + ao.on_field_change("*", _make_increment(any_cnt)) |
| 84 | + |
| 85 | + conn.send("R") # "Ready" |
| 86 | + |
| 87 | + # Keep the process alive until the test tells us to stop. |
| 88 | + select_and_recv(conn, "D") # "Done" |
| 89 | + |
| 90 | + sys.stdout.flush() |
| 91 | + sys.stderr.flush() |
| 92 | + |
| 93 | + conn.send("D") # "Done" acknowledgement |
0 commit comments