diff --git a/nslsii/epics_utils.py b/nslsii/epics_utils.py new file mode 100644 index 0000000..69ed7ec --- /dev/null +++ b/nslsii/epics_utils.py @@ -0,0 +1,37 @@ +import argparse +import socket +from socket import gaierror, herror + +from caproto.threading.client import Context + + +def get_ioc_hostname(pvname): + """A helper function to get the IOC hostname based on the provided PV.""" + + ctx = Context() + (pv,) = ctx.get_pvs(pvname) + pv.wait_for_connection() + s = pv.circuit_manager.socket + + epics_addr = s.getpeername()[0] + sci_addr = epics_addr.split(".") + sci_addr[2] = str(int(sci_addr[2]) - 3) + sci_addr = ".".join(sci_addr) + + try: + hostname = socket.gethostbyaddr(sci_addr)[0] + except (gaierror, herror): + hostname = socket.gethostbyaddr(epics_addr)[0] + + return hostname + + +def main(): + # Used by the `sync-experiment` command + + parser = argparse.ArgumentParser(description="Start or switch beamline experiment and record it in Redis") + parser.add_argument("-p", "--pv", dest="pvname", type=str, help="PV to query information about", required=True) + + args = parser.parse_args() + hostname = get_ioc_hostname(args.pvname) + print(f"IOC hostname for {args.pvname} is {hostname}") diff --git a/setup.py b/setup.py index 3725833..34ec90f 100644 --- a/setup.py +++ b/setup.py @@ -29,8 +29,8 @@ install_requires=requirements, entry_points={ "console_scripts": [ - # 'command = some.module:some_function', "sync-experiment = nslsii.sync_experiment:main", + "what-is-ioc = nslsii.epics_utils:main", ], }, )