Calling JSON store item in scripting datasource
-
Dear community,
Having not read through documentation I think it is more easier to ask in forums.
Is it possible to call a JSON store item in a scripting datasource and modify it when needed?
Example:
Client modifies some data on front end in a JSON store and then starts a testing procedure. Testing procedure is handled by a scripting data source. When the script sees that the test is complete it should write test results into the JSON store object.
Is this possible with m2m2 functions inside the scripting data source? Right now we have solved it to stop the test and that the client should then click end test button which sets the values but we would like it to be done automatically via scripting datasource.
Maybe someone can point me to the right directions?
Thanks,
Thomas -
Here is some extracts of a scripting data source I did recently minus the logic I did on the JSON.
var dao = com.serotonin.m2m2.db.dao.JsonDataDao.instance; var objectMapper = com.serotonin.m2m2.Common.getBean(com.fasterxml.jackson.databind.ObjectMapper.class, "daoObjectMapper"); var jsonDataVo = dao.getByXid('8b203a82-166a-4963-8dc5-17bba149e28b'); var dataAsString = jsonDataVo.jsonData.toString(); var data = JSON.parse(dataAsString);
You can now edit the data object as you would normally in JS
var newJsonData = objectMapper.readTree(JSON.stringify(data)); //If objectmapper is not used before saving, all objects get saved as an array. jsonDataVo.setJsonData(newJsonData); dao.save(jsonDataVo)
To save back to the JSON store.
-
-
Hi @CraigWeb ,
I was successful in following the suggestion and get access to JSON store in scripting data source. However, I cannot do the same in script area of Event Handler. I get the following error when trying to validate.
java.lang.RuntimeException: java.lang.ClassNotFoundException: com.serotonin.m2m2.Common.getBean
Any ideas?
BR,
Ricardo