Skip to content

Commit cb5e748

Browse files
committed
fix: ota
1 parent 9c84c09 commit cb5e748

5 files changed

Lines changed: 146 additions & 37 deletions

File tree

electron-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extraMetadata:
1111
files:
1212
- dist/main.js
1313
- dist/js/**/*
14+
- dist/ts/**/*
1415
- src/view/**/*
1516
- src/css/**/*
1617
- package.json

src/main.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { app, BrowserWindow, ipcMain } from "electron";
2-
import { autoUpdater } from "electron-updater";
32
import * as path from "path";
3+
import { initAutoUpdater } from "./ts/utils/ota";
44

55
let mainWindow: BrowserWindow | null = null;
66
let dataService: any;
@@ -55,43 +55,10 @@ if (!gotTheLock) {
5555
app.whenReady().then(() => {
5656
createWindow();
5757

58-
// Auto-updater configuration
59-
autoUpdater.autoDownload = true;
60-
autoUpdater.autoInstallOnAppQuit = true;
61-
autoUpdater.allowPrerelease = true; // Since version is beta
62-
autoUpdater.allowDowngrade = false;
63-
64-
if (app.isPackaged) {
65-
autoUpdater.setFeedURL({
66-
provider: 'github',
67-
owner: 'ExpTechTW',
68-
repo: 'ES-Net-Wave',
69-
vPrefixedTagName: false,
70-
});
71-
}
72-
73-
autoUpdater.on('update-available', (info) => {
74-
console.log('Update available:', info.version);
75-
});
76-
77-
autoUpdater.on('update-downloaded', (info) => {
78-
console.log('Update downloaded:', info.version);
79-
setTimeout(() => {
80-
autoUpdater.quitAndInstall(true, true);
81-
}, 3000);
58+
ipcMain.once("init-ota", () => {
59+
initAutoUpdater(() => mainWindow);
8260
});
8361

84-
autoUpdater.on('error', (err) => {
85-
console.error('Update error:', err.message);
86-
});
87-
88-
if (app.isPackaged) {
89-
autoUpdater.checkForUpdates().catch(() => {});
90-
setInterval(() => {
91-
autoUpdater.checkForUpdates().catch(() => {});
92-
}, 300000); // Check every 5 minutes
93-
}
94-
9562
mainWindow!.webContents.on("dom-ready", () => {
9663
mainWindow!.webContents.removeAllListeners("before-input-event");
9764

src/ts/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ const { ipcRenderer } = require("electron");
99

1010
let wsService: WSService | null = null;
1111
let stationSelector: StationSelector | null = null;
12+
ipcRenderer.on("ota-log", (event: IpcRendererEvent, message: string) => {
13+
console.log(`[OTA] ${message}`);
14+
});
1215

1316
function initializeApplication() {
1417
try {
18+
ipcRenderer.send("init-ota");
19+
1520
wsService = new WSService();
1621
wsService.connect();
1722
window.wsService = wsService;

src/ts/utils/ota.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { app, BrowserWindow } from 'electron';
2+
import { autoUpdater } from 'electron-updater';
3+
4+
let updateCheckInterval: NodeJS.Timeout | null = null;
5+
let getMainWindowRef: (() => BrowserWindow | null) | null = null;
6+
7+
/**
8+
* Log helper that sends log to both main process console and renderer process
9+
*/
10+
function logOTA(message: string) {
11+
console.log(`[OTA] ${message}`);
12+
if (getMainWindowRef) {
13+
const mainWindow = getMainWindowRef();
14+
if (mainWindow && !mainWindow.isDestroyed()) {
15+
mainWindow.webContents.send('ota-log', message);
16+
}
17+
}
18+
}
19+
20+
/**
21+
* Initialize auto updater with GitHub as the update source
22+
*/
23+
function initAutoUpdater(getMainWindow: () => BrowserWindow | null) {
24+
getMainWindowRef = getMainWindow;
25+
logOTA('initAutoUpdater called');
26+
try {
27+
autoUpdater.autoDownload = true;
28+
autoUpdater.autoInstallOnAppQuit = true;
29+
autoUpdater.allowPrerelease = true; // Allow beta/alpha versions
30+
autoUpdater.allowDowngrade = false;
31+
32+
if (app.isPackaged) {
33+
autoUpdater.setFeedURL({
34+
provider: 'github',
35+
owner: 'ExpTechTW',
36+
repo: 'ES-Net-Wave',
37+
vPrefixedTagName: false,
38+
});
39+
}
40+
41+
// Event handlers
42+
autoUpdater.on('checking-for-update', () => {
43+
logOTA('Checking for update...');
44+
const mainWindow = getMainWindow();
45+
if (mainWindow && !mainWindow.isDestroyed()) {
46+
mainWindow.webContents.send('update-checking');
47+
}
48+
});
49+
50+
autoUpdater.on('update-available', (info) => {
51+
logOTA(`Update available: ${info.version}`);
52+
const mainWindow = getMainWindow();
53+
if (mainWindow && !mainWindow.isDestroyed()) {
54+
mainWindow.webContents.send('update-available', info);
55+
}
56+
});
57+
58+
autoUpdater.on('update-not-available', (info) => {
59+
logOTA(`Update not available: ${info.version}`);
60+
const mainWindow = getMainWindow();
61+
if (mainWindow && !mainWindow.isDestroyed()) {
62+
mainWindow.webContents.send('update-not-available', info);
63+
}
64+
});
65+
66+
autoUpdater.on('download-progress', (progressObj) => {
67+
logOTA(`Download progress: ${progressObj.percent.toFixed(2)}%`);
68+
const mainWindow = getMainWindow();
69+
if (mainWindow && !mainWindow.isDestroyed()) {
70+
mainWindow.webContents.send('download-progress', progressObj);
71+
}
72+
});
73+
74+
autoUpdater.on('update-downloaded', (info) => {
75+
logOTA(`Update downloaded: ${info.version}`);
76+
const mainWindow = getMainWindow();
77+
if (mainWindow && !mainWindow.isDestroyed()) {
78+
mainWindow.webContents.send('update-downloaded', info);
79+
}
80+
// Wait 3 seconds before installing
81+
setTimeout(() => {
82+
autoUpdater.quitAndInstall(true, true);
83+
}, 3000);
84+
});
85+
86+
autoUpdater.on('error', (err) => {
87+
const message = err && err.message ? err.message : String(err);
88+
logOTA(`Update error: ${message}`);
89+
const mainWindow = getMainWindow();
90+
if (mainWindow && !mainWindow.isDestroyed()) {
91+
mainWindow.webContents.send('update-error', message);
92+
}
93+
});
94+
95+
startAutoUpdateScheduler();
96+
logOTA('Auto updater initialized');
97+
} catch (err) {
98+
logOTA(`Initialization error: ${err}`);
99+
}
100+
}
101+
102+
/**
103+
* Start the auto update scheduler
104+
*/
105+
function startAutoUpdateScheduler() {
106+
if (!app.isPackaged) {
107+
logOTA('Auto update skipped (not packaged)');
108+
return;
109+
}
110+
111+
if (updateCheckInterval) {
112+
logOTA('Auto update scheduler already running');
113+
return;
114+
}
115+
116+
logOTA('Auto update scheduler started');
117+
// Check immediately
118+
autoUpdater.checkForUpdates().catch(() => undefined);
119+
// Then check every 5 minutes
120+
updateCheckInterval = setInterval(() => {
121+
autoUpdater.checkForUpdates().catch(() => undefined);
122+
}, 300000);
123+
}
124+
125+
/**
126+
* Stop the auto update scheduler
127+
*/
128+
function stopAutoUpdateScheduler() {
129+
if (updateCheckInterval) {
130+
clearInterval(updateCheckInterval);
131+
updateCheckInterval = null;
132+
logOTA('Auto update scheduler stopped');
133+
}
134+
}
135+
136+
export { initAutoUpdater, stopAutoUpdateScheduler };

tsconfig.main.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"declaration": false,
1515
"sourceMap": true
1616
},
17-
"include": ["src/main.ts"],
17+
"include": ["src/main.ts", "src/ts/utils/ota.ts"],
1818
"exclude": ["node_modules", "dist"]
1919
}

0 commit comments

Comments
 (0)