Has anyone tried to decode YAML payload in Ignition using scripting ?
The application I am working on has a YAML payload which needs to be parsed to obtain specific values on the tags.
I tried using pyyaml library and it has been not working for me due to its C dependency.
Is there a work around for this issue ?
Example payload:
The 'right' way to do it would be to create a custom module that brought a Java YAML parser into scope - it looks like the two main candidates are Jackson or 'snakeyaml'.
The 'easy' way to do it would be to use the 'hack' described here:
If you drop the snakeyaml .jar file in the right location, the gateway will automatically load it to the classpath on the next restart, which will then make it available from scripting.
I took a stab at it with my limited Java knowledge, I have no idea if this will work but is a starting point
from com.esotericsoftware.yamlbeans import YamlReader as YR
from java.io import FileInputStream
filePath = 'L:\yamlFile.yaml'.toPath()
yaml = FileInputStream(filePath.read())
yamlReader = YR(yaml)
result = yamlReader.read()
print result
Placing the .jar file in the lib/core/common folder worked.
Program executed:
from com.esotericsoftware.yamlbeans import YamlReader as YR
yamlString = system.tag.read("[default]mqtttemplate.value")
yamlReader = YR(str(yamlString))
result =YR.read
I am still working on how to retrieve the data values from each object. When I read the result it just gives a return type.
What should I change to retrieve individual values ?
The “.value” should not be part of your tag path. The result of a tag read is a QualifiedValue object and you need to then pull the value out of it. See how @dkhayes117 has it on the end, outside the parens.
How to retrieve a value out of this data ?
data: ~
level: -5.937274933
press: -23.74909973
temp: 0.0009031557711
For example ---- > How to get level value ?
I was able to correct the syntax issues.
Suggestions on how to retrieve level data?
Is yaml format maintained when stored in a Memory tag ?
result only was able to get the first level of the yaml data
Thoughts ??
Well I guess it returned a string and not a hashmap or dictionary, and since it printed u'data' when you printed it, that tells me it didn’t parse the yaml except for the first line. I suspect that the yaml isn’t formatted correctly. Maybe you should take your “yaml” and paste into an online formatting validator to make sure it follows yaml structure.