Skip to content

Commit

Permalink
improve with luxon
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Nov 21, 2023
1 parent 332ca53 commit cae6dc7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
26 changes: 13 additions & 13 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { App } from 'homey' // eslint-disable-line import/no-extraneous-dependen
import axios from 'axios'
import { wrapper } from 'axios-cookiejar-support'
import { CookieJar } from 'tough-cookie'
import { DateTime } from 'luxon'
import withAPI, { getErrorMessage } from './mixins/withAPI'
import type {
LoginCredentials,
Expand Down Expand Up @@ -79,19 +80,18 @@ export = class AristonApp extends withAPI(App) {
const expires: string | null = this.homey.settings.get(
'expires',
) as HomeySettings['expires']
if (expires !== null) {
const expireAtDate: Date = new Date(expires)
expireAtDate.setDate(expireAtDate.getDate() - 1)
const ms: number = expireAtDate.getTime() - new Date().getTime()
if (ms) {
const maxTimeout: number = 2 ** 31 - 1
const interval: number = Math.min(ms, maxTimeout)
this.#loginTimeout = this.homey.setTimeout(async (): Promise<void> => {
await this.tryLogin(loginCredentials)
}, interval)
this.log('Login refresh has been scheduled')
return
}
const ms: number =
expires !== null
? Number(DateTime.fromISO(expires).minus({ days: 1 }).diffNow())
: 0
if (ms) {
const maxTimeout: number = 2 ** 31 - 1
const interval: number = Math.min(ms, maxTimeout)
this.#loginTimeout = this.homey.setTimeout(async (): Promise<void> => {
await this.tryLogin(loginCredentials)
}, interval)
this.log('Login refresh has been scheduled')
return
}
await this.tryLogin(loginCredentials)
}
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@tsconfig/node16": "^16.1.1",
"@types/homey": "npm:homey-apps-sdk-v3-types@^0.3.5",
"@types/luxon": "^3.3.4",
"@types/node": "^20.9.2",
"@types/tough-cookie": "^4.0.5",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand All @@ -33,6 +34,7 @@
"dependencies": {
"axios": "^1.6.2",
"axios-cookiejar-support": "^4.0.7",
"luxon": "^3.4.4",
"source-map-support": "^0.5.21",
"tough-cookie": "^4.1.3"
}
Expand Down

0 comments on commit cae6dc7

Please sign in to comment.