/ callbacks.py
callbacks.py
  1  import datetime
  2  from dash.dependencies import Input, Output
  3  from data_loader import load_plot
  4  import logging
  5  
  6  logger = logging.getLogger(__name__)
  7  
  8  def register_callbacks(app):
  9      """Register all callbacks with the app"""
 10      
 11      @app.callback(
 12          [
 13              # Graph figures
 14              Output("aare-temp-graph", "figure"),
 15              Output("aare-flow-graph", "figure"),
 16              Output("reuss-temp-graph", "figure"),
 17              Output("reuss-flow-graph", "figure"),
 18              # Latest measurements for display
 19              Output("aare-temp-value", "children"),
 20              Output("aare-flow-value", "children"),
 21              Output("aare-level-value", "children"),
 22              Output("reuss-temp-value", "children"),
 23              Output("reuss-flow-value", "children"),
 24              Output("reuss-level-value", "children"),
 25              # Last updated timestamp
 26              Output("last-updated", "children"),
 27          ],
 28          Input("interval-refresh", "n_intervals"),
 29      )
 30      def update_plots(n):
 31          """Update all plots and measurements"""
 32          try:
 33              # Get temperature plots and latest values for Aare
 34              aare_temp_plot, aare_temp_latest = load_plot(
 35                  station_id="2135", 
 36                  title="Temperatur", 
 37                  plot_type="temperature"
 38              )
 39              
 40              # Get temperature plots and latest values for Reuss
 41              reuss_temp_plot, reuss_temp_latest = load_plot(
 42                  station_id="2152", 
 43                  title="Temperatur", 
 44                  plot_type="temperature"
 45              )
 46  
 47              # Get flow and water level plots and latest values for Aare
 48              aare_flow_plot, aare_flow_latest = load_plot(
 49                  station_id="2135", 
 50                  title="Abfluss und Wasserstand", 
 51                  plot_type="flow"
 52              )
 53  
 54              # Get flow and water level plots and latest values for Reuss
 55              reuss_flow_plot, reuss_flow_latest = load_plot(
 56                  station_id="2152", 
 57                  title="Abfluss und Wasserstand", 
 58                  plot_type="flow"
 59              )
 60  
 61              # Additional layout adjustments to ensure titles are visible
 62              for plot in [aare_temp_plot, reuss_temp_plot, aare_flow_plot, reuss_flow_plot]:
 63                  if "layout" in plot:
 64                      plot["layout"]["margin"] = {"t": 50}  # Add top margin for title
 65                      plot["layout"]["title"]["x"] = 0.5  # Center the title
 66  
 67              # Format latest measurement values for display
 68              current_time = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")
 69  
 70              # Temperature values
 71              aare_temp_display = (
 72                  f"{aare_temp_latest.get('value', 'N/A'):.1f} °C"
 73                  if "value" in aare_temp_latest
 74                  else "N/A"
 75              )
 76              reuss_temp_display = (
 77                  f"{reuss_temp_latest.get('value', 'N/A'):.1f} °C"
 78                  if "value" in reuss_temp_latest
 79                  else "N/A"
 80              )
 81  
 82              # Flow values
 83              aare_flow_display = (
 84                  f"{aare_flow_latest.get('flow', {}).get('value', 'N/A'):.1f} m³/s"
 85                  if "flow" in aare_flow_latest
 86                  else "N/A"
 87              )
 88              reuss_flow_display = (
 89                  f"{reuss_flow_latest.get('flow', {}).get('value', 'N/A'):.1f} m³/s"
 90                  if "flow" in reuss_flow_latest
 91                  else "N/A"
 92              )
 93  
 94              # Water level values
 95              aare_level_display = (
 96                  f"{aare_flow_latest.get('level', {}).get('value', 'N/A'):.2f} m"
 97                  if "level" in aare_flow_latest
 98                  else "N/A"
 99              )
100              reuss_level_display = (
101                  f"{reuss_flow_latest.get('level', {}).get('value', 'N/A'):.2f} m"
102                  if "level" in reuss_flow_latest
103                  else "N/A"
104              )
105  
106              # Last updated message
107              last_updated = f"Letzte Aktualisierung: {current_time}"
108  
109              return (
110                  aare_temp_plot,
111                  aare_flow_plot,
112                  reuss_temp_plot,
113                  reuss_flow_plot,
114                  aare_temp_display,
115                  aare_flow_display,
116                  aare_level_display,
117                  reuss_temp_display,
118                  reuss_flow_display,
119                  reuss_level_display,
120                  last_updated,
121              )
122          
123          except Exception as e:
124              logger.error(f"Error in update_plots callback: {str(e)}")
125              # Return empty values in case of error
126              return (
127                  {}, {}, {}, {},
128                  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A",
129                  f"Fehler beim Laden der Daten: {str(e)}"
130              )