Class: DWDSensor

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_chat/tools/weather/dwd_sensor.rb

Overview

A sensor implementation that fetches real-time temperature data from the German Weather Service (DWD). This sensor connects to DWD’s open data API to retrieve the latest air temperature measurements for a specified weather station.

The sensor expects DWD data files in ZIP format containing CSV data with the following structure:

  • Files are named like: 10minutenwerte_TU_00433_now.zip

  • Data is stored in CSV format with semicolon separators

  • Temperature values are in the TT_10 column (in Celsius)

  • Timestamps are in MESS_DATUM column (format: YYYYMMDDHHMM)

Example usage:

sensor = DWDSensor.new(sensor_id: 'station_1', station_id: '00433')
temperature = sensor.measure

Constant Summary collapse

DEFAULT_URL_TEMPLATE =

Template for the download URL

"https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/10_minutes/air_temperature/now/10minutenwerte_TU_%{station_id}_now.zip"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sensor_id:, station_id:, url_template: DEFAULT_URL_TEMPLATE, logger: Logger.new($stderr)) ⇒ DWDSensor

Initializes a new sensor reader instance with the specified parameters.

Parameters:

  • sensor_id (String)

    the unique identifier for the sensor

  • station_id (String)

    the unique identifier for the weather station

  • url_template (String) (defaults to: DEFAULT_URL_TEMPLATE)

    the URL template for fetching sensor data

  • logger (Logger) (defaults to: Logger.new($stderr))

    the logger instance to use for logging messages



29
30
31
32
33
34
35
# File 'lib/ollama_chat/tools/weather/dwd_sensor.rb', line 29

def initialize(sensor_id:, station_id:, url_template: DEFAULT_URL_TEMPLATE, logger: Logger.new($stderr))
  @sensor_id     = sensor_id
  @station_id    = station_id
  @url_template  = url_template
  @last_modified = nil
  @logger        = logger
end

Instance Attribute Details

#sensor_idObject (readonly)

The sensor_id method provides read-only access to the identifier of the sensor.

instance



42
43
44
# File 'lib/ollama_chat/tools/weather/dwd_sensor.rb', line 42

def sensor_id
  @sensor_id
end

Instance Method Details

#measureArray<Time, Float>?

The measure method reads temperature data from a sensor and returns timestamped measurements.

Returns:

  • (Array<Time, Float>, nil)

    an array containing the timestamp and temperature reading, or nil if no valid temperature could be read

  • (nil)

    if no temperature reading was available



50
51
52
53
54
55
56
57
58
59
# File 'lib/ollama_chat/tools/weather/dwd_sensor.rb', line 50

def measure
  @logger.info "Starting to read temperature from #{self.class} #{@sensor_id}"
  time, temp = read_temperature
  if time && temp
    @logger.info "Read temperature from #{self.class} #{@sensor_id} at #{time.iso8601}: #{temp}"
    [ time, temp ]
  else
    nil
  end
end