Created
November 26, 2024 23:22
-
-
Save zoernert/e3ed7140a309aa08bc7b245542b13aff to your computer and use it in GitHub Desktop.
IOMeter POC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require('dotenv').config(); | |
| const axios = require('axios'); | |
| class IOMeterClient { | |
| constructor(token, secret) { | |
| this.endpoint = 'https://provider-api.prod.iometer.cloud/v1/query'; | |
| this.auth = Buffer.from(`${token}:${secret}`).toString('base64'); | |
| // Axios Instance mit vorkonfigurierten Headers | |
| this.client = axios.create({ | |
| baseURL: 'https://provider-api.prod.iometer.cloud/v1', | |
| headers: { | |
| 'Authorization': `Basic ${this.auth}`, | |
| 'Content-Type': 'application/json', | |
| } | |
| }); | |
| } | |
| async query(graphqlQuery) { | |
| try { | |
| const response = await this.client.post('/query', { | |
| query: graphqlQuery | |
| }); | |
| return response.data; | |
| } catch (error) { | |
| if (error.response) { | |
| throw new Error(`API Error: ${error.response.status} - ${error.response.data}`); | |
| } else if (error.request) { | |
| throw new Error('Keine Antwort vom Server erhalten'); | |
| } else { | |
| throw new Error(`Fehler beim Request: ${error.message}`); | |
| } | |
| } | |
| } | |
| async getMeterReadings() { | |
| const query = ` | |
| query Provider { | |
| provider { | |
| installations { | |
| meters { | |
| id | |
| number | |
| latestReadings { | |
| time | |
| obisCode | |
| value | |
| unit | |
| } | |
| } | |
| } | |
| } | |
| } | |
| `; | |
| const result = await this.query(query); | |
| // Strukturierte Aufbereitung der Zählerstände | |
| const readings = []; | |
| const installations = result.data.provider.installations; | |
| for (const installation of installations) { | |
| for (const meter of installation.meters) { | |
| for (const reading of meter.latestReadings) { | |
| readings.push({ | |
| meterId: meter.id, | |
| meterNumber: meter.number, | |
| time: reading.time, | |
| obisCode: reading.obisCode, | |
| value: reading.value, | |
| unit: reading.unit | |
| }); | |
| } | |
| } | |
| } | |
| return readings; | |
| } | |
| } | |
| // Beispielverwendung: | |
| async function main() { | |
| const client = new IOMeterClient( | |
| process.env.IOMETER_TOKEN, | |
| process.env.IOMETER_SECRET | |
| ); | |
| try { | |
| const readings = await client.getMeterReadings(); | |
| console.log('Zählerstände:', JSON.stringify(readings, null, 2)); | |
| } catch (error) { | |
| console.error('Fehler beim Abrufen der Zählerstände:', error); | |
| } | |
| } | |
| // Ausführen der Hauptfunktion | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment