added some error checking if pm2.5 value read out fails on esp8266

This commit is contained in:
simon 2021-02-19 10:10:28 +07:00
parent 383e09f69d
commit 9fb0846bc4
2 changed files with 19 additions and 10 deletions

View File

@ -8,6 +8,8 @@ def input_process(data):
parsing aqi post data and combine it with weather data
return: dict of combined values
"""
# error check
error_found = False
# get weather data
try:
with open('dyn/weather.json', 'r') as f:
@ -24,6 +26,9 @@ def input_process(data):
aqi, aqi_category = get_AQI(pm25)
json_dict['aqi_value'] = float(aqi)
json_dict['aqi_category'] = aqi_category
if pm25 == 0:
# something went wrong
error_found = True
# set timestamp
now = datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
@ -32,7 +37,7 @@ def input_process(data):
json_dict['epoch_time'] = epoch_time
# combine the two and return
json_dict.update(weather_data_json)
return json_dict
return json_dict, error_found
def get_AQI(pm25):

View File

@ -66,15 +66,19 @@ def ingest():
data = request.json
if data:
# populate data dict
json_dict = aqi_parser.input_process(data)
# save to db
time_stamp = db_insert.db_connect(config, json_dict)
print(f'db insert done at {time_stamp}')
# save to webserver
data = json.dumps(json_dict)
with open('dyn/air.json', 'w') as f:
f.write(data)
print(data)
json_dict, error_found = aqi_parser.input_process(data)
if error_found:
print('pm25 read failed')
print(json_dict)
else:
# save to db
time_stamp = db_insert.db_connect(config, json_dict)
print(f'db insert done at {time_stamp}')
# save to webserver
data = json.dumps(json_dict)
with open('dyn/air.json', 'w') as f:
f.write(data)
print(data)
return 'ingest'