-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempting fixes for 'object destroyed' bugs, battery limiter now per…
…sists after restart.
- Loading branch information
Showing
5 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,104 @@ | ||
/** @format */ | ||
|
||
import { BrowserWindow, IpcMain } from 'electron'; | ||
import { setBatteryLimiter, whichCharger } from './WMI/HardwareControl'; | ||
import { | ||
buildAtkWmi, | ||
setBatteryLimiter, | ||
whichCharger, | ||
} from './WMI/HardwareControl'; | ||
import getLogger from '../Logger'; | ||
import Shell from 'node-powershell'; | ||
import { checkTaskExists } from '../AutoLaunch'; | ||
|
||
const LOGGER = getLogger('BatteryListener'); | ||
|
||
const batteryCommand = (enabled: boolean, comnd: string) => | ||
enabled | ||
? `$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "${comnd}"' | ||
$Trigger = New-ScheduledTaskTrigger -AtLogOn | ||
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries | ||
Register-ScheduledTask G14ControlBatteryLimit -Action $Action -Trigger $Trigger -Settings $Settings -RunLevel Highest` | ||
: `Unregister-ScheduledTask -TaskName G14ControlBatteryLimit -Confirm:$false`; | ||
|
||
export const buildBatterySaverListener = (win: BrowserWindow, ipc: IpcMain) => { | ||
ipc.handle('setBatteryLimiter', async (event, value: number) => { | ||
let modified = await setBatteryLimiter(value); | ||
if (modified) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
await setBatteryLimiter(value); | ||
let modd = (await setLimit(value)) as boolean; | ||
return modd; | ||
}); | ||
|
||
ipc.handle('isPlugged', async (event) => { | ||
let plugType = await whichCharger(); | ||
return plugType; | ||
}); | ||
}; | ||
|
||
const removeTask = async () => { | ||
return new Promise((resolve) => { | ||
let shell = new Shell({ executionPolicy: 'Bypass', noProfile: true }); | ||
shell.addCommand( | ||
`Unregister-ScheduledTask -TaskName G14ControlBatteryLimit -Confirm:$false` | ||
); | ||
shell | ||
.invoke() | ||
.then((resul) => { | ||
if (resul) { | ||
LOGGER.error( | ||
'There was a problem removing battery limit Task Scheduler command.\n' + | ||
JSON.stringify(resul, null, 2) | ||
); | ||
resolve(false); | ||
shell.dispose(); | ||
} else { | ||
LOGGER.info(`Successfully removed previous task:\n${resul}`); | ||
resolve(true); | ||
shell.dispose(); | ||
} | ||
}) | ||
.catch((err) => { | ||
LOGGER.error( | ||
'There was a problem removing battery limit Task Scheduler command.\n' + | ||
JSON.stringify(err, null, 2) | ||
); | ||
resolve(false); | ||
shell.dispose(); | ||
}); | ||
}); | ||
}; | ||
|
||
const setLimit = async (amt: number) => { | ||
return new Promise(async (resolve) => { | ||
let exist = await checkTaskExists('G14ControlBatteryLimit'); | ||
if (exist) { | ||
await removeTask(); | ||
} | ||
let command = buildAtkWmi('DEVS', '0x00120057', amt); | ||
let addCommand = batteryCommand(true, command); | ||
let sh = new Shell({ | ||
executionPolicy: 'Bypass', | ||
noProfile: true, | ||
}); | ||
sh.addCommand(addCommand); | ||
sh.invoke() | ||
.then((result) => { | ||
if (result) { | ||
LOGGER.info('Result of limit command: \n' + result); | ||
resolve(true); | ||
sh.dispose(); | ||
} else { | ||
LOGGER.error( | ||
'There was a problem setting battery limit Task Scheduler command.' | ||
); | ||
resolve(false); | ||
sh.dispose(); | ||
} | ||
}) | ||
.catch((err) => { | ||
LOGGER.error( | ||
'There was an error setting Task Scheduler task: \n' + err | ||
); | ||
resolve(false); | ||
sh.dispose(); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters