dot na zanikach
This commit is contained in:
41
app.py
41
app.py
@@ -1,22 +1,20 @@
|
||||
import os
|
||||
import warnings
|
||||
|
||||
os.environ['EVENTLET_NO_GREENDNS'] = 'yes'
|
||||
import eventlet
|
||||
eventlet.monkey_patch()
|
||||
eventlet.monkey_patch(all=True)
|
||||
|
||||
from flask import Flask, render_template, jsonify, request
|
||||
from flask_socketio import SocketIO
|
||||
from influxdb import InfluxDBClient
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
import config
|
||||
import os
|
||||
|
||||
warnings.filterwarnings("ignore", category=DeprecationWarning, module="eventlet")
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = config.FLASK_CONFIG['secret_key']
|
||||
socketio = SocketIO(app,
|
||||
cors_allowed_origins="*",
|
||||
async_mode='eventlet',
|
||||
ping_timeout=60,
|
||||
ping_interval=25)
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
|
||||
|
||||
def get_influx_client():
|
||||
client = InfluxDBClient(
|
||||
@@ -35,10 +33,10 @@ def get_current_voltage(phase_id):
|
||||
try:
|
||||
result = client.query(query)
|
||||
points = list(result.get_points())
|
||||
if points:
|
||||
if points and points[0].get('value') is not None:
|
||||
return {'voltage': round(float(points[0]['value']), 2), 'timestamp': points[0]['time']}
|
||||
except Exception as e:
|
||||
print(f"Influx Error Phase {phase_id}: {e}")
|
||||
print(f"Current Error: {e}")
|
||||
finally:
|
||||
client.close()
|
||||
return {'voltage': 0, 'timestamp': None}
|
||||
@@ -57,7 +55,13 @@ def api_timeseries(phase_id):
|
||||
query = config.PHASES[phase_id]['query'].replace('$timeFilter', cfg['filter']).replace('$__interval', cfg['interval'])
|
||||
try:
|
||||
result = client.query(query)
|
||||
data = [{'time': p['time'], 'voltage': round(p['mean'], 2)} for p in result.get_points() if p.get('mean') is not None]
|
||||
data = []
|
||||
for p in result.get_points():
|
||||
val = p.get('voltage') or p.get('min') or p.get('mean') or p.get('value')
|
||||
if val is not None:
|
||||
data.append({'time': p['time'], 'voltage': round(float(val), 2)})
|
||||
else:
|
||||
data.append({'time': p['time'], 'voltage': 0})
|
||||
return jsonify(data)
|
||||
except Exception as e:
|
||||
print(f"History Error: {e}")
|
||||
@@ -66,19 +70,16 @@ def api_timeseries(phase_id):
|
||||
client.close()
|
||||
|
||||
def background_voltage_update():
|
||||
print("Background worker started...")
|
||||
while True:
|
||||
try:
|
||||
voltages = {'timestamp': None}
|
||||
for pid in config.PHASES.keys():
|
||||
data = get_current_voltage(pid)
|
||||
voltages[f'phase{pid}'] = data['voltage']
|
||||
if data['timestamp']: voltages['timestamp'] = data['timestamp']
|
||||
|
||||
res = get_current_voltage(pid)
|
||||
voltages[f'phase{pid}'] = res['voltage']
|
||||
if res['timestamp']: voltages['timestamp'] = res['timestamp']
|
||||
socketio.emit('voltage_update', voltages)
|
||||
except Exception as e:
|
||||
print(f"Worker Loop Error: {e}")
|
||||
|
||||
print(f"Worker Error: {e}")
|
||||
eventlet.sleep(config.CHART_CONFIG['update_interval'] / 1000)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user