Skip to content

Commit

Permalink
feat(mqtt)!: Complete rework of MQTT API & Add new content (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 authored Apr 11, 2024
1 parent 79e83a0 commit 48ce862
Show file tree
Hide file tree
Showing 61 changed files with 2,038 additions and 1,332 deletions.
13 changes: 5 additions & 8 deletions code/components/jomjol_controlGPIO/server_GPIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,12 @@ bool GpioHandler::readConfig()
return false;
}

#ifdef ENABLE_MQTT
// std::string mainTopicMQTT = "";
#ifdef ENABLE_MQTT
std::string mainTopicMQTT = mqttServer_getMainTopic();
if (mainTopicMQTT.length() > 0)
{
mainTopicMQTT = mainTopicMQTT + "/GPIO";
ESP_LOGD(TAG, "MAINTOPICMQTT found");
}
#endif // ENABLE_MQTT
mainTopicMQTT = mainTopicMQTT + "/device/gpio";
#endif // ENABLE_MQTT

bool registerISR = false;
while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
{
Expand Down Expand Up @@ -447,7 +444,7 @@ void GpioHandler::registerGpioUri()

httpd_uri_t camuri = { };
camuri.method = HTTP_GET;
camuri.uri = "/GPIO";
camuri.uri = "/gpio";
camuri.handler = callHandleHttpRequest;
camuri.user_ctx = (void*)this;
httpd_register_uri_handler(_httpServer, &camuri);
Expand Down
34 changes: 16 additions & 18 deletions code/components/jomjol_controlcamera/ClassControllCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,23 @@ CCamera::CCamera()

void CCamera::powerResetCamera()
{
#if PWDN_GPIO_NUM > -1 // Use reset only if pin is available (PWDN_GPIO_NC == -1)
ESP_LOGD(TAG, "Resetting camera by power cycling");
gpio_config_t conf;
conf.intr_type = GPIO_INTR_DISABLE;
conf.pin_bit_mask = 1LL << PWDN_GPIO_NUM;
conf.mode = GPIO_MODE_OUTPUT;
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&conf);

// carefull, logic is inverted compared to reset pin
gpio_set_level(PWDN_GPIO_NUM, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(PWDN_GPIO_NUM, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
return;
#if PWDN_GPIO_NUM == -1 // Use reset only if pin is available
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No power down pin availbale to reset camera");
#else
ESP_LOGD(TAG, "Power pin not defined. Software power reset not available");
return;
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Resetting camera by power down");
gpio_config_t conf;
conf.intr_type = GPIO_INTR_DISABLE;
conf.pin_bit_mask = 1LL << PWDN_GPIO_NUM;
conf.mode = GPIO_MODE_OUTPUT;
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&conf);

// Be careful, logic is inverted compared to reset pin
gpio_set_level(PWDN_GPIO_NUM, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(PWDN_GPIO_NUM, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_controlcamera/server_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ esp_err_t handler_camera(httpd_req_t *req)

// Default usage message when handler gets called without any parameter
const std::string RESTUsageInfo =
"00: Handler usage:<br>"
"Handler usage:<br>"
"1. Set camera parameter:<br>"
"- '/camera?task=set_parameter&flashtime=0.1&flashintensity=1&brightness=-2&contrast=0& "
"saturation=0&sharpness=1&exposurecontrolmode=0&autoexposurelevel=0&manualexposurevalue=1200& "
Expand Down
11 changes: 5 additions & 6 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,15 @@ void hard_restart()

void task_reboot(void *DeleteMainFlow)
{
// write a reboot, to identify a reboot by purpouse
// Write a reboot, to identify a reboot by purpouse
FILE* pfile = fopen("/sdcard/reboot.txt", "w");
std::string _s_zw= "reboot";
fwrite(_s_zw.c_str(), strlen(_s_zw.c_str()), 1, pfile);
fclose(pfile);

vTaskDelay(3000 / portTICK_PERIOD_MS);

// Kill main task if executed in extra task, if not don't kill parent task to force reboot
if ((bool)DeleteMainFlow) {
DeleteMainFlowTask(); // Kill main task if executed in extra task, if not don't kill parent task
DeleteMainFlowTask();
}

/* Stop service tasks */
Expand All @@ -615,9 +614,10 @@ void task_reboot(void *DeleteMainFlow)
StatusLEDOff();
esp_camera_deinit();

vTaskDelay(3000 / portTICK_PERIOD_MS);
WIFIDestroy();

vTaskDelay(3000 / portTICK_PERIOD_MS);
vTaskDelay(1000 / portTICK_PERIOD_MS);
esp_restart(); // Reset type: CPU reset (Reset both CPUs)

vTaskDelay(5000 / portTICK_PERIOD_MS);
Expand Down Expand Up @@ -665,7 +665,6 @@ esp_err_t handler_reboot(httpd_req_t *req)
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
httpd_resp_set_type(req, "text/plain");

httpd_resp_sendstr(req, "Reboot initiated");

doReboot();
Expand Down
86 changes: 54 additions & 32 deletions code/components/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {
#include "ClassLogFile.h"
#include "time_sntp.h"
#include "Helper.h"
#include "system.h"
#include "statusled.h"
#include "server_ota.h"
#include "server_help.h"
Expand Down Expand Up @@ -59,7 +60,8 @@ void ClassFlowControll::SetInitialParameter(void)
disabled = false;
readParameterDone = false;
setActStatus(std::string(FLOW_NO_TASK));
setActFlowError(false);
flowStateErrorInRow = 0;
flowStateDeviationInRow = 0;
}


Expand Down Expand Up @@ -386,7 +388,7 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
#ifdef ENABLE_MQTT
else if (toUpper(_type).compare("[MQTT]") == 0)
{
cfc = new ClassFlowMQTT(&FlowControll);
cfc = new ClassFlowMQTT(flowpostprocessing);
if(cfc) {
flowMQTT = (ClassFlowMQTT*) cfc;
FlowControlPublish.push_back(cfc);
Expand Down Expand Up @@ -577,7 +579,7 @@ bool ClassFlowControll::doFlowImageEvaluation(std::string time)
setActStatus(TranslateAktstatus(FlowControll[i]->name()));
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Process state: " + getActStatus());
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", getActStatus(), 1, false);
MQTTPublish(mqttServer_getMainTopic() + "/process/status/process_state", getActStatus(), 1, false);
#endif //ENABLE_MQTT

if (!FlowControll[i]->doFlow(time)) {
Expand All @@ -587,10 +589,14 @@ bool ClassFlowControll::doFlowImageEvaluation(std::string time)
if (FlowControll[i]->getFlowState()->EventCode[j] < 0) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Error occured during processing of state \"" + getActStatus() + "\"");
result = false;
flowStateErrorInRow++;
flowStateDeviationInRow = 0;
break;
}
else {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Deviation occured during processing of state \"" + getActStatus() + "\"");
flowStateDeviationInRow++;
flowStateErrorInRow = 0;
}
}

Expand All @@ -616,7 +622,7 @@ bool ClassFlowControll::doFlowPublishData(std::string time)
setActStatus(TranslateAktstatus(FlowControlPublish[i]->name()));
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Process state: " + getActStatus());
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", getActStatus(), 1, false);
MQTTPublish(mqttServer_getMainTopic() + "/process/status/process_state", getActStatus(), 1, false);
#endif //ENABLE_MQTT

if (!FlowControlPublish[i]->doFlow(time)) {
Expand All @@ -626,10 +632,14 @@ bool ClassFlowControll::doFlowPublishData(std::string time)
if (FlowControll[i]->getFlowState()->EventCode[j] < 0) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Error occured during processing of state \"" + getActStatus() + "\"");
result = false;
flowStateErrorInRow++;
flowStateDeviationInRow = 0;
break;
}
else {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Deviation occured during processing of state \"" + getActStatus() + "\"");
flowStateDeviationInRow++;
flowStateErrorInRow = 0;
}
}

Expand All @@ -652,7 +662,7 @@ bool ClassFlowControll::doFlowTakeImageOnly(std::string time)
setActStatus(TranslateAktstatus(FlowControll[i]->name()));
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Process state: " + getActStatus());
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", getActStatus(), 1, false);
MQTTPublish(mqttServer_getMainTopic() + "/process/status/process_state", getActStatus(), 1, false);
#endif //ENABLE_MQTT

if (!FlowControlPublish[i]->doFlow(time)) {
Expand All @@ -661,11 +671,15 @@ bool ClassFlowControll::doFlowTakeImageOnly(std::string time)
for (int j = 0; j < FlowControll[i]->getFlowState()->EventCode.size(); j++) {
if (FlowControll[i]->getFlowState()->EventCode[j] < 0) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Error occured during processing of state \"" + getActStatus() + "\"");
flowStateErrorInRow++;
flowStateDeviationInRow = 0;
result = false;
break;
}
else {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Deviation occured during processing of state \"" + getActStatus() + "\"");
flowStateDeviationInRow++;
flowStateErrorInRow = 0;
}
}

Expand Down Expand Up @@ -704,9 +718,9 @@ void ClassFlowControll::PostProcessEventHandler()
for (int i = 0; i < FlowStatePublishEvent.size(); ++i) {
for (int j = 0; j < FlowControlPublish.size(); ++j) {
if (FlowStatePublishEvent[i]->ClassName.compare(FlowControlPublish[j]->name()) == 0) {
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, FlowStateEvaluationEvent[i]->ClassName + "-> doPostProcessEventHandling");
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, FlowStatePublishEvent[i]->ClassName + "-> doPostProcessEventHandling");
FlowControlPublish[j]->doPostProcessEventHandling();
FlowControll[j]->presetFlowStateHandler(true); // Reinit after processing
FlowControlPublish[j]->presetFlowStateHandler(true); // Reinit after processing
}
}
}
Expand All @@ -715,7 +729,7 @@ void ClassFlowControll::PostProcessEventHandler()
}


float ClassFlowControll::getProcessingInterval(void)
float ClassFlowControll::getProcessInterval(void)
{
return AutoInterval;
}
Expand All @@ -734,6 +748,13 @@ bool ClassFlowControll::isAutoStart(long &_interval)
}


void ClassFlowControll::setActStatus(std::string _aktstatus)
{
aktstatus = _aktstatus;
aktstatusWithTime = "[" + getCurrentTimeString("%H:%M:%S") + "] " + _aktstatus;
}


std::string ClassFlowControll::getActStatusWithTime()
{
return aktstatusWithTime;
Expand All @@ -746,22 +767,32 @@ std::string ClassFlowControll::getActStatus()
}


void ClassFlowControll::setActFlowError(bool _aktflowerror)
void ClassFlowControll::setFlowStateError()
{
aktflowerror = _aktflowerror;
flowStateErrorInRow++;
flowStateDeviationInRow = 0;
}


bool ClassFlowControll::getActFlowError()
void ClassFlowControll::clearFlowStateEventInRowCounter()
{
return aktflowerror;
flowStateErrorInRow = 0;
flowStateDeviationInRow = 0;
}


void ClassFlowControll::setActStatus(std::string _aktstatus)
int ClassFlowControll::getFlowStateErrorOrDeviation()
{
aktstatus = _aktstatus;
aktstatusWithTime = "[" + getCurrentTimeString("%H:%M:%S") + "] " + _aktstatus;
if (flowStateErrorInRow >= FLOWSTATE_ERROR_DEVIATION_IN_ROW_LIMIT)
return MULTIPLE_ERROR_IN_ROW;
else if (flowStateDeviationInRow >= FLOWSTATE_ERROR_DEVIATION_IN_ROW_LIMIT)
return MULTIPLE_DEVIATION_IN_ROW;
else if (flowStateErrorInRow > 0)
return SINGLE_ERROR;
else if (flowStateDeviationInRow > 0)
return SINGLE_DEVIATION;
else
return NONE;
}


Expand Down Expand Up @@ -823,23 +854,14 @@ void ClassFlowControll::AnalogDrawROI(CImageBasis *_zw)
#ifdef ENABLE_MQTT
bool ClassFlowControll::StartMQTTService()
{
/* Start the MQTT service */
for (int i = 0; i < FlowControlPublish.size(); ++i) {
if (FlowControlPublish[i]->name().compare("ClassFlowMQTT") == 0) {
return ((ClassFlowMQTT*) (FlowControlPublish[i]))->Start(AutoInterval);
}
}
return false;
if (flowMQTT == NULL)
return false;

return flowMQTT->Start(AutoInterval);
}
#endif //ENABLE_MQTT


std::string ClassFlowControll::getJSON()
{
return flowpostprocessing->GetJSON();
}


/* Return all available numbers names (number sequences)*/
std::string ClassFlowControll::getNumbersName()
{
Expand Down Expand Up @@ -947,8 +969,8 @@ std::string ClassFlowControll::getNumbersValue(int _position, int _type)
case READOUT_TYPE_RATE_PER_MIN:
return (*flowpostprocessing->GetNumbers())[_position]->sRatePerMin;

case READOUT_TYPE_RATE_PER_PROCESSING:
return (*flowpostprocessing->GetNumbers())[_position]->sRatePerProcessing;
case READOUT_TYPE_RATE_PER_INTERVAL:
return (*flowpostprocessing->GetNumbers())[_position]->sRatePerInterval;

default:
return "";
Expand Down Expand Up @@ -1016,8 +1038,8 @@ std::string ClassFlowControll::getReadoutAll(int _type)
case READOUT_TYPE_RATE_PER_MIN:
out = out + (*numbers)[i]->sRatePerMin;
break;
case READOUT_TYPE_RATE_PER_PROCESSING:
out = out + (*numbers)[i]->sRatePerProcessing;
case READOUT_TYPE_RATE_PER_INTERVAL:
out = out + (*numbers)[i]->sRatePerInterval;
break;
}
if (i < (*numbers).size()-1)
Expand Down
Loading

0 comments on commit 48ce862

Please sign in to comment.