<commit_before>/*
Copyright (c) 2013-2018 Ubidots.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Developed and maintained by <NAME> for IoT Services Inc
@jotathebest at github: https://github.com/jotathebest
*/

#include "Ubidots.h"
#include "UbiProtocol.h"
#include "UbiTypes.h"
#include "UbiBuilder.h"
#include "UbiConstants.h"

/**************************************************************************
* Overloaded constructors
***************************************************************************/

Ubidots::Ubidots(char* token, IotProtocol iot_protocol) {
  builder(token, UBI_INDUSTRIAL, iot_protocol);
}

Ubidots::Ubidots(char* token, UbiServer server, IotProtocol iot_protocol) {
  builder(token, server, iot_protocol);
}

void Ubidots::builder(char* token, UbiServer server, IotProtocol iot_protocol) {
  _iot_protocol = iot_protocol;
  UbiBuilder builder(server, token, _iot_protocol);
  _dots = (Value *)malloc(MAX_VALUES * sizeof(Value));
  _context = (ContextUbi *)malloc(MAX_VALUES * sizeof(ContextUbi));
  _ubiProtocol = builder.builder();
  _token = <PASSWORD>;
  _current_value=0;
  _current_context=0;
  String particle_id_str = System.deviceID();
  _default_device_label = new char[particle_id_str.length() + 1];
  strcpy(_default_device_label, particle_id_str.c_str());
}

/***************************************************************************
FUNCTIONS TO SEND DATA
***************************************************************************/

/**
 * Add a value of variable to save
 * @arg variable_label [Mandatory] variable label where the dot will be stored
 * @arg value [Mandatory] Dot value
 * @arg context [optional] Dot context to store. Default NULL
 * @arg dot_timestamp_seconds [optional] Dot timestamp in seconds, usefull for datalogger. Default NULL
 * @arg dot_timestamp_millis [optional] Dot timestamp in millis to add to dot_timestamp_seconds, usefull for datalogger.
 */

void Ubidots::add(char *variable_label, float value) {
  return add(variable_label, value, NULL, NULL, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context) {
  return add(variable_label, value, context, NULL, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context, long unsigned dot_timestamp_seconds) {
  return add(variable_label, value, context, dot_timestamp_seconds, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context, unsigned long dot_timestamp_seconds, unsigned int dot_timestamp_millis) {
  _dirty = true;
  (_dots + _current_value)->variable_label = variable_label;
  (_dots + _current_value)->dot_value = value;
  (_dots + _current_value)->dot_context = context;
  (_dots + _current_value)->dot_timestamp_seconds = dot_timestamp_seconds;
  (_dots + _current_value)->dot_timestamp_millis = dot_timestamp_millis;
  _current_value++;
  if (_current_value > MAX_VALUES) {
    if (_debug) {
      Serial.println(F("You are sending more than the maximum of consecutive variables"));
    }
    _current_value = MAX_VALUES;
  }
}

/**
 * Sends data to Ubidots
 * @arg device_label [Mandatory] device label where the dot will be stored
 * @arg device_name [optional] Name of the device to be created (supported only for TCP/UDP)
 * @arg flags [Optional] Particle publish flags for webhooks
 */

bool Ubidots::send() {
  UbiFlags *flags = new UbiFlags();
  return send(_default_device_label, _default_device_label, flags);
}

bool Ubidots::send(const char* device_label) {
  UbiFlags *flags = new UbiFlags();
  return send(device_label, device_label, flags);
}

bool Ubidots::send(const char* device_label, const char* device_name) {
  UbiFlags *flags = new UbiFlags();
  return send(device_label, device_name, flags);
}

bool Ubidots::send(const char* device_label, PublishFlags flag) {
  UbiFlags *flags = new UbiFlags();
  flags->particle_flag = flag;
  return send(device_label, device_label, flags);
}

bool Ubidots::send(const char* device_label, const char* device_name, UbiFlags* flags) {
  // Builds the payload
  char* payload = (char *) malloc(sizeof(char) * MAX_BUFFER_SIZE);
  if (_iot_protocol == UBI_TCP || _iot_protocol == UBI_UDP) {
    buildTcpPayload(payload, device_label, device_name);
  } else {
    buildHttpPayload(payload);
  }

  // Sends data
  if (_debug) {
    Serial.println("Sending data...");
  }

  bool result = _ubiProtocol->sendData(device_label, device_name, payload, flags);
  free(payload);
  delete flags;
  if (result) {
    _dirty = false;
    _current_value = 0;
  }
  return result;
}

float Ubidots::get(const char* device_label, const char* variable_label) {<commit_msg>current value should be restarted if data cannot be sent<commit_after>/*
Copyright (c) 2013-2018 Ubidots.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Developed and maintained by <NAME> for IoT Services Inc
@jotathebest at github: https://github.com/jotathebest
*/

#include "Ubidots.h"
#include "UbiProtocol.h"
#include "UbiTypes.h"
#include "UbiBuilder.h"
#include "UbiConstants.h"

/**************************************************************************
* Overloaded constructors
***************************************************************************/

Ubidots::Ubidots(char* token, IotProtocol iot_protocol) {
  builder(token, UBI_INDUSTRIAL, iot_protocol);
}

Ubidots::Ubidots(char* token, UbiServer server, IotProtocol iot_protocol) {
  builder(token, server, iot_protocol);
}

void Ubidots::builder(char* token, UbiServer server, IotProtocol iot_protocol) {
  _iot_protocol = iot_protocol;
  UbiBuilder builder(server, token, _iot_protocol);
  _dots = (Value *)malloc(MAX_VALUES * sizeof(Value));
  _context = (ContextUbi *)malloc(MAX_VALUES * sizeof(ContextUbi));
  _ubiProtocol = builder.builder();
  _token = <PASSWORD>;
  _current_value=0;
  _current_context=0;
  String particle_id_str = System.deviceID();
  _default_device_label = new char[particle_id_str.length() + 1];
  strcpy(_default_device_label, particle_id_str.c_str());
}

/***************************************************************************
FUNCTIONS TO SEND DATA
***************************************************************************/

/**
 * Add a value of variable to save
 * @arg variable_label [Mandatory] variable label where the dot will be stored
 * @arg value [Mandatory] Dot value
 * @arg context [optional] Dot context to store. Default NULL
 * @arg dot_timestamp_seconds [optional] Dot timestamp in seconds, usefull for datalogger. Default NULL
 * @arg dot_timestamp_millis [optional] Dot timestamp in millis to add to dot_timestamp_seconds, usefull for datalogger.
 */

void Ubidots::add(char *variable_label, float value) {
  return add(variable_label, value, NULL, NULL, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context) {
  return add(variable_label, value, context, NULL, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context, long unsigned dot_timestamp_seconds) {
  return add(variable_label, value, context, dot_timestamp_seconds, NULL);
}

void Ubidots::add(char *variable_label, float value, char *context, unsigned long dot_timestamp_seconds, unsigned int dot_timestamp_millis) {
  _dirty = true;
  (_dots + _current_value)->variable_label = variable_label;
  (_dots + _current_value)->dot_value = value;
  (_dots + _current_value)->dot_context = context;
  (_dots + _current_value)->dot_timestamp_seconds = dot_timestamp_seconds;
  (_dots + _current_value)->dot_timestamp_millis = dot_timestamp_millis;
  _current_value++;
  if (_current_value > MAX_VALUES) {
    if (_debug) {
      Serial.println(F("You are sending more than the maximum of consecutive variables"));
    }
    _current_value = MAX_VALUES;
  }
}

/**
 * Sends data to Ubidots
 * @arg device_label [Mandatory] device label where the dot will be stored
 * @arg device_name [optional] Name of the device to be created (supported only for TCP/UDP)
 * @arg flags [Optional] Particle publish flags for webhooks
 */

bool Ubidots::send() {
  UbiFlags *flags = new UbiFlags();
  return send(_default_device_label, _default_device_label, flags);
}

bool Ubidots::send(const char* device_label) {
  UbiFlags *flags = new UbiFlags();
  return send(device_label, device_label, flags);
}

bool Ubidots::send(const char* device_label, const char* device_name) {
  UbiFlags *flags = new UbiFlags();
  return send(device_label, device_name, flags);
}

bool Ubidots::send(const char* device_label, PublishFlags flag) {
  UbiFlags *flags = new UbiFlags();
  flags->particle_flag = flag;
  return send(device_label, device_label, flags);
}

bool Ubidots::send(const char* device_label, const char* device_name, UbiFlags* flags) {
  // Builds the payload
  char* payload = (char *) malloc(sizeof(char) * MAX_BUFFER_SIZE);
  if (_iot_protocol == UBI_TCP || _iot_protocol == UBI_UDP) {
    buildTcpPayload(payload, device_label, device_name);
  } else {
    buildHttpPayload(payload);
  }

  // Sends data
  if (_debug) {
    Serial.println("Sending data...");
  }

  bool result = _ubiProtocol->sendData(device_label, device_name, payload, flags);
  free(payload);
  delete flags;
  if (result) {
    _dirty = false;
    _current_value = 0;
  }

  _current_value = 0;
  return result;
}

float Ubidots::get(const char* device_label, const char* variable_label) {
{'fraction_non_alphanumeric': 0.14402560455192034, 'fraction_numerical': 0.0022226173541963017, 'mean_word_length': 5.540116279069768, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2306209', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3283, 'n_words': 1094}