EPICS Home

Experimental Physics and Industrial Control System


 
1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <20242025  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <20242025 
<== Date ==> <== Thread ==>

Subject: Re: Publish JSON structure as PVA
From: "Veseli, Sinisa via Tech-talk" <tech-talk at aps.anl.gov>
To: EPICS Tech Talk <tech-talk at aps.anl.gov>, Ralph Lange <ralph.lange at gmx.de>
Date: Wed, 10 Apr 2024 17:32:59 +0000
Hi Ralph,

The attached example (converter plus input file used for testing) probably isn't perfect, but it might be useful as a start.

When I run it in my environment it serves object on the 'jsonObject' channel and produces something like this:

$ python jsonToPva.py
structure
...
    structure codec
        string name 
        string parameters 
    long compressedSize 60000
    long uncompressedSize 60000
    structure[] dimension
        structure 
            long size 300
            long offset 0
            long fullSize 300
            long binning 1
            boolean reverse false
        structure 
            long size 200
            long offset 0
            long fullSize 200
            long binning 1
            boolean reverse false
    long uniqueId 246
    structure dataTimeStamp
        long secondsPastEpoch 1712765362
        long nanoseconds 566374540
        long userTag 0
 ...

$ pvget -r dataTimeStamp jsonObject
jsonObject structure 
    structure dataTimeStamp
        long secondsPastEpoch 1712765362
        long nanoseconds 566374540
        long userTag 0



Sinisa


import json
import pvaccess as pva

class JsonToPvaConverter:

    PVA_DATA_TYPE_MAP = {
        bool  : pva.BOOLEAN,
        int   : pva.LONG,
        float : pva.DOUBLE,
        str   : pva.STRING,
        type(None) : pva.STRING,
    }

    @classmethod
    def getPvaType(cls, value):
        if isinstance(value, dict):
            typeDict = {}
            for k,v in value.items():
                typeDict[k] = cls.getPvaType(v)
            return typeDict
        elif isinstance(value, list):
            if value:
                return [cls.getPvaType(value[0])]
            else:
                return [cls.getPvaType(None)]
        return cls.PVA_DATA_TYPE_MAP[type(value)]

    @classmethod
    def scrubValueDict(cls, valueDict):
        for k, v in list(valueDict.items()):
            if v is None:
                del valueDict[k]
            elif isinstance(v, dict):
               cls. scrubValueDict(v)
        return valueDict

    @classmethod
    def toPvObject(cls, s):
        valueDict = json.loads(s)
        typeDict = cls.getPvaType(valueDict)
        valueDict = cls.scrubValueDict(valueDict)
        pvObject = pva.PvObject(typeDict,valueDict)
        return pvObject

if __name__ == '__main__':
    s = open('t.json').read()
    pvObject = JsonToPvaConverter.toPvObject(s)
    print(pvObject)
    s = pva.PvaServer('jsonObject', pvObject)
    import time
    time.sleep(100)

--
Siniša Veseli
Scientific Software Engineering & Data Management
Advanced Photon Source
Argonne National Laboratory
sveseli at anl.gov
(630)252-9182

From: Tech-talk <tech-talk-bounces at aps.anl.gov> on behalf of Ralph Lange via Tech-talk <tech-talk at aps.anl.gov>
Sent: Wednesday, April 10, 2024 8:27 AM
To: EPICS Tech Talk <tech-talk at aps.anl.gov>
Subject: Publish JSON structure as PVA
 
Dear all,

Does anyone have an application / PVA server that reads a JSON structure (e.g., from file) and publishes it as pvAccess?

Cheers,
~Ralph

Attachment: t.json.gz
Description: t.json.gz

Attachment: jsonToPva.py.gz
Description: jsonToPva.py.gz


Replies:
Re: Publish JSON structure as PVA Ralph Lange via Tech-talk
References:
Publish JSON structure as PVA Ralph Lange via Tech-talk

Navigate by Date:
Prev: Re: urldefense.us URLs? (was: New Git mirror for the SNL-Sequencer (code and docs)) Johnson, Andrew N. via Tech-talk
Next: Re: Publish JSON structure as PVA Ralph Lange via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <20242025 
Navigate by Thread:
Prev: Publish JSON structure as PVA Ralph Lange via Tech-talk
Next: Re: Publish JSON structure as PVA Ralph Lange via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <20242025