diff --git a/CHANGELOG.md b/CHANGELOG.md index 0800800..ec951e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [9.5.3] - (28.11.2024) + +## Changes + +- better handle cookie and own token +- config schema updated +- cleanup + ## [9.5.2] - (28.11.2024) ## Changes diff --git a/config.schema.json b/config.schema.json index 9352f96..bfec5c4 100644 --- a/config.schema.json +++ b/config.schema.json @@ -27,7 +27,7 @@ "type": "string", "placeholder": "IP Address or Hostname", "format": "hostname", - "description": "Here set the envoy ip address/hostname or leave empty, will use default path envoy.local.", + "description": "Here set the envoy IP Address or Hostname, if not set default path envoy.local will be used. For firmware v7.x.x please set IP Address", "required": false }, "envoyFirmware7xx": { @@ -40,7 +40,6 @@ "envoyFirmware7xxTokenGenerationMode": { "title": "Token Generation Mode", "type": "integer", - "default": 0, "oneOf": [ { "title": "Enlighten Credentials", diff --git a/package.json b/package.json index 6929178..c9c454f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Enphase Envoy", "name": "homebridge-enphase-envoy", - "version": "9.5.2", + "version": "9.5.3", "description": "Homebridge plugin for Photovoltaic Energy System manufactured by Enphase.", "license": "MIT", "author": "grzegorz914", diff --git a/src/envoydevice.js b/src/envoydevice.js index 222286f..2d2f1ea 100644 --- a/src/envoydevice.js +++ b/src/envoydevice.js @@ -1059,28 +1059,41 @@ class EnvoyDevice extends EventEmitter { return true; }; + if (this.checkJwtTokenRunning) { + return false; + }; + this.checkJwtTokenRunning = true; + + //check cookie are valid + const cookieValid = this.cookie === this.oldCookie; + + //validate own token if (this.envoyFirmware7xxTokenGenerationMode === 1) { + if (cookieValid) { + return true; + }; + + //validate own JWT token try { + this.emit('warn', `Cookie not valid, validating.`); await this.validateJwtToken(); + + this.checkJwtTokenRunning = false; return true; } catch (error) { + this.checkJwtTokenRunning = false; throw new Error(`Check own JWT token error: ${error.message || error}`); }; }; - if (this.checkJwtTokenRunning) { - return false; - }; + //use token from enlighten try { - this.checkJwtTokenRunning = true; - //read token from file const data = await this.readData(this.envoyTokenFile); const parsedData = JSON.parse(data); parsedData.token ? parsedData : null; const tokenValid = parsedData && parsedData.expires_at >= Math.floor(Date.now() / 1000) + 60; - const cookieValid = this.cookie === this.oldCookie; const debug = this.enableDebugMode ? this.emit('warn', `JWT Token: ${tokenValid ? 'Valid' : 'Not valid'}, cookie: ${cookieValid ? 'Valid' : 'Not valid'}`) : false; if (tokenValid && cookieValid) { @@ -1088,14 +1101,14 @@ class EnvoyDevice extends EventEmitter { return true; } - //JWT token + //get new JWT token const emit = !tokenValid ? this.emit('warn', `JWT Token expired, refreshing.`) : false; const wait = !tokenValid ? await new Promise(resolve => setTimeout(resolve, 30000)) : false; const getToken = await this.getJwtToken(); - //Cookie + //validate JWT tokken const wait1 = !cookieValid ? await new Promise(resolve => setTimeout(resolve, 2000)) : false; - const emit1 = !cookieValid ? this.emit('warn', `Cookie not valid, refreshing.`) : false; + const emit1 = !cookieValid ? this.emit('warn', `Cookie not valid, validating.`) : false; const validateToken = getToken || !cookieValid ? await this.validateJwtToken() : false; this.checkJwtTokenRunning = false; @@ -1187,7 +1200,7 @@ class EnvoyDevice extends EventEmitter { }); this.oldCookie = this.cookie; - this.emit('success', `Cookie refresh success.`); + this.emit('success', `Cookie validate success.`); return true; } catch (error) {