|
7 | 7 | from tempfile import NamedTemporaryFile |
8 | 8 | from argparse import ArgumentParser |
9 | 9 | from multiprocessing.connection import Client |
| 10 | +import threading |
10 | 11 |
|
11 | 12 | from softioc import softioc, builder, asyncio_dispatcher |
12 | 13 |
|
13 | 14 | from conftest import ADDRESS, select_and_recv |
14 | 15 |
|
15 | 16 | if __name__ == "__main__": |
16 | | - # Being run as an IOC, so parse args and set prefix |
17 | | - parser = ArgumentParser() |
18 | | - parser.add_argument('prefix', help="The PV prefix for the records") |
19 | | - parsed_args = parser.parse_args() |
20 | | - builder.SetDeviceName(parsed_args.prefix) |
21 | | - |
22 | | - # Load the base records without DTYP fields |
23 | | - with open(Path(__file__).parent / "hw_records.db", "rb") as inp: |
24 | | - with NamedTemporaryFile(suffix='.db', delete=False) as out: |
25 | | - for line in inp.readlines(): |
26 | | - if not re.match(rb"\s*field\s*\(\s*DTYP", line): |
27 | | - out.write(line) |
28 | | - softioc.dbLoadDatabase( |
29 | | - out.name, substitutions=f"device={parsed_args.prefix}") |
30 | | - os.unlink(out.name) |
31 | | - |
32 | | - # Override DTYPE and OUT, and provide a callback |
33 | | - gain = builder.boolOut("GAIN", on_update=print) |
34 | | - softioc.devIocStats(parsed_args.prefix) |
35 | | - |
36 | | - # Run the IOC |
37 | | - builder.LoadDatabase() |
38 | | - event_loop = asyncio.get_event_loop() |
39 | | - softioc.iocInit(asyncio_dispatcher.AsyncioDispatcher(event_loop)) |
40 | | - |
41 | 17 | with Client(ADDRESS) as conn: |
| 18 | + # Being run as an IOC, so parse args and set prefix |
| 19 | + parser = ArgumentParser() |
| 20 | + parser.add_argument('prefix', help="The PV prefix for the records") |
| 21 | + parsed_args = parser.parse_args() |
| 22 | + builder.SetDeviceName(parsed_args.prefix) |
| 23 | + |
| 24 | + # Load the base records without DTYP fields |
| 25 | + with open(Path(__file__).parent / "hw_records.db", "rb") as inp: |
| 26 | + with NamedTemporaryFile(suffix='.db', delete=False) as out: |
| 27 | + for line in inp.readlines(): |
| 28 | + if not re.match(rb"\s*field\s*\(\s*DTYP", line): |
| 29 | + out.write(line) |
| 30 | + softioc.dbLoadDatabase( |
| 31 | + out.name, substitutions=f"device={parsed_args.prefix}") |
| 32 | + os.unlink(out.name) |
| 33 | + |
| 34 | + # Override DTYPE and OUT, and provide a callback |
| 35 | + gain = builder.boolOut("GAIN", on_update=print) |
| 36 | + softioc.devIocStats(parsed_args.prefix) |
| 37 | + |
| 38 | + # Run the IOC |
| 39 | + builder.LoadDatabase() |
| 40 | + event_loop = asyncio.get_event_loop() |
| 41 | + worker = threading.Thread(target=event_loop.run_forever) |
| 42 | + worker.daemon = True |
| 43 | + worker.start() |
| 44 | + softioc.iocInit(asyncio_dispatcher.AsyncioDispatcher(event_loop)) |
| 45 | + |
42 | 46 | conn.send("R") # "Ready" |
43 | 47 |
|
44 | 48 | # Make sure coverage is written on epicsExit |
|
0 commit comments