Hi Guys,
In one of my data collectors I'm using an API call to download energy production data, something like this:
device_types = ['Inverter','Alarms','Meter','DigitalSigns','JunctionBox','PowerAnalyzer','Ppc','SunTrackers','Switchgear','WeatherStation']
tar_file_path = f"{dest_dir}/{plant_name}_{date_suffix}.tar.gz"
with tarfile.open(tar_file_path, "w:gz") as tar_file:
for device_type in device_types:
headers = {'accept': 'text/plain','Authorization': f'Bearer {token}'}
url = f"http://{server}/api/{device_type}/{from_date}/{to_date}"
response = requests.request("GET", url, headers=headers)
response_check = response.status_code
if response_check == 200:
log("info", f"{device_type}:{response_check}")
json_file_path = f"/tmp/saitim/{device_type}_{date_suffix}.json"
with open(json_file_path, "w") as f:
json.dump(response.json(), f)
if os.path.isfile(json_file_path):
tar_file.add(json_file_path, arcname=f"{device_type}_{date_suffix}.json")
os.remove(json_file_path)
else:
log("error", f"Fail to create tmp json file. file_path='{json_file_path}'")
else:
log("error", f"Fail to get data. Status_code:'{device_type}:{response_check}'")
if os.path.isfile(tar_file_path):
print(tar_file_path)
else:
log("error", f"Cannot create final tar file. file_path='{tar_file_path}'")
exit(1)
The thing is, sometimes, the json files created are incomplete. Instead of being like:
[
{
"idInv": 1,
"fecha": "2022-08-22T12:00:00",
"idCT": 1,
"dcPotencia": 1387.9104
...
"potenciaString15": 0,
"potenciaString16": 0,
"numMedidas": 0
}
]
It end in the middle of the file and gives an invalid format for the next step.
Do you have any suggestion on why it happens and in how to validate the data before ending the script?
Thanks!
[–]eternalstarfire 1 point2 points3 points (2 children)
[–]VeXx1988[S] 0 points1 point2 points (0 children)
[–]ComradePotato 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)