EvaluationEfficiencyOptimiz.../davisAPI/davisAPI.py.save.1

70 lines
2.4 KiB
Groff
Raw Normal View History

2024-10-08 17:47:23 +04:00
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
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', 'DateStampUTC', 'DewPoint', 'HeatIndex', 'ETDay', 'HeatIndex',
'HumIn', 'HumOut', 'LeafWetness', 'Pressure', 'RainDay', 'RainMonth', 'RainRate', 'RainStorm',
'RainYear', 'SoilMoist', '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))
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))
try:
ports = serial.tools.list_ports.comports()
available_ports = {}
for port in ports:
if port.manufacturer == 'Silicon Labs':
available_ports['/dev/' + port.name] = port.vid
# COM = 'COM8' # '/dev/ttyUSB0'
# COM1 = '/dev/ttyUSB1'
print(ports)
print(available_ports)
device = VantagePro(available_ports
devices = [VantagePro(port) for port in available_ports.keys()]
while True:
for i in range(len(devices)):
write_data(devices[i], list(available_ports.keys())[i], False)
time.sleep(15)
except Exception as e:
logger.error('Device_error' + str(e))