const fs = require("fs"); const path = require("path"); const { applyEnvOverrides } = require("../config"); const { loadEnv } = require("../env"); const DEFAULT_WEATHER_CONFIG = { timezone: "Europe/Paris", defaultHour: 12, defaultMinute: 0, windowMinutes: 60, precipitationThreshold: 0.1, providers: { influxdb: { windowMinutes: 60, }, openMeteo: { timezone: null, pressureOffset: 0, illuminanceToLuxFactor: 126.7, }, }, }; function loadWeatherConfig(configPath = path.resolve(__dirname, "..", "..", "config.json")) { loadEnv(); let raw = {}; if (fs.existsSync(configPath)) { try { raw = JSON.parse(fs.readFileSync(configPath, "utf8")); } catch (error) { console.error(`Unable to read weather config at ${configPath}: ${error.message}`); } } const withEnv = applyEnvOverrides(raw); const weather = withEnv.weather || {}; const providers = { ...DEFAULT_WEATHER_CONFIG.providers, ...(weather.providers || {}), }; providers.influxdb = { ...DEFAULT_WEATHER_CONFIG.providers.influxdb, ...(weather.providers?.influxdb || {}), }; providers.openMeteo = { ...DEFAULT_WEATHER_CONFIG.providers.openMeteo, timezone: weather.providers?.openMeteo?.timezone || weather.timezone || DEFAULT_WEATHER_CONFIG.timezone, ...(weather.providers?.openMeteo || {}), }; return { ...DEFAULT_WEATHER_CONFIG, ...weather, providers, }; } module.exports = { DEFAULT_WEATHER_CONFIG, loadWeatherConfig, };