Telegram Bot Api Client  0.6.1
Client_for_accessing_Telegram's_Bot_API
JsonWebClient.h
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef JsonWebClient_h
14 #define JsonWebClient_h
15 
16 #include "TBCDebug.h"
17 #include "Arduino.h"
18 #include <Client.h>
19 #include <ArduinoJson.h>
20 
21 #ifndef JWC_BUFF_SIZE
22 #ifdef ESP8266
23 #define JWC_BUFF_SIZE 1000
24 #else
25 #define JWC_BUFF_SIZE 10000
26 #endif
27 #endif
28 
29 
47 enum class JwcProcessError : int
48 {
50  Ok = 0,
52  HttpErr = -1,
55  MsgTooBig = -2,
57  MsgJsonErr = -3
58 };
59 
61 static String JwcProcessErrorString[] = {"Ok", "HttpErr", "MsgTooBig", "MsgJsonErr"};
62 
63 static String toString(JwcProcessError err)
64 {
65  return JwcProcessErrorString[(-1) * (int) err];
66 }
67 
68 
69 
70 // Inspired by PubSubClient by Nick O'Leary (http://knolleary.net)
71 #ifdef ESP8266
72 #include <functional>
73 #define JWC_CALLBACK_MESSAGE_SIGNATURE std::function<void(void*, JwcProcessError, JsonObject&)> callbackSuccess
74 #define JWC_CALLBACK_ERROR_SIGNATURE std::function<void(void*, JwcProcessError, Client*)> callbackError
75 #else
76 #define JWC_CALLBACK_MESSAGE_SIGNATURE void (*callbackSuccess)(void*, JwcProcessError, JsonObject&)
77 #define JWC_CALLBACK_ERROR_SIGNATURE void (*callbackError)(void*, JwcProcessError, Client*)
78 #endif
79 
95 enum class JwcClientState : int
96 {
98  Unconnected = 0,
100  Connected = 1,
102  Waiting = 2,
104  Headers = 3,
106  Json = 4
107 };
108 
110 static String JwcClientStateString[] = {"Unconnected", "Connected", "Waiting", "Headers", "Json"};
111 
112 static String toString(JwcClientState state)
113 {
114  return JwcClientStateString[(int) state];
115 }
116 
117 
137 {
138  private:
142  Client* NetClient;
144  String Host;
146  int Port;
148  long ContentLength = JWC_BUFF_SIZE;
150  bool HttpStatusOk = false;
158  void reConnect();
172  bool processHeader();
180  bool processJson();
181 
182  public:
195  JsonWebClient (
196  Client* netClient,
197  String host,
198  int port,
199  void* callBackObject,
213  bool fire (String commands[], int count);
230  bool loop();
239  bool stop();
240 };
241 #endif
Definition: JsonWebClient.h:136
JwcClientState state()
Current state of the client.
Definition: JsonWebClient.cpp:157
bool stop()
Stops the client.
Definition: JsonWebClient.cpp:53
bool HttpStatusOk
Definition: JsonWebClient.h:150
void reConnect()
Reconnects to host.
Definition: JsonWebClient.cpp:32
bool processJson()
Process JSON.
Definition: JsonWebClient.cpp:78
JWC_CALLBACK_ERROR_SIGNATURE
Definition: JsonWebClient.h:164
bool loop()
Method to poll client processing.
Definition: JsonWebClient.cpp:116
void * CallBackObject
Definition: JsonWebClient.h:160
String Host
Definition: JsonWebClient.h:144
JwcClientState State
Definition: JsonWebClient.h:140
long ContentLength
Definition: JsonWebClient.h:148
bool fire(String commands[], int count)
Executes a list of commands.
Definition: JsonWebClient.cpp:162
int Port
Definition: JsonWebClient.h:146
Client * NetClient
Definition: JsonWebClient.h:142
JsonWebClient(Client *netClient, String host, int port, void *callBackObject, JWC_CALLBACK_MESSAGE_SIGNATURE, JWC_CALLBACK_ERROR_SIGNATURE)
Definition: JsonWebClient.cpp:15
JWC_CALLBACK_MESSAGE_SIGNATURE
Definition: JsonWebClient.h:162
bool processHeader()
Process a header.
Definition: JsonWebClient.cpp:61