dls-controls / aioca

Asynchronous Channel Access client for asyncio and Python using libca via ctypes
Apache License 2.0
6 stars 3 forks source link

Return a waveform from a REST API #35

Closed zhangt58 closed 1 year ago

zhangt58 commented 1 year ago

Hi, it seems the waveform data does not return as expected in a REST API. For a regular PV with scalar value returns, it returns the single value as requested, but when requesting a waveform PV value, the same API returns a dict like:

 {
    "name": "PHY:SM_SNP_LAST_NOTE",
    "ok": true,
    "element_count": 2048,
    "datatype": 4
  }

I can get the expected waveform of ca_array in ipython with await aioca.caget. The version of aioca I'm using is 1.3.

I'm seeking some answers on the FastAPI side (which I'm using to build the REST API), while hoping that maybe I missed something on the aioca side. Appreciate to any help. Tong

coretl commented 1 year ago

I suspect there is some odd interaction between FastAPI's serialization of numpy arrays, and aioca (via epicscorelibs) usage of a subclass of numpy arrays. It's suspicious that the attributes that are set on the array are the only keys in the serialized dict: https://github.com/mdavidsaver/epicscorelibs/blob/master/src/python/epicscorelibs/ca/dbr.py#L801-L804

Please could you post the FastAPI issue and the code that generated the error?

zhangt58 commented 1 year ago

Thanks for pointing out the code lines that eventually helped me understand where those dict keys come from. And your guess was right, the default FastAPI jsonizer (jsonable_encoder) does not handle ca_array data as expected, as such, I constructed the JSON response with Response class.