* Initialize a weather provider * @param {object} config The configuration object
(config)
| 26 | * @param {object} config The configuration object |
| 27 | */ |
| 28 | async initWeatherProvider (config) { |
| 29 | const identifier = config.weatherProvider.toLowerCase(); |
| 30 | const instanceId = config.instanceId; |
| 31 | |
| 32 | Log.log(`Attempting to initialize provider ${identifier} for instance ${instanceId}`); |
| 33 | |
| 34 | if (this.providers[instanceId]) { |
| 35 | Log.log(`Weather provider ${identifier} already initialized for instance ${instanceId}, re-sending WEATHER_INITIALIZED`); |
| 36 | // Client may have restarted (e.g. page reload) - re-send so it recovers location name |
| 37 | this.sendSocketNotification("WEATHER_INITIALIZED", { |
| 38 | instanceId, |
| 39 | locationName: this.providers[instanceId].locationName |
| 40 | }); |
| 41 | // Push cached data immediately so reconnecting clients don't wait for next scheduled fetch |
| 42 | if (this.lastData[instanceId]) { |
| 43 | this.sendSocketNotification("WEATHER_DATA", this.lastData[instanceId]); |
| 44 | } |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | // Dynamically load the provider module |
| 50 | const providerPath = path.join(__dirname, "providers", `${identifier}.js`); |
| 51 | Log.log(`Loading provider from: ${providerPath}`); |
| 52 | const ProviderClass = require(providerPath); |
| 53 | |
| 54 | // Create provider instance |
| 55 | const provider = new ProviderClass(config); |
| 56 | |
| 57 | // Set up callbacks before initializing |
| 58 | provider.setCallbacks( |
| 59 | (data) => { |
| 60 | // On data received |
| 61 | const payload = { instanceId, type: config.type, data }; |
| 62 | this.lastData[instanceId] = payload; |
| 63 | this.sendSocketNotification("WEATHER_DATA", payload); |
| 64 | }, |
| 65 | (errorInfo) => { |
| 66 | // On error |
| 67 | this.sendSocketNotification("WEATHER_ERROR", { |
| 68 | instanceId, |
| 69 | error: errorInfo.message || "Unknown error", |
| 70 | translationKey: errorInfo.translationKey |
| 71 | }); |
| 72 | } |
| 73 | ); |
| 74 | |
| 75 | await provider.initialize(); |
| 76 | this.providers[instanceId] = provider; |
| 77 | |
| 78 | this.sendSocketNotification("WEATHER_INITIALIZED", { |
| 79 | instanceId, |
| 80 | locationName: provider.locationName |
| 81 | }); |
| 82 | |
| 83 | // Start periodic fetching |
| 84 | provider.start(); |
| 85 |
nothing calls this directly
no test coverage detected