Junichi Katsu / Milkcocoa-os

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

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 __MILKCOCOA_THREAD
00011 
00012 #define RECV_TIMEOUT            500
00013 #define MILKCOCOA_SUBSCRIBERS   8
00014 #define START_THREAD            1
00015 
00016 class DataElement {
00017   public:
00018     DataElement();
00019     DataElement(char *json_string);
00020     void setValue(const char *key, const char *v);
00021     void setValue(const char *key, int v);
00022     void setValue(const char *key, double v);
00023     char *toCharArray();
00024     char *getString(const char *key);
00025     int getInt(const char *key);
00026     float getFloat(const char *key);
00027 
00028   private:
00029     char json_msg[256];
00030 };
00031 
00032 typedef void (*GeneralFunction) (MQTT::MessageData& elem);
00033 
00034 class MilkcocoaSubscriber {
00035     public:
00036         GeneralFunction cb;
00037         char topic[80];
00038         MilkcocoaSubscriber(GeneralFunction _cb);
00039 };
00040 
00041 class Milkcocoa {
00042  public:
00043   Milkcocoa(NetworkInterface* nif);
00044   Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id);
00045   Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session);
00046   static Milkcocoa* createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret);
00047   void set_network_settinng(const char *host, uint16_t port, const char *_app_id, const char *client_id);
00048   void connect();
00049   void loop();
00050   void send_massage();
00051   void close();
00052   bool push(const char *path, DataElement dataelement);
00053   bool push(const char *path, char *data);
00054   bool send(const char *path, DataElement dataelement);
00055   bool send(const char *path, char *data);
00056   bool on(const char *path, const char *event, GeneralFunction cb);
00057 #ifdef __MILKCOCOA_THREAD
00058   void setLoopCycle(int cycle);
00059   void start();
00060 #endif
00061 private:
00062   char servername[64];
00063   int16_t portnum;
00064   char _clientid[64];
00065   char username[32];
00066   char password[32];
00067   char app_id[32];
00068   char _message[256];
00069   int16_t loop_cycle;
00070 
00071   MQTTInterface* ipstack;
00072   MClient *client;
00073   GeneralFunction _cb;
00074   MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];
00075 
00076 #ifdef __MILKCOCOA_THREAD
00077   Thread cycleThread1;
00078   Thread cycleThread2;
00079   void cycle_Thread1(void);
00080   void cycle_Thread2(void);
00081   static void threadStarter1(void const *p);
00082   static void threadStarter2(void const *p);
00083 #endif
00084   typedef struct {
00085     char message[192];
00086     char topic[64];
00087   } milkcocoa_message_t;
00088   Mail<milkcocoa_message_t, 4> message_box;
00089   int connectting;
00090   int mqtt_connectting;
00091 
00092 };
00093 
00094 #endif