From 10c2acb0f802ea91279bae43404472bb20ed8e56 Mon Sep 17 00:00:00 2001
From: "Robert L. Read" <read.robert@gmail.com>
Date: Mon, 1 Mar 2021 14:46:28 -0600
Subject: [PATCH] significant bug fixed in breath computation

---
 breath_plot.html                              |   6 +++---
 js/respiration_math.js                        |  13 +++++++++---
 .../bindings/build/Release/bindings.node      | Bin 55516 -> 55516 bytes
 server.js                                     |  20 ++++++++++++++++--
 4 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/breath_plot.html b/breath_plot.html
index c570058..5f78ed0 100644
--- a/breath_plot.html
+++ b/breath_plot.html
@@ -28,7 +28,7 @@ Breath Plot: COVID-19 Respiration Analysis Software
 	<!-- Load plotly.js into the DOM -->
   <script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
 
-  <script src='./js/respiration_math.js'></script>
+  <script src='/respiration_math.js'></script>
 
   <title>Public Invention Respiration Analysis</title>
 
@@ -585,7 +585,7 @@ var intervalID = null;
 
 // This sould be better as time, but is easier
 // to do as number of samples.
-var MAX_SAMPLES_TO_STORE_S = 160;
+var MAX_SAMPLES_TO_STORE_S = 2000;
 var MAX_REFRESH = false;
 var samples = [];
 var INITS_ONLY = true;
@@ -1380,7 +1380,7 @@ function retrieveAndPlot(){
                 samples = samples.filter((s, index, self) =>
                                          s.ms >= (last_ms - DESIRED_DURATION_S*1000));
               }
-              console.log(samples);
+//              console.log(samples);
               if (samples.length > 0)
                 process(samples);
               console.log("END",LAST_SAMPLE_DATE);
diff --git a/js/respiration_math.js b/js/respiration_math.js
index 55c3b70..09790d4 100644
--- a/js/respiration_math.js
+++ b/js/respiration_math.js
@@ -122,7 +122,7 @@ function compute_fio2_mean(secs,samples) {
   }
 }
 
-
+// WARNING! With a low number of breaths, this may be wrong.
 function compute_respiration_rate(secs,samples,transitions,breaths) {
   // In order to compute the number of breaths
   // in the last s seconds, I compute those breaths
@@ -175,6 +175,7 @@ function compute_respiration_rate(secs,samples,transitions,breaths) {
       i--;
     }
     if ((cnt > 1) && (first_inhale_ms != last_inhale_ms)) {
+      // I now think this math is specious!!
       var inhalation_duration = last_inhale_ms - first_inhale_ms;
       var inhalation_duration_min = inhalation_duration / (60.0 * 1000.0);
       var rr = (cnt - 1) / inhalation_duration_min;
@@ -588,7 +589,7 @@ function testWorkSynthetic(){ // breaths give us inspiration transition points
     var vole = 0;
 
     var breaths = [];
-    var expiring = true;
+    var expiring = false;
 
     for(var i = 0; i < transitions.length; i++) {
       // We're looking for the end of the inhalation here!!
@@ -676,6 +677,12 @@ function computeMovingWindowTrace(samples,t,v) {
     var voli = 0;
     var vole = 0;
 
+    // This code was robust when I breathed through a mask,
+    // but on clean simulations with negative flow, seemes to
+    // go awry...
+    // It think really it makes more sense to find the first
+    // transition from a non-inspiring state to an inspiring state
+    // and start there.
     for(var i = 0; i < transitions.length; i++) {
       // We're looking for the end of the inhalation here!!
       if (((i -1) >= 0) && transitions[i-1].state == 1 && (transitions[i].state == 0 || transitions[i].state == -1 )) {
@@ -698,7 +705,7 @@ function computeMovingWindowTrace(samples,t,v) {
         vole = integrateSamples(last,transitions[i].sample,flows);
         last = transitions[i].sample;
       }
-      if (!expiring && transitions[i].state == -1) {
+      if (!expiring && (transitions[i].state == -1)) {
         expiring = true;
         voli = integrateSamples(last,transitions[i].sample,flows);
         last = transitions[i].sample;
diff --git a/node_modules/@serialport/bindings/build/Release/bindings.node b/node_modules/@serialport/bindings/build/Release/bindings.node
index 39bf8d4022b7bcd4b02aca29aa55228e808d17c6..0334927bbb4cbf9267d3375558293a5ae20a66a4 100755
GIT binary patch
delta 510
zcmcb!k@?O><_$ks1(-H}@%{45;`ZXxKZ{S_U2}Oe2U`ILW5?u5zP&)woIf5&PUqKT
z?AUyepOFzr9Tuo%0aA{lIn10L3=9mf#XMMNY~Cg@6DT=3K*}CSPLP@cBvqwBy8NYU
zK@w+WjsZDSWQ`aZXKX$$=L8btQYZzI4GKj-^0Pt(P(DXd3n)K%y3#kGP@gi$taHkz
zfaFXS4WRTPl@yS)y4p{mkfOR1H^_b;<^qlvSzeP3HH0TOs@DKz{;7i$t7|ZUEYZ@`
z0dm4M=Q9Cye%Jm16#Ae8GT1=(G?4tGTMd-1&;#1rF?q3m2;+juKlIChWWE82d}iPP
zbg#Oh21q)<2<$l{kiHn>5}>||#tI;bPbOVJRkfy*KpX*ckg5Q4kSZCAOrWYZ3!thQ
zo0nT!i%WxC?9u6Z!?U~2;6<(yF!*#|JWv3N0mJZbfHE*-Co6`63`h(G`CBIp#Lfvz
z5d>>w4&?CY3_ak{&9Jj$^TRMdCZME2WHHc=C6R$_K+d=LCLpIW;Uq8$H#=vtu>kG$
V$~6P3+uWFMFUlCP`CyB52msdCxWfPd

delta 510
zcmcb!k@?O><_$ks1uB?K^980^JlSw``ZdOyvg*woYy}*Q3no|c?FEwN{P93?I=?34
zg3Sl{85x1pVS!o}Amu2U!_2vWfq~(*m<Q{I&D$hq0wpI0NZA9)2~tylq^dMXm%nr^
zNaC!_F(7A(tPvyQhRw(2oIqk+3Z+1@L7@mpepaXe%I7F*0p%x8SNaAN>Qe@pbx!#d
zkesQa0hB(Zk^+)eSNjPRQdD>12HEe!T)^=n%WJZshVbM@^%|heKXs5|bqywvC0d#~
zKu);kd?ujI@7h0rLLYQM1{>&}29kettAWxLdO&*@OkS)X!nkAd5B)MAnQs6hpBXp+
z-K%b>0g?_d0(;H~q%X#}1gP(#u>wfqlSvm)RjuhH5J$iqq$<E1q)Nsj6R4`q0;p=k
z=H-^w;?f`&dvv<q@a(QLc#*3F3_jf#4-|l6z%cw9pbQMz$%>&M0}?|){?-Wtv2(&w
z1i>1a135f8Ll1a#GwfWj`C*tJ6HwA1vKVN`lE^?dAm>|r6OdDxa1t1Wo1HV+Sb+9=
V<(dK2ZEnoB7iA3Ie6U421OV_}yHWrE

diff --git a/server.js b/server.js
index add905f..95e8c5e 100755
--- a/server.js
+++ b/server.js
@@ -8,6 +8,10 @@ var express = require('express');
 const cors = require('cors');
 var app = express();
 app.use(cors());
+
+// NOTE: We could enumerate porst with "SerialPort.list()"...
+// that would be a little friendlier for people who can't find
+// the filename of their serial port!
 const SerialPort = require('serialport'); //https://serialport.io/docs/guide-usage
 const Readline = require('@serialport/parser-readline');
 
@@ -43,6 +47,9 @@ sport.on('error', function(err) {
   console.log('Error: ', err.message)
 })
 
+// Note: I now believe we need to listen here for
+// Acknowledgements and do something special with them to
+// thottle the serial buffer.
 
 parser.on('data', data =>{
   // Let's see if the data is a PIRDS event...
@@ -139,12 +146,21 @@ app.get('/api/pircs', function(req, res) {
 
 		res.setHeader("Content-Type", "application/json");
 		res.setHeader('Access-Control-Allow-Origin', '*');
-		res.status(200).send(x);
+	  res.status(200).send(x);
+// The documentaiton supports this:
+// function writeAndDrain (data, callback) {
+//  port.write(data)
+//  port.drain(callback)
+// }
+          // Howewver, we are fundamentally asynchronous in processing
+          // requests, so it is unclear how this would work!
+          // Possibly we could call drain first!
+          sport.drain( () =>
 		sport.write(x, (err) => {
 			if (err) {
 			return console.log('Error on write: ', err.message);
 			}
-		});
+		}));
 	}
 });
 
-- 
GitLab