2024-11-05 03:05:17 +04:00
|
|
|
import gc
|
2024-10-08 17:47:23 +04:00
|
|
|
import logging
|
2024-11-05 03:05:17 +04:00
|
|
|
import time
|
2024-11-05 21:57:32 +04:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from pprint import pprint
|
2024-10-08 22:37:24 +04:00
|
|
|
import mariadb
|
2024-10-08 17:47:23 +04:00
|
|
|
import serial.tools.list_ports
|
2024-11-05 03:05:17 +04:00
|
|
|
|
2024-11-05 21:57:32 +04:00
|
|
|
#from PyWeather.weather.stations.davis import VantagePro
|
2024-11-05 03:05:17 +04:00
|
|
|
from prediction import run_prediction_module
|
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)
|
|
|
|
|
2024-11-05 03:05:17 +04:00
|
|
|
console_handler = logging.StreamHandler()
|
|
|
|
console_handler.setLevel(logging.DEBUG)
|
|
|
|
console_handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
|
|
|
|
logger.addHandler(console_handler)
|
|
|
|
|
2024-10-08 17:47:23 +04:00
|
|
|
|
|
|
|
def write_data(device, station, send=True):
|
|
|
|
try:
|
2024-11-05 00:12:15 +04:00
|
|
|
# device.parse()
|
2024-10-08 17:47:23 +04:00
|
|
|
data = device.fields
|
2024-11-05 03:05:17 +04:00
|
|
|
logger.info(data)
|
2024-10-08 22:37:24 +04:00
|
|
|
if len(data) < 1:
|
|
|
|
return
|
|
|
|
else:
|
2024-11-05 03:05:17 +04:00
|
|
|
logger.info(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:
|
2024-11-05 03:05:17 +04:00
|
|
|
logger.info(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-11-05 00:12:15 +04:00
|
|
|
|
2024-11-05 03:05:17 +04:00
|
|
|
def get_previous_values(cursor):
|
2024-11-05 21:57:32 +04:00
|
|
|
cursor.execute("SELECT SunRise, SunSet, WindDir, DateStamp FROM weather_data ORDER BY DateStamp DESC LIMIT 1")
|
2024-11-05 03:05:17 +04:00
|
|
|
result = cursor.fetchone()
|
|
|
|
|
|
|
|
if result is None:
|
2024-11-05 21:57:32 +04:00
|
|
|
return None, None, None, None
|
2024-11-05 03:05:17 +04:00
|
|
|
|
2024-11-05 21:57:32 +04:00
|
|
|
sun_rise, sun_set, wind_dir, datestamp = result
|
|
|
|
return sun_rise, sun_set, wind_dir, datestamp
|
2024-11-05 03:05:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
def save_prediction_to_db(predictions):
|
|
|
|
try:
|
|
|
|
|
2024-11-05 21:57:32 +04:00
|
|
|
sun_rise, sun_set, wind_dir, datestamp = get_previous_values(cursor)
|
2024-11-05 03:05:17 +04:00
|
|
|
|
|
|
|
fields = ['DateStamp', 'SunRise', 'SunSet', 'WindDir'] + list(predictions.keys())
|
|
|
|
placeholders = ', '.join(['%s'] * len(fields))
|
|
|
|
field_names = ', '.join(fields)
|
|
|
|
|
2024-11-05 21:57:32 +04:00
|
|
|
values = [datestamp + timedelta(minutes = 1), sun_rise, sun_set, wind_dir] + list(predictions.values())
|
|
|
|
pprint(dict(zip(fields, values)))
|
2024-11-05 03:05:17 +04:00
|
|
|
sql = f"INSERT INTO weather_data ({field_names}) VALUES ({placeholders})"
|
|
|
|
# cursor.execute(sql, values)
|
|
|
|
# conn.commit()
|
|
|
|
logger.info("Save prediction to db success!")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(str(e))
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
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-11-05 21:57:32 +04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
available_ports = {}
|
|
|
|
|
|
|
|
for port in ports:
|
|
|
|
if port.serial_number == '0001':
|
|
|
|
available_ports[port.name] = port.vid
|
|
|
|
|
|
|
|
devices = [VantagePro(port) for port in available_ports.keys()]
|
|
|
|
while True:
|
|
|
|
for i in range(1):
|
|
|
|
if len(devices) != 0:
|
|
|
|
logger.info(devices)
|
|
|
|
# write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
|
|
|
|
else:
|
|
|
|
raise Exception('Can`t connect to device')
|
|
|
|
time.sleep(60)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error('Device_error' + str(e))
|
|
|
|
predictions = run_prediction_module()
|
|
|
|
#logger.info(predictions)
|
|
|
|
if predictions is not None:
|
|
|
|
save_prediction_to_db(predictions)
|
|
|
|
time.sleep(60)
|
2024-10-08 22:37:24 +04:00
|
|
|
|
2024-11-05 21:57:32 +04:00
|
|
|
|
|
|
|
#todo переписать под influx, для линухи приколы сделать
|