2024-10-08 22:37:24 +04:00
|
|
|
from PyWeather.weather.stations.davis import VantagePro
|
2024-10-08 17:47:23 +04:00
|
|
|
import logging
|
2024-10-08 22:37:24 +04:00
|
|
|
import mariadb
|
2024-10-08 17:47:23 +04:00
|
|
|
import serial.tools.list_ports
|
2024-10-08 22:37:24 +04:00
|
|
|
import gc
|
|
|
|
import time
|
|
|
|
from pprint import pprint
|
2024-10-08 17:47:23 +04:00
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(filename="Stations.log",
|
|
|
|
format='%(asctime)s %(message)s',
|
|
|
|
filemode='a')
|
|
|
|
logger = logging.getLogger('davis_api')
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
def write_data(device, station, send=True):
|
|
|
|
try:
|
2024-10-08 22:37:24 +04:00
|
|
|
#device.parse()
|
2024-10-08 17:47:23 +04:00
|
|
|
data = device.fields
|
2024-10-08 22:37:24 +04:00
|
|
|
print(data)
|
|
|
|
if len(data) < 1:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print(data)
|
2024-10-08 17:47:23 +04:00
|
|
|
fields = ['BarTrend', 'CRC', 'DateStamp', 'DewPoint', 'HeatIndex', 'ETDay', 'HeatIndex',
|
|
|
|
'HumIn', 'HumOut', 'Pressure', 'RainDay', 'RainMonth', 'RainRate', 'RainStorm',
|
|
|
|
'RainYear', 'SunRise', 'SunSet', 'TempIn', 'TempOut', 'WindDir', 'WindSpeed',
|
|
|
|
'WindSpeed10Min']
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
if send:
|
2024-10-08 22:37:24 +04:00
|
|
|
placeholders = ', '.join(['%s'] * len(fields))
|
|
|
|
field_names = ', '.join(fields)
|
|
|
|
sql = f"INSERT INTO weather_data ({field_names}) VALUES ({placeholders})"
|
|
|
|
values = [data[field] for field in fields]
|
|
|
|
cursor.execute(sql, values)
|
|
|
|
conn.commit()
|
2024-10-08 17:47:23 +04:00
|
|
|
else:
|
|
|
|
pprint(data)
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
del data
|
|
|
|
del fields
|
|
|
|
gc.collect()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(str(e))
|
|
|
|
raise e
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
try:
|
2024-10-08 22:37:24 +04:00
|
|
|
conn = mariadb.connect(
|
|
|
|
user="wind",
|
|
|
|
password="wind",
|
|
|
|
host="193.124.203.110",
|
|
|
|
port=3306,
|
|
|
|
database="wind_towers"
|
|
|
|
)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
except mariadb.Error as e:
|
|
|
|
logger.error('DB_ERR: ' + str(e))
|
2024-10-08 17:47:23 +04:00
|
|
|
raise e
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
try:
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
available_ports = {}
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
for port in ports:
|
|
|
|
if port.serial_number == '0001':
|
2024-10-08 22:37:24 +04:00
|
|
|
available_ports[port.name] = port.vid
|
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
devices = [VantagePro(port) for port in available_ports.keys()]
|
2024-10-08 22:37:24 +04:00
|
|
|
print(available_ports)
|
2024-10-08 17:47:23 +04:00
|
|
|
while True:
|
|
|
|
for i in range(len(devices)):
|
2024-10-08 22:37:24 +04:00
|
|
|
print(devices[i].fields)
|
|
|
|
#write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
|
|
|
|
time.sleep(1)
|
2024-10-08 17:47:23 +04:00
|
|
|
except Exception as e:
|
2024-10-08 22:37:24 +04:00
|
|
|
logger.error('Device_error: ' + str(e))
|
2024-10-08 17:47:23 +04:00
|
|
|
raise e
|
|
|
|
|