homebridge-melcloud-control 4.0.0-beta.27 → 4.0.0-beta.29

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/package.json +1 -1
  2. package/src/melcloud.js +32 -31
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.27",
4
+ "version": "4.0.0-beta.29",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/melcloud.js CHANGED
@@ -267,49 +267,50 @@ class MelCloud extends EventEmitter {
267
267
  }
268
268
  }
269
269
 
270
- if (!loginBtn) {
271
- await browser.close();
272
- return;
273
- }
270
+ if (!loginBtn) throw new Error('Login button not found on homepage');
274
271
 
275
- // Kliknij i czekaj na zmianę adresu
276
- const [resp] = await Promise.all([
277
- page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 20000 }),
272
+ await Promise.all([
278
273
  loginBtn.click(),
274
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
279
275
  ]);
280
276
 
281
- // Wypisz adres po przekierowaniu
282
- const currentUrl = page.url();
277
+ // Poczekaj, strona przekieruje do Cognito login
278
+ await page.waitForSelector('input[name="username"]', { timeout: 15000 });
283
279
 
284
- // Zamknij przeglądarkę
285
- await browser.close();
280
+ this.emit('warn', 'Typing credentials...');
281
+ await page.type('input[name="username"]', this.user, { delay: 50 });
282
+ await page.type('input[name="password"]', this.passwd, { delay: 50 });
286
283
 
287
- // Utwórz dane logowania
288
- const form = new URLSearchParams();
289
- form.append('input[username]', this.user);
290
- form.append('input[password]', this.passwd);
284
+ // Kliknij przycisk logowania
285
+ const button1 = await page.$('input[type="submit"]');
286
+ await Promise.all([
287
+ button1.click(),
288
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
289
+ ]);
291
290
 
292
- // Wyślij POST za pomocą axios
293
- this.emit('warn', 'Sending login request via axios...');
294
- const response = await axios.post(currentUrl, {
295
- data: form,
296
- timeout: 10000
297
- });
298
- this.emit('warn', `Login response: ${JSON.stringify(response)}`);
291
+ let c1 = null, c2 = null;
292
+ const start = Date.now();
299
293
 
300
- this.emit('warn', `Login request status: ${response}`);
301
- if (response.status !== 200) {
302
- this.emit('error', `Login failed with status code: ${response.status}`);
303
- return null;
294
+ // Loop max 20s, czekamy pojawią się cookies
295
+ while ((!c1 || !c2) && Date.now() - start < 20000) {
296
+ const cookies = await page.cookies();
297
+ c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
298
+ c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
299
+ if (!c1 || !c2) await new Promise(r => setTimeout(r, 500));
304
300
  }
305
301
 
306
- const cookie = response.headers['set-cookie'];
307
- if (!cookie) {
308
- this.emit('warn', `No cookie returned from login. Response headers: ${JSON.stringify(response.headers)}`);
309
- return null;
302
+ if (!c1 || !c2) {
303
+ await browser.close();
304
+ throw new Error(' Cookies C1/C2 not found after login');
310
305
  }
311
306
 
312
- return cookie;
307
+ // Zapis do pliku
308
+ const data = { C1: c1, C2: c2, date: new Date().toISOString() };
309
+ await this.functions.saveData(this.cookiesFile, data);
310
+
311
+ this.emit('warn', 'Login successful.');
312
+ return data;
313
+
313
314
  } catch (err) {
314
315
  this.emit('error', `Login failed: ${err.message}`);
315
316
  return null;