Skip to content

Commit

Permalink
Merge pull request #66 from WLANThermo-nano/RotateNextion
Browse files Browse the repository at this point in the history
Rotate and calibrate nextion via web
  • Loading branch information
tuniii committed Mar 15, 2020
2 parents 7712f8a + 0e85a4a commit eb810bb
Show file tree
Hide file tree
Showing 19 changed files with 318 additions and 195 deletions.
23 changes: 23 additions & 0 deletions src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
****************************************************/
#include "API.h"
#include "system/SystemBase.h"
#include "display/DisplayBase.h"
#include "Version.h"
#include "WebHandler.h"
#include "DbgPrint.h"
Expand All @@ -28,6 +29,14 @@ API::API()
{
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Display JSON Object - Send everytime when connect to API
void API::displayObj(JsonObject &jObj)
{
jObj["updname"] = gDisplay->getUpdateName();
jObj["orientation"] = (uint16_t)gDisplay->getOrientation();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Device JSON Object - Send everytime when connect to API
void API::deviceObj(JsonObject &jObj)
Expand Down Expand Up @@ -241,6 +250,16 @@ void API::updateObj(JsonObject &jObj)
// nach einer bestimmten Version fragen
if (gSystem->otaUpdate.getRequestedVersion() != "false")
jObj["version"] = gSystem->otaUpdate.getRequestedVersion();

if (gSystem->otaUpdate.getRequestedFile() != "")
{
jObj["file"] = gSystem->otaUpdate.getRequestedFile();
// include also preleases for file request
jObj["prerelease"] = true;
}

if (gSystem->otaUpdate.getForceFlag())
jObj["force"] = gSystem->otaUpdate.getForceFlag();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -333,6 +352,10 @@ void API::settingsObj(JsonObject &jObj)
_aktor.add("DAMPER");*/
//TODO

// DISPLAY
JsonObject &_display = jObj.createNestedObject("display");
displayObj(_display);

// IOT
JsonObject &_iot = jObj.createNestedObject("iot");
iotObj(_iot);
Expand Down
1 change: 1 addition & 0 deletions src/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class API
{
public:
API();
static void displayObj(JsonObject &jObj);
static void deviceObj(JsonObject &jObj);
static void systemObj(JsonObject &jObj, bool settings = false);
static void channelAry(JsonArray &jAry, int cc);
Expand Down
236 changes: 105 additions & 131 deletions src/Cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ int Cloud::log_length = 0;
int Cloud::log_typ = 0;
int Cloud::apicontent = 0;
AsyncClient *Cloud::apiClient = NULL;
byte Cloud::server_state = NULL;
bool Cloud::clientlog = false;
int Cloud::apiindex = 0;
int Cloud::urlindex = 0;
int Cloud::parindex = 0;

enum
{
Expand Down Expand Up @@ -99,25 +95,12 @@ void Cloud::update()
// First get time from server before sending data
if (now() < 31536000)
{
if (Cloud::sendAPI(0))
{
Cloud::apiindex = NOAPI;
Cloud::urlindex = APILINK;
Cloud::parindex = NOPARA;
Cloud::sendAPI(2);
}
Cloud::sendAPI(NOAPI, APILINK, NOPARA);
}
else if (0u == intervalCounter)
{
intervalCounter = config.interval;

if (Cloud::sendAPI(0))
{
Cloud::apiindex = APICLOUD;
Cloud::urlindex = CLOUDLINK;
Cloud::parindex = NOPARA;
Cloud::sendAPI(2);
}
Cloud::sendAPI(APICLOUD, CLOUDLINK, NOPARA);
}
}
else
Expand Down Expand Up @@ -404,136 +387,127 @@ void Cloud::printClient(const char *link, int arg)

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Send to API
bool Cloud::sendAPI(int check)
bool Cloud::sendAPI(int apiIndex, int urlIndex, int parIndex)
{
static CloudData cloudData = {0, 0, 0};

if (apiClient)
return false; //client already exists

apiClient = new AsyncClient();
if (!apiClient)
return false; //could not allocate client

apiClient->onError([](void *arg, AsyncClient *client, int error) {
IPRINTP("e:Client");
apiClient = NULL;
delete client;
}, NULL);

if (check == 0)
cloudData.apiIndex = apiIndex;
cloudData.urlIndex = urlIndex;
cloudData.parIndex = parIndex;

apiClient->onConnect([](void *arg, AsyncClient *client)
{
if (apiClient)
return false; //client already exists
CloudData *pCloudData = (CloudData*)arg;
//printClient(serverurl[urlindex].page.c_str(),CLIENTCONNECT);
apicontent = false;

apiClient = new AsyncClient();
if (!apiClient)
return false; //could not allocate client
apiClient->onError(NULL, NULL);

apiClient->onError([](void *arg, AsyncClient *client, int error) {
IPRINTP("e:Client");
client->onDisconnect([](void *arg, AsyncClient *c)
{
//printClient(serverurl[urlindex].page.c_str() ,DISCONNECT);
apiClient = NULL;
delete client;
},
NULL);
}
else if (check == 2)
{ // Senden ueber Server

server_state = 0;
apiClient->onConnect([](void *arg, AsyncClient *client) {
//printClient(serverurl[urlindex].page.c_str(),CLIENTCONNECT);
apicontent = false;

apiClient->onError(NULL, NULL);

client->onDisconnect([](void *arg, AsyncClient *c) {
//printClient(serverurl[urlindex].page.c_str() ,DISCONNECT);
apiClient = NULL;
delete c;
},
NULL);

client->onData([](void *arg, AsyncClient *c, void *data, size_t len) {
String payload((char *)data);
//Serial.println(millis());
if (clientlog)
Serial.println(payload);
//Serial.println(len);

if (payload.indexOf("HTTP/1.1") > -1)
{ // Time Stamp
readUTCfromHeader(payload);
}
delete c;
}, &cloudData);

if ((payload.indexOf("200 OK") > -1))
{ // 200 Header
server_state = 1; // Server Communication: yes
readContentLengthfromHeader(payload, len);
checkContentTypfromHeader(payload, len);

if (log_length > 0)
{ // Content available
apicontent = 1;
if (log_typ == 1 && payload.indexOf("{") > -1)
{ // JSON: Body belongs to header
apicontent = payload.indexOf("{") + 1;
if ((len - (apicontent - 1)) != log_length)
Serial.println("[WARNING]: Content-Length unequal");
Serial.println("Body belongs to header");
}
else
return; // Header alone
}
}
else if (payload.indexOf("302 Found") > -1)
{ // 302 Header: new API-Links
readLocation(payload, len);
}
client->onData([](void *arg, AsyncClient *c, void *data, size_t len)
{
CloudData *pCloudData = (CloudData*)arg;
String payload((char *)data);
//Serial.println(millis());
if (clientlog)
Serial.println(payload);
//Serial.println(len);

//} else if (payload.indexOf("500 Internal Server Error") > -1) { // 500 Header: new API-Links
// Serial.println("Fehler im Verbindungsaufbau");
if (payload.indexOf("HTTP/1.1") > -1)
{ // Time Stamp
readUTCfromHeader(payload);
}

if (apicontent)
{ // Body: 1 part
if (log_typ == 1)
{ // JSON
bodyWebHandler.setServerAPI(NULL, (uint8_t *)data + (apicontent - 1));
if ((payload.indexOf("200 OK") > -1))
{ // 200 Header
readContentLengthfromHeader(payload, len);
checkContentTypfromHeader(payload, len);

if (log_length > 0)
{ // Content available
apicontent = 1;
if (log_typ == 1 && payload.indexOf("{") > -1)
{ // JSON: Body belongs to header
apicontent = payload.indexOf("{") + 1;
if ((len - (apicontent - 1)) != log_length)
Serial.println("[WARNING]: Content-Length unequal");
Serial.println("Body belongs to header");
}
apicontent = 0;
log_length -= len; // Option das nicht alles auf einmal kommt bleibt offen
//Serial.println(log_length);
else
return; // Header alone
}
else if (log_length > 0)
{ // Body: current part
log_length -= len; // leeren?
//Serial.println(log_length);
}
else if (payload.indexOf("302 Found") > -1)
{ // 302 Header: new API-Links
readLocation(payload, len);
}

//} else if (payload.indexOf("500 Internal Server Error") > -1) { // 500 Header: new API-Links
// Serial.println("Fehler im Verbindungsaufbau");

if (apicontent)
{ // Body: 1 part
if (log_typ == 1)
{ // JSON
bodyWebHandler.setServerAPI(NULL, (uint8_t *)data + (apicontent - 1));
}
},
NULL);

//send the request
//printClient(serverurl[urlindex].page.c_str() ,SENDTO);
String message = API::apiData(apiindex);
String adress = createCommand(POSTMETH, parindex, serverurl[urlindex].page.c_str(), serverurl[urlindex].host.c_str(), message.length());
adress += message;
client->write(adress.c_str());
if (clientlog)
Serial.println(adress);
apiindex = 0;
urlindex = 0;
parindex = 0;
},
NULL);

if (!apiClient->connect(serverurl[urlindex].host.c_str(), 80))
{
printClient(serverurl[urlindex].page.c_str(), CONNECTFAIL);
AsyncClient *client = apiClient;
apiClient = NULL;
delete client;
}
apicontent = 0;
log_length -= len; // Option das nicht alles auf einmal kommt bleibt offen
//Serial.println(log_length);
}
else if (log_length > 0)
{ // Body: current part
log_length -= len; // leeren?
//Serial.println(log_length);
}
}, &cloudData);

//send the request
//printClient(serverurl[urlindex].page.c_str() ,SENDTO);
String message = API::apiData(pCloudData->apiIndex);
String adress = createCommand(POSTMETH, pCloudData->parIndex, serverurl[pCloudData->urlIndex].page.c_str(), serverurl[pCloudData->urlIndex].host.c_str(), message.length());
adress += message;
client->write(adress.c_str());
if (clientlog)
Serial.println(adress);
}, &cloudData);

if (!apiClient->connect(serverurl[cloudData.urlIndex].host.c_str(), 80))
{
printClient(serverurl[cloudData.urlIndex].page.c_str(), CONNECTFAIL);
AsyncClient *client = apiClient;
apiClient = NULL;
delete client;
}

return true; // Nachricht kann gesendet werden
}

void Cloud::check_api()
void Cloud::checkAPI()
{
Serial.println("check_api");
if (sendAPI(0))
{ // blockt sich selber, so dass nur ein Client gleichzeitig offen
apiindex = APIUPDATE;
urlindex = APILINK;
parindex = NOPARA;
sendAPI(2);
}
Serial.println("checkAPI");
sendAPI(APIUPDATE, APILINK, NOPARA);
}

// GET/POST Generator
String Cloud::createCommand(bool meth, int para, const char *link, const char *host, int content)
{
Expand Down
15 changes: 9 additions & 6 deletions src/Cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ typedef struct
uint16_t interval;
} CloudConfig;

typedef struct
{
int apiIndex;
int urlIndex;
int parIndex;
} CloudData;

enum
{
NOAPI,
Expand Down Expand Up @@ -77,12 +84,9 @@ class Cloud
void setConfig(CloudConfig newConfig);
String newToken();
uint8_t state;
static void check_api();
static bool sendAPI(int check);
static void checkAPI();
static bool sendAPI(int apiIndex, int urlIndex, int parIndex);

static int apiindex;
static int urlindex;
static int parindex;
static uint8_t serverurlCount;
static ServerData serverurl[]; // 0:api, 1: note, 2:cloud
static bool clientlog;
Expand All @@ -102,6 +106,5 @@ class Cloud
static int log_typ;
static int apicontent;
static AsyncClient *apiClient;
static byte server_state;
uint16_t intervalCounter;
};
Loading

0 comments on commit eb810bb

Please sign in to comment.