Spaces:
Sleeping
Sleeping
Commit ·
b598381
1
Parent(s): 37f4cc2
updated logging
Browse files7 unmodified lines891011121321 unmodified lines353637383940414243444 unmodified lines4950515253545556575859607 unmodified linesimport plotly.express as pximport plotly.graph_objects as goimport os# Global variablesdata_points = []21 unmodified linesdf = table.to_pandas().sort_values(by="time")return dfexcept:KeyError("Error with InfluxDB Integration")def on_connect(client, userdata, flags, reason_code, properties=None):print(f"Connected with result code {reason_code}")# Subscribing in on_connect() means that if we lose the connection and# reconnect then subscriptions will be renewed.client.subscribe(MQTT_TOPIC)4 unmodified linestry:payload = message.payload.decode()value = float(payload) # Assume the payload is a single float valuedata_points.append({"timestamp": time.time(), "current": value, "datetime": datetime.now().strftime('%H:%M:%S')})# Keep only the last MAX_DATA_POINTSif len(data_points) > MAX_DATA_POINTS:data_points.pop(0) # Remove the oldest data pointexcept ValueError:print(f"Received invalid payload: {payload}")# Set up MQTT clientclient = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,protocol=mqtt.MQTTv5,transport='websockets')7 unmodified lines8910111213141521 unmodified lines37383940414243444546474 unmodified lines525354555657585960616263647 unmodified linesimport plotly.express as pximport plotly.graph_objects as goimport osimport logginglogger = logging.getLogger(__name__)# Global variablesdata_points = []21 unmodified linesdf = table.to_pandas().sort_values(by="time")return dfexcept:logger.error('InfluxDB Integration Error')KeyError("Error with InfluxDB Integration")def on_connect(client, userdata, flags, reason_code, properties=None):logger.info(f"Connected with result code {reason_code}")# Subscribing in on_connect() means that if we lose the connection and# reconnect then subscriptions will be renewed.client.subscribe(MQTT_TOPIC)4 unmodified linestry:payload = message.payload.decode()value = float(payload) # Assume the payload is a single float valuelogger.info(f'received value: {value}')data_points.append({"timestamp": time.time(), "current": value, "datetime": datetime.now().strftime('%H:%M:%S')})# Keep only the last MAX_DATA_POINTSif len(data_points) > MAX_DATA_POINTS:data_points.pop(0) # Remove the oldest data pointexcept ValueError:logger.error(f"Received invalid payload: {payload}")# Set up MQTT clientclient = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,protocol=mqtt.MQTTv5,transport='websockets')
gradio/app.py+6-2