diff --git a/breath_plot.html b/breath_plot.html index ecb8a22d21d078719a2a38b2194c2a4885c62d5d..fb095817c875fe567b90a51e58098f9de0293b04 100644 --- a/breath_plot.html +++ b/breath_plot.html @@ -1416,33 +1416,43 @@ $( document ).ready(function() { // CONTROL PANEL INIT START - $("#control-slot button").click(function() { var lh = $("#dserverurl").val(); - $.ajax({url: lh+"/vent.html", - success: function(result) { - //console.log("result "+ result); + $.ajax({url: lh+"/vent.html"}) + .done(function(result) { + // Show the control panel $("#control-slot").html(result); + // Send PIRCS commands when START button is pressed $("#control-start").click(function(event) { - // Send a command to a connected device via serial port + console.log("Sending PIRCS..."); + var dict = { + M: $("#control-mode").val(), + B: $("#control-rr").val(), + I: $("#control-ie").val(), + P: $("#control-pinsp").val(), + E: $("#control-peep").val() + } - var rr = $("#control-rr").val(); - console.log("rr: " + rr); - var lh = "http://localhost:5000"; - $.ajax({url: lh+"/api/set?rr="+rr, - success: function(result) { + for (var k in dict){ + $.ajax({ + //url: lh+"/api/pircs?com=C&par="+parName+"&int="+interp+"&mod="+modifier+"&val="+val, + type: 'GET', + url: 'http://localhost:5000/api/pircs/', + dataType: 'json', + data: { com: "C", par: k, int: "T", mod: 0, val: dict[k] } + }).done(function(result) { console.log("result: " + JSON.stringify(result)); - }, - error: function(xhr, ajaxOptions, thrownError) { + }).fail(function(xhr, ajaxOptions, thrownError) { console.log("Error! " + xhr.status); console.log(thrownError); - } - }); - + }) + + } }); + // Update values on slider change $("#control-mode").on("input", () => { var m = $("#control-mode").val(); if (m === "0"){ @@ -1468,15 +1478,14 @@ $( document ).ready(function() { $("#control-peep").on("input", () => { $("#control-peep-val").html($("#control-peep").val()); }); - - }, - error: function(xhr, ajaxOptions, thrownError) { - console.log("Error! " + xhr.status); - console.log(thrownError); - } - }); + }) + .fail(function(){ + console.log("Couldn't open control panel!"); + }); }); + + // CONTROL PANEL INIT END diff --git a/server.js b/server.js index 6ef48baadd3a1812b5a64b8c6bc9a6aa8e8ed5c0..25bf8b42416c41c72a815112c3aea22e33cd46ac 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ var express = require('express'); +const cors = require('cors'); var app = express(); +app.use(cors()); const SerialPort = require('serialport'); //https://serialport.io/docs/guide-usage const Readline = require('@serialport/parser-readline'); @@ -43,6 +45,70 @@ app.get('/api/set', function(req, res) { }); }); +// /api/pircs?com=C&par=P&int=T&mod=0&val=400 +app.get('/api/pircs', function(req, res) { + var err = ''; + var x = '{ '; + if (req.query.com){ + x += '"com" : "' + req.query.com + '" , '; + } else { + err += "com not defined! "; + } + if (req.query.par){ + x += '"par" : "' + req.query.par + '" , '; + } else { + err += "par not defined! "; + } + if (req.query.int){ + x += '"int" : "' + req.query.int + '" , '; + } else { + err += "int not defined! "; + } + if (req.query.mod){ + x += '"mod" : ' + req.query.mod + ' , '; + } else { + err += "mod not defined! "; + } + if (req.query.val){ + x += '"val" : ' + req.query.val; + } else { + err += "val not defined! "; + } + x += ' }\n'; + + if (err.length > 0){ + err += "\n" + res.setHeader("Content-Type", "text/plain"); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.status(400).send(err); + } else { + + res.setHeader("Content-Type", "application/json"); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.status(200).send(x); + + port.write(x, (err) => { + if (err) { + return console.log('Error on write: ', err.message); + } + }); + } + + // { "com" : "C" , "par" : "P" , "int" : "T" , "mod" : 0 , "val" : 400 } +}); + +// /api/pircs2/C/P/T/0/400 +app.get('/api/pircs2/:com/:par/:int/:mod/:val', function(req,res) { + res.send(req.params); + port.write(JSON.stringify(req.params)+'\n', (err) => { + if (err) { + return console.log('Error on write: ', err.message); + } + }); + + // JSON.stringify returns: {"com":"C","par":"P","int":"T","mod":"0","val":"400"} +}) + /*app.get('/rr/:rr', function(req,res) { res.send(req.params); port.write(JSON.stringify(req.params)+'\n', (err) => {