{"size": 3265, "ext": "js", "max_stars_repo_path": "core/device/SparkCoreMock.js", "max_stars_repo_name": "jessekoester/MakeShiftBrewCore", "max_stars_repo_stars_event_min_datetime": "2015-01-01T15:46:33.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-04T11:58:24.000Z", "max_issues_repo_path": "core/device/SparkCoreMock.js", "max_issues_repo_name": "jessekoester/MakeShiftBrewCore", "max_issues_repo_issues_event_min_datetime": "2016-03-20T03:25:35.000Z", "max_issues_repo_issues_event_max_datetime": "2016-04-12T16:11:19.000Z", "max_forks_repo_path": "core/device/SparkCoreMock.js", "max_forks_repo_name": "jessekoester/MakeShiftBrewCore", "max_forks_repo_forks_event_min_datetime": "2015-02-10T15:39:12.000Z", "max_forks_repo_forks_event_max_datetime": "2017-08-13T15:09:37.000Z"}
{"max_stars_count": 110, "max_issues_count": 2, "max_forks_count": 24, "avg_line_length": 17.8415300546, "max_line_length": 115, "alphanum_fraction": 0.5672281776}
/*
 * SparkCore
 *
 * @module SparkCore
 */

var events = require('events');
var coreEmitter = new events.EventEmitter();

var PIDController = require('liquid-pid');

var Logger = require('../module/Logger');
var LOG = 'SparkCoreMock';

var pwm = 0;
var pointTemp = 25;

var cWater = 4183.2;                                      // Heat capacity J/(kg*°C)
var m = 40.0;                                             // Weigh of the water (kg)
var Ta = 20;                                              // Temperature of the environment (your room, etc.) (°C)
var Rth = 0.024;                                          // Thermal conductivity, total thermal resistance between
var deltaT;                                               // Delta temperature
var T0 = 20;                                              // Start temperature of the water (°C)
var temp = T0;                                            // Actual temperature of the water (°C)
var dt;

var pidController;

var PMAX = 4000;

var INTERVAL_TEMP = 1000;
var INTERVAL_PWM = 1000;

pidController = new PIDController({
  Kp: 25,
  Ki: 1000,
  Kd: 9,
  temp: {
    ref: T0,
    Pmax: PMAX
  }
});

var connected = false;

var core1;


/*
 * Initialize
 *
 * @method init
 */
exports.init = function () {
  var core1Emit;

  core1 = new events.EventEmitter();

  core1Emit = core1.emit;

  // Pipe emitter
  // FIXME: anything better?
  core1.emit = function() {
    var args = Array.prototype.slice.call(arguments);

    // Original
    core1Emit.apply(core1, args);

    // Root emitter
    coreEmitter.emit.apply(coreEmitter, args);
  };

  // Fake connect
  core1.emit('connect');
};


/*
 * Calculate temp
 *
 * @method calculateTemp
 * @param {Number} _pwm
 * @param {Date} date
 */
function calculateTemp(_pwm, date) {
  var deltaTime = date || new Date();
  deltaTime = deltaTime.getTime();

  pwm = _pwm || pwm;

  if (dt) {
    deltaTime = deltaTime - dt.getTime();
    deltaTime /= 1000;

    // Delta Temperature in the k. step
    deltaT = (pwm - (temp - Ta) / Rth) * deltaTime / (cWater * m);
    temp += deltaT;

    if(temp > 100) {
      temp = 100;
    }
  }

  dt = new Date();
}


/*
 * Calculate pwm
 *
 * @method calculatePWM
 */
function calculatePWM() {
  pwm = pidController.calculate(temp);
}


/*
 * Set point
 *
 * @method setPoint
 * @param {String} pointStr
 * @callback
 */
exports.setPoint = function (pointStr, callback) {
  if (connected === false) {
    return;
  }

  pointTemp = Number(pointStr);

  pidController.setPoint(pointTemp);

  Logger.info('setPoint to core1', LOG, { point: pointStr });

  return callback();
};


/*
 * Init fake events
 *
 * @method initFakeEvents
 */
function initFakeEvents() {
  // Temp info interval
  setInterval(function () {
    calculateTemp();

    core1.emit('tempInfo', { data: temp });
  }, INTERVAL_TEMP);


// PWM info interval
  setInterval(function () {
    calculatePWM();

    core1.emit('pwmInfo', { data: (pwm / PMAX) * 100 });
  }, INTERVAL_PWM);
}


/*
 * Events
 *
 */

// Connect
coreEmitter.on('connect', function () {
  connected = true;

  initFakeEvents();
});

// Error
coreEmitter.on('error', function (err) {
  Logger.error('Error happened', LOG, {
    err: err
  });
});

exports.coreEmitter = coreEmitter;
