84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
#from PyWeather.weather.stations.davis import VantagePro
|
|
import logging
|
|
import mariadb
|
|
import serial.tools.list_ports
|
|
import gc
|
|
import time
|
|
from pprint import pprint
|
|
|
|
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:
|
|
# device.parse()
|
|
data = device.fields
|
|
print(data)
|
|
if len(data) < 1:
|
|
return
|
|
else:
|
|
print(data)
|
|
fields = ['BarTrend', 'CRC', 'DateStamp', 'DewPoint', 'HeatIndex', 'ETDay', 'HeatIndex',
|
|
'HumIn', 'HumOut', 'Pressure', 'RainDay', 'RainMonth', 'RainRate', 'RainStorm',
|
|
'RainYear', 'SunRise', 'SunSet', 'TempIn', 'TempOut', 'WindDir', 'WindSpeed',
|
|
'WindSpeed10Min']
|
|
|
|
if send:
|
|
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()
|
|
else:
|
|
print(data)
|
|
|
|
del data
|
|
del fields
|
|
gc.collect()
|
|
except Exception as e:
|
|
logger.error(str(e))
|
|
raise e
|
|
|
|
|
|
try:
|
|
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))
|
|
raise e
|
|
|
|
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()]
|
|
devices = {}
|
|
|
|
while True:
|
|
for i in range(1):
|
|
if len(devices) != 0:
|
|
print(devices)
|
|
# write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
|
|
else:
|
|
print('bashmak')
|
|
time.sleep(1)
|
|
except Exception as e:
|
|
logger.error('Device_error: ' + str(e))
|
|
raise e
|
|
|