Hello,
in a scheduled event script, i try to connect via an API with system.net.httpGet().
When i try to connect, suddenly the designer i can't connect to the gateway anymore. I have "Gateway connection lost - post error, error code = 400"
Below the script i try to run.
Basycally i want to connect to an API and then searching the intersection between rows and column, copy the respective value, and writing it into a tag.
Appreciate if anybody could help.
from datetime import datetime, timedelta
# API URL
url = "http://abc"
# Dictionary of columns and corresponding tags
colonne_tag = {
"column_name": "xxx/Consumi/kWh_60"
}
# Request data from API
response = system.net.httpGet(url)
if response and response.strip():
# Get the current time minus two hours
ora_target = (datetime.now() - timedelta(hours=2)).strftime("%d-%m-%Y;%H:00")
# Split data into rows using CRLF
righe = response.split("\r\n")
# Check if there is data and at least a header row
if len(righe) >= 2:
# Get the header and map columns
intestazione = righe[0].split("\t")
colonne_mappa = {colonna: idx for idx, colonna in enumerate(intestazione) if colonna in colonne_tag}
# Search for the row with the target date and time
for riga in righe[1:]:
# Ignore rows containing "!"
if "!" in riga:
continue
colonne = riga.split("\t")
if len(colonne) == 22 and colonne[0] + ";" + colonne[1] == ora_target:
for colonna, indice in colonne_mappa.items():
try:
valore_colonna = colonne[indice].replace(",", ".") # Convert value to standard numeric format
valore_float = float(valore_colonna)
# Write the value to the tag with the specific timestamp
system.tag.storeTagHistory("prov:Local_SQL", [colonne_tag[colonna]], [datetime.now() - timedelta(hours=2)], [valore_float])
except (ValueError, IndexError):
continue # Ignore conversion or index errors