-
Notifications
You must be signed in to change notification settings - Fork 8
/
carbs.js
90 lines (75 loc) · 3.26 KB
/
carbs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var food = require('./files/last_meals.json');
var jsonfood = JSON.stringify(food);
var karbs = JSON.parse(jsonfood);
var carbrate = 0;
var COB = 0;
const dotenv = require('dotenv');
var result = require('dotenv').config();
const carbAbsTime = parseInt(process.env.CARBS_ABS_TIME); // meal absoption time in min defautl 360 or 6 hours
const fast_carbAbsTime = carbAbsTime/6; // = 1 h or 60 min
const slow_carbAbsTime = carbAbsTime/1.5; // = 4 h or 240 min
const ISF = process.env.ISF; // insulin sensitivity factor in mmol/l/U, default 2
const CR = process.env.CR; // carb ratio in g/U, default 10
let timeSinceMealAct = karbs.map(entry => {
var t = entry.time;
var carbs_g = entry.carbs;
// the first 40g of every meal are always considered fast carbs
var fast = Math.min(entry.carbs,40);
// the amount exceeding 40 grams will be randomly split into fast and slow carbs
var rest = entry.carbs - fast;
var FSR = (Math.random() * (0.4 - 0.1) + 0.1); // FSR = FAST RANDOM RATIO
// all fast carbs counted together
var fast_carbs = fast + (FSR * rest);
// the remainder is slow carbs
var slow_carbs = (1-FSR) * rest;
//console.log(fast_random_ratio);
//console.log(fast_carbs);
//console.log(slow_carbs);
console.log('carbs_g:',carbs_g,'fast:', fast, 'rest:', rest, 'fast_carbs:', fast_carbs, 'slow_carbs:', slow_carbs);
if (t < (fast_carbAbsTime/2)) {
var AT2 = Math.pow(fast_carbAbsTime,2);
var fast_carbrate = (fast_carbs * 4 * t)/AT2;
var COB = (fast_carbs*2*Math.pow(t,2))/AT2;
}
else if (t < (fast_carbAbsTime)) {
var fast_carbrate = (fast_carbs * 4 / fast_carbAbsTime)*(1 -(t/fast_carbAbsTime));
var AAA = (4*fast_carbs/fast_carbAbsTime);
var BBB = Math.pow(t,2)/(2*fast_carbAbsTime);
var COB = (AAA * (t-BBB)) - fast_carbs;
} else {
var fast_carbrate = 0;
var COB = 0;
console.log('fast carb absorption rate:', fast_carbrate);
}
if (t < (slow_carbAbsTime/2)) {
var AT2 = Math.pow(slow_carbAbsTime,2);
var slow_carbrate = (slow_carbs * 4 * t)/AT2;
var COB = (slow_carbs*2*Math.pow(t,2))/AT2;
}
else if (t < (slow_carbAbsTime)) {
var slow_carbrate = (slow_carbs * 4 / slow_carbAbsTime)*(1 -(t/slow_carbAbsTime));
var AAA = (4*slow_carbs/slow_carbAbsTime);
var BBB = Math.pow(t,2)/(2*slow_carbAbsTime);
var COB = (AAA * (t-BBB)) - slow_carbs;
} else {
var slow_carbrate = 0;
var COB = 0;
console.log('slow carb absorption rate:', slow_carbrate);
}
return { ...entry,
time: t,
fast_carbrate: fast_carbrate,
slow_carbrate: slow_carbrate,
all_carbrate: fast_carbrate + slow_carbrate
};
});
console.log(timeSinceMealAct);
var totalCarbRate = timeSinceMealAct.reduce(function(tot, arr) {
return tot + arr.all_carbrate;
},0);
console.log(totalCarbRate);
let carbrateString = JSON.stringify(totalCarbRate);
const fs = require('fs');
fs.writeFile("./files/latest_carbs.json", carbrateString, function(err, result) {
if(err) console.log('error', err);
});