<commit_before>'use strict';

var mongoose = require('mongoose'),
    apiLogin = require('../models/login'),
    apiThermostats = require('../models/thermostat'),
    Thermostat = mongoose.model('Thermostat'),
    ThermostatMeasurement = mongoose.model('ThermostatMeasurement'),
    _ = require('lodash');

require('../../../thermostats/server/models/thermostat');


mongoose.connect(process.env.MONGOLAB_URI);

var thermostatAttributesMap = { 
  'id': 'greenMomitId',
  'type': 'greenMomitType',
  'userId': 'greenMomitUserId',
  'name': 'name',
  'alarms': 'alarms',
  'shared': 'shared',
  'address': 'address',
  'invited': 'invited',
  'active': 'active',
  'connected': 'connected',
  'lastConnection': 'lastConnection',
  'parameters': 'parameters'
};

var measurementAttributes = [
  'recordTime',
  'temperatureValue',
  'humidityValue',
  'relays',
  'temperatureSetPointValue',
  'tminHeatValue',
  'tmaxHeatValue',
  'tminColdValue',
  'tmaxColdValue',
  'workingModeValue',
  'presenceModeTimeValue',
  'useModeValue',
  'tempUnitValue',
  'temperature2Value',
  'luminosityValue',
  'hardwareStateValue',
  'proximityValue',
  'savingTimesValue',
  'WiFi_RSSIValue',
  'LCD_StateValue',
  'stateValue',
  'Usual_Night_LuminosityValue',
  'WifiVersionValue'
];


function localThermostatData(remoteData){
  var localData = {};

  _.forIn(thermostatAttributesMap, function(localKey, remoteKey, obj){ 
    localData[localKey] = remoteData[remoteKey]; 
  });
  
  localData.lastConnection = new Date(localData.lastConnection);
  return localData;
}

function localMeasurementData(remoteData, thermostatId){
  var localData = {};
  _.forEach(measurementAttributes, function(key){ localData[key] = remoteData[key]; });

  localData.thermostatId = thermostatId;
  localData.recordTime = new Date(localData.recordTime);
  return localData;
}

function upsertMeasurement(thermostat, data){
  console.log('upserting the measurement data');
  var errorHandler = function(error){
    if(error)
      console.error(
        'error upserting the measurement for thermostat: ' +
        thermostat.greenMomitId +
        ' and recordTime: ' +
        measurementData.recordTime);
  };
  
  var measurementData = localMeasurementData(data.record, thermostat._id);
  var queryParams = {recordTime: measurementData.recordTime};
  var queryOptions = {upsert: true};

  ThermostatMeasurement.update(queryParams, measurementData, queryOptions, errorHandler);
}


setInterval(function(){
      
  console.log('processing the data update');

  apiLogin.beginLoginProcess(function(loginData){
    
    apiThermostats.getThermostats(loginData.sessionToken, function(thermostatsData){
      _.forEach(thermostatsData, function(data){
        
        var thermostatData = localThermostatData(data);
        
        Thermostat.findOneAndUpdate(
          {greenMomitId: thermostatData.greenMomitId},
          thermostatData,
          {upsert: true, new: true},
          function(error, thermostat){
            if(error !== null || error !== undefined)
              upsertMeasurement(thermostat, data);
            else
              console.error('error updating the termostat with greenMomitId: ' + thermostatData.greenMomitId);
        });
      });
    });
  });


}, 300000); // 300k ms or 5 mins

<commit_msg>fix bug with the requires order when executing the update script with foreman<commit_after>'use strict';

require('../../../thermostats/server/models/thermostat');

var mongoose = require('mongoose'),
    apiLogin = require('../models/login'),
    apiThermostats = require('../models/thermostat'),
    Thermostat = mongoose.model('Thermostat'),
    ThermostatMeasurement = mongoose.model('ThermostatMeasurement'),
    _ = require('lodash');



mongoose.connect(process.env.MONGOLAB_URI);

var thermostatAttributesMap = { 
  'id': 'greenMomitId',
  'type': 'greenMomitType',
  'userId': 'greenMomitUserId',
  'name': 'name',
  'alarms': 'alarms',
  'shared': 'shared',
  'address': 'address',
  'invited': 'invited',
  'active': 'active',
  'connected': 'connected',
  'lastConnection': 'lastConnection',
  'parameters': 'parameters'
};

var measurementAttributes = [
  'recordTime',
  'temperatureValue',
  'humidityValue',
  'relays',
  'temperatureSetPointValue',
  'tminHeatValue',
  'tmaxHeatValue',
  'tminColdValue',
  'tmaxColdValue',
  'workingModeValue',
  'presenceModeTimeValue',
  'useModeValue',
  'tempUnitValue',
  'temperature2Value',
  'luminosityValue',
  'hardwareStateValue',
  'proximityValue',
  'savingTimesValue',
  'WiFi_RSSIValue',
  'LCD_StateValue',
  'stateValue',
  'Usual_Night_LuminosityValue',
  'WifiVersionValue'
];


function localThermostatData(remoteData){
  var localData = {};

  _.forIn(thermostatAttributesMap, function(localKey, remoteKey, obj){ 
    localData[localKey] = remoteData[remoteKey]; 
  });
  
  localData.lastConnection = new Date(localData.lastConnection);
  return localData;
}

function localMeasurementData(remoteData, thermostatId){
  var localData = {};
  _.forEach(measurementAttributes, function(key){ localData[key] = remoteData[key]; });

  localData.thermostatId = thermostatId;
  localData.recordTime = new Date(localData.recordTime);
  return localData;
}

function upsertMeasurement(thermostat, data){
  console.log('upserting the measurement data');
  var errorHandler = function(error){
    if(error)
      console.error(
        'error upserting the measurement for thermostat: ' +
        thermostat.greenMomitId +
        ' and recordTime: ' +
        measurementData.recordTime);
  };
  
  var measurementData = localMeasurementData(data.record, thermostat._id);
  var queryParams = {recordTime: measurementData.recordTime};
  var queryOptions = {upsert: true};

  ThermostatMeasurement.update(queryParams, measurementData, queryOptions, errorHandler);
}


setInterval(function(){
      
  console.log('processing the data update');

  apiLogin.beginLoginProcess(function(loginData){
    
    apiThermostats.getThermostats(loginData.sessionToken, function(thermostatsData){
      _.forEach(thermostatsData, function(data){
        
        var thermostatData = localThermostatData(data);
        
        Thermostat.findOneAndUpdate(
          {greenMomitId: thermostatData.greenMomitId},
          thermostatData,
          {upsert: true, new: true},
          function(error, thermostat){
            if(error !== null || error !== undefined)
              upsertMeasurement(thermostat, data);
            else
              console.error('error updating the termostat with greenMomitId: ' + thermostatData.greenMomitId);
        });
      });
    });
  });


}, 300000); // 300k ms or 5 mins


{'fraction_non_alphanumeric': 0.13115735888606078, 'fraction_numerical': 0.0032939062733942205, 'mean_word_length': 4.083713850837139, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887384', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2060, 'n_words': 370}