Obsługa API ebok PGNiG

Odczyt danych z konta ebok PGNiG - stan licznika, faktury itp. Aby pobrać dane przez API należy wpisać swoje dane logowania do node Prepare Login Data

[{"id":"b59e4bb0528d4a39","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"2077be37fcb53e45","type":"group","z":"b59e4bb0528d4a39","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["2747395beb3700a1","7b964f16b1c1bcc3","a4318bd8d15d76f8","03c9a2b7f06d1158","d3d17709c3b33579","35a3701407ec2017","212d40e9317d27ca","828cc8672038c4d1","fb84ca564c6e3613","05ffb7dc1396ea68","1c7e66e8fedb6aa6","1f1be005bef632df","8653f4589bd878bc","93c71c9627efb81f","cd3371eb42164fa7","900d551d8062fc33"],"x":134,"y":219,"w":1252,"h":262},{"id":"2747395beb3700a1","type":"inject","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 16 * * 1,2,3,4,5","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":300,"wires":[["7b964f16b1c1bcc3"]]},{"id":"7b964f16b1c1bcc3","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Prepare Login Data","func":"msg.headers = {\n    'Content-Type': 'application/json',\n    'Accept': 'application/json'\n};\nmsg.payload = {\n    \"identificator\": \"[email protected]\",\n    \"accessPin\": \"twojehasło\",\n    \"rememberLogin\": false,\n    \"DeviceId\": \"123\",\n    \"DeviceName\": \"Home Assistant: 99.9.999.99<br>\",\n    \"DeviceType\": \"Web\"\n};\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":300,"wires":[["a4318bd8d15d76f8"]]},{"id":"a4318bd8d15d76f8","type":"http request","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Login","method":"POST","ret":"obj","url":"https://ebok.pgnig.pl/auth/login?api-version=3.0","tls":"","x":550,"y":300,"wires":[["d3d17709c3b33579"]]},{"id":"03c9a2b7f06d1158","type":"http request","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Get Meters List","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://ebok.pgnig.pl/crm/get-ppg-list?api-version=3.0","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":260,"y":380,"wires":[["35a3701407ec2017","212d40e9317d27ca"]]},{"id":"d3d17709c3b33579","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Get token and prepare Meters Request","func":"msg.authToken = msg.payload.Token;\nmsg.headers = {\n    'Content-Type': 'application/json',\n    'Accept': 'application/json',\n    'AuthToken': msg.authToken\n};\nif (msg.authToken != null) {\n    node.status({ fill: \"green\", shape: \"ring\", text: \"Token OK\" });\n    return msg;\n}\nelse {\n    node.status({ fill: \"red\", shape: \"ring\", text: \"Brak autoryzacji\" });\n    return null;\n}","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":300,"wires":[["03c9a2b7f06d1158"]]},{"id":"35a3701407ec2017","type":"debug","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Meters List","active":false,"console":"false","complete":"true","x":390,"y":260,"wires":[]},{"id":"212d40e9317d27ca","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Prepare Meter Readings Request","func":"msg.headers = {\n    'Content-Type': 'application/json',\n    'Accept': 'application/json',\n    'AuthToken': msg.authToken\n};\nmsg.url = \"https://ebok.pgnig.pl/crm/get-all-ppg-readings-for-meter?pageSize=10&pageNumber=1&api-version=3.0&idPpg=\" + msg.payload.PpgList[0].IdPPG;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":380,"wires":[["828cc8672038c4d1"]]},{"id":"828cc8672038c4d1","type":"http request","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Get Meter Readings","method":"GET","ret":"obj","url":"","tls":"","x":780,"y":380,"wires":[["fb84ca564c6e3613","05ffb7dc1396ea68","8653f4589bd878bc"]]},{"id":"fb84ca564c6e3613","type":"debug","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Meter Readings","active":false,"console":"false","complete":"true","x":1120,"y":280,"wires":[]},{"id":"05ffb7dc1396ea68","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Prepare Invoices Request","func":"msg.headers = {\n    'Content-Type': 'application/json',\n    'Accept': 'application/json',\n    'AuthToken': msg.authToken\n};\nmsg.url = \"https://ebok.pgnig.pl/crm/get-invoices-v2?pageNumber=1&pageSize=12&api-version=3.0\";\nreturn msg;","outputs":1,"x":550,"y":440,"wires":[["1c7e66e8fedb6aa6"]]},{"id":"1c7e66e8fedb6aa6","type":"http request","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Get Invoices","method":"GET","ret":"obj","url":"","tls":"","x":750,"y":440,"wires":[["cd3371eb42164fa7"]]},{"id":"1f1be005bef632df","type":"catch","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"","scope":["a4318bd8d15d76f8"],"uncaught":false,"x":550,"y":260,"wires":[[]]},{"id":"8653f4589bd878bc","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Generate InfluxDB Data","func":"const readings = msg.payload.MeterReadings;\nconst influxData = [];\n\nreadings.forEach(reading => {\n    if (reading.Type === \"Receiver\") {\n        const meterNumber = reading.MeterNumber;\n        const value = reading.Value;\n        let date = new Date(reading.ReadingDateLocal);\n        date.setUTCHours(20, 0, 0, 0); // Ustaw godzinę na 20:00\n        const timestamp = date.getTime() / 1000;\n\n        const influxLine = `gaz,MeterNumber=${meterNumber} metry=${value} ${timestamp}`;\n        influxData.push(influxLine);\n    }\n});\ndelete msg.url;\nmsg.payload = influxData.join('\\n');\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1030,"y":380,"wires":[["900d551d8062fc33"]]},{"id":"93c71c9627efb81f","type":"debug","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"InfluxDB Output","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1120,"y":320,"wires":[]},{"id":"cd3371eb42164fa7","type":"function","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Generate Influxdb data","func":"// Odczytaj dane z msg.payload\nlet invoices = msg.payload.InvoicesList;\n\nlet influxData = invoices.map(invoice => {\n    // Ekstrahuj pierwszą literę z Number\n    let firstChar = invoice.Number.charAt(0);\n    \n    // Określ rodzaj tagu\n    let rodzaj = firstChar === 'P' ? 'rachunek' : (firstChar === 'N' ? 'odsetki' : '');\n\n    // Jeśli rodzaj nie jest ani 'rachunek' ani 'odsetki', pomiń ten wpis\n    if (rodzaj === '') return null;\n\n    // Ekstrahuj month i year\n    let parts = invoice.Number.split('/');\n    let month = parts[2].replace(/^0+/, '');\n    let year = parts[3];\n\n    // Data w formacie unix timestamp (sekundy)\n    let timestamp = new Date(invoice.PayingDeadlineDate).getTime() / 1000;\n\n    // Stwórz wpis w formacie InfluxDB\n    return `gaz,rodzaj=${rodzaj},month=${month},year=${year} GrossAmount=${invoice.GrossAmount},Wear=${invoice.Wear},WearKWH=${invoice.WearKWH} ${timestamp}`;\n});\n\n// Usuń null wartości (pomięte wpisy)\ninfluxData = influxData.filter(data => data !== null);\n\n// Zwróć przetworzone dane\nmsg.payload = influxData.join('\\n');\ndelete msg.url;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1040,"y":440,"wires":[["900d551d8062fc33"]]},{"id":"900d551d8062fc33","type":"http request","z":"b59e4bb0528d4a39","g":"2077be37fcb53e45","name":"Post to Influx DB","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:8086/write?db=influx&u=xury&p=tajnehaslo&precision=s","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":1270,"y":420,"wires":[["93c71c9627efb81f"]]},{"id":"b40b595c100d5066","type":"comment","z":"b59e4bb0528d4a39","name":"Wstaw swoje dane logowania do node \"Prepare Login Data\"","info":"","x":360,"y":200,"wires":[]}]

Flow Info

Created 10 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • catch (x1)
  • comment (x1)
  • debug (x3)
  • function (x6)
  • http request (x5)
  • inject (x1)
Other

Tags

  • pgnig
  • gaz
  • api
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option