weather-pydash/data.py

76 lines
2.9 KiB
Python
Raw Normal View History

import requests
import ast
from datetime import datetime
from time import sleep
import pickle
2025-01-14 14:50:58 -07:00
import json
import math
2025-01-11 14:42:38 -07:00
import numpy as np
def convert_float(value):
2025-01-11 14:42:38 -07:00
try:
return float(value)
except:
2025-01-14 14:50:58 -07:00
return None
def convert_int(value):
try:
return int(value)
except:
return None
sensors_url = "http://192.168.1.221/get_livedata_info"
sensors_response = requests.get(sensors_url)
sensors_dict = ast.literal_eval(sensors_response.content.decode('utf-8'))
den_dict = sensors_dict['wh25'][0]
outdoor_dict = sensors_dict['ch_aisle'][0]
lroom_dict = sensors_dict['ch_aisle'][1]
roffice_dict = sensors_dict['ch_aisle'][2]
toffice_dict = sensors_dict['ch_aisle'][3]
broom_dict = sensors_dict['ch_aisle'][4]
lightning_dict = sensors_dict['lightning'][0]
try:
2025-01-14 14:50:58 -07:00
with open('/home/tonydero/projects/weather-pydash/output/saved_sensors_dict.pkl', 'rb') as f:
sensors_dict = pickle.load(f)
except:
sensors_dict = {}
datum_dict = {}
datum_dict['den_temp'] = convert_float(den_dict['intemp'])
datum_dict['den_humi'] = convert_float(den_dict['inhumi'].strip("%"))/100
datum_dict['den_pabs'] = convert_float(den_dict['abs'].strip(" inHg"))
datum_dict['den_prel'] = convert_float(den_dict['rel'].strip(" inHg"))
datum_dict['lroom_temp'] = convert_float(lroom_dict['temp'])
datum_dict['lroom_humi'] = convert_float(lroom_dict['humidity'].strip("%"))/100
2025-01-14 14:50:58 -07:00
datum_dict['lroom_batt'] = convert_int(lroom_dict['battery'])
datum_dict['roffice_temp'] = convert_float(roffice_dict['temp'])
datum_dict['roffice_humi'] = convert_float(roffice_dict['humidity'].strip("%"))/100
2025-01-14 14:50:58 -07:00
datum_dict['roffice_batt'] = convert_int(roffice_dict['battery'])
datum_dict['toffice_temp'] = convert_float(toffice_dict['temp'])
datum_dict['toffice_humi'] = convert_float(toffice_dict['humidity'].strip("%"))/100
2025-01-14 14:50:58 -07:00
datum_dict['toffice_batt'] = convert_int(toffice_dict['battery'])
datum_dict['broom_temp'] = convert_float(broom_dict['temp'])
datum_dict['broom_humi'] = convert_float(broom_dict['humidity'].strip("%"))/100
2025-01-14 14:50:58 -07:00
datum_dict['broom_batt'] = convert_int(broom_dict['battery'])
datum_dict['outdoor_temp'] = convert_float(outdoor_dict['temp'])
datum_dict['outdoor_humi'] = convert_float(outdoor_dict['humidity'].strip("%"))/100
2025-01-14 14:50:58 -07:00
datum_dict['outdoor_batt'] = convert_int(outdoor_dict['battery'])
datum_dict['lghtng_dist'] = convert_float(lightning_dict['distance'])
datum_dict['lghtng_date'] = lightning_dict['date']
datum_dict['lghtng_time'] = lightning_dict['timestamp']
datum_dict['lghtng_scnt'] = convert_float(lightning_dict['count'])
2025-01-14 14:50:58 -07:00
datum_dict['lghtng_batt'] = convert_int(lightning_dict['battery'])
2025-01-14 14:50:58 -07:00
sensors_dict[datetime.now()] = datum_dict
2025-01-14 14:50:58 -07:00
with open('/home/tonydero/projects/weather-pydash/output/saved_sensors_dict.pkl', 'wb') as f:
pickle.dump(sensors_dict, f)
2025-01-14 14:50:58 -07:00
with open('/home/tonydero/projects/weather-pydash/output/saved_sensors_dict.json', 'w', encoding='utf-8') as f:
json.dump(sensors_dict, f, ensure_ascii=False, indent=4)