Junichi Katsu / Mbed OS mbed-os-example-milkcocoa

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Milkcocoa.h Source File

Milkcocoa.h

00001 #ifndef _MILKCOCOA_H_
00002 #define _MILKCOCOA_H_
00003 
00004 #include "mbed.h"
00005 #include "MQTTmbed.h"
00006 #include "MQTTClient.h"
00007 #include "MClient.h"
00008 #include "rtos.h"
00009 
00010 #define RECV_TIMEOUT    500
00011 #define MILKCOCOA_SUBSCRIBERS 8
00012 #define START_THREAD 1
00013 
00014 class DataElement {
00015   public:
00016     DataElement();
00017     DataElement(char *json_string);
00018     void setValue(const char *key, const char *v);
00019     void setValue(const char *key, int v);
00020     void setValue(const char *key, double v);
00021     char *toCharArray();
00022     char *getString(const char *key);
00023     int getInt(const char *key);
00024     float getFloat(const char *key);
00025 
00026   private:
00027     char json_msg[256];
00028 };
00029 
00030 typedef void (*GeneralFunction) (MQTT::MessageData& elem);
00031 
00032 class MilkcocoaSubscriber {
00033     public:
00034         GeneralFunction cb;
00035     char topic[100];
00036         MilkcocoaSubscriber(GeneralFunction _cb);
00037 };
00038 
00039 class Milkcocoa {
00040  public:
00041  
00042 //  Milkcocoa(const char *host, uint16_t port, const char *_app_id, const char *client_id);
00043   Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id);
00044   Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session);
00045   static Milkcocoa* createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret);
00046   void connect();
00047   void loop();
00048   bool push(const char *path, DataElement dataelement);
00049   bool send(const char *path, DataElement dataelement);
00050   bool on(const char *path, const char *event, GeneralFunction cb);
00051   void setLoopCycle(int cycle);
00052   void start();
00053 
00054 private:
00055   char servername[64];
00056   int16_t portnum;
00057   char _clientid[64];
00058   char username[32];
00059   char password[32];
00060   const char *app_id;
00061   int16_t loop_cycle;
00062 
00063   MClient *client;
00064   GeneralFunction _cb;
00065   MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];
00066   Thread cycleThread;
00067   void cycle_Thread(void);
00068   static void threadStarter(void const *p);
00069 };
00070 
00071 
00072 #endif