Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianScholz committed Jul 7, 2017
1 parent e576743 commit 4aafe81
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 98 deletions.
173 changes: 76 additions & 97 deletions lib/drivers/abbott/abbottFreeStyleLibre.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var TZOUtil = require('../../TimezoneOffsetUtil');
var isBrowser = typeof window !== 'undefined';
var debug = isBrowser ? require('bows')('FreeStyleLibreDriver') : console.log;


module.exports = function (config) {
var cfg = _.clone(config);
var hidDevice = config.deviceComms;
Expand All @@ -55,37 +54,9 @@ module.exports = function (config) {
EOT : 0x04
};

var probe = function(cb){
debug('not using probe for ' + DEVICE_MODEL_NAME);
cb();
};

var getOneRecord = function (data, callback) {
callback(null, {});
};

var processReadings = function(readings) {
return {};
};

var prepBGData = function(progress, data) {
return {};
};

var buildPacket = function (commandType, data) {
var packetLen = 2 + data.length;
var buf = new ArrayBuffer(HID_PACKET_SIZE);
var bytes = new Uint8Array(buf, 0, HID_PACKET_SIZE);
var counter = struct.pack(bytes, 0, 'bb', commandType, data.length);
if (data.length) {
counter += struct.pack(bytes, counter, data.length + 'Z', data);
}
return buf;
};

var readResponse = function (commandType, timeout, cb) {
var abortTimer = setTimeout(function () {
debug('TIMEOUT');
debug('readResponse: TIMEOUT');
var e = new Error('Timeout error.');
e.name = 'TIMEOUT';
return cb(e, null);
Expand All @@ -100,21 +71,22 @@ module.exports = function (config) {
if (raw === undefined || raw.length === 0) {
return callback(false);
}
debug('received: ' + raw.length + ': ' + raw);
debug('readResponse: received: ' + raw.length + ': "' + raw + '"');

var packetHeadStruct = 'bb';
var packetHeadLength = struct.structlen(packetHeadStruct);
var packetHead = struct.unpack(raw, 0, packetHeadStruct, ['commandType', 'dataLen']);
debug('packetHead: type: ' + packetHead['commandType'] + ', len: ' + packetHead['dataLen']);
debug('readResponse: packetHead: type: ' + packetHead['commandType'] + ', len: ' + packetHead['dataLen']);

if (commandType !== null && packetHead['commandType'] !== commandType) {
debug('Invalid packet from ' + DEVICE_MODEL_NAME);
debug('readResponse: Invalid packet from ' + DEVICE_MODEL_NAME);
clearTimeout(abortTimer);
return callback(new Error('Invalid USB packet received.'));
}

message += raw.slice(packetHeadStruct.length);
message += raw.slice(packetHeadLength, packetHeadLength + packetHead['dataLen']);

if (packetHead['dataLen'] <= HID_PACKET_SIZE - packetHeadStruct.length) {
if (packetHead['dataLen'] <= HID_PACKET_SIZE - packetHeadLength) {
clearTimeout(abortTimer);
return callback(true);
}
Expand All @@ -136,59 +108,61 @@ module.exports = function (config) {
var validateChecksum = function(data, expectedChecksum) {
var calculatedChecksum = data.split('')
.reduce(function(a, b) { return a + b.charCodeAt(0); }, 0);
debug('checksum: ' + calculatedChecksum + ' ?= ' + expectedChecksum);
debug('validateChecksum: checksum: ' + calculatedChecksum + ' ?= ' + expectedChecksum);
return calculatedChecksum == expectedChecksum;
};

var parseTextResponse = function(response, cb) {
var buildPacket = function (commandType, data) {
var packetLen = 2 + data.length;
var buf = new ArrayBuffer(HID_PACKET_SIZE);
var bytes = new Uint8Array(buf, 0, HID_PACKET_SIZE);
var counter = struct.pack(bytes, 0, 'bb', commandType, data.length);
if (data.length) {
counter += struct.pack(bytes, counter, data.length + 'Z', data);
}
return buf;
};

var sendCommand = function (command, response, data, cb) {
debug('sendCommand: Sending command: ', command, ', data: ', data);
hidDevice.send(buildPacket(command, data), function() {
readResponse(response, READ_TIMEOUT, cb);
});
};

var parseTextResponse = function(response) {
var match = TEXT_RESPONSE_FORMAT.exec(response);
if (!match) {
return cb(new Error('Invalid response format.'), response);
return new Error('Invalid response format.');
}
var data = match[1];
var checksum = parseInt(match[2], 16);
var result = match[3];

if (result === 'OK') {
if (validateChecksum(data, checksum)) {
return cb(null, data);
return data;
} else {
return cb(new Error('Invalid checksum.'), response);
return new Error('Invalid checksum.');
}
} else {
return cb(new Error('Result was not OK: ' + result), response);
return new Error('Device response was not "OK", but "' + result + '"');
}
};

var sendTextCommand = function (data, cb) {
hidDevice.send(buildPacket(TEXT_COMMAND, data), function() {
readResponse(TEXT_COMMAND, READ_TIMEOUT, cb);
});
};

var sendCommand = function (command, data, cb) {
debug('Sending command: ', command, ', data: ', data);
hidDevice.send(buildPacket(command, data), function() {
readResponse(null, READ_TIMEOUT, cb);
});
};

var requestTextResponse = function(data, successCallback, errorCallback) {
sendTextCommand(data, function (err, response) {

sendCommand(TEXT_COMMAND, TEXT_COMMAND, data, function (err, response) {
if (err) {
debug('err: ', err);
debug('requestTextResponse: error: ', err);
return errorCallback(err, response);
}

debug('response: "' + response + '"');
parseTextResponse(response, function (err, data) {
if (!err) {
return successCallback(data.replace(/\r\n$/, ''));
} else {
return errorCallback(err, response);
}
});
debug('requestTextResponse: response: "' + response + '"');
data = parseTextResponse(response);
if (data instanceof Error) {
return errorCallback(data, response);
}
return successCallback(data.replace(/\r\n$/, ''));
});
};

Expand Down Expand Up @@ -219,19 +193,24 @@ module.exports = function (config) {

var initCommunication = function(cb) {
var initFunctions = [
function(cb) { sendCommand(0x04, '', cb); },
function(cb) { sendCommand(0x05, '', cb); },
function(cb) { sendCommand(0x15, '', cb); },
function(cb) { sendCommand(0x01, '', cb); },
function(cb) { sendCommand(0x04, null, '', cb); },
function(cb) { sendCommand(0x05, null, '', cb); },
function(cb) { sendCommand(0x15, null, '', cb); },
function(cb) { sendCommand(0x01, null, '', cb); },
];
async.series(initFunctions, function(err, result) {
cb(err, result);
});
};

var probe = function(cb){
debug('probe: not using probe for ' + DEVICE_MODEL_NAME);
cb();
};

return {
detect: function(deviceInfo, cb){
debug('no detect function needed', arguments);
debug('detect: no detect function needed', arguments);
cb(null, deviceInfo);
},

Expand All @@ -248,9 +227,12 @@ module.exports = function (config) {
if (err) {
return cb(err);
}
data.disconnect = false;
progress(100);
cb(null, data);
initCommunication(function(err, result) {
// ignore results of init as it seems not to be relevant to the following communication
data.disconnect = false;
progress(100);
cb(null, data);
});
});
},

Expand All @@ -263,30 +245,27 @@ module.exports = function (config) {
getDBRecordNumber
];
var counter = 0;
initCommunication(function(err, result) {
// ignore results, just continue with the getter functions
async.series(getterFunctions, function(err, result) {
counter += 1;
progress(100 * (counter / getterFunctions.length));

if (err) {
debug('getConfigInfo: ', err);
return cb(err, null);
async.series(getterFunctions, function(err, result) {
counter += 1;
progress(100 * (counter / getterFunctions.length));

if (err) {
debug('getConfigInfo: ', err);
return cb(err, null);
}

//debug('getConfigInfo: result: ', result);
data.connect = true;
result.forEach(function(element) {
if (typeof element === 'object') {
debug('getConfigInfo: result object: ', element);
_.assign(data.deviceInfo, element);
}
}, this);
debug('getConfigInfo: data: ', data);

//debug('result: ', result);
data.connect = true;
result.forEach(function(element) {
if (typeof element === 'object') {
debug('result object: ', element);
_.assign(data.deviceInfo, element);
}
}, this);
debug('data: ', data);

cb(null, data);
});
});
cb(null, data);
});
},

fetchData: function (progress, data, cb) {
Expand All @@ -305,8 +284,8 @@ module.exports = function (config) {
debug('in uploadData');
progress(0);

// TODO: enable acutall upload
return cb(new Error('not yet implemented'), data);
// TODO: enable acutal upload
cb(new Error('not yet implemented'), data);
/*
var sessionInfo = {
deviceTags: ['bgm', 'cgm'],
Expand Down
2 changes: 1 addition & 1 deletion lib/drivers/abbott/cli/fslibre.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/usr/bin/env babel-node

global.__DEBUG__ = true;

Expand Down

0 comments on commit 4aafe81

Please sign in to comment.