Peter Ferland / Cayenne-MQTT-mbed-MTSAS

Fork of Cayenne-MQTT-mbed by myDevicesIoT

Files at this revision

API Documentation at this revision

Comitter:
jburhenn
Date:
Wed Oct 26 16:33:46 2016 -0600
Parent:
7:e3b2a154ab7a
Child:
10:3d8c7c89d47a
Commit message:
Updated credential ordering to match dashboard. Added init function to set the credentials. Added setDefaultMessageHandler override so member functions can be used for message handling. Updated namesspace name to be more specific.

Changed in this revision

src/CayenneMQTTClient/CayenneMQTTClient.h Show annotated file Show diff for this revision Revisions of this file
src/CayenneUtils/CayenneDataArray.h Show annotated file Show diff for this revision Revisions of this file
src/CayenneUtils/CayenneDefines.h Show annotated file Show diff for this revision Revisions of this file
--- a/src/CayenneMQTTClient/CayenneMQTTClient.h	Tue Oct 25 16:23:07 2016 -0600
+++ b/src/CayenneMQTTClient/CayenneMQTTClient.h	Wed Oct 26 16:33:46 2016 -0600
@@ -24,7 +24,7 @@
 #include "../CayenneUtils/CayenneUtils.h"
 #include "../CayenneUtils/CayenneDataArray.h"
 
-namespace Cayenne
+namespace CayenneMQTT
 {
 	/**
 	* Cayenne message data passed to message handler functions.
@@ -38,6 +38,20 @@
 		const char* type; /**< The type of data in the message, if it exists, otherwise NULL. */
 		CayenneValuePair values[CAYENNE_MAX_MESSAGE_VALUES]; /**< The unit/value data pairs in the message. The units and values can be NULL. */
 		size_t valueCount; /**< The count of items in the values array. */
+
+		/**
+		* Get value at specified index.
+		* @param[in] index Index of value to retrieve, if none is specified it gets the first value.
+		* @return Value at the specified index, can be NULL.
+		*/
+		const char* getValue(size_t index = 0) const { return values[index].value; }
+
+		/**
+		* Get unit at specified index.
+		* @param[in] index Index of unit to retrieve, if none is specified it gets the first unit.
+		* @return Unit at the specified index, can be NULL.
+		*/
+		const char* getUnit(size_t index = 0) const { return values[index].unit; }
 	} MessageData;
 	
 	/**
@@ -58,14 +72,31 @@
 		/**
 		* Create an Cayenne MQTT client object.
 		* @param[in] network Pointer to an instance of the Network class. Must be connected to the endpoint before calling MQTTClient connect.
+		* @param[in] username Cayenne username
+		* @param[in] password Cayenne password
+		* @param[in] clientID Cayennne client ID
 		* @param[in] command_timeout_ms Timeout for commands in milliseconds.
 		*/
-		MQTTClient(Network& network, unsigned int command_timeout_ms = 30000) :	Base(network, command_timeout_ms), _username(NULL), _clientID(NULL)
+		MQTTClient(Network& network, const char* username = NULL, const char* password = NULL, const char* clientID = NULL, unsigned int command_timeout_ms = 30000) : 
+			Base(network, command_timeout_ms), _username(username), _password(password), _clientID(clientID)
 		{
 			Base::setDefaultMessageHandler(this, &MQTTClient::mqttMessageArrived);
 		};
 
 		/**
+		* Initialize connection credentials.
+		* @param[in] username Cayenne username
+		* @param[in] password Cayenne password
+		* @param[in] clientID Cayennne client ID
+		*/
+		void init(const char* username, const char* password, const char* clientID)
+		{
+			_username = username;
+			_password = password;
+			_clientID = clientID;
+		};
+
+		/**
 		* Set default handler function called when a message is received.
 		* @param[in] handler Function called when message is received, if no other handlers exist for the topic.
 		*/
@@ -75,20 +106,29 @@
 		};
 
 		/**
-		* Connect to the Cayenne server.
+		* Set default handler function called when a message is received.
+		* @param item Address of initialized object
+		* @param handler Function called when message is received, if no other handlers exist for the topic.
+		*/
+		template<class T>
+		void setDefaultMessageHandler(T *item, void (T::*handler)(MessageData&))
+		{
+			_defaultMessageHandler.attach(item, handler);
+		}
+
+		/**
+		* Connect to the Cayenne server. Connection credentials must be initialized before calling this function.
 		* @param[in] username Cayenne username
+		* @param[in] password Cayenne password
 		* @param[in] clientID Cayennne client ID
-		* @param[in] password Password
 		* @return success code
 		*/
-		int connect(const char* username, const char* clientID, const char* password) {
+		int connect() {
 			MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
 			data.MQTTVersion = 3;
-			_clientID = clientID;
-			_username = username;
+			data.username.cstring = const_cast<char*>(_username);
+			data.password.cstring = const_cast<char*>(_password);
 			data.clientID.cstring = const_cast<char*>(_clientID);
-			data.username.cstring = const_cast<char*>(_username);
-			data.password.cstring = const_cast<char*>(password);
 			return Base::connect(data);
 		};
 
@@ -396,6 +436,7 @@
 
 	private:
 		const char* _username;
+		const char* _password;
 		const char* _clientID;
 		struct CayenneMessageHandlers
 		{
--- a/src/CayenneUtils/CayenneDataArray.h	Tue Oct 25 16:23:07 2016 -0600
+++ b/src/CayenneUtils/CayenneDataArray.h	Wed Oct 26 16:33:46 2016 -0600
@@ -24,7 +24,7 @@
 
 // C++ version of the data array. This is defined here so it requires no source file.
 
-namespace Cayenne
+namespace CayenneMQTT
 {
 	/**
 	* @class DataArray
@@ -289,11 +289,7 @@
 		*/
 		inline void add(const __FlashStringHelper* unit, const float value) {
 			char str[33];
-#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
-            dtostrf(value, 5, 3, str);
-#else
-            snprintf(str, 33, "%2.3f", value);
-#endif
+			dtostrf(value, 5, 3, str);
 			add(unit, str);
 		}
 
@@ -304,11 +300,7 @@
 		*/
 		inline void add(const __FlashStringHelper* unit, const double value) {
 			char str[33];
-#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
-            dtostrf(value, 5, 3, str);
-#else
-            snprintf(str, 33, "%2.3f", value);
-#endif
+			dtostrf(value, 5, 3, str);
 			add(unit, str);
 		}
 
@@ -337,7 +329,7 @@
 	};
 }
 
-typedef Cayenne::DataArray<> CayenneDataArray;
+typedef CayenneMQTT::DataArray<> CayenneDataArray;
 
 #else
 
--- a/src/CayenneUtils/CayenneDefines.h	Tue Oct 25 16:23:07 2016 -0600
+++ b/src/CayenneUtils/CayenneDefines.h	Wed Oct 26 16:33:46 2016 -0600
@@ -37,13 +37,13 @@
 
 //Comment this out to prevent digital and analog specific code from being compiled. If you only need to send
 //and receive standard channel data you can comment this out to decrease the program size.
-#define DIGITAL_AND_ANALOG_SUPPORT
+//#define DIGITAL_AND_ANALOG_SUPPORT
 
 //Comment this out if you don't need to subscribe to data or system info payloads.
 //#define PARSE_INFO_PAYLOADS
 
 //Some defines for AVR microcontrollers to allow easier usage of memory in program space.
-#if defined(__AVR__)
+#if defined(__AVR__) || defined(ARDUINO_ARCH_SAM)
 #include <avr/pgmspace.h>
 #define CAYENNE_USING_PROGMEM
 #define CAYENNE_PROGMEM PROGMEM