homebridge-tauron-elicznik 0.0.3-beta.1 → 0.0.3-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +71 -60
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -4,16 +4,14 @@ const path = require('path');
4
4
  const fs = require('fs');
5
5
  const fsPromises = fs.promises;
6
6
  const axios = require('axios');
7
+ const https = require('https');
7
8
 
8
9
  const PLUGIN_NAME = 'homebridge-tauron-elicznik';
9
10
  const PLATFORM_NAME = 'tauroneLicznik';
10
11
 
11
- const SERVICE_URL = 'https://elicznik.tauron-dystrybucja.pl';
12
- const LOGIN_URL = 'https://logowanie.tauron-dystrybucja.pl/login';
13
- const CHARTS_URL = 'https://elicznik.tauron-dystrybucja.pl/index/charts';
14
- const HEADERS = {
15
- 'content-type': 'application/x-www-form-urlencoded'
16
- };
12
+ const url = 'https://logowanie.tauron-dystrybucja.pl/login';
13
+ const chartUrl = 'https://elicznik.tauron-dystrybucja.pl/index/charts';
14
+ const headers = { 'cache-control': 'no-cache' };
17
15
 
18
16
  let Accessory, Characteristic, Service, Categories, UUID;
19
17
 
@@ -57,7 +55,7 @@ module.exports = (api) => {
57
55
  Characteristic.tauroneLicznikEnergyExport = tauroneLicznikEnergyExport;
58
56
 
59
57
  class tauroneLicznikEnergyService extends Service {
60
- constructor(displayName, subtype, ) {
58
+ constructor(displayName, subtype,) {
61
59
  super(displayName, '00000001-000A-1000-8000-0026BB765291', subtype);
62
60
  // Mandatory Characteristics
63
61
  this.addCharacteristic(Characteristic.tauroneLicznikEnergyImport);
@@ -140,73 +138,86 @@ class eLicznikDevice {
140
138
  }
141
139
 
142
140
  //Check device state
143
- setInterval(function () {
141
+ setInterval(() => {
144
142
  if (this.checkDeviceInfo) {
145
143
  this.getDeviceInfo();
146
144
  }
147
145
  if (this.checkDeviceState) {
148
146
  this.updateDeviceState();
149
147
  }
150
- }.bind(this), this.refreshInterval * 1000);
148
+ }, 30000);
151
149
  }
152
150
 
153
- async getDeviceInfo() {
151
+ getDeviceInfo() {
154
152
  this.log.debug('Device: %s %s, requesting Device Info.', this.meterId, this.name);
155
- try {
156
- this.log('-------- %s --------', this.name);
157
- this.log('Manufacturer: %s', this.manufacturer);
158
- this.log('Model: %s', this.modelName);
159
- this.log('Meter Id: %s', this.meterId);
160
- this.log('Serialnr: %s', this.serialNumber);
161
- this.log('Firmware: %s', this.firmwareRevision);
162
- this.log('----------------------------------');
163
-
164
- this.checkDeviceInfo = false;
165
- this.updateDeviceState();
166
- } catch (error) {
167
- this.log.error('Device: %s %s, Device Info eror: %s, state: Offline, trying to reconnect', this.meterId, this.name, error);
168
- this.checkDeviceInfo = true;
169
- }
153
+
154
+ this.log('-------- %s --------', this.name);
155
+ this.log('Manufacturer: %s', this.manufacturer);
156
+ this.log('Model: %s', this.modelName);
157
+ this.log('Meter Id: %s', this.meterId);
158
+ this.log('Serialnr: %s', this.serialNumber);
159
+ this.log('Firmware: %s', this.firmwareRevision);
160
+ this.log('----------------------------------');
161
+
162
+ this.checkDeviceInfo = false;
163
+ this.updateDeviceState();
170
164
  }
171
165
 
172
166
  async updateDeviceState() {
173
167
  this.log.debug('Device: %s %s, requesting Device state.', this.meterId, this.name);
174
- try {
175
- const url = `${CHARTS_URL}?dane[chartYear]=2021&dane[paramType]=year&dane[checkOZE]=on&dane[smartNr]=${this.meterId}`;
176
- const options = {
177
- method: 'POST',
178
- url: url
179
- };
180
-
181
- const login = await axios.post(LOGIN_URL, {
182
- username: this.user,
183
- password: this.passwd,
184
- });
185
- this.log('hhhhhhhh', login)
186
- const data = await axios.post(options);
187
- this.log('gggggggg', data.data)
188
-
189
- const energyImport = 0;
190
- const energyExport = 0;
191
- if (this.tauroneLicznikEnergyService) {
192
- this.tauroneLicznikEnergyService
193
- .updateCharacteristic(Characteristic.tauroneLicznikEnergyImport, energyImport)
194
- .updateCharacteristic(Characteristic.tauroneLicznikEnergyExport, energyExport);
195
- }
196
- this.energyImport = energyImport;
197
- this.energyExport = energyExport;
198
-
199
- this.checkDeviceState = true;
200
168
 
201
- //start prepare accessory
202
- if (this.startPrepareAccessory) {
203
- //this.prepareAccessory();
204
- }
205
- } catch (error) {
206
- this.log.error('Device: %s %s, update Device state error: %s', this.meterId, this.name, error);
207
- this.checkDeviceState = false;
208
- this.checkDeviceInfo = true;
169
+ // Add login details & meter ID here:
170
+ const payload = {
171
+ 'username': username,
172
+ 'password': password,
173
+ 'service': 'https://elicznik.tauron-dystrybucja.pl'
209
174
  }
175
+
176
+ const url = 'https://logowanie.tauron-dystrybucja.pl/login';
177
+ const charturl = 'https://elicznik.tauron-dystrybucja.pl/index/charts';
178
+ const headers = { 'cache-control': 'no-cache' };
179
+
180
+ const axiosInstance = axios.create({
181
+ httpsAgent: new https.Agent({
182
+ ciphers: 'DEFAULT@SECLEVEL=1'
183
+ })
184
+ });
185
+
186
+ axiosInstance.post(url, payload, { headers: headers })
187
+ .then((response) => {
188
+ const chart_year = {
189
+ "dane[chartYear]": 2020,
190
+ "dane[paramType]": "year",
191
+ "dane[smartNr]": meter_id,
192
+ "dane[checkOZE]": "on"
193
+ };
194
+ return axiosInstance.post(charturl, chart_year, { headers: headers });
195
+ })
196
+ .then((response) => {
197
+ this.log('Device: %s, update Device state: %s', this.name, response.data);
198
+
199
+ const energyImport = 0;
200
+ const energyExport = 0;
201
+ if (this.tauroneLicznikEnergyService) {
202
+ this.tauroneLicznikEnergyService
203
+ .updateCharacteristic(Characteristic.tauroneLicznikEnergyImport, energyImport)
204
+ .updateCharacteristic(Characteristic.tauroneLicznikEnergyExport, energyExport);
205
+ }
206
+ this.energyImport = energyImport;
207
+ this.energyExport = energyExport;
208
+
209
+ this.checkDeviceState = true;
210
+
211
+ //start prepare accessory
212
+ if (this.startPrepareAccessory) {
213
+ //this.prepareAccessory();
214
+ }
215
+ })
216
+ .catch((error) => {
217
+ this.log.error('Device: %s, update Device state error: %s', this.name, error);
218
+ this.checkDeviceState = false;
219
+ this.checkDeviceInfo = true;
220
+ });
210
221
  }
211
222
 
212
223
  //Prepare accessory
@@ -238,7 +249,7 @@ class eLicznikDevice {
238
249
  //Prepare service
239
250
  this.log.debug('prepareTauroneLicznikService');
240
251
  //power and energy
241
- this.tauroneLicznikEnergyService = new Service.tauroneLicznikEnergyService('Meter ' + this.meterId, 'tauroneLicznikEnergyService');
252
+ this.tauroneLicznikEnergyService = new Service.tauroneLicznikEnergyService(`Meter ${this.meterId}`, 'tauroneLicznikEnergyService');
242
253
  this.tauroneLicznikEnergyService.getCharacteristic(Characteristic.tauroneLicznikEnergyImport)
243
254
  .onGet(async () => {
244
255
  const value = this.energyImport;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Tauron eLicznik",
3
3
  "name": "homebridge-tauron-elicznik",
4
- "version": "0.0.3-beta.1",
4
+ "version": "0.0.3-beta.10",
5
5
  "description": "Homebridge plugin (https://github.com/homebridge/homebridge) to read data from Tauron eLicznik.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -28,10 +28,10 @@
28
28
  ],
29
29
  "engines": {
30
30
  "node": ">=14.0.0",
31
- "homebridge": ">=1.3.0"
31
+ "homebridge": ">=1.4.0"
32
32
  },
33
33
  "dependencies": {
34
- "axios": ">=0.25.0"
34
+ "axios": ">=1.2.1"
35
35
  },
36
36
  "keywords": [
37
37
  "homebridge",