<commit_before>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var EventEmitter = require('events');
var util = require('util');
var endpoint = require('azure-iot-common').endpoint;
var ArgumentError = require('azure-iot-common').errors.ArgumentError;
var cron = require('node-crontab');

var defaultOptions = {
  // 'interval' is a number, expressed in seconds. Default will poll for messages once per second.
  interval: 1,
  // 'at' is a Date object. Message(s) will be received at this time.
  at: null,
  // 'cron' is a cron string. Message(s) will be received according to the interval defined by the cron string.
  cron: null,
  // 'drain' is a boolean indicating whether all messages should be received at the same time (as opposed to one at a time, if set to 'false')
  drain: true
};

/**
 * @class HttpReceiver
 * @classdesc Provides a receiver link that can pull messages from the IoT Hub service and settle them.
 * 
 * @emits message When a message is received
 * @emits errorReceived When there was an error trying to receive messages
 * 
 */
function HttpReceiver(config, httpHelper) {
	EventEmitter.call(this);
  this._config = config;
  this._http = httpHelper;
  
  this._opts = defaultOptions;
  this._cronObj = null;
  this._intervalObj = null;
  this._timeoutObj = null;
  this._receiverStarted = false;
  
  
  this.on('newListener', function () {
    if (!this._receiverStarted) {
      this._startReceiver();
    }
  }.bind(this));
  
  this.on('removeListener', function () {
    if (this._receiverStarted && this.listenerCount('message') === 0) {
      this._stopReceiver();
    }
  }.bind(this));
}

util.inherits(HttpReceiver, EventEmitter);

HttpReceiver.prototype._startReceiver = function startReceiver() {
  if (!this._cronObj && !this._intervalObj && !this._timeoutObj) {
    if (this._opts.interval) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_021: [If opts.interval is set, messages should be received repeatedly at that interval]*/
      this._intervalObj = setInterval(this._receive.bind(this), this._opts.interval * 1000); // this._opts.interval is in seconds but setInterval takes milliseconds.
      this._receiverStarted = true;
    } else if (this._opts.at) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_003: [if opts.at is set, messages shall be received at the Date and time specified.]*/
      var now = new Date().getTime();
      var at = new Date(this._opts.at).getTime();
      var diff = at - now;
      if (diff < 0) {
        // Looks like we're already late. receive messages right away.
        // We do not set the "_receiverStarted" value though because we're not starting anything.
        this._receive.bind(this)();
      } else {
        this._timeoutObj = setTimeout(this._receive.bind(this), diff);    
        this._receiverStarted = true;  
      }
    } else if (this._opts.cron) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_020: [If opts.cron is set messages shall be received according to the schedule described by the expression.]*/<commit_msg>Http receiver shouldn't fail when opts.at > now<commit_after>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var EventEmitter = require('events');
var util = require('util');
var endpoint = require('azure-iot-common').endpoint;
var ArgumentError = require('azure-iot-common').errors.ArgumentError;
var cron = require('node-crontab');

var defaultOptions = {
  // 'interval' is a number, expressed in seconds. Default will poll for messages once per second.
  interval: 1,
  // 'at' is a Date object. Message(s) will be received at this time.
  at: null,
  // 'cron' is a cron string. Message(s) will be received according to the interval defined by the cron string.
  cron: null,
  // 'drain' is a boolean indicating whether all messages should be received at the same time (as opposed to one at a time, if set to 'false')
  drain: true
};

/**
 * @class HttpReceiver
 * @classdesc Provides a receiver link that can pull messages from the IoT Hub service and settle them.
 * 
 * @emits message When a message is received
 * @emits errorReceived When there was an error trying to receive messages
 * 
 */
function HttpReceiver(config, httpHelper) {
	EventEmitter.call(this);
  this._config = config;
  this._http = httpHelper;
  
  this._opts = defaultOptions;
  this._cronObj = null;
  this._intervalObj = null;
  this._timeoutObj = null;
  this._receiverStarted = false;
  
  
  this.on('newListener', function () {
    if (!this._receiverStarted) {
      this._startReceiver();
    }
  }.bind(this));
  
  this.on('removeListener', function () {
    if (this._receiverStarted && this.listenerCount('message') === 0) {
      this._stopReceiver();
    }
  }.bind(this));
}

util.inherits(HttpReceiver, EventEmitter);

HttpReceiver.prototype._startReceiver = function startReceiver() {
  if (!this._cronObj && !this._intervalObj && !this._timeoutObj) {
    if (this._opts.interval) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_021: [If opts.interval is set, messages should be received repeatedly at that interval]*/
      this._intervalObj = setInterval(this._receive.bind(this), this._opts.interval * 1000); // this._opts.interval is in seconds but setInterval takes milliseconds.
      this._receiverStarted = true;
    } else if (this._opts.at) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_003: [if opts.at is set, messages shall be received at the Date and time specified.]*/
      var at = new Date(this._opts.at).getTime();
      var diff = Math.max(at - Date.now(), 0);
      this._timeoutObj = setTimeout(this._receive.bind(this), diff);    
      this._receiverStarted = true;  
    } else if (this._opts.cron) {
      /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_020: [If opts.cron is set messages shall be received according to the schedule described by the expression.]*/
{'fraction_non_alphanumeric': 0.11107438016528925, 'fraction_numerical': 0.007272727272727273, 'mean_word_length': 3.6296863045141547, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1057532', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1765, 'n_words': 595}