|
| 1 | +# This code allows you to install a orion-ld broker in a linux system |
| 2 | +# The manuals are here https://github.com/FIWARE/context.Orion-LD/tree/develop/doc/manuals-ld |
| 3 | +# |
| 4 | +# INSTALL NGSI LD broker (OrionLD) |
| 5 | +# sudo docker pull mongo:3.6 |
| 6 | +# sudo docker pull fiware/orion-ld |
| 7 | +# sudo docker network create fiware_default |
| 8 | +# sudo docker run -d --name=mongo-db --network=fiware_default --expose=27017 mongo:3.6 --bind_ip_all --smallfiles |
| 9 | +# sudo docker run -d --name fiware-orionld -h orion --network=fiware_default -p 1026:1026 fiware/orion-ld -dbhost mongo-db |
| 10 | +# |
| 11 | +# TO RELAUNCH (only if you have already installed a broker in the same machine) |
| 12 | +# sudo docker stop fiware-orionld |
| 13 | +# sudo docker rm fiware-orionld |
| 14 | +# sudo docker stop mongo-db |
| 15 | +# sudo docker rm mongo-db |
| 16 | +# sudo docker network rm fiware_default |
| 17 | +# |
| 18 | +# CHECK INSTANCES |
| 19 | +# curl -X GET 'http://localhost:1026/version' |
| 20 | +# curl -X GET http://localhost:1026/ngsi-ld/v1/entities?local=true&limit=1000 |
| 21 | +# |
| 22 | +# now the python code you can use to insert some value in the context broker |
| 23 | + |
| 24 | +from pysmartdatamodels import pysmartdatamodels as sdm |
| 25 | +import subprocess |
| 26 | +import requests |
| 27 | +import sys |
| 28 | + |
| 29 | +print("Searching broker...") |
| 30 | +serverUrl = None |
| 31 | +for _url in ["http://localhost:1026", "https://localhost:1026"]: |
| 32 | + try: |
| 33 | + if requests.get(f"{_url}/version", timeout=5).status_code == 200: |
| 34 | + serverUrl = _url |
| 35 | + print(f"Broker found at {serverUrl}") |
| 36 | + break |
| 37 | + except Exception: |
| 38 | + pass |
| 39 | +if serverUrl is None: |
| 40 | + serverUrl = input( |
| 41 | + "No broker found at default locations. Enter broker URL (or press Enter to abort): " |
| 42 | + ).strip() |
| 43 | + if serverUrl: |
| 44 | + try: |
| 45 | + if requests.get(f"{serverUrl}/version", timeout=5).status_code != 200: |
| 46 | + print("WARNING: Cannot connect to broker. Script cannot provide results.") |
| 47 | + sys.exit(1) |
| 48 | + except Exception: |
| 49 | + print("WARNING: Cannot connect to broker. Script cannot provide results.") |
| 50 | + sys.exit(1) |
| 51 | + else: |
| 52 | + print("WARNING: No broker URL provided. Script cannot provide results.") |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | +dataModel = "Electrolyzer" |
| 56 | +subject = "dataModel.EnergyStorage" |
| 57 | + |
| 58 | +status = "ON" |
| 59 | +attribute = "status" |
| 60 | +value = status |
| 61 | +# Updates the attribute in the broker; creates the entity if it does not exist |
| 62 | +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) |
| 63 | + |
| 64 | +maxPowerConsumed = 3000 |
| 65 | +attribute = "maxPowerConsumed" |
| 66 | +value = maxPowerConsumed |
| 67 | +# Updates the attribute in the broker; creates the entity if it does not exist |
| 68 | +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) |
| 69 | + |
| 70 | +maxHydrogenFlowGenerated = 600 |
| 71 | +attribute = "maxHydrogenFlowGenerated" |
| 72 | +value = maxHydrogenFlowGenerated |
| 73 | +# Updates the attribute in the broker; creates the entity if it does not exist |
| 74 | +print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUrl, updateThenCreate=True)) |
| 75 | + |
| 76 | +print("In case you have installed the orion-ld broker (see header comments)") |
| 77 | +print("Execute this to check the entity was inserted:") |
| 78 | +command = ['curl', '-X', 'GET', 'http://localhost:1026/ngsi-ld/v1/entities?local=true&limit=1000'] |
| 79 | +result = subprocess.run(command, capture_output=True, text=True) |
| 80 | +print(result.stdout) |
0 commit comments