Skip to content

Commit 80f2bd1

Browse files
authored
Merge pull request #128 from damachine/feat-plugin
- feat: add CC changes to allow Plugins to have a resart function
2 parents e955b7c + e9763ac commit 80f2bd1

3 files changed

Lines changed: 68 additions & 40 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.1
1+
2.1.2

etc/coolercontrol/plugins/coolerdash/ui/cc-plugin-lib.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ const close = () => {
180180
window.parent.postMessage({ type: 'close' }, document.location.origin)
181181
}
182182

183+
/* Restart the daemon & UI. This has the effect of applying any plugin changes to service configs. */
184+
const restart = () => {
185+
window.parent.postMessage({ type: 'restart' }, document.location.origin)
186+
}
187+
183188
// Data Exchange Functions
184189
/////////////////////////////////////////////////////////////
185190

etc/coolercontrol/plugins/coolerdash/ui/index.html

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,9 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
714714
</div>
715715

716716
<!-- Restart Notice -->
717-
<div style="margin-top: 16px; padding: 12px; background: rgba(255, 140, 0, 0.1); border-left: 3px solid #ff8c00; border-radius: 4px;">
718-
<p style="margin: 0; color: var(--text-primary); font-size: 13px;">
719-
<strong>Important:</strong> After saving, restart the plugin to apply changes:<br>
720-
<code style="background: rgba(0,0,0,0.3); padding: 2px 6px; border-radius: 3px; font-size: 12px; margin-top: 4px; display: inline-block;">systemctl restart cc-plugin-coolerdash.service</code>
717+
<div style="margin-top: 16px; padding: 12px; background: rgba(255, 140, 0, 0.05); border-left: 3px solid #ff8c00; border-radius: 4px;">
718+
<p style="margin: 0; color: var(--text-dim); font-size: 13px;">
719+
<strong>Note:</strong> Changes are applied immediately — the plugin will be restarted automatically after Save or Reset.
721720
</p>
722721
</div>
723722
</div>
@@ -1068,23 +1067,19 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
10681067
console.log("Saving config:", config);
10691068

10701069
try {
1071-
// 1. Save to CoolerControl plugin system (for UI persistence)
1072-
await savePluginConfig(config);
1073-
console.log("Config saved to CoolerControl");
1074-
1075-
// 2. Write to config.json (for daemon consumption)
1070+
//Write to config.json (for daemon consumption)
10761071
const writeSuccess = await writeConfigToFile(config);
10771072

1078-
// 3. Show success message with restart instructions
1079-
alert(
1080-
"Configuration saved successfully!\n\n" +
1081-
"Plugin restart required to apply changes.\n\n" +
1082-
"Simply run:\n" +
1083-
"systemctl restart cc-plugin-coolerdash.service"
1084-
);
1073+
//Arrange restart to happen after plugin confirms config save (short delay to allow file write)
1074+
requestRestartAfterSave(1000);
1075+
1076+
//Save to CoolerControl plugin system (for UI persistence) — this triggers configSaved and the restart callback
1077+
await savePluginConfig(config);
1078+
1079+
alert("Configuration saved successfully!\n\nChanges are applied immediately — the plugin will be restarted automatically after Save or Reset.");
10851080

1086-
// Close window after successful save
1087-
setTimeout(() => window.close(), 100);
1081+
// Close window after short delay
1082+
setTimeout(() => window.close(), 300);
10881083

10891084
} catch (error) {
10901085
console.error("Save failed:", error);
@@ -1147,10 +1142,41 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
11471142
}
11481143
}
11491144

1145+
// Request a restart shortly after a successful config save.
1146+
// Uses the plugin API's successfulConfigSaveCallback if available, otherwise listens for a configSaved postMessage.
1147+
function requestRestartAfterSave(delayMs = 800) {
1148+
const doRestart = () => {
1149+
try {
1150+
if (typeof restart === 'function') {
1151+
// restart() is provided by cc-plugin-lib and posts a message to the parent
1152+
restart();
1153+
} else {
1154+
// fallback: attempt API-based restart
1155+
const cfg = buildConfig();
1156+
restartPluginDaemon(cfg);
1157+
}
1158+
} catch (e) {
1159+
console.warn('Restart attempt failed:', e);
1160+
}
1161+
};
1162+
1163+
if (typeof successfulConfigSaveCallback === 'function') {
1164+
successfulConfigSaveCallback(() => setTimeout(doRestart, delayMs));
1165+
} else {
1166+
const handler = (event) => {
1167+
if (event.data && event.data.type === 'configSaved') {
1168+
window.removeEventListener('message', handler);
1169+
setTimeout(doRestart, delayMs);
1170+
}
1171+
};
1172+
window.addEventListener('message', handler);
1173+
}
1174+
}
1175+
11501176
// Write config to config.json via update script
11511177
async function writeConfigToFile(config) {
11521178
try {
1153-
// Method 1: Try to write via REST API (requires authentication)
1179+
// Try to write via REST API (requires authentication)
11541180
const apiAddress = config.daemon?.address || 'http://localhost:11987';
11551181
const password = config.daemon?.password || '';
11561182

@@ -1171,7 +1197,7 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
11711197
}
11721198
}
11731199

1174-
// Method 2: Fallback - Use local file write
1200+
// Fallback - Use local file write
11751201
// Create a JSON file blob and download it as workaround
11761202
const blob = new Blob([JSON.stringify(config, null, 2)], { type: 'application/json' });
11771203
const url = URL.createObjectURL(blob);
@@ -1202,11 +1228,7 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
12021228
// Reset to defaults
12031229
async function resetConfig() {
12041230
try {
1205-
// 1. Write factory defaults to CoolerControl plugin storage
1206-
await savePluginConfig(FACTORY_DEFAULTS);
1207-
console.log("Factory defaults saved to CoolerControl");
1208-
1209-
// 2. Write to config.json file
1231+
// Write to config.json file (daemon consumption)
12101232
try {
12111233
await writeConfigToFile(FACTORY_DEFAULTS);
12121234
console.log("Factory defaults written to config.json");
@@ -1216,19 +1238,20 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
12161238
console.info(JSON.stringify(FACTORY_DEFAULTS, null, 2));
12171239
}
12181240

1219-
// 3. Update form with factory defaults
1241+
// Update form with factory defaults
12201242
populateForm(FACTORY_DEFAULTS);
12211243

1222-
console.log("Reset to factory defaults - config.json overwritten");
1223-
alert(
1224-
'Configuration reset to factory defaults!\n\n' +
1225-
'Changes saved to CoolerControl.\n\n' +
1226-
'Plugin restart required:\n' +
1227-
'systemctl restart cc-plugin-coolerdash.service'
1228-
);
1244+
// Arrange restart to happen after plugin confirms config save (short delay to allow file write)
1245+
requestRestartAfterSave(1000);
1246+
1247+
// Save factory defaults to CoolerControl plugin storage (this triggers configSaved)
1248+
await savePluginConfig(FACTORY_DEFAULTS);
1249+
console.log("Factory defaults saved to CoolerControl");
1250+
1251+
alert('Configuration reset to factory defaults!\n\nChanges are applied immediately — the plugin will be restarted automatically after Save or Reset.');
12291252

1230-
// Close window after successful reset
1231-
setTimeout(() => window.close(), 100);
1253+
// Close window after short delay
1254+
setTimeout(() => window.close(), 300);
12321255
} catch (error) {
12331256
console.error("Failed to reset config:", error);
12341257
alert('Failed to reset configuration:\n\n' + error.message);
@@ -1240,14 +1263,14 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
12401263
try {
12411264
console.log("Initializing CoolerDash UI...");
12421265

1243-
// 1. Load defaults from config.json
1266+
// Load defaults from config.json
12441267
await loadDefaultConfig();
12451268

1246-
// 2. Load user config from CoolerControl
1269+
// Load user config from CoolerControl
12471270
let config = await getPluginConfig();
12481271
console.log("User config:", config);
12491272

1250-
// 3. Merge with defaults
1273+
// Merge with defaults
12511274
if (!config || Object.keys(config).length === 0) {
12521275
console.log("Using defaults");
12531276
config = DEFAULT_CONFIG;
@@ -1265,7 +1288,7 @@ <h3 style="margin: 24px 0 16px; font-size: 16px; color: var(--text-dim);">Labels
12651288
};
12661289
}
12671290

1268-
// 4. Display in UI
1291+
// Display in UI
12691292
populateForm(config);
12701293
console.log("UI initialized");
12711294
} catch (error) {

0 commit comments

Comments
 (0)