EvaluationEfficiencyOptimiz.../davisAPI/davisAPI.py
2024-10-08 17:47:23 +04:00

78 lines
2.5 KiB
Python

from influxdb_client import InfluxDBClient, Point
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 serial.tools.list_ports
import os
import sys
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
points = []
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:
for field in fields:
points.append(Point(station).field(field, data[field]))
write_api.write(bucket=bucket, record=points)
else:
pprint(data)
# tables = query_api.query(f'from(bucket:"{bucket}") |> range(start: -1h)')
del data
del points
del fields
gc.collect()
except Exception as e:
logger.error(str(e))
raise e
# убрать
try:
token = "2CEePET3ss2khtjsdGrJap8mVzHhR2dRwuyK3NuFBvDgGtOMSi6Jstsrp2o-OANzD8fxB73PsTyIbqgbnokoXQ=="
bucket = 'wind'
org = "UlSTU"
url = "http://influxdb.athene.tech/"
client = InfluxDBClient(url=url, token=token, org=org)
write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()
except Exception as e:
logger.error('DB_ERR:' + str(e))
raise e
try:
ports = serial.tools.list_ports.comports()
available_ports = {}
for port in ports:
# print(port)
if port.serial_number == '0001':
available_ports['/dev/'+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()]
# print(available_ports)
while True:
for i in range(len(devices)):
write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
time.sleep(15) # лучше 60
except Exception as e:
logger.error('Device_error' + str(e))
raise e