жизнь прекрасна

This commit is contained in:
an.lazarev 2024-10-08 22:37:24 +04:00
parent df7d94de8f
commit eca504de11
40 changed files with 42 additions and 40 deletions

View File

@ -1,13 +1,10 @@
from influxdb_client import InfluxDBClient, Point from PyWeather.weather.stations.davis import VantagePro
from influxdb_client.client.write_api import SYNCHRONOUS
from weather.stations.davis import VantagePro
import gc
from pprint import pprint
import time
import logging import logging
import mariadb
import serial.tools.list_ports import serial.tools.list_ports
import os import gc
import sys import time
from pprint import pprint
logging.basicConfig(filename="Stations.log", logging.basicConfig(filename="Stations.log",
@ -17,61 +14,66 @@ logger = logging.getLogger('davis_api')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
def write_data(device, station, send=True): def write_data(device, station, send=True):
try: try:
device.parse() #device.parse()
data = device.fields data = device.fields
points = [] print(data)
if len(data) < 1:
return
else:
print(data)
fields = ['BarTrend', 'CRC', 'DateStamp', 'DewPoint', 'HeatIndex', 'ETDay', 'HeatIndex', fields = ['BarTrend', 'CRC', 'DateStamp', 'DewPoint', 'HeatIndex', 'ETDay', 'HeatIndex',
'HumIn', 'HumOut', 'Pressure', 'RainDay', 'RainMonth', 'RainRate', 'RainStorm', 'HumIn', 'HumOut', 'Pressure', 'RainDay', 'RainMonth', 'RainRate', 'RainStorm',
'RainYear', 'SunRise', 'SunSet', 'TempIn', 'TempOut', 'WindDir', 'WindSpeed', 'RainYear', 'SunRise', 'SunSet', 'TempIn', 'TempOut', 'WindDir', 'WindSpeed',
'WindSpeed10Min'] 'WindSpeed10Min']
if send: if send:
for field in fields: placeholders = ', '.join(['%s'] * len(fields))
points.append(Point(station).field(field, data[field])) field_names = ', '.join(fields)
write_api.write(bucket=bucket, record=points) sql = f"INSERT INTO weather_data ({field_names}) VALUES ({placeholders})"
values = [data[field] for field in fields]
cursor.execute(sql, values)
conn.commit()
else: else:
pprint(data) pprint(data)
# tables = query_api.query(f'from(bucket:"{bucket}") |> range(start: -1h)')
del data del data
del points
del fields del fields
gc.collect() gc.collect()
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
raise e raise e
# убрать
try: try:
token = "2CEePET3ss2khtjsdGrJap8mVzHhR2dRwuyK3NuFBvDgGtOMSi6Jstsrp2o-OANzD8fxB73PsTyIbqgbnokoXQ==" conn = mariadb.connect(
bucket = 'wind' user="wind",
org = "UlSTU" password="wind",
url = "http://influxdb.athene.tech/" host="193.124.203.110",
client = InfluxDBClient(url=url, token=token, org=org) port=3306,
write_api = client.write_api(write_options=SYNCHRONOUS) database="wind_towers"
query_api = client.query_api() )
except Exception as e: cursor = conn.cursor()
logger.error('DB_ERR:' + str(e)) except mariadb.Error as e:
logger.error('DB_ERR: ' + str(e))
raise e raise e
try: try:
ports = serial.tools.list_ports.comports() ports = serial.tools.list_ports.comports()
available_ports = {} available_ports = {}
for port in ports: for port in ports:
# print(port)
if port.serial_number == '0001': if port.serial_number == '0001':
available_ports['/dev/'+port.name] = port.vid available_ports[port.name] = port.vid
# COM = 'COM8' # '/dev/ttyUSB0'
# COM1 = '/dev/ttyUSB1'
# available_ports = {'/dev/ttyUSB0':'st1'}
devices = [VantagePro(port) for port in available_ports.keys()] devices = [VantagePro(port) for port in available_ports.keys()]
# print(available_ports) print(available_ports)
while True: while True:
for i in range(len(devices)): for i in range(len(devices)):
write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True) print(devices[i].fields)
time.sleep(15) # лучше 60 #write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
time.sleep(1)
except Exception as e: except Exception as e:
logger.error('Device_error' + str(e)) logger.error('Device_error: ' + str(e))
raise e raise e

View File

@ -8,7 +8,7 @@ for port, desc, hwid in sorted(ports):
available_ports = {} available_ports = {}
for port in ports: for port in ports:
print(port.serial_number) print(port.serial_number)
available_ports['/dev/' + port.name] = port.vid available_ports[port.name] = port.vid
devices = [port for port in available_ports.keys()] devices = [port for port in available_ports.keys()]
print(devices) print(devices)
print(ports[1].manufacturer) print(ports[1].manufacturer)

View File

@ -4,6 +4,6 @@ import sys
#filename = sys.argv[1] #filename = sys.argv[1]
filename = "davisAPI.py" filename = "davisAPI.py"
while True: while True:
print('Start'+filename) print('Start '+filename)
p = Popen("python davisAPI\\" + filename, shell=True) p = Popen("python ./" + filename, shell=True)
p.wait() p.wait()