<commit_before>/*
	iot_att.cpp - SmartLiving.io Arduino library 

	Morse.cpp - Library for flashing Morse code.
	Created by <NAME>, November 2, 2007.
	Released into the public domain.
*/

#define DEBUG					//turns on debugging in the IOT library. comment out this line to save memory.


#include "iot_att.h"
#include <Time.h>

#define RETRYDELAY 5000					//the nr of milliseconds that we pause before retrying to create the connection
#define ETHERNETDELAY 1000		//the nr of milliseconds that we pause to give the ethernet board time to start
#define MQTTPORT 1883

#ifdef DEBUG
char HTTPSERVTEXT[] = "connection HTTP Server";
char MQTTSERVTEXT[] = "connection MQTT Server";
char FAILED_RETRY[] = " failed,retry";
char SUCCESTXT[] = " established";
#endif

//create the object
ATTDevice::ATTDevice(String deviceId, String clientId, String clientKey)
{
	_deviceId = deviceId;
	_clientId = clientId;
	_clientKey = clientKey;
}

//connect with the http server
bool ATTDevice::Connect(byte mac[], char httpServer[])
{
	_serverName = httpServer;					//keep track of this value while working with the http server.
	if (Ethernet.begin(mac) == 0) 				// Initialize the Ethernet connection:
	{	
		Serial.println(F("DHCP failed,end"));
		return false;							//we failed to connect
	}
	delay(ETHERNETDELAY);							// give the Ethernet shield a second to initialize:
	
	#ifdef DEBUG
	Serial.println(F("Connecting"));
	#endif

	while (!_client.connect(httpServer, 80)) 		// if you get a connection, report back via serial:
	{
		#ifdef DEBUG
		Serial.print(HTTPSERVTEXT);
		Serial.println(FAILED_RETRY);
		#endif
		delay(RETRYDELAY);
	}

	#ifdef DEBUG
	Serial.print(HTTPSERVTEXT);
	Serial.println(SUCCESTXT);
	#endif
	delay(ETHERNETDELAY);							// another small delay: sometimes the card is not yet ready to send the asset info.
	return true;									//we have created a connection succesfully.
}

//create or update the specified asset.
void ATTDevice::AddAsset(String id, String name, String description, bool isActuator, String type)
{
    // form a JSON-formatted string:
    String jsonString = "{\"name\":\"" + name + "\",\"description\":\"" + description + "\",\"is\":\"";
	if(isActuator) 
		jsonString += "actuator";
	else 
		jsonString += "sensor";
    jsonString += "\",\"profile\": { \"type\":\"" + type + "\" }, \"deviceId\":\"" + _deviceId + "\" }";
    
    Serial.println(jsonString);  									//show it on the screen

    // Make a HTTP request:
    _client.println("PUT /api/asset/" + _deviceId + id + " HTTP/1.1");
    _client.print(F("Host: "));
    _client.println(_serverName);
    _client.println(F("Content-Type: application/json"));
    _client.print(F("Auth-ClientKey: "));_client.println(_clientKey);
    _client.print(F("Auth-ClientId: "));_client.println(_clientId); 
	_client.print("Content-Length: ");
	_client.println(jsonString.length());
    _client.println();
    _client.println(jsonString);
    _client.println();
 
    delay(ETHERNETDELAY);
	GetHTTPResult();			//get the response from the server and show it.
}

//connect with the http server and broker<commit_msg>better memory support for creating assets<commit_after>/*
	iot_att.cpp - SmartLiving.io Arduino library 

	Morse.cpp - Library for flashing Morse code.
	Created by <NAME>, November 2, 2007.
	Released into the public domain.
*/

#define DEBUG					//turns on debugging in the IOT library. comment out this line to save memory.


#include "iot_att.h"
#include <Time.h>

#define RETRYDELAY 5000					//the nr of milliseconds that we pause before retrying to create the connection
#define ETHERNETDELAY 1000		//the nr of milliseconds that we pause to give the ethernet board time to start
#define MQTTPORT 1883

#ifdef DEBUG
char HTTPSERVTEXT[] = "connection HTTP Server";
char MQTTSERVTEXT[] = "connection MQTT Server";
char FAILED_RETRY[] = " failed,retry";
char SUCCESTXT[] = " established";
#endif

//create the object
ATTDevice::ATTDevice(String deviceId, String clientId, String clientKey)
{
	_deviceId = deviceId;
	_clientId = clientId;
	_clientKey = clientKey;
}

//connect with the http server
bool ATTDevice::Connect(byte mac[], char httpServer[])
{
	_serverName = httpServer;					//keep track of this value while working with the http server.
	if (Ethernet.begin(mac) == 0) 				// Initialize the Ethernet connection:
	{	
		Serial.println(F("DHCP failed,end"));
		return false;							//we failed to connect
	}
	delay(ETHERNETDELAY);							// give the Ethernet shield a second to initialize:
	
	#ifdef DEBUG
	Serial.println(F("Connecting"));
	#endif

	while (!_client.connect(httpServer, 80)) 		// if you get a connection, report back via serial:
	{
		#ifdef DEBUG
		Serial.print(HTTPSERVTEXT);
		Serial.println(FAILED_RETRY);
		#endif
		delay(RETRYDELAY);
	}

	#ifdef DEBUG
	Serial.print(HTTPSERVTEXT);
	Serial.println(SUCCESTXT);
	#endif
	delay(ETHERNETDELAY);							// another small delay: sometimes the card is not yet ready to send the asset info.
	return true;									//we have created a connection succesfully.
}

//create or update the specified asset.
void ATTDevice::AddAsset(String id, String name, String description, bool isActuator, String type)
{
    // Make a HTTP request:
    _client.println("PUT /api/asset/" + _deviceId + id + " HTTP/1.1");
    _client.print(F("Host: "));
    _client.println(_serverName);
    _client.println(F("Content-Type: application/json"));
    _client.print(F("Auth-ClientKey: "));_client.println(_clientKey);
    _client.print(F("Auth-ClientId: "));_client.println(_clientId); 
	
	_client.print("Content-Length: ");
	int length = name.length() + description.length() + type.length() + _deviceId.length() + 77;
	if(isActuator) 
		length += 8;
	else 
		length += 6;
	_client.println(length);
	
    _client.println();
    
	_client.print("{\"name\":\""); 
	_client.print(name);
	_client.print("\",\"description\":\"");
	_client.print(description);
	_client.print("\",\"is\":\"");
	if(isActuator) 
		_client.print("actuator");
	else 
		_client.print("sensor");
    _client.print("\",\"profile\": { \"type\":\"");
	_client.print(type);
	_client.print("\" }, \"deviceId\":\"");
	_client.print(_deviceId);
	_client.print("\" }");
	_client.println();
    _client.println();
 
    delay(ETHERNETDELAY);
	GetHTTPResult();			//get the response from the server and show it.
}

//connect with the http server and broker
{'fraction_non_alphanumeric': 0.13709423258745668, 'fraction_numerical': 0.007563819728963126, 'mean_word_length': 3.8598774885145484, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 7, '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': '1182945', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2024, 'n_words': 598}