Skip to content

Commit

Permalink
Change types from const char * to String
Browse files Browse the repository at this point in the history
  • Loading branch information
eykamp committed Feb 4, 2018
1 parent 6063a83 commit 596c194
Showing 1 changed file with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void publish(PubSubClient& client, const String& eventName, T data) {
}

// Build message
String message = "{\"client_id\": \"" + String(id) + "\", \"event_name\": \"" + eventName + "\", \"data\": \"" + String(data) + "\"}";
String message = "{\"client_id\": \"" + id + "\", \"event_name\": \"" + eventName + "\", \"data\": \"" + String(data) + "\"}";

if (DEBUG_MODE) {
Serial.print("Sending message via MQTT: ");
Expand All @@ -282,23 +282,17 @@ void setKey(char* proKey, PubSubClient& client) {
proKey = proKey;

// Generate MQTT random ID
String randomId;
randomId = gen_random(6);

// Assign ID
strncpy(id, randomId.c_str(), ID_SIZE);
id = gen_random(6);

// Build topics IDs
String inTopic = randomId + String(proKey) + String("_in");
String outTopic = randomId + String(proKey) + String("_out");
String inTopic = id + String(proKey) + String("_in");
String outTopic = id + String(proKey) + String("_out");

strcpy(in_topic, inTopic.c_str());
strcpy(out_topic, outTopic.c_str());

// Build client ID
String clientId = randomId + String(proKey);
strcpy(client_id, clientId.c_str());

String client_id = id + String(proKey);

This comment has been minimized.

Copy link
@OidaTiftla

OidaTiftla Jun 23, 2018

Contributor

I'm just asking, if this change is the intended behavior, because the client_id gets no longer copied to the class member variable (it's now only a local variable in this function)?

This comment has been minimized.

Copy link
@eykamp

eykamp Jun 25, 2018

Author Contributor

It is not intended. It is a regression.

This comment has been minimized.

Copy link
@eykamp

eykamp Jun 25, 2018

Author Contributor

See PR #240

}

#endif
Expand Down Expand Up @@ -735,7 +729,7 @@ void publish_proto(T& client, const String& eventName, V value) {
// Format data
String data = "name=" + eventName + "&data=" + String(value);

Serial.println("POST /" + String(id) + "/events HTTP/1.1");
Serial.println("POST /" + id + "/events HTTP/1.1");
Serial.println("Host: " + String(remote_server) + ":" + String(port));
Serial.println(F("Content-Type: application/x-www-form-urlencoded"));
Serial.print(F("Content-Length: "));
Expand Down Expand Up @@ -813,10 +807,10 @@ void handle_callback(PubSubClient& client, char* topic, byte* payload, unsigned
Serial.print("Size of MQTT message: ");
Serial.println(strlen(answer));
Serial.print("Size of client ID: ");
Serial.println(strlen(client_id));
Serial.println(client_id.length());
}

int max_message_size = 128 - 20 - strlen(client_id);
int max_message_size = 128 - 20 - client_id.length();

if (strlen(answer) < max_message_size) {
client.publish(out_topic, answer);
Expand Down Expand Up @@ -882,7 +876,7 @@ void reconnect(PubSubClient& client) {
Serial.print(F("Attempting MQTT connection..."));

// Attempt to connect
if (client.connect(client_id)) {
if (client.connect(client_id.c_str())) {
if (private_mqtt_server) {
Serial.println(F("Connected to MQTT server"));
}
Expand Down Expand Up @@ -1447,19 +1441,18 @@ void function(char * function_name, int (*f)(String)){
}

// Set device ID
void set_id(char *device_id){
void set_id(const String& device_id) {

strncpy(id, device_id, ID_SIZE);
id = device_id.substring(0, ID_SIZE);

#if defined(PubSubClient_h)

// Generate MQTT random ID
String randomId;
randomId = gen_random(6);
String randomId = gen_random(6);

// Build topics IDs
String inTopic = randomId + String(id) + String("_in");
String outTopic = randomId + String(id) + String("_out");
String inTopic = randomId + id + String("_in");
String outTopic = randomId + id + String("_out");

strcpy(in_topic, inTopic.c_str());
strcpy(out_topic, outTopic.c_str());
Expand All @@ -1468,9 +1461,7 @@ void set_id(char *device_id){
// outTopic.toCharArray(out_topic, outTopic.length());

// Build client ID
String clientId = randomId + String(id);
strcpy(client_id, clientId.c_str());
// clientId.toCharArray(client_id, clientId.length());
String client_id = randomId + id;

if (DEBUG_MODE) {
Serial.print("Input MQTT topic: ");
Expand Down Expand Up @@ -1552,13 +1543,6 @@ void set_name(const String& device_name){
device_name.toCharArray(name, NAME_SIZE);
}

// Set device ID
void set_id(const String& device_id){

device_id.toCharArray(id, ID_SIZE);
set_id(id);
}

// Remove last char from buffer
void removeLastBufferChar() {

Expand Down Expand Up @@ -1885,7 +1869,7 @@ void setMQTTServer(char* new_mqtt_server){
int port;

char name[NAME_SIZE];
char id[ID_SIZE+1];
String id;
String arguments;

// Output uffer
Expand All @@ -1907,7 +1891,7 @@ void setMQTTServer(char* new_mqtt_server){
char in_topic[ID_SIZE + 17];
char out_topic[ID_SIZE + 17];
char publish_topic[ID_SIZE + 10];
char client_id[ID_SIZE + 17];
String client_id;

// Subscribe topics & handlers
uint8_t subscriptions_index;
Expand Down

0 comments on commit 596c194

Please sign in to comment.