-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (124 loc) · 4.02 KB
/
index.js
File metadata and controls
153 lines (124 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const path = require('path');
const fs = require('fs/promises');
const { app, BrowserWindow, ipcMain } = require('electron');
const serial = require('./serial');
const { parseSerialCommand } = require('./serial-protocol');
let mainWindow;
function formatPortLabel(port) {
const portPath = String(port?.path || '');
const windowsMatch = portPath.match(/^COM(\d+)$/i);
if (windowsMatch) {
return `COM ${windowsMatch[1]}`;
}
const segments = portPath.split(/[\\/]/).filter(Boolean);
return segments[segments.length - 1] || portPath;
}
function createWindow() {
const rendererEntry = path.join(__dirname, 'web', 'dist', 'index.html');
mainWindow = new BrowserWindow({
width: 1416,
height: 818,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadFile(rendererEntry);
mainWindow.on('closed', () => {
mainWindow = null;
});
}
function startSerialScan() {
serial.stopScan();
serial.scan((ports) => {
if (!mainWindow || mainWindow.isDestroyed()) {
return;
}
const formatted = ports.map((port) => ({
path: port.path,
label: formatPortLabel(port)
}));
mainWindow.webContents.send('serial-ports', formatted);
});
}
function forwardParsedSerialData(message) {
if (!mainWindow || mainWindow.isDestroyed()) {
return;
}
if (typeof message !== 'string') {
return;
}
const parsed = parseSerialCommand(message);
if (!parsed) {
return;
}
mainWindow.webContents.send('serial-data', parsed);
}
app.whenReady().then(() => {
createWindow();
startSerialScan();
ipcMain.handle('serial-connect', async (_event, portPath) => {
if (!portPath) {
throw new Error('No serial port path provided');
}
await serial.connect(portPath);
serial.on('log', forwardParsedSerialData);
serial.on('module', forwardParsedSerialData);
serial.on('status', forwardParsedSerialData);
serial.on('balancing', forwardParsedSerialData);
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('serial-status', { connected: true, path: portPath });
}
return { connected: true };
});
ipcMain.handle('serial-disconnect', async () => {
serial.disconnect();
startSerialScan();
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('serial-status', { connected: false });
}
return { connected: false };
});
ipcMain.handle('serial-send', async (_event, message) => {
if (typeof message !== 'string' || !message.trim()) {
throw new Error('No serial message provided');
}
const sent = serial.send(message.trim());
if (!sent) {
throw new Error('Failed to send serial message');
}
return { sent: true };
});
ipcMain.handle('read-module-names', async () => {
const filePath = path.join(__dirname, 'modules.json');
try {
const content = await fs.readFile(filePath, 'utf8');
const parsed = JSON.parse(content);
return parsed && typeof parsed === 'object' ? parsed : {};
} catch (error) {
if (error && error.code === 'ENOENT') {
return {};
}
throw error;
}
});
serial.onClose(() => {
startSerialScan();
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('serial-status', { connected: false });
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
startSerialScan();
}
});
});
app.on('window-all-closed', () => {
serial.stopScan();
if (process.platform !== 'darwin') {
app.quit();
}
});