Code coverage report for lib/ds18b20.js

Statements: 12.24% (6 / 49)      Branches: 0% (0 / 20)      Functions: 0% (0 / 8)      Lines: 12.24% (6 / 49)      Ignored: none     

All files » lib/ » ds18b20.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 91 92 93 94 95 96 97                        1                       1                                   1                     1                                 1                                       1            
'use strict';
 
//
// Get and temperature from connected sensors.
//
// @chamerling
//
 
var fs = require('fs');
 
var W1_FILE = '/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves';
 
function parseHexData(data) {
  var arr = data.split(' ');
 
  if (arr[1].charAt(0) === 'f') {
    var x = parseInt('0xffff' + arr[1].toString() + arr[0].toString(), 16);
    return (-((~x + 1) * 0.0625));
  } else if (arr[1].charAt(0) === '0') {
    return parseInt('0x0000' + arr[1].toString() + arr[0].toString(), 16) * 0.0625;
  }
  throw new Error('Can not parse data');
}
 
function parseDecimalData(data) {
  var arr = data.split('\n');
 
  if (arr[0].indexOf('YES') > -1) {
    var output = data.match(/t=(-?(\d+))/);
    return Math.round(output[1] / 100) / 10;
  } else if (arr[0].indexOf('NO') > -1) {
    return false;
  }
  throw new Error('Can not get temperature');
}
 
var parsers = {
  'hex': parseHexData,
  'decimal': parseDecimalData,
  'default': parseDecimalData
};
 
function parseData(data, options) {
  var parser = options.parser || 'default';
  if (!parsers[parser]) {
    parser = 'default';
  }
  return parsers[parser](data);
}
module.exports.parseData = parseData;
 
// Get all connected sensor IDs as array
// @param callback(err, array)
function sensors(callback) {
 
  fs.readFile(W1_FILE, 'utf8', function(err, data) {
    if (err) {
      return callback(err);
    }
 
    var parts = data.split('\n');
    parts.pop();
    return callback(null, parts);
  });
}
module.exports.sensors = sensors;
 
// Get the temperature of a given sensor
// @param sensor : The sensor ID
// @param callback : callback (err, value)
function temperature(sensor, options, callback) {
  if (options instanceof Function) {
    callback = options;
    options = {};
  }
 
  fs.readFile('/sys/bus/w1/devices/' + sensor + '/w1_slave', 'utf8', function(err, data) {
    if (err) {
      return callback(err);
    }
 
    try {
      return callback(null, parseData(data, options));
    } catch(e) {
      return callback(new Error('Can not read temperature for sensor ' + sensor));
    }
  });
};
module.exports.temperature = temperature;
 
function temperatureSync(sensor, options) {
  options = options || {};
  var data = fs.readFileSync('/sys/bus/w1/devices/' + sensor + '/w1_slave', 'utf8');
  return parseData(data, options);
};
module.exports.temperatureSync = temperatureSync;