SpeedyLoader/renderer.js

338 lines
10 KiB
JavaScript
Raw Normal View History

2018-12-31 05:59:18 -08:00
const serialport = require('serialport')
2019-01-01 15:47:54 -08:00
const {ipcRenderer} = require("electron")
2019-01-14 14:34:00 -08:00
const {remote} = require('electron')
const { shell } = require('electron')
2018-12-31 05:59:18 -08:00
function refreshSerialPorts()
{
serialport.list((err, ports) => {
console.log('Serial ports found: ', ports);
2018-12-31 05:59:18 -08:00
if (err) {
document.getElementById('serialDetectError').textContent = err.message
return
} else {
document.getElementById('serialDetectError').textContent = ''
}
if (ports.length === 0) {
document.getElementById('serialDetectError').textContent = 'No ports discovered'
}
select = document.getElementById('portsSelect');
//Clear the current options
for (i = 0; i <= select.options.length; i++)
2018-12-31 05:59:18 -08:00
{
select.remove(0); //Always 0 index (As each time an item is removed, everything shuffles up 1 place)
2018-12-31 05:59:18 -08:00
}
//Load the current serial values
2019-01-01 15:47:54 -08:00
for(var i = 0; i < ports.length; i++)
2018-12-31 05:59:18 -08:00
{
var newOption = document.createElement('option');
newOption.value = ports[i].comName;
newOption.innerHTML = ports[i].comName;
select.add(newOption);
2018-12-31 05:59:18 -08:00
}
var button = document.getElementById("btnInstall")
if(ports.length > 0)
{
select.selectedIndex = 0;
button.disabled = false;
}
else { button.disabled = true; }
2018-12-31 05:59:18 -08:00
})
}
2019-01-01 19:47:18 -08:00
function refreshDetails()
2019-01-01 15:47:54 -08:00
{
var selectElement = document.getElementById('versionsSelect');
var version = selectElement.options[selectElement.selectedIndex].value;
var url = "https://api.github.com/repos/noisymime/speeduino/releases/tags/" + version;
document.getElementById('detailsHeading').innerHTML = version;
2019-01-01 15:47:54 -08:00
2019-01-01 19:47:18 -08:00
var request = require('request');
const options = {
url: url,
headers: {
'User-Agent': 'request'
}
};
request.get(options, function (error, response, body) {
if (!error ) {
console.log(body);
var result = JSON.parse(body);
// Continue with your processing here.
textField = document.getElementById('detailsText');
//Need to convert the Markdown that comes from Github to HTML
var myMarked = require('marked');
textField.innerHTML = myMarked(result.body);
}
});
//Finally, make the details section visible
document.getElementById('details').style.display = "inline";
//And jump to it
window.location.href = "#details";
2019-01-01 15:47:54 -08:00
}
function refreshAvailableFirmwares()
{
//Disable the buttons. These are only re-enabled if the retrieve is successful
var DetailsButton = document.getElementById("btnDetails");
var ChoosePortButton = document.getElementById("btnChoosePort");
DetailsButton.disabled = true;
ChoosePortButton.disabled = true;
2019-01-01 15:47:54 -08:00
var request = require('request');
request.get('http://speeduino.com/fw/versions', {timeout: 10000}, function (error, response, body)
{
select = document.getElementById('versionsSelect');
2019-01-01 15:47:54 -08:00
if (!error && response.statusCode == 200) {
var lines = body.split('\n');
// Continue with your processing here.
2019-01-01 15:47:54 -08:00
for(var i = 0;i < lines.length;i++)
{
var newOption = document.createElement('option');
newOption.value = lines[i];
newOption.innerHTML = lines[i];
select.appendChild(newOption);
}
select.selectedIndex = 0;
//Re-enable the buttons
DetailsButton.disabled = false;
ChoosePortButton.disabled = false;
2019-01-01 15:47:54 -08:00
}
else if(error)
{
console.log("Error retrieving available firmwares");
var newOption = document.createElement('option');
if(error.code === 'ETIMEDOUT')
{
newOption.value = "Connection timed out";
newOption.innerHTML = "Connection timed out";
}
else
{
newOption.value = "Cannot retrieve firmware list";
newOption.innerHTML = "Cannot retrieve firmware list. Check internet connection and restart";
}
select.appendChild(newOption);
}
else if(response.statusCode == 404)
{
}
}
);
2019-01-01 15:47:54 -08:00
}
function downloadHex()
2018-12-31 05:59:18 -08:00
{
2018-12-31 05:59:18 -08:00
var e = document.getElementById('versionsSelect');
var DLurl = "http://speeduino.com/fw/bin/" + e.options[e.selectedIndex].value + ".hex";
console.log("Downloading: " + DLurl);
2018-12-31 05:59:18 -08:00
//Download the Hex file
ipcRenderer.send("download", {
url: DLurl,
properties: {directory: "downloads"}
});
}
function downloadIni()
{
var e = document.getElementById('versionsSelect');
2018-12-31 05:59:18 -08:00
var DLurl = "http://speeduino.com/fw/" + e.options[e.selectedIndex].value + ".ini";
console.log("Downloading: " + DLurl);
//Download the ini file
2018-12-31 05:59:18 -08:00
ipcRenderer.send("download", {
url: DLurl,
properties: {directory: "downloads"}
});
2018-12-31 05:59:18 -08:00
}
//Installing the Windows drivers
function installDrivers()
{
ipcRenderer.send("installWinDrivers", {
});
}
2019-07-08 21:56:05 -07:00
function downloadTune()
{
var e = document.getElementById('versionsSelect');
var DLurl = "https://raw.githubusercontent.com/noisymime/speeduino/" + e.options[e.selectedIndex].value + "/reference/Base%20Tunes/Speeduino%20base%20tune.msq";
console.log("Downloading: " + DLurl);
//Download the ini file
ipcRenderer.send("download", {
url: DLurl,
properties: {directory: "downloads"}
});
}
2019-01-01 15:47:54 -08:00
function uploadFW()
2018-12-31 05:59:18 -08:00
{
//Jump to the progress section
window.location.href = "#progress";
2019-01-03 23:54:39 -08:00
//Start the spinner
var spinner = document.getElementById('progressSpinner');
//Disable the Re-burn/re-install button
var reinstallButton = document.getElementById("btnReinstall")
reinstallButton.disabled = true;
2019-01-03 23:54:39 -08:00
//Remove any old icons
spinner.classList.remove('fa-pause');
spinner.classList.remove('fa-check');
spinner.classList.remove('fa-times');
spinner.classList.add('fa-spinner');
2019-01-14 14:21:56 -08:00
//Hide the terminal section incase it was there from a previous burn attempt
document.getElementById('terminalSection').style.display = "none";
//Same for the ini location link
document.getElementById('iniFileText').style.display = "none";
2019-01-14 14:21:56 -08:00
2019-01-01 19:47:18 -08:00
var statusText = document.getElementById('statusText');
2019-01-04 04:10:03 -08:00
var burnPercentText = document.getElementById('burnPercent');
statusText.innerHTML = "Downloading INI file"
downloadIni();
2019-01-01 19:47:18 -08:00
2019-01-04 04:10:03 -08:00
ipcRenderer.on("download complete", (event, file, state) => {
console.log("Saved file: " + file); // Full file path
var extension = file.substr(file.length - 3);
if(extension == "ini")
{
statusText.innerHTML = "Downloading firmware"
document.getElementById('iniFileText').style.display = "block"
document.getElementById('iniFileLocation').innerHTML = file
downloadHex();
}
else if(extension == "hex")
{
2019-01-14 14:21:56 -08:00
statusText.innerHTML = "Beginning upload..."
//Retrieve the select serial port
var e = document.getElementById('portsSelect');
uploadPort = e.options[e.selectedIndex].value;
console.log("Using port: " + uploadPort);
//Begin the upload
ipcRenderer.send("uploadFW", {
port: uploadPort,
firmwareFile: file
});
}
console.log();
});
ipcRenderer.on("upload completed", (event, code) => {
2019-01-02 06:05:30 -08:00
statusText.innerHTML = "Upload to arduino completed successfully!";
2019-01-04 04:10:03 -08:00
burnPercentText.innerHTML = "";
//Turn the spinner off
2019-01-03 23:54:39 -08:00
spinner.classList.remove('fa-spinner');
spinner.classList.add('fa-check');
//Re-enable the re-burn button
reinstallButton.disabled = false;
2019-01-01 15:47:54 -08:00
});
2019-01-04 04:10:03 -08:00
ipcRenderer.on("upload percent", (event, percent) => {
2019-01-14 14:21:56 -08:00
statusText.innerHTML = "Uploading firmware to board"
2019-01-04 04:10:03 -08:00
burnPercentText.innerHTML = " (" + percent + "%)";
});
ipcRenderer.on("upload error", (event, code) => {
2019-01-14 14:21:56 -08:00
statusText.innerHTML = "Upload to Speeduino failed";
//Mke the terminal/error section visible
2019-01-03 23:54:39 -08:00
document.getElementById('terminalSection').style.display = "block";
document.getElementById('terminalText').innerHTML = code;
2019-01-03 23:54:39 -08:00
spinner.classList.remove('fa-spinner');
spinner.classList.add('fa-times');
reinstallButton.disabled = false;
});
2018-12-31 05:59:18 -08:00
}
//Opens a native file manager window at the location of the downloaded ini file
function openFileMgr()
{
var location = document.getElementById('iniFileLocation').innerHTML
if (location != "")
{
shell.showItemInFolder(location);
}
}
2019-01-14 14:34:00 -08:00
function quit()
{
let w = remote.getCurrentWindow();
w.close();
}
2019-07-02 00:03:07 -07:00
function checkForUpdates()
{
var url = "https://api.github.com/repos/speeduino/SpeedyLoader/releases/latest";
2019-01-26 18:01:17 -08:00
2019-07-02 00:03:07 -07:00
//document.getElementById('detailsHeading').innerHTML = version;
var request = require('request');
const options = {
url: url,
headers: {
'User-Agent': 'request'
}
};
request.get(options, function (error, response, body) {
if (!error )
{
var result = JSON.parse(body);
latest_version = result.tag_name.substring(1);
console.log("Latest version: " + latest_version);
var semver = require('semver');
if(semver.gt(latest_version, remote.app.getVersion()))
{
//New version has been found
document.getElementById('update_url').setAttribute("href", result.html_url);
document.getElementById('update_text').style.display = "block";
}
}
});
}
window.onload = function () {
2019-01-26 18:01:17 -08:00
//Adds the current version number to the Titlebar
document.getElementById('title').innerHTML = "Speeduino Universal Firmware Loader (v" + remote.app.getVersion() + ")"
2019-07-02 00:03:07 -07:00
refreshAvailableFirmwares();
refreshSerialPorts();
checkForUpdates();
};