{"size": 1343, "ext": "js", "max_stars_repo_path": "dht22.js", "max_stars_repo_name": "HalleyInteractive/dht22-node-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "dht22.js", "max_issues_repo_name": "HalleyInteractive/dht22-node-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "dht22.js", "max_forks_repo_name": "HalleyInteractive/dht22-node-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.984375, "max_line_length": 65, "alphanum_fraction": 0.5927029039}
(function() {
  'use strict';

  let sensor = require('node-dht-sensor');
  let readout = {
    temperature: 0,
    humidity: 0,
  };

  const GPIO_PIN = 4;

  class DHT22 {

    constructor() {
      let initialised = sensor.initialize(22, GPIO_PIN);
      if(!initialised) {
        console.log('Failed to initialise sensor');
      }
    }

    read() {
      readout = sensor.read();
      return readout;
    }

    temperature() {
      return this.roundToOneDecimal(readout.temperature);
    }

    humidity() {
      return this.roundToOneDecimal(readout.humidity);
    }

    readout() {
      let tmpReadout = {
        temperature: this.roundToOneDecimal(readout.temperature),
        humidity: this.roundToOneDecimal(readout.humidity),
      };
      return tmpReadout;
    }

    /**
    * Rounds a float to a single decimal place.
    * @param number {number} Number to round.
    * @return number {number} Rounded number.
    */
    roundToOneDecimal(number) {
      return Number(Math.round(number+'e2')+'e-2');
    }

    /**
    * roundHalf
    * Rounds a given floating number to it's closes .5 value
    * @param num {float} Floating number
    * @return {float} Floating number rounded to closest .5 value
    **/
    roundHalf(num) {
      return Math.round(num * 2) / 2;
    }

  }
  module.exports = new DHT22();
})();
