Skip to content

Commit

Permalink
Auto power switching with start on boot / resume / unlock screen, rem…
Browse files Browse the repository at this point in the history
…oved export config notice for updates.
  • Loading branch information
aredden committed Jan 10, 2021
1 parent f8cd402 commit 57b8628
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
82 changes: 82 additions & 0 deletions electron/src/StartupPlan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/** @format */

import { setG14ControlPlan } from './IPCEvents/G14ControlPlans';
import { whichCharger } from './IPCEvents/WMI/HardwareControl';
import { Notification } from 'electron';
import getLogger from './Logger';

const LOGGER = getLogger('StartupPlan');

const applyPlan = async (plano: G14ControlPlan, config: G14Config) => {
let plan = plano;
if (typeof plano === 'string') {
plan = config.plans.find((pln) => pln.name === plano);
} else {
plan = plano;
}

LOGGER.info(`Recieved plan:\n${JSON.stringify(plan, null, 2)}`);
let curve: FanCurveConfig;
if (plan.fanCurve) {
curve = config.fanCurves.find((curv) => {
return curv.name === plan.fanCurve;
}) as FanCurveConfig;
}
let radj: RyzenadjConfigNamed;
if (plan.ryzenadj) {
radj = config.ryzenadj.options.find((rdj) => {
return rdj.name === plan.ryzenadj;
});
}

if ((curve || !plan.fanCurve) && (radj || !plan.ryzenadj)) {
let full: FullG14ControlPlan = {
name: plan.name,
fanCurve: curve,
ryzenadj: radj,
armouryCrate: plan.armouryCrate,
boost: plan.boost,
windowsPlan: plan.windowsPlan,
graphics: plan.graphics,
};
let result = await setG14ControlPlan(full);
if (result) {
return true;
} else {
LOGGER.error('Failed to set G14Control plan.');
return false;
}
} else {
LOGGER.info(
`Could not find curve or ryzenadj plan with names:\nryzen: ${plan.ryzenadj}\ncurve: ${plan.fanCurve}`
);
return false;
}
};

export const initStartupPlan = async (config: G14Config) => {
let { autoSwitch } = config;
if (autoSwitch && autoSwitch.enabled) {
let result = await whichCharger();
if (result) {
let { ac, dc, usb } = result;
if ((ac || usb) && autoSwitch.acPlan) {
let result = await applyPlan(autoSwitch.acPlan, config);
if (result) {
new Notification({
title: 'Auto Switching',
body: 'Switched to: ' + autoSwitch.acPlan.name,
}).show();
}
} else if (dc && autoSwitch.dcPlan) {
let result = await applyPlan(autoSwitch.dcPlan, config);
if (result) {
new Notification({
title: 'Auto Switching',
body: 'Switched to: ' + autoSwitch.dcPlan.name,
}).show();
}
}
}
}
};
10 changes: 10 additions & 0 deletions electron/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { isUndefined } from 'lodash';
import { buildTaskbarMenu } from './Taskbar';
import { NotificationConstructorOptions } from 'electron/main';
import { initSwitch } from './AutoPowerSwitching';
import { initStartupPlan } from './StartupPlan';
const LOGGER = getLogger('Main');
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
Expand Down Expand Up @@ -290,6 +291,7 @@ export async function createWindow(
}
}
browserWindow.setMenu(null);
initStartupPlan(g14Config);
return { tray, browserWindow, g14Config, trayContext };
}

Expand Down Expand Up @@ -342,6 +344,14 @@ powerMonitor.on('shutdown', () => {
}
});

powerMonitor.on('unlock-screen', () => {
initStartupPlan(getConfig());
});

powerMonitor.on('resume', () => {
initStartupPlan(getConfig());
});

powerMonitor.on('suspend', () => {
LOGGER.info('Windows is suspending (sent from powermonitor)');
browserWindow.hide();
Expand Down
6 changes: 1 addition & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ export default class App extends Component<Props, State> {
}}
onCancel={this.cancelExitAndUpdate}>
<ReactMarkdownWithHtml allowDangerousHtml>
{updateText
? updateText +
"\n\nMake sure to export config using the dropdown menu if you don't want to lose your settings!"
: 'No release notes available.' +
"\n\nMake sure to export config using the dropdown menu if you don't want to lose your settings!"}
{updateText ? updateText : 'No release notes available.'}
</ReactMarkdownWithHtml>
</Modal>
</>
Expand Down

0 comments on commit 57b8628

Please sign in to comment.