Skip to content

Commit 7452b88

Browse files
authored
Requests to luftdaten should be in try except
Requests to luftdaten should be in try except block. If script runs in background and and network (e.g. DNS) error occured, the script stops running.
1 parent 3e4b64c commit 7452b88

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

examples/luftdaten.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,39 @@ def send_to_luftdaten(values, id):
118118
pm_values_json = [{"value_type": key, "value": val} for key, val in pm_values.items()]
119119
temp_values_json = [{"value_type": key, "value": val} for key, val in temp_values.items()]
120120

121-
resp_1 = requests.post(
122-
"https://api.luftdaten.info/v1/push-sensor-data/",
123-
json={
124-
"software_version": "enviro-plus 0.0.1",
125-
"sensordatavalues": pm_values_json
126-
},
127-
headers={
128-
"X-PIN": "1",
129-
"X-Sensor": id,
130-
"Content-Type": "application/json",
131-
"cache-control": "no-cache"
132-
}
133-
)
134-
135-
resp_2 = requests.post(
136-
"https://api.luftdaten.info/v1/push-sensor-data/",
137-
json={
138-
"software_version": "enviro-plus 0.0.1",
139-
"sensordatavalues": temp_values_json
140-
},
141-
headers={
142-
"X-PIN": "11",
143-
"X-Sensor": id,
144-
"Content-Type": "application/json",
145-
"cache-control": "no-cache"
146-
}
147-
)
121+
try:
122+
resp_1 = requests.post(
123+
"https://api.luftdaten.info/v1/push-sensor-data/",
124+
json={
125+
"software_version": "enviro-plus 0.0.1",
126+
"sensordatavalues": pm_values_json
127+
},
128+
headers={
129+
"X-PIN": "1",
130+
"X-Sensor": id,
131+
"Content-Type": "application/json",
132+
"cache-control": "no-cache"
133+
}
134+
)
135+
except:
136+
pass
137+
138+
try:
139+
resp_2 = requests.post(
140+
"https://api.luftdaten.info/v1/push-sensor-data/",
141+
json={
142+
"software_version": "enviro-plus 0.0.1",
143+
"sensordatavalues": temp_values_json
144+
},
145+
headers={
146+
"X-PIN": "11",
147+
"X-Sensor": id,
148+
"Content-Type": "application/json",
149+
"cache-control": "no-cache"
150+
}
151+
)
152+
except:
153+
pass
148154

149155
if resp_1.ok and resp_2.ok:
150156
return True

0 commit comments

Comments
 (0)