Skip to content

Commit

Permalink
start getting browserify-express hooked up to expose a shared IOB module
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Mar 1, 2015
1 parent d616b6c commit 7f550b8
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
bower_components/
node_modules/


bundle/bundle.out.js

.idea/
*.iml
my.env
Expand Down
12 changes: 12 additions & 0 deletions bundle/bundle.source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function () {

window.Nightscout = window.Nightscout || {};

window.Nightscout = {
iob: require('../lib/iob')()
};

console.info("Nightscout bundle ready", window.Nightscout);

})();

81 changes: 81 additions & 0 deletions lib/iob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

function iobTotal(treatments, profile, time) {
var iob= 0;
var activity = 0;
if (!treatments) return {};
if (typeof time === 'undefined') {
time = new Date();
}

treatments.forEach(function(treatment) {
if(treatment.created_at < time) {
var tIOB = iobCalc(treatment, profile, time);
if (tIOB && tIOB.iobContrib) iob += tIOB.iobContrib;
if (tIOB && tIOB.activityContrib) activity += tIOB.activityContrib;
}
});
return {
iob: iob,
activity: activity
};
}

function iobCalc(treatment, profile, time) {

var dia=profile.dia;
var scaleFactor = 3.0/dia;
var peak = 75;
var sens=profile.sens;
var iobContrib, activityContrib;
var t = time;
if (typeof t === 'undefined') {
t = new Date();
}


if (treatment.insulin) {
var bolusTime=new Date(treatment.created_at);
var minAgo=scaleFactor*(t-bolusTime)/1000/60;

if (minAgo < 0) {
iobContrib=0;
activityContrib=0;
}
if (minAgo < peak) {
var x = minAgo/5+1;
iobContrib=treatment.insulin*(1-0.001852*x*x+0.001852*x);
activityContrib=sens*treatment.insulin*(2/dia/60/peak)*minAgo;

}
else if (minAgo < 180) {
var x = (minAgo-75)/5;
iobContrib=treatment.insulin*(0.001323*x*x - .054233*x + .55556);
activityContrib=sens*treatment.insulin*(2/dia/60-(minAgo-peak)*2/dia/60/(60*dia-peak));
}
else {
iobContrib=0;
activityContrib=0;
}
return {
iobContrib: iobContrib,
activityContrib: activityContrib
};
} else {
return '';
}
}

function IOB(opts) {

var IOB = {
total: iobTotal
};

return IOB;

}

if (window) window.IOB = IOB;

module.exports = IOB;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"pushover-notifications": "0.2.0",
"sgvdata": "0.0.2",
"socket.io": "^0.9.17",
"git-rev": "git://github.com/bewest/git-rev.git"
"git-rev": "git://github.com/bewest/git-rev.git",
"browserify-express": "^0.1.4"
},
"devDependencies": {
"istanbul": "~0.3.5",
Expand Down
13 changes: 13 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ var staticFiles = express.static(env.static_files, {maxAge: 60 * 60 * 1000});
// serve the static content
app.use(staticFiles);

var browserify_express = require('browserify-express');
var bundle = browserify_express({
entry: __dirname + '/bundle/bundle.source.js',
watch: __dirname + '/lib/',
mount: '/public/js/bundle.js',
verbose: true,
//minify: true,
bundle_opts: { debug: true }, // enable inline sourcemap on js files
write_file: __dirname + '/bundle/bundle.out.js'
});

app.use(bundle);

// Handle errors with express's errorhandler, to display more readable error messages.
var errorhandler = require('errorhandler');
//if (process.env.NODE_ENV === 'development') {
Expand Down
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ <h1 class="customTitle">Nightscout</h1>
<audio src="/audio/alarm2.mp3" preload="auto" loop="true" class="urgent alarm2 mp3" type="audio/mp3"></audio>
</div>

<script src="/public/js/bundle.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/api/v1/status.js"></script>
<script src="/bower_components/d3/d3.min.js"></script>
Expand Down

0 comments on commit 7f550b8

Please sign in to comment.