жизнь прекрасна
This commit is contained in:
parent
df7d94de8f
commit
eca504de11
@ -1,13 +1,10 @@
|
||||
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
|
||||
from PyWeather.weather.stations.davis import VantagePro
|
||||
import logging
|
||||
import mariadb
|
||||
import serial.tools.list_ports
|
||||
import os
|
||||
import sys
|
||||
import gc
|
||||
import time
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
logging.basicConfig(filename="Stations.log",
|
||||
@ -17,61 +14,66 @@ logger = logging.getLogger('davis_api')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
|
||||
def write_data(device, station, send=True):
|
||||
try:
|
||||
device.parse()
|
||||
#device.parse()
|
||||
data = device.fields
|
||||
points = []
|
||||
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:
|
||||
for field in fields:
|
||||
points.append(Point(station).field(field, data[field]))
|
||||
write_api.write(bucket=bucket, record=points)
|
||||
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:
|
||||
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))
|
||||
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:
|
||||
# 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'}
|
||||
available_ports[port.name] = port.vid
|
||||
|
||||
devices = [VantagePro(port) for port in available_ports.keys()]
|
||||
# print(available_ports)
|
||||
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
|
||||
print(devices[i].fields)
|
||||
#write_data(devices[i], 'st' + str(available_ports[list(available_ports.keys())[i]]), True)
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
logger.error('Device_error' + str(e))
|
||||
logger.error('Device_error: ' + str(e))
|
||||
raise e
|
||||
|
||||
|
@ -8,7 +8,7 @@ for port, desc, hwid in sorted(ports):
|
||||
available_ports = {}
|
||||
for port in ports:
|
||||
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()]
|
||||
print(devices)
|
||||
print(ports[1].manufacturer)
|
||||
|
@ -4,6 +4,6 @@ import sys
|
||||
#filename = sys.argv[1]
|
||||
filename = "davisAPI.py"
|
||||
while True:
|
||||
print('Start'+filename)
|
||||
p = Popen("python davisAPI\\" + filename, shell=True)
|
||||
print('Start '+filename)
|
||||
p = Popen("python ./" + filename, shell=True)
|
||||
p.wait()
|
||||
|
Loading…
Reference in New Issue
Block a user