Skip to content

Commit

Permalink
Tzachi shared node phase2 (#1370)
Browse files Browse the repository at this point in the history
* move oref0-calculate-iob to shared node.

Signed-off-by: Tzachi Dar <[email protected]>

* move oref0-meal to shared node.

Signed-off-by: Tzachi Dar <[email protected]>

* Add more files to strict mode.

Signed-off-by: Tzachi Dar <[email protected]>

* move oref0-get-profile.js to shared node.

Signed-off-by: Tzachi Dar <[email protected]>

* start using oref0-get-profile

Signed-off-by: Tzachi Dar <[email protected]>

* Fix issues that have been found when running autosens.js in strict mode.

Signed-off-by: Tzachi Dar <[email protected]>

* move oref0-get-ns-entries to shared node.

Signed-off-by: Tzachi Dar <[email protected]>

* Fix creation of test data for oref0-get-profile.

Signed-off-by: Tzachi Dar <[email protected]>

* Add printing to oref0-get-ns-entries and it's successors.

Signed-off-by: Tzachi Dar <[email protected]>

* Add printing to oref0-meal (only very high level)

Signed-off-by: Tzachi Dar <[email protected]>

* Fix some minor errors found in testing.

Signed-off-by: Tzachi Dar <[email protected]>

* comment out echo dir_name from ns-loop.log

* comment out echo dir_name in pump-loop.log

Co-authored-by: Scott Leibrand <[email protected]>
  • Loading branch information
tzachi-dar and scottleibrand authored Dec 20, 2021
1 parent da70837 commit 912ab07
Show file tree
Hide file tree
Showing 31 changed files with 463 additions and 170 deletions.
13 changes: 7 additions & 6 deletions bin/oref0-autosens-history.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
'use strict';

/*
Determine Basal
Expand All @@ -16,10 +17,10 @@
*/

var basal = require('../lib/profile/basal');
var detect = require('../lib/determine-basal/autosens');
var detectSensitivity = require('../lib/determine-basal/autosens');

if (!module.parent) {
var detectsensitivity = init();
//var detectsensitivity = init(); // I don't see where this variable is used, so deleted it.

var argv = require('yargs')
.usage("$0 <glucose.json> <pumphistory.json> <profile.json> <readings_per_run> [outputfile.json]")
Expand Down Expand Up @@ -135,16 +136,16 @@ if (!module.parent) {
var ratioArray = [];
do {
detection_inputs.deviations = 96;
detect(detection_inputs);
var result = detectSensitivity(detection_inputs);
for(i=0; i<readings_per_run; i++) {
detection_inputs.glucose_data.shift();
}
console.error(ratio, newisf, detection_inputs.glucose_data[0].dateString);
console.error(result.ratio, result.newisf, detection_inputs.glucose_data[0].dateString);

var obj = {
"dateString": detection_inputs.glucose_data[0].dateString,
"sensitivityRatio": ratio,
"ISF": newisf
"sensitivityRatio": result.ratio,
"ISF": result.newisf
}
ratioArray.unshift(obj);
if (output_file) {
Expand Down
31 changes: 22 additions & 9 deletions bin/oref0-calculate-iob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

'use strict';
/*
Insulin On Board (IOB) calculations.
Expand All @@ -19,13 +19,16 @@
*/

var generate = require('../lib/iob');
var fs = require('fs');
function usage ( ) {
console.log('usage: ', process.argv.slice(0, 2), '<pumphistory-zoned.json> <profile.json> <clock-zoned.json> [autosens.json] [pumphistory-24h-zoned.json]');

}

if (!module.parent) {
var argv = require('yargs')


var oref0_calculate_iob = function oref0_calculate_iob(argv_params) {
var argv = require('yargs')(argv_params)
.usage("$0 <pumphistory-zoned.json> <profile.json> <clock-zoned.json> [<autosens.json>] [<pumphistory-24h-zoned.json>]")
.strict(true)
.help('help');
Expand All @@ -46,21 +49,21 @@ if (!module.parent) {
var pumphistory_24_input = inputs[4];

var cwd = process.cwd();
var pumphistory_data = require(cwd + '/' + pumphistory_input);
var profile_data = require(cwd + '/' + profile_input);
var clock_data = require(cwd + '/' + clock_input);
var pumphistory_data = JSON.parse(fs.readFileSync(cwd + '/' + pumphistory_input));
var profile_data = JSON.parse(fs.readFileSync(cwd + '/' + profile_input));
var clock_data = JSON.parse(fs.readFileSync(cwd + '/' + clock_input));

var autosens_data = null;
if (autosens_input) {
try {
autosens_data = require(cwd + '/' + autosens_input);
autosens_data = JSON.parse(fs.readFileSync(cwd + '/' + autosens_input));
} catch (e) {}
//console.error(autosens_input, JSON.stringify(autosens_data));
}
var pumphistory_24_data = null;
if (pumphistory_24_input) {
try {
pumphistory_24_data = require(cwd + '/' + pumphistory_24_input);
pumphistory_24_data = JSON.parse(fs.readFileSync(cwd + '/' + pumphistory_24_input));
} catch (e) {}
}

Expand All @@ -77,6 +80,16 @@ if (!module.parent) {
}

var iob = generate(inputs);
console.log(JSON.stringify(iob));
return(JSON.stringify(iob));
}

if (!module.parent) {
// remove the first parameter.
var command = process.argv;
command.shift();
command.shift();
var result = oref0_calculate_iob(command)
console.log(result);
}

exports = module.exports = oref0_calculate_iob
14 changes: 7 additions & 7 deletions bin/oref0-detect-sensitivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var detect = require('../lib/determine-basal/autosens');
var detectSensitivity = require('../lib/determine-basal/autosens');

if (!module.parent) {
var argv = require('yargs')
Expand Down Expand Up @@ -112,14 +112,14 @@ if (!module.parent) {
};
console.error("Calculating sensitivity using 8h of non-exluded data");
detection_inputs.deviations = 96;
detect(detection_inputs);
var ratio8h = ratio;
var newisf8h = newisf;
var result = detectSensitivity(detection_inputs);
var ratio8h = result.ratio;
var newisf8h = result.newisf;
console.error("Calculating sensitivity using all non-exluded data (up to 24h)");
detection_inputs.deviations = 288;
detect(detection_inputs);
var ratio24h = ratio;
var newisf24h = newisf;
result = detectSensitivity(detection_inputs);
var ratio24h = result.ratio;
var newisf24h = result.newisf;
if ( ratio8h < ratio24h ) {
console.error("Using 8h autosens ratio of",ratio8h,"(ISF",newisf8h+")");
} else {
Expand Down
67 changes: 48 additions & 19 deletions bin/oref0-get-ns-entries.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
'use strict';

/*
oref0 Nightscout treatment fetch tool
Expand All @@ -25,13 +26,16 @@ var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var network = require('network');
var shared_node = require('./oref0-shared-node-utils');
var console_error = shared_node.console_error;
var console_log = shared_node.console_log;
var initFinalResults = shared_node.initFinalResults;

var safe_errors = ['ECONNREFUSED', 'ESOCKETTIMEDOUT', 'ETIMEDOUT'];
var log_errors = true;
var oref0_get_ns_engtires = function oref0_get_ns_engtires(argv_params, print_callback, final_result) {
var safe_errors = ['ECONNREFUSED', 'ESOCKETTIMEDOUT', 'ETIMEDOUT'];
var log_errors = true;

if (!module.parent) {

var argv = require('yargs')
var argv = require('yargs')(argv_params)
.usage("$0 ns-glucose.json NSURL API-SECRET <hours>")
.strict(true)
.help('help');
Expand All @@ -45,7 +49,7 @@ if (!module.parent) {

if ([null, '--help', '-h', 'help'].indexOf(glucose_input) > 0) {
usage();
process.exit(0);
process.exit(0); //???????
}

var nsurl = params._.slice(1, 2).pop();
Expand Down Expand Up @@ -88,21 +92,21 @@ if (!module.parent) {
, headers: headers
};

if (log_errors) console.error('Connected to ' + ip +', testing for xDrip API availability');
if (log_errors) console_error(final_result, 'Connecting to ' + ip +', testing for xDrip API availability');

request(options, function(error, res, data) {
var failed = false;
if (res && res.statusCode == 403) {
console.error("Load from xDrip failed: API_SECRET didn't match");
console_error(final_result, "Load from xDrip failed: API_SECRET didn't match");
failed = true;
}

if (error) {
if (safe_errors.includes(error.code)) {
if (log_errors) console.error('Load from local xDrip timed out, likely not connected to xDrip hotspot');
if (log_errors) console_error(final_result, 'Load from local xDrip timed out, likely not connected to xDrip hotspot');
log_errors = false;
} else {
if (log_errors) console.error("Load from xDrip failed", error);
if (log_errors) console_error(final_result, "Load from xDrip failed", error);
log_errors = false;
failed = true;
}
Expand All @@ -111,12 +115,18 @@ if (!module.parent) {
}

if (!failed && data) {
console.error("CGM results loaded from xDrip");
console_error(final_result, "CGM results loaded from xDrip");
processAndOutput(data);
return true;
}

if (failed && callback) callback();
if (failed && callback) {
// printing will happen in the callback
callback();
} else {
print_callback(final_result);
}

});

return false;
Expand All @@ -131,7 +141,7 @@ if (!module.parent) {
fs.readFile(outputPath, 'utf8', function(err, fileContent) {

if (err) {
console.error(err);
console_error(final_result, err);
} else {
try {
glucosedata = JSON.parse(fileContent);
Expand All @@ -147,10 +157,11 @@ if (!module.parent) {
glucosedata = null;
}
} catch (e) {
console.error(e);
console_error(final_result, e);
}
}
loadFromNightscoutWithDate(lastDate, glucosedata);
// callback will happen in loadFromNightscoutWithDate
});
}

Expand Down Expand Up @@ -181,18 +192,19 @@ if (!module.parent) {
if (res && (res.statusCode == 200 || res.statusCode == 304)) {

if (data) {
console.error("Got CGM results from Nightscout");
console_error(final_result, "Got CGM results from Nightscout");
processAndOutput(data);
} else {
console.error("Got Not Changed response from Nightscout, assuming no new data is available");
console_error(final_result, "Got Not Changed response from Nightscout, assuming no new data is available");
// output old file
if (!_.isNil(glucosedata)) {
console.log(JSON.stringify(glucosedata));
console_log(final_result, JSON.stringify(glucosedata));
}
}
} else {
console.error("Loading CGM data from Nightscout failed", error);
console_error(final_result, "Loading CGM data from Nightscout failed", error);
}
print_callback(final_result);
});

}
Expand All @@ -203,11 +215,28 @@ if (!module.parent) {
sgvrecord.glucose = sgvrecord.sgv;
});

console.log(JSON.stringify(glucosedata));
console_log(final_result, JSON.stringify(glucosedata));
}

network.get_gateway_ip(function(err, ip) {
loadFromxDrip(nsCallback, ip);
});
}

function print_callback(final_result) {
console.log(final_result.stdout);
console.error(final_result.err);
}


if (!module.parent) {
var final_result = initFinalResults();

// remove the first parameter.
var command = process.argv;
command.shift();
command.shift();
var result = oref0_get_ns_engtires(command, print_callback, final_result)
}

exports = module.exports = oref0_get_ns_engtires
Loading

0 comments on commit 912ab07

Please sign in to comment.