FreeDATA/gui/main.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-09-04 18:23:58 +00:00
const {
app,
BrowserWindow,
ipcMain
} = require('electron')
2021-07-17 07:02:56 +00:00
const path = require('path')
2021-09-04 18:23:58 +00:00
const fs = require('fs')
2021-09-04 15:11:20 +00:00
2021-09-04 18:23:58 +00:00
app.setName("codec2-FreeDATA");
2021-09-04 15:11:20 +00:00
2021-09-04 18:54:01 +00:00
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.config")
2021-09-04 18:23:58 +00:00
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
var configPath = path.join(configFolder, 'config.json')
2021-09-04 14:33:17 +00:00
2021-09-04 18:23:58 +00:00
// create folder if not exists
if (!fs.existsSync(configFolder)) {
fs.mkdirSync(configFolder);
}
2021-09-04 14:33:17 +00:00
2021-09-04 18:23:58 +00:00
// create config file if not exists
2021-09-04 18:54:01 +00:00
var configContent = `
{
"tnc_host": "192.168.178.163",
"tnc_port": "3000",
"daemon_host": "192.168.178.163",
"daemon_port": "3001",
"mycall": "AA0AA",
"mygrid": "JN40aa",
"deviceid": "2028",
"deviceport": "/dev/ttyUSB0",
"serialspeed": "9600",
"ptt": "RTS",
"spectrum": "scatter",
"tnclocation": "localhost"
}
`;
2021-09-04 18:23:58 +00:00
if (!fs.existsSync(configPath)) {
2021-09-04 18:54:01 +00:00
fs.writeFileSync(configPath, configContent)
2021-09-04 18:23:58 +00:00
}
2021-09-10 15:59:33 +00:00
/*
// Creates receivedFiles folder if not exists
// https://stackoverflow.com/a/26227660
var appDataFolder = process.env.HOME
var applicationFolder = path.join(appDataFolder, "codec2-FreeDATA");
var receivedFilesFolder = path.join(applicationFolder, "receivedFiles");
// https://stackoverflow.com/a/13544465
fs.mkdir(receivedFilesFolder, {
recursive: true
}, function(err) {
console.log(err);
});
*/
2021-09-04 18:23:58 +00:00
const config = require(configPath);
const exec = require('child_process').exec;
2021-07-23 15:40:44 +00:00
2021-07-17 07:02:56 +00:00
let win = null;
let data = null;
2021-09-04 14:33:17 +00:00
var daemonProcess = null;
2021-07-24 07:06:22 +00:00
function createWindow() {
win = new BrowserWindow({
width: 1220,
2021-09-10 15:08:44 +00:00
height: 780,
2021-08-28 13:50:59 +00:00
autoHideMenuBar: true,
icon: __dirname + '/src/app-icon.png',
2021-07-24 07:06:22 +00:00
webPreferences: {
//preload: path.join(__dirname, 'preload-main.js'),
preload: require.resolve('./preload-main.js'),
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: false, //https://stackoverflow.com/questions/53390798/opening-new-window-electron/53393655 https://github.com/electron/remote
}
})
2021-08-28 13:50:59 +00:00
// hide menu bar
win.setMenuBarVisibility(false)
2021-09-04 18:23:58 +00:00
2021-07-24 07:06:22 +00:00
//open dev tools
2021-09-08 16:22:41 +00:00
/*win.webContents.openDevTools({
2021-07-24 07:06:22 +00:00
mode: 'undocked',
activate: true,
})
2021-09-05 15:30:46 +00:00
*/
2021-07-24 07:06:22 +00:00
win.loadFile('src/index.html')
/*
2021-07-17 07:02:56 +00:00
data = new BrowserWindow({
2021-07-24 07:06:22 +00:00
height: 900,
width: 600,
parent: win,
webPreferences: {
preload: require.resolve('./preload-data.js'),
nodeIntegration: true,
}
})
//open dev tools
2021-07-24 07:06:22 +00:00
data.webContents.openDevTools({
mode: 'undocked',
activate: true,
})
2021-09-08 16:22:41 +00:00
2021-07-24 07:06:22 +00:00
data.loadFile('src/data-module.html')
data.hide()
*/
2021-07-17 07:02:56 +00:00
// Emitted when the window is closed.
2021-07-24 07:06:22 +00:00
win.on('closed', function() {
2021-07-17 07:02:56 +00:00
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
data = null;
})
2021-07-24 07:06:22 +00:00
/*
data.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
})
*/
2021-07-17 07:02:56 +00:00
2021-07-24 07:06:22 +00:00
// https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron
2021-08-08 09:43:50 +00:00
/*
2021-07-24 07:06:22 +00:00
data.on('close', function(evt) {
evt.preventDefault();
data.hide()
});
2021-08-08 09:43:50 +00:00
*/
2021-07-17 07:02:56 +00:00
}
app.whenReady().then(() => {
2021-07-24 07:06:22 +00:00
createWindow()
2021-09-04 18:23:58 +00:00
2021-09-04 14:33:17 +00:00
// start daemon
// https://stackoverflow.com/a/5775120
2021-09-04 18:54:01 +00:00
console.log("Starting Daemon")
2021-09-04 14:33:17 +00:00
daemonProcess = exec('./daemon', function callback(error, stdout, stderr) {
2021-09-04 18:23:58 +00:00
// result
2021-09-04 18:54:01 +00:00
console.log(stdout)
2021-09-04 18:23:58 +00:00
console.log(error)
2021-09-04 18:54:01 +00:00
console.log(stderr)
2021-09-04 18:23:58 +00:00
});
2021-07-17 07:02:56 +00:00
2021-07-24 07:06:22 +00:00
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
2021-07-17 07:02:56 +00:00
})
app.on('window-all-closed', () => {
2021-09-04 14:33:17 +00:00
// kill daemon process
daemonProcess.kill('SIGINT');
2021-07-17 07:02:56 +00:00
2021-07-24 07:06:22 +00:00
if (process.platform !== 'darwin') {
app.quit()
}
2021-07-17 07:02:56 +00:00
})
// IPC HANDLER
2021-09-04 18:23:58 +00:00
/*
ipcMain.on('show-data-window', (event, arg) => {
2021-07-17 07:02:56 +00:00
data.show()
});
*/
2021-07-17 07:02:56 +00:00
ipcMain.on('request-update-tnc-state', (event, arg) => {
win.webContents.send('action-update-tnc-state', arg);
//data.webContents.send('action-update-tnc-state', arg);
2021-07-17 07:02:56 +00:00
});
2021-08-07 18:57:36 +00:00
/*
2021-07-23 15:40:44 +00:00
ipcMain.on('request-update-data-state', (event, arg) => {
//win.webContents.send('action-update-data-state', arg);
//data.webContents.send('action-update-data-state', arg);
2021-07-23 15:40:44 +00:00
});
ipcMain.on('request-update-heard-stations', (event, arg) => {
2021-07-25 14:35:50 +00:00
win.webContents.send('action-update-heard-stations', arg);
2021-07-23 15:40:44 +00:00
});
2021-08-07 18:57:36 +00:00
*/
2021-07-17 07:02:56 +00:00
ipcMain.on('request-update-daemon-state', (event, arg) => {
win.webContents.send('action-update-daemon-state', arg);
});
ipcMain.on('request-update-daemon-connection', (event, arg) => {
win.webContents.send('action-update-daemon-connection', arg);
});
ipcMain.on('run-tnc-command', (event, arg) => {
2021-09-04 18:23:58 +00:00
win.webContents.send('run-tnc-command', arg);
});
2021-08-16 18:04:03 +00:00
ipcMain.on('request-update-rx-buffer', (event, arg) => {
2021-09-04 18:23:58 +00:00
win.webContents.send('action-update-rx-buffer', arg);
2021-09-08 16:22:41 +00:00
});