FreeDATA/gui/preload-main.js

272 lines
9.8 KiB
JavaScript
Raw Normal View History

2021-07-17 07:03:40 +00:00
const sock = require('./sock.js')
const daemon = require('./daemon.js')
2021-07-23 15:40:44 +00:00
const configPath = './config.json'
const config = require(configPath);
2021-07-24 07:06:22 +00:00
const {
ipcRenderer
} = require('electron');
2021-07-23 15:40:44 +00:00
const fs = require('fs');
// START INTERVALL COMMAND EXECUTION FOR STATES
2021-07-24 07:06:22 +00:00
setInterval(daemon.getDaemonState, 1000)
setInterval(sock.getTncState, 250)
setInterval(sock.getDataState, 500)
setInterval(sock.getHeardStations, 500)
2021-07-23 15:40:44 +00:00
// UPDATE FFT DEMO
2021-07-24 07:06:22 +00:00
updateFFT = function(fft) {
var fft = Array.from({
length: 2048
}, () => Math.floor(Math.random() * 10));
spectrum.addData(fft);
2021-07-23 15:40:44 +00:00
}
2021-07-24 07:06:22 +00:00
setInterval(updateFFT, 250)
2021-07-23 15:40:44 +00:00
2021-07-24 07:06:22 +00:00
// WINDOW LISTENER
2021-07-23 15:40:44 +00:00
window.addEventListener('DOMContentLoaded', () => {
2021-07-24 07:06:22 +00:00
// LOAD SETTINGS
document.getElementById("tnc_adress").value = config.tnc_host
document.getElementById("tnc_port").value = config.tnc_port
document.getElementById("myCall").value = config.mycall
document.getElementById("myGrid").value = config.mygrid
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// Create spectrum object on canvas with ID "waterfall"
2021-07-17 07:03:40 +00:00
global.spectrum = new Spectrum(
"waterfall", {
spectrumPercent: 20
2021-07-24 07:06:22 +00:00
});
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// on change port and host
2021-07-23 15:40:44 +00:00
2021-07-24 07:06:22 +00:00
document.getElementById("tnc_adress").addEventListener("change", () => {
console.log(document.getElementById("tnc_adress").value)
config.tnc_host = document.getElementById("tnc_adress").value
config.daemon_host = document.getElementById("tnc_adress").value
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2021-07-23 15:40:44 +00:00
});
2021-07-24 07:06:22 +00:00
document.getElementById("tnc_port").addEventListener("change", () => {
config.tnc_port = document.getElementById("tnc_port").value
config.daemon_port = document.getElementById("tnc_port").value + 1
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
});
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// saveMyCall button clicked
document.getElementById("saveMyCall").addEventListener("click", () => {
2021-07-17 07:03:40 +00:00
callsign = document.getElementById("myCall").value
2021-07-23 15:40:44 +00:00
config.mycall = callsign
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2021-07-17 07:03:40 +00:00
sock.saveMyCall(callsign)
2021-07-24 07:06:22 +00:00
});
// saveMyGrid button clicked
document.getElementById("saveMyGrid").addEventListener("click", () => {
2021-07-17 07:03:40 +00:00
grid = document.getElementById("myGrid").value
2021-07-23 15:40:44 +00:00
config.mygrid = grid
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2021-07-17 07:03:40 +00:00
sock.saveMyGrid(grid)
2021-07-24 07:06:22 +00:00
2021-07-23 15:40:44 +00:00
});
2021-07-24 07:06:22 +00:00
// startPing button clicked
2021-07-24 07:06:22 +00:00
document.getElementById("sendPing").addEventListener("click", () => {
dxcallsign = document.getElementById("dxCall").value
sock.sendPing(dxcallsign)
2021-07-24 07:06:22 +00:00
});
// sendCQ button clicked
document.getElementById("sendCQ").addEventListener("click", () => {
sock.sendCQ()
});
// startTNC button clicked
document.getElementById("startTNC").addEventListener("click", () => {
2021-07-17 07:03:40 +00:00
var rx_audio = document.getElementById("audio_input_selectbox").value
2021-07-24 07:06:22 +00:00
var tx_audio = document.getElementById("audio_output_selectbox").value
2021-07-17 07:03:40 +00:00
var deviceid = document.getElementById("hamlib_deviceid").value
var deviceport = document.getElementById("hamlib_deviceport").value
var ptt = document.getElementById("hamlib_ptt").value
daemon.startTNC(rx_audio, tx_audio, deviceid, deviceport, ptt)
2021-07-24 07:06:22 +00:00
setTimeout(function() {
sock.saveMyCall(config.mycall);
}, 5000);
setTimeout(function() {
sock.saveMyGrid(config.mygrid);
}, 5000);
})
// stopTNC button clicked
document.getElementById("stopTNC").addEventListener("click", () => {
2021-07-17 07:03:40 +00:00
daemon.stopTNC()
2021-07-24 07:06:22 +00:00
})
// openDataModule button clicked
document.getElementById("openDataModule").addEventListener("click", () => {
//data.show()
let Data = {
message: "Hello World !"
};
2021-07-24 07:06:22 +00:00
ipcRenderer.send('show-data-window', Data);
})
2021-07-17 07:03:40 +00:00
})
2021-07-24 07:06:22 +00:00
ipcRenderer.on('action-update-tnc-state', (event, arg) => {
// PTT STATE
if (arg.ptt_state == 'True') {
document.getElementById("ptt_state").className = "btn btn-danger";
console.log("PTT TRUE!!!")
} else if (arg.ptt_state == 'False') {
document.getElementById("ptt_state").className = "btn btn-success";
} else {
document.getElementById("ptt_state").className = "btn btn-secondary"
}
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// BUSY STATE
if (arg.busy_state == 'BUSY') {
document.getElementById("busy_state").className = "btn btn-danger";
} else if (arg.busy_state == 'IDLE') {
document.getElementById("busy_state").className = "btn btn-success";
} else {
document.getElementById("busy_state").className = "btn btn-secondary"
}
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// ARQ STATE
if (arg.arq_state == 'DATA') {
document.getElementById("arq_state").className = "btn btn-warning";
} else if (arg.arq_state == 'IDLE') {
document.getElementById("arq_state").className = "btn btn-secondary";
} else {
document.getElementById("arq_state").className = "btn btn-secondary"
}
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// RMS
document.getElementById("rms_level").setAttribute("aria-valuenow", arg.rms_level)
document.getElementById("rms_level").setAttribute("style", "width:" + arg.rms_level + "%;")
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
// CHANNEL STATE
if (arg.channel_state == 'RECEIVING_SIGNALLING') {
document.getElementById("signalling_state").className = "btn btn-success";
document.getElementById("data_state").className = "btn btn-secondary";
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
} else if (arg.channel_state == 'SENDING_SIGNALLING') {
document.getElementById("signalling_state").className = "btn btn-danger";
document.getElementById("data_state").className = "btn btn-secondary";
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
} else if (arg.channel_state == 'RECEIVING_DATA') {
document.getElementById("signalling_state").className = "btn btn-secondary";
document.getElementById("data_state").className = "btn btn-success";
} else if (arg.channel_state == 'SENDING_DATA') {
document.getElementById("signalling_state").className = "btn btn-secondary";
document.getElementById("data_state").className = "btn btn-danger";
} else {
document.getElementById("signalling_state").className = "btn btn-secondary"
document.getElementById("busy_state").className = "btn btn-secondary"
}
// SET FREQUENCY
document.getElementById("frequency").value = arg.frequency
// SET MODE
document.getElementById("mode").value = arg.mode
// SET BANDWITH
document.getElementById("bandwith").value = arg.bandwith
});
2021-07-17 07:03:40 +00:00
ipcRenderer.on('action-update-daemon-state', (event, arg) => {
2021-07-24 07:06:22 +00:00
// UPDATE AUDIO INPUT
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
if (document.getElementById("audio_input_selectbox").length != arg.input_devices.length) {
2021-07-17 07:03:40 +00:00
document.getElementById("audio_input_selectbox").innerHTML = ""
2021-07-24 07:06:22 +00:00
for (i = 0; i < arg.input_devices.length; i++) {
2021-07-17 07:03:40 +00:00
var option = document.createElement("option");
option.text = arg.input_devices[i]['NAME'];
option.value = arg.input_devices[i]['ID'];
document.getElementById("audio_input_selectbox").add(option);
}
}
// UPDATE AUDIO OUTPUT
2021-07-24 07:06:22 +00:00
if (document.getElementById("audio_output_selectbox").length != arg.output_devices.length) {
2021-07-17 07:03:40 +00:00
document.getElementById("audio_output_selectbox").innerHTML = ""
2021-07-24 07:06:22 +00:00
for (i = 0; i < arg.output_devices.length; i++) {
2021-07-17 07:03:40 +00:00
var option = document.createElement("option");
option.text = arg.output_devices[i]['NAME'];
option.value = arg.output_devices[i]['ID'];
document.getElementById("audio_output_selectbox").add(option);
}
}
2021-07-24 07:06:22 +00:00
// TNC RUNNING STATE
document.getElementById("tnc_running_state").innerHTML = arg.tnc_running_state;
if (arg.tnc_running_state == "running") {
document.getElementById('hamlib_deviceid').disabled = true
document.getElementById('hamlib_deviceport').disabled = true
document.getElementById('hamlib_ptt').disabled = true
document.getElementById('audio_input_selectbox').disabled = true
document.getElementById('audio_output_selectbox').disabled = true
document.getElementById('stopTNC').disabled = false
document.getElementById('startTNC').disabled = true
document.getElementById('myCall').disabled = false
document.getElementById('saveMyCall').disabled = false
document.getElementById('myGrid').disabled = false
document.getElementById('saveMyGrid').disabled = false
2021-07-17 07:03:40 +00:00
} else {
2021-07-24 07:06:22 +00:00
document.getElementById('hamlib_deviceid').disabled = false
document.getElementById('hamlib_deviceport').disabled = false
document.getElementById('hamlib_ptt').disabled = false
document.getElementById('audio_input_selectbox').disabled = false
document.getElementById('audio_output_selectbox').disabled = false
document.getElementById('stopTNC').disabled = true
document.getElementById('startTNC').disabled = false
document.getElementById('myCall').disabled = true
document.getElementById('saveMyCall').disabled = true
document.getElementById('myGrid').disabled = true
document.getElementById('saveMyGrid').disabled = true
2021-07-17 07:03:40 +00:00
}
2021-07-24 07:06:22 +00:00
});
2021-07-17 07:03:40 +00:00
ipcRenderer.on('action-update-daemon-connection', (event, arg) => {
2021-07-24 07:06:22 +00:00
if (arg.daemon_connection == 'open') {
document.getElementById("daemon_connection_state").className = "btn btn-success";
}
2021-07-24 07:06:22 +00:00
if (arg.daemon_connection == 'opening') {
document.getElementById("daemon_connection_state").className = "btn btn-warning";
}
if (arg.daemon_connection == 'closed') {
document.getElementById("daemon_connection_state").className = "btn btn-danger";
}
2021-07-24 07:06:22 +00:00
});
2021-07-24 07:06:22 +00:00
ipcRenderer.on('run-tnc-command', (event, arg) => {
if (arg.command == 'saveMyCall') {
sock.saveMyCall(arg.callsign)
}
2021-07-24 07:06:22 +00:00
if (arg.command == 'saveMyGrid') {
sock.saveMyGrid(arg.grid)
}
2021-07-24 07:06:22 +00:00
if (arg.command == 'ping') {
sock.sendPing(arg.dxcallsign)
}
});