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
|