Skip to content

Commit

Permalink
Feat/watch ip change on cards (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Waller <git@julusian.co.uk>
Co-authored-by: arc-alex <ac@omnivox.dk>
  • Loading branch information
3 people authored Aug 19, 2023
1 parent 6ebaf36 commit 8c6a6fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class CardGenerator {
}
}

//#TODO: make llocal ip reactive
function getIPAddress() {
for (const devName in networkInterfaces()) {
const iface = networkInterfaces()[devName]
Expand Down
33 changes: 26 additions & 7 deletions src/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,24 @@ export class DeviceManager {
})

this.statusString = 'Connecting'
this.showStatusCard(this.statusString, true)

this.scanDevices()

client.on('connected', () => {
console.log('connected')

this.showStatusCard('Connected')
this.showStatusCard('Connected', false)

this.registerAll()
})
client.on('disconnected', () => {
console.log('disconnected')

this.showStatusCard('Disconnected')
this.showStatusCard('Connecting', true)
})
client.on('ipChange', () => {
this.showStatusCard()
this.showStatusCard('Connecting', true)
})

client.on(
Expand Down Expand Up @@ -412,13 +413,31 @@ export class DeviceManager {
}
}

private showStatusCard(status?: string): void {
if (status !== undefined) {
this.statusString = status
private statusCardTimer: NodeJS.Timer | undefined
private showStatusCard(message: string, runLoop: boolean): void {
this.statusString = message

if (this.statusCardTimer) {
clearInterval(this.statusCardTimer)
delete this.statusCardTimer
}

if (runLoop) {
let dots = ''
this.statusCardTimer = setInterval(() => {
dots += ' .'
if (dots.length > 7) dots = ''

this.doDrawStatusCard(message + dots)
}, 1000)
}

this.doDrawStatusCard(message)
}

private doDrawStatusCard(message: string) {
for (const dev of this.devices.values()) {
dev.showStatus(this.client.host, this.statusString)
dev.showStatus(this.client.host, message)
}
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4954,4 +4954,4 @@ zx@^7.2.3:
ps-tree "^1.2.0"
webpod "^0"
which "^3.0.0"
yaml "^2.2.2"
yaml "^2.2.2"

0 comments on commit 8c6a6fc

Please sign in to comment.