ROVER - Hono interaction library using curl is created for the Rover
!! Current Hono state is not final and just temporarily implemented for prototyping.
Signed-off-by: Mustafa Ozcelikors <mozcelikors@gmail.com>
diff --git a/rover/src/RaspberryTest.cpp b/rover/src/RaspberryTest.cpp
index 36a34da..a0a18ec 100644
--- a/rover/src/RaspberryTest.cpp
+++ b/rover/src/RaspberryTest.cpp
@@ -43,6 +43,8 @@
#include "interfaces.h"
#include "pthread_monitoring/collect_thread_name.h"
+#include "hono_interaction/hono_interaction.h"
+
//Please comment the line below to work with SR-04 sensor instead of GROOVE for rear proximity sensing.
//#define USE_GROOVE_SENSOR 1
@@ -128,6 +130,10 @@
int main()
{
+ //To test the hono interaction library
+ //registerDeviceToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771);
+ //sendEventDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"Bearing",0.5);
+
RefreshThreadList();
CollectProcessID();
diff --git a/rover/src/hono_interaction/hono_interaction.cpp b/rover/src/hono_interaction/hono_interaction.cpp
new file mode 100644
index 0000000..a78f566
--- /dev/null
+++ b/rover/src/hono_interaction/hono_interaction.cpp
@@ -0,0 +1,312 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * Hono interaction library for Rover
+ *
+ * Authors:
+ * M. Ozcelikors, R.Hottger
+ * <mozcelikors@gmail.com> <robert.hoettger@fh-dortmund.de>
+ *
+ * Usage:
+ * To test the hono interaction library:
+ *
+ * registerDeviceToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771);
+ * sendEventDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"Bearing",0.5);
+ *
+ */
+
+#include "hono_interaction.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+//Buffers to work with strings
+char buffer[256];
+char num_buffer[33];
+
+/**
+ * Function Name: handleCode
+ * Description: Handles code that is returned with HTTP response
+ * Arguments: int code
+ * Returns: Generates a status value
+ * If the action completed -> returns 1
+ * If the action failed -> returns 0
+ */
+int handleCode(int code)
+{
+ int status = 0;
+ switch (code)
+ {
+ case 200: //Accepted
+ printf("200 Accepted\n");
+ printf("Registration of device to Hono instance accepted.\n");
+ status = 1;
+ break;
+ case 201: //Created
+ printf("201 Created\n");
+ printf("Registration of device to Hono instance is done.\n");
+ status = 1;
+ break;
+ case 202:
+ status = 1; //Telemetry data accepted
+ break;
+ case 203:
+ case 204:
+ case 205:
+ case 206:
+ case 207:
+ case 208:
+ case 209:
+ status = 1;
+ break;
+ case 400:
+ fprintf(stderr, ("400 Bad Request\n"));
+ status = 0;
+ break;
+ case 403:
+ fprintf(stderr, ("403 Forbidden\n"));
+ status = 0;
+ break;
+ case 409:
+ fprintf(stderr, ("409 Conflict\n"));
+ status = 0;
+ break;
+ case 503:
+ fprintf(stderr, ("503 Service Unavailable. You may need to create a consumer."));
+ status = 0;
+ break;
+ case 401:
+ case 402:
+ case 404:
+ case 405:
+ case 406:
+ case 407:
+ case 408:
+ fprintf(stderr, ("Error\n"));
+ status = 0;
+ break;
+ default:
+ status = 0;
+ break;
+ }
+ return status;
+}
+
+/**
+ * Function Name: registerDeviceToHonoInstance
+ * Description: Registers a device to Eclipse Hono instance.
+ * A device should be registered only once.
+ * Arguments: char * host_name
+ * int port
+ * char * tenant_name
+ * int device_id
+ * Returns: Successfully returns status data.
+ * If the action completed -> returns 1
+ * If the action failed -> returns 0
+ */
+int registerDeviceToHonoInstance (char * host_name, int port, char * tenant_name, int device_id)
+{
+ FILE *fp;
+ int code;
+ int status;
+
+ //Prepare command as string
+ //Example: "curl -X POST -i -d 'device_id=4711' http://localhost:28080/registration/DEFAULT_TENANT"
+ sprintf(buffer, "curl -X POST -i -d 'device_id=");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", device_id);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "' http://");
+ strcat(buffer, host_name);
+ strcat(buffer, ":");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", port);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "/registration/");
+ strcat(buffer, tenant_name);
+
+ //Print the command that is created
+ printf("Command=%s\n",buffer);
+
+ //Execute the command
+ fp = popen(buffer,"r");
+
+ //Get and Parse the output
+ fgets(buffer, 13, fp); //Get the string HTTP/1.1 XXX
+
+ //Prepare the response code
+ num_buffer[0] = buffer[9];
+ num_buffer[1] = buffer[10];
+ num_buffer[2] = buffer[11];
+ num_buffer[3] = 0;
+ code = atoi (num_buffer);
+
+ //Print the code
+ printf("Code=%d\n",code);
+
+ //Print the response
+ printf("Response=%s\n",buffer);
+
+ //Handle the code and return status: 1-succeeded 0-failed
+ status = handleCode(code);
+
+ //Close the file
+ fclose(fp);
+
+ //Return status
+ return status;
+}
+
+
+/**
+ * Function Name: sendTelemetryDataToHonoInstance
+ * Description: Sends telemetry data to a hono instance with given
+ * host name and port
+ * Arguments: char * host_name
+ * int port
+ * char * tenant_name
+ * int device_id
+ * char * field
+ * double value
+ * Returns: Successfully returns status data.
+ * If the action completed -> returns 1
+ * If the action failed -> returns 0
+ */
+int sendTelemetryDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value)
+{
+ FILE *fp;
+ int code;
+ int status;
+
+ //Prepare command as string
+ //Example: "curl -X PUT -i -H 'Content-Type: application/json' --data-binary '{"Bearing": 0.5}' http://idial.institute:8080/telemetry/DEFAULT_TENANT/4711"
+ sprintf(buffer, "curl -X PUT -i -H 'Content-Type: application/json' --data-binary '{\"");
+ strcat(buffer, field);
+ strcat(buffer, "\": ");
+ snprintf(num_buffer, sizeof(num_buffer), "%f", value);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "}' http://");
+ strcat(buffer, host_name);
+ strcat(buffer, ":");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", port);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "/telemetry/");
+ strcat(buffer, tenant_name);
+ strcat(buffer, "/");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", device_id);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+
+ //Print the command that is created
+ printf("Command=%s\n",buffer);
+
+ //Execute the command
+ fp = popen(buffer,"r");
+
+ //Get and Parse the output
+ fgets(buffer, 13, fp); //Get the string HTTP/1.1 XXX
+
+ //Prepare the response code
+ num_buffer[0] = buffer[9];
+ num_buffer[1] = buffer[10];
+ num_buffer[2] = buffer[11];
+ num_buffer[3] = 0;
+ code = atoi (num_buffer);
+
+ //Print the code
+ printf("Code=%d\n",code);
+
+ //Print the response
+ printf("Response=%s\n",buffer);
+
+ //Handle the code and return status: 1-succeeded 0-failed
+ status = handleCode(code);
+
+ //Close the file
+ fclose(fp);
+
+ //Return status
+ return status;
+}
+
+
+/**
+ * Function Name: sendEventDataToHonoInstance
+ * Description: Sends event data to a hono instance with given
+ * host name and port
+ * Arguments: char * host_name
+ * int port
+ * char * tenant_name
+ * int device_id
+ * char * field
+ * double value
+ * Returns: Successfully returns status data.
+ * If the action completed -> returns 1
+ * If the action failed -> returns 0
+ */
+int sendEventDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value)
+{
+ FILE *fp;
+ int code;
+ int status;
+
+ //Prepare command as string
+ //Example: "curl -X PUT -i -H 'Content-Type: application/json' --data-binary '{"Bearing": 0.5}' http://idial.institute:8080/event/DEFAULT_TENANT/4711"
+ sprintf(buffer, "curl -X PUT -i -H 'Content-Type: application/json' --data-binary '{\"");
+ strcat(buffer, field);
+ strcat(buffer, "\": ");
+ snprintf(num_buffer, sizeof(num_buffer), "%f", value);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "}' http://");
+ strcat(buffer, host_name);
+ strcat(buffer, ":");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", port);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+ strcat(buffer, "/event/");
+ strcat(buffer, tenant_name);
+ strcat(buffer, "/");
+ snprintf(num_buffer, sizeof(num_buffer), "%d", device_id);
+ strcat(buffer, num_buffer);
+ num_buffer[0] = 0; //Clear array
+
+ //Print the command that is created
+ printf("Command=%s\n",buffer);
+
+ //Execute the command
+ fp = popen(buffer,"r");
+
+ //Get and Parse the output
+ fgets(buffer, 13, fp); //Get the string HTTP/1.1 XXX
+
+ //Prepare the response code XXX
+ num_buffer[0] = buffer[9];
+ num_buffer[1] = buffer[10];
+ num_buffer[2] = buffer[11];
+ num_buffer[3] = 0;
+ code = atoi (num_buffer);
+
+ //Print the code
+ printf("Code=%d\n",code);
+
+ //Print the response
+ printf("Response=%s\n",buffer);
+
+ //Handle the code and return status: 1-succeeded 0-failed
+ status = handleCode(code);
+
+ //Close the file
+ fclose(fp);
+
+ //Return status
+ return status;
+}
+
diff --git a/rover/src/hono_interaction/hono_interaction.h b/rover/src/hono_interaction/hono_interaction.h
new file mode 100644
index 0000000..572b533
--- /dev/null
+++ b/rover/src/hono_interaction/hono_interaction.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * Hono interaction library for Rover - Header file
+ *
+ * Authors:
+ * M. Ozcelikors, R.Hottger
+ * <mozcelikors@gmail.com> <robert.hoettger@fh-dortmund.de>
+ *
+ */
+
+#ifndef HONO_INTERACTION_HONO_INTERACTION_H_
+#define HONO_INTERACTION_HONO_INTERACTION_H_
+
+
+//Interfaces
+
+int registerDeviceToHonoInstance (char * host_name, int port, char * tenant_name, int device_id);
+
+int sendTelemetryDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value);
+
+int sendEventDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value);
+
+
+#endif /* HONO_INTERACTION_HONO_INTERACTION_H_ */