Skip to content

Commit

Permalink
fix(powerMonitor): experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
lemnik committed Oct 10, 2023
1 parent 53e18c1 commit cf340b0
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions packages/plugin-electron-device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,53 @@ module.exports = (app, screen, process, filestore, NativeClient, powerMonitorPro
}
})

powerMonitorPromise.then(powerMonitor => {
let powerMonitor = { getSystemIdleTime: () => 'nope' }

powerMonitorPromise.then(pm => {
client.addMetadata('device', {
// the value for 'idleThreshold' doesn't matter here because it is ignored
// if the system is locked and that's the only state we care about
isLocked: powerMonitor.getSystemIdleState(1) === 'locked',
isLocked: pm.getSystemIdleState(1) === 'locked',
// note: this is only available from Electron 12, earlier versions cannot
// read the battery state without 'on-ac'/'on-battery' events
usingBattery: powerMonitor.onBatteryPower
usingBattery: pm.onBatteryPower
})

powerMonitor.on('on-ac', () => {
pm.on('on-ac', () => {
client.addMetadata('device', { usingBattery: false })
})

powerMonitor.on('on-battery', () => {
pm.on('on-battery', () => {
client.addMetadata('device', { usingBattery: true })
})

powerMonitor.on('unlock-screen', () => {
pm.on('unlock-screen', () => {
client.addMetadata('device', { isLocked: false })
})

powerMonitor.on('lock-screen', () => {
pm.on('lock-screen', () => {
client.addMetadata('device', { isLocked: true })
})

client.addOnError(event => {
event.device = Object.assign(
{},
event.device,
device,
{
freeMemory: kibibytesToBytes(process.getSystemMemoryInfo().free),
time: new Date()
}
)

event.addMetadata('device', { idleTime: powerMonitor.getSystemIdleTime() })

setDefaultUserId(event)
}, true)
powerMonitor = pm
})

client.addOnError(event => {
event.device = Object.assign(
{},
event.device,
device,
{
freeMemory: kibibytesToBytes(process.getSystemMemoryInfo().free),
time: new Date()
}
)

event.addMetadata('device', { idleTime: powerMonitor.getSystemIdleTime() })

setDefaultUserId(event)
}, true)

app.whenReady().then(() => {
// on windows, app.getLocale won't return the locale until the app is ready
const locale = app.getLocale()
Expand Down

0 comments on commit cf340b0

Please sign in to comment.