Fix MQTT Null publish

This commit is contained in:
Gregory Schmidt 2021-10-10 17:05:55 -08:00
parent 0327f9428e
commit 445b6ee13f

View File

@ -60,7 +60,6 @@ private:
static const char _str_subFormat[]; static const char _str_subFormat[];
static const char _str_topicFormat[]; static const char _str_topicFormat[];
unsigned long _overlaySevenSegmentProcess() unsigned long _overlaySevenSegmentProcess()
{ {
//Do time for now. //Do time for now.
@ -186,7 +185,7 @@ private:
{ {
for (int numPerSeg = 0; numPerSeg < ssLEDPerSegment; numPerSeg++) for (int numPerSeg = 0; numPerSeg < ssLEDPerSegment; numPerSeg++)
{ {
strip.setPixelColor(indexLED+numPerSeg, 0x000000); strip.setPixelColor(indexLED + numPerSeg, 0x000000);
} }
} }
indexLED += ssLEDPerSegment; indexLED += ssLEDPerSegment;
@ -268,21 +267,25 @@ private:
return result; return result;
} }
void _publishMQTTint_P(const char* subTopic, int value) void _publishMQTTint_P(const char *subTopic, int value)
{ {
if(mqtt == NULL) return;
char buffer[64]; char buffer[64];
char valBuffer[12]; char valBuffer[12];
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic); sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
Serial.println(buffer);
sprintf_P(valBuffer, PSTR("%d"), value); sprintf_P(valBuffer, PSTR("%d"), value);
mqtt->publish(buffer, 2, true, valBuffer); mqtt->publish(buffer, 2, true, valBuffer);
} }
void _publishMQTTstr_P(const char* subTopic, String Value)
void _publishMQTTstr_P(const char *subTopic, String Value)
{ {
if(mqtt == NULL) return;
char buffer[64]; char buffer[64];
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic); sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
mqtt->publish(buffer, 2, true, Value.c_str(), Value.length()); mqtt->publish(buffer, 2, true, Value.c_str(), Value.length());
} }
void _updateMQTT() void _updateMQTT()
{ {
_publishMQTTint_P(_str_perSegment, ssLEDPerSegment); _publishMQTTint_P(_str_perSegment, ssLEDPerSegment);
@ -295,15 +298,18 @@ private:
_publishMQTTstr_P(_str_displayMask, ssDisplayMask); _publishMQTTstr_P(_str_displayMask, ssDisplayMask);
_publishMQTTstr_P(_str_displayMsg, ssDisplayMessage); _publishMQTTstr_P(_str_displayMsg, ssDisplayMessage);
} }
bool _cmpIntSetting_P(char* topic, char* payload, const char* setting, void* value){
if(strcmp_P(topic, setting) == 0) bool _cmpIntSetting_P(char *topic, char *payload, const char *setting, void *value)
{ {
*((int*)value) = strtol(payload, NULL, 10); if (strcmp_P(topic, setting) == 0)
_publishMQTTint_P(setting, *((int*)value)); {
*((int *)value) = strtol(payload, NULL, 10);
_publishMQTTint_P(setting, *((int *)value));
return true; return true;
} }
return false; return false;
} }
bool _handleSetting(char *topic, char *payload) bool _handleSetting(char *topic, char *payload)
{ {
if (_cmpIntSetting_P(topic, payload, _str_perSegment, &ssLEDPerSegment)) if (_cmpIntSetting_P(topic, payload, _str_perSegment, &ssLEDPerSegment))
@ -318,14 +324,14 @@ private:
return true; return true;
if (_cmpIntSetting_P(topic, payload, _str_scrollSpd, &ssScrollSpeed)) if (_cmpIntSetting_P(topic, payload, _str_scrollSpd, &ssScrollSpeed))
return true; return true;
if(strcmp_P(topic, _str_displayMask)==0) if (strcmp_P(topic, _str_displayMask) == 0)
{ {
ssDisplayMask = String(payload); ssDisplayMask = String(payload);
ssDisplayBuffer = ssDisplayMask; ssDisplayBuffer = ssDisplayMask;
_publishMQTTstr_P(_str_displayMask, ssDisplayMask); _publishMQTTstr_P(_str_displayMask, ssDisplayMask);
return true; return true;
} }
if(strcmp_P(topic, _str_displayMsg)==0) if (strcmp_P(topic, _str_displayMsg) == 0)
{ {
setSevenSegmentMessage(String(payload)); setSevenSegmentMessage(String(payload));
return true; return true;
@ -349,7 +355,7 @@ public:
ssDisplayMessageIdx = 0; ssDisplayMessageIdx = 0;
//If the message isn't the same, update runtime/mqtt (most calls will be resetting message scroll) //If the message isn't the same, update runtime/mqtt (most calls will be resetting message scroll)
if(!ssDisplayMessage.equals(message)) if (!ssDisplayMessage.equals(message))
{ {
_publishMQTTstr_P(_str_displayMsg, message); _publishMQTTstr_P(_str_displayMsg, message);
ssDisplayMessage = message; ssDisplayMessage = message;
@ -424,15 +430,15 @@ public:
//Trim /set and handle it //Trim /set and handle it
topic[topicLen - 4] = '\0'; topic[topicLen - 4] = '\0';
_handleSetting(topic, payload); _handleSetting(topic, payload);
} }
return true; return true;
} }
void addToConfig(JsonObject& root) void addToConfig(JsonObject &root)
{ {
JsonObject top = root[FPSTR(_str_sevenSeg)]; JsonObject top = root[FPSTR(_str_sevenSeg)];
if (top.isNull()) { if (top.isNull())
{
top = root.createNestedObject(FPSTR(_str_sevenSeg)); top = root.createNestedObject(FPSTR(_str_sevenSeg));
} }
top[FPSTR(_str_perSegment)] = ssLEDPerSegment; top[FPSTR(_str_perSegment)] = ssLEDPerSegment;
@ -445,14 +451,15 @@ public:
top[FPSTR(_str_scrollSpd)] = ssScrollSpeed; top[FPSTR(_str_scrollSpd)] = ssScrollSpeed;
} }
bool readFromConfig(JsonObject& root) bool readFromConfig(JsonObject &root)
{ {
JsonObject top = root[FPSTR(_str_sevenSeg)]; JsonObject top = root[FPSTR(_str_sevenSeg)];
bool configComplete = !top.isNull(); bool configComplete = !top.isNull();
//if sevenseg section doesn't exist return //if sevenseg section doesn't exist return
if(!configComplete) return configComplete; if (!configComplete)
return configComplete;
configComplete &= getJsonValue(top[FPSTR(_str_perSegment)], ssLEDPerSegment); configComplete &= getJsonValue(top[FPSTR(_str_perSegment)], ssLEDPerSegment);
configComplete &= getJsonValue(top[FPSTR(_str_perPeriod)], ssLEDPerPeriod); configComplete &= getJsonValue(top[FPSTR(_str_perPeriod)], ssLEDPerPeriod);
@ -466,7 +473,6 @@ public:
configComplete &= getJsonValue(top[FPSTR(_str_timeEnabled)], ssTimeEnabled); configComplete &= getJsonValue(top[FPSTR(_str_timeEnabled)], ssTimeEnabled);
configComplete &= getJsonValue(top[FPSTR(_str_scrollSpd)], ssScrollSpeed); configComplete &= getJsonValue(top[FPSTR(_str_scrollSpd)], ssScrollSpeed);
return configComplete; return configComplete;
} }
@ -480,12 +486,12 @@ public:
} }
}; };
const char SevenSegmentDisplay::_str_perSegment[] PROGMEM = "perSegment"; const char SevenSegmentDisplay::_str_perSegment[] PROGMEM = "perSegment";
const char SevenSegmentDisplay::_str_perPeriod[] PROGMEM = "perPeriod"; const char SevenSegmentDisplay::_str_perPeriod[] PROGMEM = "perPeriod";
const char SevenSegmentDisplay::_str_startIdx[] PROGMEM = "startIdx"; const char SevenSegmentDisplay::_str_startIdx[] PROGMEM = "startIdx";
const char SevenSegmentDisplay::_str_displayCfg[] PROGMEM = "displayCfg"; const char SevenSegmentDisplay::_str_displayCfg[] PROGMEM = "displayCfg";
const char SevenSegmentDisplay::_str_timeEnabled[] PROGMEM = "timeEnabled"; const char SevenSegmentDisplay::_str_timeEnabled[] PROGMEM = "timeEnabled";
const char SevenSegmentDisplay::_str_scrollSpd[] PROGMEM = "scrollSpd"; const char SevenSegmentDisplay::_str_scrollSpd[] PROGMEM = "scrollSpd";
const char SevenSegmentDisplay::_str_displayMask[] PROGMEM = "displayMask"; const char SevenSegmentDisplay::_str_displayMask[] PROGMEM = "displayMask";
const char SevenSegmentDisplay::_str_displayMsg[] PROGMEM = "displayMsg"; const char SevenSegmentDisplay::_str_displayMsg[] PROGMEM = "displayMsg";
const char SevenSegmentDisplay::_str_sevenSeg[] PROGMEM = "sevenSeg"; const char SevenSegmentDisplay::_str_sevenSeg[] PROGMEM = "sevenSeg";