First working version for both web and node.js

This commit is contained in:
2023-01-11 21:53:42 +01:00
parent 274cd54133
commit ac3e1ea0d6
3 changed files with 137 additions and 128 deletions
+20 -5
View File
@@ -31,14 +31,14 @@
<tr>
<td style="vertical-align: top;">
<table cellpadding="5">
<tr>
<tr id="FileRow">
<td><label>File name prefix:</label></td>
<td>
<input name="fileNamePrefix" id="fileNamePrefix" type="text" style="width:70%" value="SrTiO3"/>
<input name="trimPath" id="trimPath" type="text" style="width:70%;visibility:hidden;"/>
</td>
</tr>
<tr>
<tr id="FolderRow">
<td><label>Save folder:</label></td>
<td>
<input type="text" style="width:70%" id="workPath" name="workPath" readonly/>
@@ -46,6 +46,7 @@
id="browseFolde" onclick="ipcRenderer.send('browseFolder');">
</td>
</tr>
<tr>
<td><label>Number of Layers:</label></td>
<td><input name="numLayer" id="numLayer" type="number" size="3" step="1" min="1" max="100" value="1" onchange="adjust_table()"></td>
</tr>
@@ -374,8 +375,22 @@
// Open the default Layers tab
document.getElementById("btnLayers").click();
// Set up after first load (electron specific menu calls)
firstLoad();
let webOrApp = amIWeb();
if (webOrApp) {
console.log('this script is not running in Node.js');
// Run as a web app
//let row = document.getElementById("FileRow");
//row.parentNode.removeChild(row);
//row = document.getElementById("FolderRow");
//row.parentNode.removeChild(row);
document.getElementById("FileRow").style.visibility = "hidden";;
document.getElementById("FolderRow").style.visibility = "hidden";;
} else {
console.log('this script is running in Node.js');
// Run as standalone app
// Set up after first load (electron specific menu calls)
firstLoad();
}
</script>
</html>
+38 -123
View File
@@ -1045,15 +1045,25 @@ function sum(array){
function startSequence(All) {
let cmd = '';
let trimBin = All['trimPath']+"/trimspNL";
// Check if the trimspNL binary is found
if (!fileExists(trimBin)) {
// if not found, try in PATH and hope for the best
trimBin = "trimspNL";
let randStr = '';
let webOrApp = amIWeb();
// Only for Node.js
if (!webOrApp) {
// Check if the trimspNL binary is found
if (!fileExists(trimBin)) {
// if not found, try in PATH and hope for the best
trimBin = "trimspNL";
}
// Check if workPath exists otherwise create it
checkDir(All['workPath']); // from TrimSPelec.js, Electron/Node specific
} else {
// Add random string (5 char) to file names
randStr = (Math.random() + 1).toString(36).substring(7);
All["fileNamePrefix"] = randStr;
All["workPath"] = "/tmp/" + randStr;
}
// Check if workPath exists otherwise create it
checkDir(All['workPath']); // from TrimSPelec.js, Electron/Node specific
let Progress =0;
document.getElementById("pBar").style.width = Progress + "%";
document.getElementById("pBar").innerHTML = Progress + "%";
@@ -1120,15 +1130,17 @@ function startSequence(All) {
let eingabe1=CreateInpFile(All);
if (eingabe1=="ERROR") {return(0);}
let FILENAME=All["workPath"]+"/"+All["fileNamePrefix"]+"_"+ScanName+SValue;
writeAsciiFile(FILENAME+".inp",eingabe1)
// Prepare command and execute
cmd = "cd " + All["workPath"];
cmd += ";cp " + FILENAME + ".inp eingabe1.inp";
cmd += ";" + trimBin;
cmd += "; mv -f ausgabe1.rge " + FILENAME + ".reg";
cmd += "; mv -f ausgabe1.out " + FILENAME + ".out";
cmd += "; mv -f ausgabe1.err " + FILENAME + ".err";
execute(cmd);
writeAsciiFile(FILENAME+".inp",eingabe1);
if (!webOrApp) {
// Prepare command and execute
cmd = "cd " + All["workPath"];
cmd += ";cp " + FILENAME + ".inp eingabe1.inp";
cmd += ";" + trimBin;
cmd += "; mv -f ausgabe1.rge " + FILENAME + ".reg";
cmd += "; mv -f ausgabe1.out " + FILENAME + ".out";
cmd += "; mv -f ausgabe1.err " + FILENAME + ".err";
execute(cmd);
}
// Single run: End
// Read stopping profiels
@@ -1182,7 +1194,7 @@ function startSequence(All) {
writeAsciiFile(seq_file, data);
// Remove the fort.33 and other redundant files
cmd="cd "+All["workPath"]+";rm -f eingabe1.inp; rm -f fort.33";
cmd="cd " + All["workPath"] + ";rm -f eingabe1.inp; rm -f fort.33";
execute(cmd);
Progress=100;
document.getElementById("pBar").style.width = Progress + "%";
@@ -1190,17 +1202,13 @@ function startSequence(All) {
return(0);
}
function readDatFile(filename,flag) {
function readDatFile(filename) {
// Read column data file and return
// flag=1 use XMLHttpRequest
// cols - labels of columns
// data - 2D array with the columns
let lines = "";
if (flag) {
lines = loadAscii(filename);
} else {
lines = readAsciiFile(filename);
}
lines = readAsciiFile(filename);
let data = [];
let i=0; // line counter
for (let line of lines.trim().split('\n')) {
@@ -1323,106 +1331,13 @@ function startSim() {
// "http://musruser.psi.ch/cgi-bin/TrimSP.cgi?TrimSPcfg="+TrimSPcfg+'go=start';
}
// Function to load and ascii file
// async read, be careful with use
function loadAscii(file){
var xhttp;
if (file == "" || file == undefined || file == null) {
return 0;
function amIWeb() {
if (typeof module !== 'undefined' && module.exports) {
// Run as standalone app
return 0;
} else {
// Make an HTTP request using the attribute value as the file name:
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 404) {
console.log("File "+file+" not found.");
}
}
}
file += "?" + Date();
xhttp.open("GET", file, false);
xhttp.send();
return(xhttp.responseText);
// Run as a web app
return 1;
}
}
function sendToCGI(){
let args = "";
let All = prep_cfg(1);
for (key of Object.keys(All)) {
args += "&" + key + "=" + All[key] ;
}
let url = "http://musruser.psi.ch/cgi-bin/TrimSP2.cgi?go=start" + args;
console.log(url);
// document.getElementById("form_id").action = url;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Action to be performed when the document is read;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xhttp.responseText,"text/html");
console.log(xmlDoc.getElementById("links").innerHTML);
document.getElementById("links").innerHTML = xmlDoc.getElementById("links").innerHTML;
// Now we can plot things
plotThings(All);
}
};
xhttp.open("GET",url,true);
//xhttp.responseType = 'document';
xhttp.send();
}
function plotThings(All) {
let fileNamePrefix = All["fileNamePrefix"];
let path = "/tmp/"+fileNamePrefix+"/";
//let fileName = path + fileNamePrefix + ".rge";
//let rgeDat = loadAscii(fileName);
//console.log(rgeDat);
if (All["scanSeq"]) {
// For multiple runs or a scan
if (All["scanType"]=="scanVals") {
var SValues=All["scanList"].split(/,/);
var SdzValues=All["scanListdz"].split(/,/);
if (SValues.length == SdzValues.length) {All["SdzFlag"]=1;}
} else {
for (let Val=parseInt(All["scanFrom"]);Val<=parseInt(All["scanTo"]);Val=Val+parseInt(All["scanStep"])) {
SValues.push(Val);
}
}
let ScanName = "";
let ScanAttrib = "";
ScanName = "E";
ScanAttrib = "valEnergy";
if (All["comboScan"]==1) {
ScanName = "SigE";
ScanAttrib = "sigEnergy";
} else if (All["comboScan"]==2) {
ScanName = "Angle";
ScanAttrib = "valAngle";
} else if (All["comboScan"]==3) {
ScanName = "SigAngle";
ScanAttrib = "sigAngle";
} else if (All["comboScan"]==4) {
ScanName = "N";
ScanAttrib = "numberProj";
} else if (All["comboScan"]==5) {
ScanName = "Ld"+All["scandL"];
ScanAttrib = "L"+All["scandL"]+"d";
}
let iScan=0;
for (var SValue of SValues) {
let fileName = path + fileNamePrefix + "_" + ScanName + SValue + ".rge";
let [cols,data]= readDatFile(fileName,1);
console.log(fileName,cols,data);
}
} else {
// For a single run
let fileName = path + fileNamePrefix + ".rge";
let [cols,data]= readDatFile(fileName,1);
console.log(fileName,cols,data);
}
}
+79
View File
@@ -0,0 +1,79 @@
// This file contains function that are web app specific
function getFiles(dir, filelist){
fileList = [];
return fileList;
}
function execute(command) {
// exec(command, {stdio: 'inherit'});
}
function checkDir(directory) {
// Check whether directory exists, if not create it
return(1);
}
function writeAsciiFile(filename,content) {
// Write string content into ascii file filename
// Go via CGI script
//console.log(filename, content);
// Prepare CGI args
let cgiargs = "?fn="+filename;
let lines = content.split(/\n/);
for (let i=0; i<lines.length; i++) {
cgiargs += "&line" + i + "=" + lines[i];
}
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//console.log("resptext=",xhttp.responseText);
} else if (this.readyState == 4) {
console.log(xhttp.status, xhttp.statusText);
}
}
let request = "/cgi-bin/try.cgi"+cgiargs;
xhttp.open("GET", request, false);
xhttp.send();
return(1);
}
function readAsciiFile(filename) {
// Read ascii file filename and return content in a string
var xhttp;
let file = filename;
if (filename == "" || filename == undefined || filename == null) {
return 0;
} else {
// Make an HTTP request
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 404) {
console.log("File "+file+" not found.");
return(0);
}
}
}
// Force refresh, do not use cache
filename += "?" + Date();
//filename = "http://musruser.psi.ch" + filename;
xhttp.open("GET", filename, false);
xhttp.send();
return(xhttp.responseText);
}
}
function fileExists(filename) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', filename, false);
xhr.send();
if (xhr.status == "404") {
return 0;
} else {
return 1;
}
}