-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
300 lines (275 loc) · 9.93 KB
/
main.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// load libraries (if that's the right word, who knows)
const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron/main')
const path = require('node:path')
const fs = require('node:fs')
let mainWindow;
// path for resources; switch depending on whether running packaged or not
const resPath = app.isPackaged ? process.resourcesPath : __dirname;
// debug mode bool: if true, timestamps for heights are pulled from DGS file so calculation
// can be run using any test data file.
let debugMode = false;
// main window-creating function!
function createWindow () {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true
}
})
// Define a custom menu template
const menuTemplate = [
{
label: 'File ',
submenu: [
{
label: 'Debug mode on/off',
click: () => {
debugMode = !debugMode;
mainWindow.webContents.send('toggle-debug', debugMode);
}
},
{
label: 'Close ',
accelerator: 'Ctrl+W',
click: () => {
const focusedWindow = BrowserWindow.getFocusedWindow();
if (focusedWindow) {
focusedWindow.close();
}
}
},
{
label: 'Exit ',
accelerator: 'Ctrl+Q', // Shortcut for quitting the app
click() {
app.quit();
}
}
]
},
{
label: 'Help ',
submenu: [
{
label: 'Help? ',
click: () => {
createHelpWindow();
}
},
{
label: 'Manual ',
click: () => {
openManualWindow();
}
}
]
}
];
// Set the custom menu
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
mainWindow.loadFile('index.html')
// read the list of ships from file
ipcMain.handle('get-ship-options', async () => {
const data = fs.readFileSync(path.join(resPath,'./database/ships.db'), 'utf8');
const options = data.split('\n').filter(option => option.trim() !== '');
options.unshift('Choose Ship'); // for nothing selected at the start
return options;
});
// read the station database
ipcMain.handle('get-stations', async () => {
const data = fs.readFileSync(path.join(resPath,'./database/stations.db'), 'utf8');
const chunks = data.split('[STATION');
chunks.shift();
const stationDB = [];
stationDB.push({"NAME":'Choose Station',"NUMBER":"0-000","GRAVITY":"-999"});
chunks.forEach(chunk => {
const thisSta = {};
const lines = chunk.split('\n').filter(option => option.trim() !== '');
lines.forEach(line => {
if (line.startsWith('_')) {
}
else {
const [key,value] = line.split('=');
thisSta[key.trim()] = value.trim().replace(/^"|"$/g, '');
}
});
stationDB.push(thisSta);
});
return stationDB;
})
}
function openManualWindow() {
let helpWindow = new BrowserWindow({
width: 1000,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
const pdfPath = path.join(resPath,'./docs/main.pdf');
helpWindow.loadURL(`file://${pdfPath}`);
}
function createHelpWindow() {
let helpWindow = new BrowserWindow({
width: 600,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
helpWindow.loadFile('help.html');
}
// use the functions to
app.whenReady().then(() => {
//ipcMain.handle('dialog:openFile', fileReadData)
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
ipcMain.handle('open-dialog-landmeters', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({defaultPath: path.join(resPath, './database/land-cal'),properties:['openFile']});
if (!canceled) {
fs.readFile(filePaths[0],'utf8',(err,data) => {
if (err) {
console.error('error reading file:', err);
return
}
const rows = data.trim().split('\n');
rows.shift(); // first row is a date
const brackets = [];
const mgals = [];
const factors = [];
rows.forEach(row => {
const columns = row.trim().split(/\s+/);
brackets.push(parseFloat(columns[0]));
mgals.push(parseFloat(columns[1]));
factors.push(parseFloat(columns[2]));
});
mainWindow.webContents.send('landcal',[brackets,mgals,factors,filePaths[0]]);
});
}
});
ipcMain.handle('open-dialog-dgsgrav', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog(mainWindow, {
title: 'Select Gravimeter Files',
properties: ['openFile', 'multiSelections'],
});
if (!canceled) {
let rows = [];
filePaths.forEach(filepath => {
const data = fs.readFileSync(filepath,'utf8');
const lines = data.trim().split('\n');
lines.forEach((line) => {
rows.push(line);
});
});
mainWindow.webContents.send('dgsgrav',rows);
}
});
// receiving and writing tie data in reports and/or toml files
ipcMain.on('tie-data-kv-object', async (event, data) => {
console.log('Received data from renderer'); // :, data);
let templatePath = ''; // will be toml or txt report (land or not)
let filePath = '';
if (data['writeTOML']) { // writing toml for possible reread
const { canceled, filePath } = await dialog.showSaveDialog(mainWindow, {
title: 'Save tie to toml',
defaultPath: path.join(app.getPath('documents'), 'output_tie.toml'),
filters: [
{ name: 'TOML Files', extensions: ['toml'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (canceled) {
return null;
} else {
templatePath = path.join(resPath,'./templates/toml_template.txt');
generateTextFile(templatePath, filePath, data);
}
} else { // txt report, not toml
const { canceled, filePath } = await dialog.showSaveDialog(mainWindow, {
title: 'Save tie report',
defaultPath: path.join(app.getPath('documents'), 'tie_report.txt'),
filters: [
{ name: 'txt Files', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (canceled) {
return null;
} else {
templatePath = data["isLandTie"] ? path.join(resPath, './templates/report_template_landtie.txt') : path.join(resPath, './templates/report_template.txt');
generateTextFile(templatePath, filePath, data);
}
}
console.log('File written successfully:', filePath);
})
// Function to read the template, replace placeholders, and write the output
function generateTextFile(templatePath, outputPath, variables) {
// Read the template file
const template = fs.readFileSync(templatePath, 'utf8');
// Replace placeholders with variable values
let outputText = template;
for (const key in variables) {
const placeholder = `{{${key}}}`;
outputText = outputText.replace(new RegExp(placeholder, 'g'), variables[key]);
}
// Write the output to a new file
fs.writeFileSync(outputPath, outputText, 'utf8');
}
// open and read a toml file, send contents to renderer
ipcMain.handle('open-read-toml', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog();
if (!canceled) {
fs.readFile(filePaths[0],'utf8',(err,data) => {
if (err) {
console.error('error reading file:', err);
return
}
const lines = data.split('\n');
const result = {};
lines.forEach(line => {
line = line.trim()
if (!line || line.startsWith('#') || line.startsWith('[')) {
// do nothing with blank lines, commentss, and section headings
} else {
var [key, value] = line.split('=').map(part => part.trim())
// handle quoted strings
if (value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1,-1)
}
if (key.endsWith('.m') || key.endsWith('.c') || key.endsWith('.m_avg') || key.endsWith('.h')) {
key = key.replace('.','');
}
// figure out what everything is
if (!isNaN(value)) {
result[key] = Number(value);
} else if (value.startsWith('false')) {
result[key] = false;
} else if (value.startsWith('true')) {
result[key] = true;
} else if (value.startsWith('null')) {
result[key] = null;
} else if (key.endsWith('.t') || key.endsWith('.t_avg')) {
key = key.replace('.','');
result[key] = Date.parse(value);
} else {
result[key] = value; // stringsss
}
}
})
mainWindow.webContents.send('tomlread',result);
//console.log(result);
})
}
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');