From 2fe6fed0fabe3a293637cfdb4b97641a478a80e7 Mon Sep 17 00:00:00 2001 From: Alberto Abella Date: Thu, 16 May 2024 01:31:28 +0200 Subject: [PATCH] created example of code --- .../code_for_using_dataModel.SDG_Action.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Action/code/code_for_using_dataModel.SDG_Action.py diff --git a/Action/code/code_for_using_dataModel.SDG_Action.py b/Action/code/code_for_using_dataModel.SDG_Action.py new file mode 100644 index 0000000..e77a8d4 --- /dev/null +++ b/Action/code/code_for_using_dataModel.SDG_Action.py @@ -0,0 +1,61 @@ + +# # This code allows you to install a orion-ld broker in a linux system +# # The manuals are here https://github.com/FIWARE/context.Orion-LD/tree/develop/doc/manuals-ld +# +# # INSTALL NGSI LD broker (OrionLD) +# sudo docker pull mongo:3.6 +# sudo docker pull fiware/orion-ld +# sudo docker network create fiware_default +# sudo docker run -d --name=mongo-db --network=fiware_default --expose=27017 mongo:3.6 --bind_ip_all --smallfiles +# sudo docker run -d --name fiware-orionld -h orion --network=fiware_default -p 1026:1026 fiware/orion-ld -dbhost mongo-db +# +# # TO RELAUNCH (only if you have already installed a broker in the same machine) +# sudo docker stop fiware-orionld +# sudo docker rm fiware-orionld +# sudo docker stop mongo-db +# sudo docker rm mongo-db +# sudo docker network rm fiware_default +# +# # CHECK INSTANCES +# # Check the broker is running +# curl -X GET 'http://localhost:1026/version' +# +# # Check what entities are in the broker +# curl -X GET http://localhost:1026/ngsi-ld/v1/entities?local=true&limit=1000 +# +# # now the python code you can use to insert some value in the context broker according to the data model +# +from pysmartdatamodels import pysmartdatamodels as sdm +import subprocess +serverUrl = "http://localhost:1026" # supposed that your broker is installed in localhost. Edit to match your configuration +dataModel = "Action" +subject = "dataModel.SDG" +refProject = "{'type': 'Property', 'value': 'O.E.6.AY1'}" +attribute = "refProject" +value = refProject +# The next line creates the query for inserting this attribute in a NGSI-LD context broker if the attribute does not exist it creates it +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) + +executionPeriod = "{'type': 'Property', 'value': '2021S1'}" +attribute = "executionPeriod" +value = executionPeriod +# The next line creates the query for inserting this attribute in a NGSI-LD context broker if the attribute does not exist it creates it +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) + +compliancePercentage = {'type': 'Property', 'value': 0} +attribute = "compliancePercentage" +value = compliancePercentage +# The next line creates the query for inserting this attribute in a NGSI-LD context broker if the attribute does not exist it creates it +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) + +modifications = "{'type': 'Property', 'value': 'SIN MODIFICACION'}" +attribute = "modifications" +value = modifications +# The next line creates the query for inserting this attribute in a NGSI-LD context broker if the attribute does not exist it creates it +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) + +print(" In case you have installed the orion-ld broker (see comments on the header of this program)") +print(" Execute this instruction to check that the entities has been inserted") +command = ['curl', '-X', 'GET', 'http://localhost:1026/ngsi-ld/v1/entities?local=true&limit=1000'] +result = subprocess.run(command, capture_output=True, text=True) +print(result.stdout)