implementation of autotest

This commit is contained in:
Bogdan Pilyugin 2023-03-31 16:08:44 +02:00
parent 2c3076a6f9
commit 686dca116c
4 changed files with 105 additions and 41 deletions

View File

@ -75,4 +75,6 @@ void ComposeTopic(char *topic, int idx, char *service_name, char *direct);
void regUserEventHandler(void (*event_handler)(int idx, void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)); void regUserEventHandler(void (*event_handler)(int idx, void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data));
void SystemDataHandler(char *data, uint32_t len, int idx); void SystemDataHandler(char *data, uint32_t len, int idx);
mqtt_app_err_t PublicTestMQTT(int idx);
#endif /* MAIN_INCLUDE_MQTT_H_ */ #endif /* MAIN_INCLUDE_MQTT_H_ */

View File

@ -24,6 +24,7 @@
#include "HTTPServer.h" #include "HTTPServer.h"
#include "LoRaWAN.h" #include "LoRaWAN.h"
#include "Helpers.h" #include "Helpers.h"
#include "MQTT.h"
static const char *TAG = "HTTPServerPost"; static const char *TAG = "HTTPServerPost";
@ -421,6 +422,30 @@ static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData)
httpd_query_key_value(PostData, "tsr", GetSysConf()->sntpClient.SntpServerAdr, httpd_query_key_value(PostData, "tsr", GetSysConf()->sntpClient.SntpServerAdr,
sizeof(GetSysConf()->sntpClient.SntpServerAdr)); sizeof(GetSysConf()->sntpClient.SntpServerAdr));
/*MQTT Test button handlers*/
if (httpd_query_key_value(PostData, "mqtttest1", tmp, 6) == ESP_OK)
{
if(!strcmp(tmp, (const char*) "prs"))
{
ESP_LOGI(TAG,"MQTT TEST 1");
PublicTestMQTT(0);
return HTTP_IO_DONE;
}
}
if (httpd_query_key_value(PostData, "mqtttest2", tmp, 6) == ESP_OK)
{
if(!strcmp(tmp, (const char*) "prs"))
{
ESP_LOGI(TAG,"MQTT TEST 2");
PublicTestMQTT(1);
return HTTP_IO_DONE;
}
}
if (httpd_query_key_value(PostData, "save", tmp, 6) == ESP_OK || if (httpd_query_key_value(PostData, "save", tmp, 6) == ESP_OK ||
httpd_query_key_value(PostData, "apply", tmp, 6) == ESP_OK) httpd_query_key_value(PostData, "apply", tmp, 6) == ESP_OK)
{ {
@ -455,6 +480,9 @@ static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData)
return HTTP_IO_REDIRECT; return HTTP_IO_REDIRECT;
} }
} }
#endif #endif
return HTTP_IO_DONE; return HTTP_IO_DONE;
} }

View File

@ -371,4 +371,6 @@ static void mqtt2_user_event_handler(void *handler_args, esp_event_base_t base,
UserEventHandler(1, handler_args, base, event_id, event_data); UserEventHandler(1, handler_args, base, event_id, event_data);
} }
#endif #endif

View File

@ -1,4 +1,4 @@
/*! Copyright 2022 Bogdan Pilyugin /*! Copyright 2022 Bogdan Pilyugin
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,15 +34,15 @@
*/ */
/* /*
{ {
"messid":12345, "messid":12345,
"api":"2.0", "api":"2.0",
"request":"GET", "request":"GET",
"url":"api/status.json", "url":"api/status.json",
"postdata":"param1=value&param2=value", "postdata":"param1=value&param2=value",
"reload":"true" "reload":"true"
} }
*/ */
#include "MQTT.h" #include "MQTT.h"
#include "jWrite.h" #include "jWrite.h"
@ -70,44 +70,44 @@ const char apiver[] = "2.0";
const char tagGet[] = "GET"; const char tagGet[] = "GET";
const char tagPost[] = "POST"; const char tagPost[] = "POST";
const char* mqtt_app_err_descr[] = { const char *mqtt_app_err_descr[] = {
"Operation OK", "Operation OK",
"Internal error", "Internal error",
"Wrong json format", "Wrong json format",
"Key 'idmess' not found", "Key 'idmess' not found",
"Key 'idmess' value too long", "Key 'idmess' value too long",
"Key 'api' not found", "Key 'api' not found",
"API version not supported", "API version not supported",
"Key 'request' not found", "Key 'request' not found",
"Unsupported HTTP method", "Unsupported HTTP method",
"Key 'url' not found", "Key 'url' not found",
"Key 'url' value too long", "Key 'url' value too long",
"URL not found", "URL not found",
"Key 'postdata' not found", "Key 'postdata' not found",
"Key 'postdata' too long", "Key 'postdata' too long",
"File size too big", "File size too big",
"File is empty", "File is empty",
"Unknown error" "Unknown error"
}; };
const char* mqtt_app_err_breef[] = { const char *mqtt_app_err_breef[] = {
"OK", "OK",
"INTERNAL_ERR", "INTERNAL_ERR",
"WRONG_JSON_ERR", "WRONG_JSON_ERR",
"NO_ID_ERR", "NO_ID_ERR",
"ID_OVERSIZE_ERR", "ID_OVERSIZE_ERR",
"NO_API_ERR", "NO_API_ERR",
"VERSION_ERR", "VERSION_ERR",
"NO_REQUEST_ERR", "NO_REQUEST_ERR",
"UNSUPPORTED_METHOD_ERR", "UNSUPPORTED_METHOD_ERR",
"NO_URL_ERR", "NO_URL_ERR",
"URL_OVERSIZE_ERR", "URL_OVERSIZE_ERR",
"URL_NOT_FOUND_ERR", "URL_NOT_FOUND_ERR",
"NO_POSTDATA_ERR", "NO_POSTDATA_ERR",
"POSTDATA_OVERSIZE_ERR", "POSTDATA_OVERSIZE_ERR",
"FILE_OVERSIZE_ERR", "FILE_OVERSIZE_ERR",
"FILE_EMPTY_ERR", "FILE_EMPTY_ERR",
"UNKNOWN_ERR" "UNKNOWN_ERR"
}; };
typedef enum typedef enum
@ -360,7 +360,6 @@ void SystemDataHandler(char *data, uint32_t len, int idx)
ESP_LOGW(TAG, "URL=%s, DATA=%s", URL, POST_DATA); ESP_LOGW(TAG, "URL=%s, DATA=%s", URL, POST_DATA);
HTTPPostApp(NULL, URL, POST_DATA); HTTPPostApp(NULL, URL, POST_DATA);
jRead(data, "{'reload'", &result); jRead(data, "{'reload'", &result);
if (result.elements == 1 && !memcmp("true", result.pValue, result.bytelen)) if (result.elements == 1 && !memcmp("true", result.pValue, result.bytelen))
{ {
@ -401,3 +400,36 @@ api_json_err:
if (ResponceWithError(idx, file, ID, URL, api_err) != API_OK) if (ResponceWithError(idx, file, ID, URL, api_err) != API_OK)
ESP_LOGE(TAG, "Failed to allocate memory for file MQTT message"); ESP_LOGE(TAG, "Failed to allocate memory for file MQTT message");
} }
mqtt_app_err_t PublicTestMQTT(int idx)
{
char JSONMess[MAX_ERROR_JSON];
jwOpen(JSONMess, MAX_ERROR_JSON, JW_OBJECT, JW_PRETTY);
time_t now;
time(&now);
jwObj_int("time", (unsigned int) now);
jwObj_string("event", "MQTT_TEST_MESSAGE)");
jwEnd();
jwClose();
char *buf = (char*) malloc(strlen(JSONMess) + 1);
if (buf)
{
memcpy(buf, JSONMess, strlen(JSONMess));
MQTT_DATA_SEND_STRUCT DSS;
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
DSS.raw_data_ptr = buf;
DSS.data_length = strlen(JSONMess);
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
return API_OK;
else
{
free(buf);
return API_INTERNAL_ERR;
}
}
else
{ // ERR internal error on publish error
return API_INTERNAL_ERR;
}
}