Cleanup of some code

This commit is contained in:
Robert van den Breemen 2022-01-14 00:36:05 +01:00
parent 1022326128
commit 0529d7fda4
7 changed files with 77 additions and 28 deletions

View File

@ -117,6 +117,7 @@ void handleMQTTcallback(char* topic, byte* payload, unsigned int length) {
strlcpy(msgPayload, (char *)payload, msglen);
if (strcasecmp(topic, "homeassistant/status") == 0) {
//incoming message on status, detect going down
bHAcycle = true; //exprimental buid 20220109 - turning off detection of HA going down
if (strcasecmp(msgPayload, "offline") == 0){
//home assistant went down
DebugTln(F("Home Assistant went offline!"));
@ -252,7 +253,8 @@ void handleMQTT()
if (MQTTclient.connected())
{
reconnectAttempts = 0;
Debugln(F(" .. connected\r"));
MQTTDebugln(F(" .. connected\r"));
Debugln(F("MQTT connected"));
stateMQTT = MQTT_STATE_IS_CONNECTED;
MQTTDebugTln(F("Next State: MQTT_STATE_IS_CONNECTED"));
// birth message, sendMQTT retains by default
@ -515,6 +517,7 @@ void doAutoConfigure(bool bForcaAll = false){
if ((getMQTTConfigDone((byte)i)==true) || bForcaAll) {
MQTTDebugTf("Sending auto configuration for sensor %d\r\n", i);
doAutoConfigureMsgid((byte)i);
doBackgroundTasks();
}
}
// bool success = doAutoConfigure("config"); // the string "config" should match every line non-comment in mqttha.cfg
@ -524,9 +527,17 @@ bool doAutoConfigureMsgid(byte OTid)
{
bool _result = false;
if (!settingMQTTenable) return _result;
if (!MQTTclient.connected()) {DebugTln(F("Error: MQTT broker not connected.")); return _result;}
if (!isValidIP(MQTTbrokerIP)) {DebugTln(F("Error: MQTT broker IP not valid.")); return _result;}
if (!settingMQTTenable) {
return _result;
}
if (!MQTTclient.connected()) {
DebugTln(F("Error: MQTT broker not connected."));
return _result;
}
if (!isValidIP(MQTTbrokerIP)) {
DebugTln(F("Error: MQTT broker IP not valid."));
return _result;
}
byte lineID = 39; // 39 is unused in OT protocol so is a safe value
String sMsg = "";
@ -537,15 +548,22 @@ bool doAutoConfigureMsgid(byte OTid)
const char *cfgFilename = "/mqttha.cfg";
LittleFS.begin();
if (!LittleFS.exists(cfgFilename)) {DebugTln(F("Error: confuration file not found.")); return _result;}
if (!LittleFS.exists(cfgFilename)) {
DebugTln(F("Error: confuration file not found."));
return _result;
}
fh = LittleFS.open(cfgFilename, "r");
if (!fh) {DebugTln(F("Error: could not open confuration file.")); return _result;}
if (!fh) {
DebugTln(F("Error: could not open confuration file."));
return _result;
}
//Lets go read the config and send it out to MQTT line by line
while (fh.available())
{ //read file line by line, split and send to MQTT (topic, msg)
{
//read file line by line, split and send to MQTT (topic, msg)
feedWatchDog(); //start with feeding the dog
String sLine = fh.readStringUntil('\n');
@ -594,7 +612,7 @@ bool doAutoConfigureMsgid(byte OTid)
//sendMQTT(CSTR(sTopic), CSTR(sMsg), (sTopic.length() + sMsg.length()+2));
sendMQTT(sTopic, sMsg);
resetMQTTBufferSize();
delay(10);
// delay(10);
_result = true;
// TODO: enable this break if we are sure the old config dump method is no longer needed

View File

@ -104,14 +104,20 @@ void sendMQTTstateinformation(){
//===================[ Reset OTGW ]===============================
void resetOTGW() {
sPICfwversion ="No version found"; //reset versionstring
//sPICfwversion ="No version found"; //reset versionstring
OTGWSerial.resetPic();
//then read the first response of the firmware to make sure it reads it
String resp = OTGWSerial.readStringUntil('\n');
resp.trim();
OTGWDebugTf("Received firmware version: [%s] [%s] (%d)\r\n", CSTR(resp), OTGWSerial.firmwareVersion(), strlen(OTGWSerial.firmwareVersion()));
bOTGWonline = (resp.length()>0);
if (bOTGWonline) sPICfwversion = String(OTGWSerial.firmwareVersion());
if (bOTGWonline)
{
sPICfwversion = String(OTGWSerial.firmwareVersion());
} else {
//try it one more time
sPICfwversion = getpicfwversion();
}
OTGWDebugTf("Current firmware version: %s\r\n", CSTR(sPICfwversion));
}
//===================[ getpicfwversion ]===========================
@ -123,7 +129,9 @@ String getpicfwversion(){
if (p >= 0) {
p += sizeof(OTGW_BANNER);
_ret = line.substring(p);
} else _ret ="No version found";
} else {
_ret ="No version found";
}
OTGWDebugTf("Current firmware version: %s\r\n", CSTR(_ret));
_ret.trim();
return _ret;
@ -1679,6 +1687,11 @@ void processOT(const char *buf, int len){
OTcurrentSystemState.error04++;
OTGWDebugTf("\r\nError 04 = %d\r\n",OTcurrentSystemState.error04);
sendMQTTData(F("Error 04"), String(OTcurrentSystemState.error04));
} else if (strstr(buf, OTGW_BANNER)!=NULL){
//found a banner, so get the version of PIC
char *p = strstr(buf, OTGW_BANNER);
p += sizeof(OTGW_BANNER);
sPICfwversion = String(p);
} else {
OTGWDebugTf("Not processed, received from OTGW => (%s) [%d]\r\n", buf, len);
}

View File

@ -424,6 +424,29 @@ void str_cstrlit(const char *str, char *buffer, size_t buflen)
}
*buffer = '\0';
}
String strHTTPmethod(HTTPMethod method)
{
switch (method)
{
case HTTPMethod::HTTP_GET:
return "GET";
case HTTPMethod::HTTP_POST:
return "POST";
case HTTPMethod::HTTP_PUT:
return "PUT";
case HTTPMethod::HTTP_PATCH:
return "PATCH";
case HTTPMethod::HTTP_DELETE:
return "DELETE";
case HTTPMethod::HTTP_OPTIONS:
return "OPTIONS";
case HTTPMethod::HTTP_HEAD:
return "HEAD";
default:
return "";
}
}
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a

View File

@ -20,6 +20,7 @@
//=======================================================================
void processAPI()
{
char URI[50] = "";
@ -27,13 +28,7 @@ void processAPI()
strlcpy( URI, httpServer.uri().c_str(), sizeof(URI) );
if (httpServer.method() == HTTP_GET)
RESTDebugTf("from[%s] URI[%s] method[GET] \r\n"
, httpServer.client().remoteIP().toString().c_str()
, URI);
else RESTDebugTf("from[%s] URI[%s] method[PUT] \r\n"
, httpServer.client().remoteIP().toString().c_str()
, URI);
RESTDebugTf("from[%s] URI[%s] method[%s] \r\n", httpServer.client().remoteIP().toString().c_str(), URI, strHTTPmethod(httpServer.method()).c_str());
if (ESP.getFreeHeap() < 8500) // to prevent firmware from crashing!
{

Binary file not shown.

View File

@ -2,15 +2,15 @@
#define _VERSION_MAJOR 0
#define _VERSION_MINOR 9
#define _VERSION_PATCH 3
#define _VERSION_BUILD 1610
#define _VERSION_GITHASH "7eeae02"
#define _VERSION_PRERELEASE beta
#define _VERSION_DATE "09-01-2022"
#define _VERSION_TIME "01:37:39"
#define _VERSION_BUILD 1624
#define _VERSION_GITHASH "1022326"
#define _VERSION_PRERELEASE experimental
#define _VERSION_DATE "14-01-2022"
#define _VERSION_TIME "00:26:12"
#define _SEMVER_CORE "0.9.3"
#define _SEMVER_BUILD "0.9.3+1610"
#define _SEMVER_GITHASH "0.9.3+7eeae02"
#define _SEMVER_FULL "0.9.3-beta+7eeae02"
#define _SEMVER_NOBUILD "0.9.3-beta (09-01-2022)"
#define _VERSION "0.9.3-beta+7eeae02 (09-01-2022)"
#define _SEMVER_BUILD "0.9.3+1624"
#define _SEMVER_GITHASH "0.9.3+1022326"
#define _SEMVER_FULL "0.9.3-experimental+1022326"
#define _SEMVER_NOBUILD "0.9.3-experimental (14-01-2022)"
#define _VERSION "0.9.3-experimental+1022326 (14-01-2022)"
//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver