|
| 1 | +import asyncio |
| 2 | +import time |
| 3 | + |
| 4 | +from p4p.server import Server, StaticProvider |
| 5 | +from p4p.server.asyncio import SharedPV |
| 6 | + |
| 7 | +from fastcs.attributes import Attribute, AttrR, AttrRW, AttrW |
| 8 | +from fastcs.controller import Controller |
| 9 | + |
| 10 | +from .util import cast_from_p4p_type, cast_to_p4p_type, get_nt_from_datatype |
| 11 | + |
| 12 | +_pvs = set() |
| 13 | + |
| 14 | + |
| 15 | +class AttrWHandler: |
| 16 | + def __init__(self, attr_w: AttrW | AttrRW): |
| 17 | + self._attr_w = attr_w |
| 18 | + |
| 19 | + async def put(self, pv, op): |
| 20 | + value = op.value() |
| 21 | + raw_value = value.raw.value |
| 22 | + |
| 23 | + await self._attr_w.process_without_display_update( |
| 24 | + cast_from_p4p_type(self._attr_w.datatype, raw_value) |
| 25 | + ) |
| 26 | + |
| 27 | + pv.post(value, timestamp=time.time()) |
| 28 | + op.done() |
| 29 | + |
| 30 | + |
| 31 | +def make_shared_pv(attribute: Attribute) -> SharedPV: |
| 32 | + kwargs = { |
| 33 | + "nt": get_nt_from_datatype(attribute.datatype), |
| 34 | + "initial": cast_to_p4p_type( |
| 35 | + attribute.datatype, attribute.datatype.initial_value |
| 36 | + ), |
| 37 | + "timestamp": time.time(), |
| 38 | + } |
| 39 | + if type(attribute) is AttrW or type(attribute) is AttrRW: |
| 40 | + kwargs["handler"] = AttrWHandler(attribute) |
| 41 | + |
| 42 | + shared_pv = SharedPV(**kwargs) |
| 43 | + |
| 44 | + if type(attribute) is AttrR or type(attribute) is AttrRW: |
| 45 | + shared_pv.post( |
| 46 | + cast_to_p4p_type(attribute.datatype, attribute.get()), timestamp=time.time() |
| 47 | + ) |
| 48 | + |
| 49 | + async def on_update(value): |
| 50 | + shared_pv.post( |
| 51 | + cast_to_p4p_type(attribute.datatype, value), timestamp=time.time() |
| 52 | + ) |
| 53 | + |
| 54 | + attribute.set_update_callback(on_update) |
| 55 | + |
| 56 | + return shared_pv |
| 57 | + |
| 58 | + |
| 59 | +async def make_attribute_providers( |
| 60 | + prefix_root: str, controller: Controller |
| 61 | +) -> dict[str, StaticProvider]: |
| 62 | + providers = {} |
| 63 | + |
| 64 | + for single_mapping in controller.get_controller_mappings(): |
| 65 | + path = single_mapping.controller.path |
| 66 | + pv_prefix = ":".join([prefix_root] + path) |
| 67 | + provider = StaticProvider(pv_prefix) |
| 68 | + providers[pv_prefix] = provider |
| 69 | + |
| 70 | + for attr_name, attribute in single_mapping.attributes.items(): |
| 71 | + pv_name = f"{pv_prefix}:{attr_name.title().replace('_', '')}" |
| 72 | + |
| 73 | + shared_pv = make_shared_pv(attribute) |
| 74 | + |
| 75 | + _pvs.add(shared_pv) |
| 76 | + provider.add(pv_name, shared_pv) |
| 77 | + |
| 78 | + return providers |
| 79 | + |
| 80 | + |
| 81 | +class P4PIOC: |
| 82 | + def __init__( |
| 83 | + self, |
| 84 | + pv_prefix: str, |
| 85 | + controller: Controller, |
| 86 | + ): |
| 87 | + self.pv_prefix = pv_prefix |
| 88 | + self.controller = controller |
| 89 | + |
| 90 | + async def run(self): |
| 91 | + """To be ran in the same event loop as `self.loop`.""" |
| 92 | + |
| 93 | + """ |
| 94 | + if asyncio.get_running_loop() is not self.loop: |
| 95 | + raise RuntimeError("Must run in the same event loop as `self.loop`") |
| 96 | + """ |
| 97 | + |
| 98 | + providers = await make_attribute_providers(self.pv_prefix, self.controller) |
| 99 | + |
| 100 | + endless_event = asyncio.Event() |
| 101 | + with Server(providers.values()): |
| 102 | + await endless_event.wait() |
0 commit comments