'use strict';

var engine = require('share2nightscout-bridge');

function init (env) {
  if (env.extendedSettings.bridge && env.extendedSettings.bridge.accountName && env.extendedSettings.bridge.password) {
    return create(env);
  } else {
    console.info('Dexcom bridge not enabled');
  }
}

function bridged (err, glucose) {
  if (err) {
    console.error('Bridge error: ', err);
  } else {
    entries.create(glucose, function stored (err) {
      if (err) {
        console.error('Bridge storage error: ', err);
      }
    });
  }
}

function create (env) {

  var bridge = { };

  var config = {
    accountName: env.extendedSettings.bridge.accountName
    , password: env.extendedSettings.bridge.password
  };

  var interval = env.extendedSettings.bridge.interval || 60000 * 2.5;

  var fetch_config = {
    maxCount: env.extendedSettings.bridge.maxCount || 1
    , minutes: env.extendedSettings.bridge.minutes || 1440
  };

  var opts = {
    login: config
    , fetch: fetch_config
    , nightscout: { }
    , maxFailures: env.extendedSettings.bridge.maxFailures || 3
    , firstFetchCount: env.extendedSettings.bridge.firstFetchCount || 3
  };

  bridge.startEngine = function startEngine (entries) {

    opts.callback = bridged;

    setInterval(engine(opts), interval);
  };

  return bridge;
}

init.create = create;
init.bridged = bridged;
exports = module.exports = init;

@@ -306,16 +306,47 @@
 bridged 
+(entries) %7B%0A  function payload 
 (err, gl
@@ -350,24 +350,26 @@
  glucose) %7B%0A
+  
   if (err) %7B
@@ -365,24 +365,26 @@
  if (err) %7B%0A
+  
     console.
@@ -411,24 +411,26 @@
  ', err);%0A  
+ 
+ 
 %7D else %7B%0A   
@@ -422,24 +422,26 @@
    %7D else %7B%0A
+  
     entries.
@@ -476,24 +476,26 @@
 red (err) %7B%0A
+  
       if (er
@@ -503,24 +503,26 @@
 ) %7B%0A        
+  
 console.erro
@@ -551,24 +551,26 @@
 r: ', err);%0A
+  
       %7D%0A    
@@ -573,15 +573,41 @@
     
+  
 %7D);%0A  
-%7D
+  %7D%0A  %7D%0A  return payload;
 %0A%7D%0A%0A
@@ -1307,16 +1307,25 @@
  bridged
+(entries)
 ;%0A%0A    s

fix bug