|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + oref0 Nightscout treatment fetch tool |
| 5 | +
|
| 6 | + Collects latest treatment data from Nightscout, with support for sending the |
| 7 | + If-Modified-Since header and not outputting the report file on 304 Not Modified |
| 8 | + response. |
| 9 | +
|
| 10 | + Released under MIT license. See the accompanying LICENSE.txt file for |
| 11 | + full terms and conditions |
| 12 | +
|
| 13 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + THE SOFTWARE. |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +var crypto = require('crypto'); |
| 24 | +var request = require('request'); |
| 25 | +var _ = require('lodash'); |
| 26 | +var fs = require('fs'); |
| 27 | +var network = require('network'); |
| 28 | + |
| 29 | +var safe_errors = ['ECONNREFUSED', 'ESOCKETTIMEDOUT', 'ETIMEDOUT']; |
| 30 | +var log_errors = true; |
| 31 | + |
| 32 | +if (!module.parent) { |
| 33 | + |
| 34 | + var argv = require('yargs') |
| 35 | + .usage("$0 ns-glucose.json NSURL API-SECRET <hours>") |
| 36 | + .strict(true) |
| 37 | + .help('help'); |
| 38 | + |
| 39 | + function usage() { |
| 40 | + argv.showHelp(); |
| 41 | + } |
| 42 | + |
| 43 | + var params = argv.argv; |
| 44 | + var glucose_input = params._.slice(0, 1).pop(); |
| 45 | + |
| 46 | + if ([null, '--help', '-h', 'help'].indexOf(glucose_input) > 0) { |
| 47 | + usage(); |
| 48 | + process.exit(0); |
| 49 | + } |
| 50 | + |
| 51 | + var nsurl = params._.slice(1, 2).pop(); |
| 52 | + if (nsurl && nsurl.charAt(nsurl.length - 1) == "/") nsurl = nsurl.substr(0, nsurl.length - 1); // remove trailing slash if it exists |
| 53 | + |
| 54 | + var apisecret = params._.slice(2, 3).pop(); |
| 55 | + var hours = Number(params._.slice(3, 4).pop()); |
| 56 | + var records = 1000; |
| 57 | + |
| 58 | + if (hours > 0) { |
| 59 | + records = 12 * hours; |
| 60 | + } |
| 61 | + |
| 62 | + if (!glucose_input || !nsurl || !apisecret) { |
| 63 | + usage(); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + if (apisecret.length != 40) { |
| 68 | + var shasum = crypto.createHash('sha1'); |
| 69 | + shasum.update(apisecret); |
| 70 | + apisecret = shasum.digest('hex'); |
| 71 | + } |
| 72 | + |
| 73 | + var cwd = process.cwd(); |
| 74 | + var outputPath = cwd + '/' + glucose_input; |
| 75 | + |
| 76 | + function loadFromxDrip(callback, ip) { |
| 77 | + var headers = { |
| 78 | + 'api-secret': apisecret |
| 79 | + }; |
| 80 | + |
| 81 | + var uri = 'http://' + ip + ':17580/sgv.json?count=' + records; // 192.168.43.1 |
| 82 | + |
| 83 | + var options = { |
| 84 | + uri: uri |
| 85 | + , json: true |
| 86 | + , timeout: 10000 |
| 87 | + , headers: headers |
| 88 | + }; |
| 89 | + |
| 90 | + if (log_errors) console.error('Connected to ' + ip +', testing for xDrip API availability'); |
| 91 | + |
| 92 | + request(options, function(error, res, data) { |
| 93 | + var failed = false; |
| 94 | + if (res && res.statusCode == 403) { |
| 95 | + console.error("Load from xDrip failed: API_SECRET didn't match"); |
| 96 | + failed = true; |
| 97 | + } |
| 98 | + |
| 99 | + if (error) { |
| 100 | + if (safe_errors.includes(error.code)) { |
| 101 | + if (log_errors) console.error('Load from local xDrip timed out, likely not connected to xDrip hotspot'); |
| 102 | + log_errors = false; |
| 103 | + } else { |
| 104 | + if (log_errors) console.error("Load from xDrip failed", error); |
| 105 | + log_errors = false; |
| 106 | + failed = true; |
| 107 | + } |
| 108 | + |
| 109 | + failed = true; |
| 110 | + } |
| 111 | + |
| 112 | + if (!failed && data) { |
| 113 | + console.error("CGM results loaded from xDrip"); |
| 114 | + processAndOutput(data); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + if (failed && callback) callback(); |
| 119 | + }); |
| 120 | + |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + var nsCallback = function loadFromNightscout() { |
| 125 | + // try Nightscout |
| 126 | + |
| 127 | + var lastDate; |
| 128 | + var glucosedata; |
| 129 | + |
| 130 | + fs.readFile(outputPath, 'utf8', function(err, fileContent) { |
| 131 | + |
| 132 | + if (err) { |
| 133 | + console.error(err); |
| 134 | + } else { |
| 135 | + try { |
| 136 | + glucosedata = JSON.parse(fileContent); |
| 137 | + |
| 138 | + if (glucosedata.constructor == Array) { //{ throw "Glucose data file doesn't seem to be valid"; } |
| 139 | + _.forEach(glucosedata, function findLatest(sgvrecord) { |
| 140 | + var d = new Date(sgvrecord.dateString); |
| 141 | + if (!lastDate || lastDate < d) { |
| 142 | + lastDate = d; |
| 143 | + } |
| 144 | + }); |
| 145 | + } else { |
| 146 | + glucosedata = null; |
| 147 | + } |
| 148 | + } catch (e) { |
| 149 | + console.error(e); |
| 150 | + } |
| 151 | + } |
| 152 | + loadFromNightscoutWithDate(lastDate, glucosedata); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + function loadFromNightscoutWithDate(lastDate, glucosedata) { |
| 157 | + |
| 158 | + var headers = { |
| 159 | + 'api-secret': apisecret |
| 160 | + }; |
| 161 | + |
| 162 | + if (!_.isNil(lastDate)) { |
| 163 | + headers["If-Modified-Since"] = lastDate.toISOString(); |
| 164 | + } |
| 165 | + |
| 166 | + var uri = nsurl + '/api/v1/entries/sgv.json?count=' + records; |
| 167 | + var options = { |
| 168 | + uri: uri |
| 169 | + , json: true |
| 170 | + , headers: headers |
| 171 | + }; |
| 172 | + |
| 173 | + request(options, function(error, res, data) { |
| 174 | + if (res && (res.statusCode == 200 || res.statusCode == 304)) { |
| 175 | + |
| 176 | + if (data) { |
| 177 | + console.error("Got CGM results from Nightscout"); |
| 178 | + processAndOutput(data); |
| 179 | + } else { |
| 180 | + console.error("Got Not Changed response from Nightscout, assuming no new data is available"); |
| 181 | + // output old file |
| 182 | + if (!_.isNil(glucosedata)) { |
| 183 | + console.log(JSON.stringify(glucosedata)); |
| 184 | + } |
| 185 | + } |
| 186 | + } else { |
| 187 | + console.error("Loading CGM data from Nightscout failed", error); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + function processAndOutput(glucosedata) { |
| 194 | + |
| 195 | + _.forEach(glucosedata, function findLatest(sgvrecord) { |
| 196 | + sgvrecord.glucose = sgvrecord.sgv; |
| 197 | + }); |
| 198 | + |
| 199 | + console.log(JSON.stringify(glucosedata)); |
| 200 | + } |
| 201 | + |
| 202 | + network.get_gateway_ip(function(err, ip) { |
| 203 | + loadFromxDrip(nsCallback, ip); |
| 204 | + }); |
| 205 | + |
| 206 | +} |
0 commit comments