"improved" topic macro, added backup status and topic

This commit is contained in:
Your Name 2021-05-27 22:53:49 +02:00
parent 057e2b37c3
commit 2c5d61a124

View File

@ -35,13 +35,15 @@
#define AMOUNT_SENOR_QUERYS 8 #define AMOUNT_SENOR_QUERYS 8
#define MAX_TANK_DEPTH 1000 #define MAX_TANK_DEPTH 1000
#define TEST_TOPIC "roundtrip\0" #define TEST_TOPIC "roundtrip\0"
#define BACKUP_TOPIC "$implementation/config/backup/set\0"
#define BACKUP_STATUS_TOPIC "$implementation/config/backup\0"
#define getTopic \ #define getTopic(test, topic) \
char *topic = new char[strlen(Homie.getConfiguration().mqtt.baseTopic) + strlen(Homie.getConfiguration().deviceId) + 1 + strlen(TEST_TOPIC) + 1]; \ char *topic = new char[strlen(Homie.getConfiguration().mqtt.baseTopic) + strlen(Homie.getConfiguration().deviceId) + 1 + strlen(test) + 1]; \
strcpy(topic, Homie.getConfiguration().mqtt.baseTopic); \ strcpy(topic, Homie.getConfiguration().mqtt.baseTopic); \
strcat(topic, Homie.getConfiguration().deviceId); \ strcat(topic, Homie.getConfiguration().deviceId); \
strcat(topic, "/"); \ strcat(topic, "/"); \
strcat(topic, TEST_TOPIC); strcat(topic, test);
/****************************************************************************** /******************************************************************************
* FUNCTION PROTOTYPES * FUNCTION PROTOTYPES
@ -214,8 +216,6 @@ void readOneWireSensors(bool withMQTT)
ds18b20Address[6], ds18b20Address[6],
ds18b20Address[7]); ds18b20Address[7]);
if (valid) if (valid)
{ {
Serial << "OneWire sensor " << String(buf) << " has value " << temp << endl; Serial << "OneWire sensor " << String(buf) << " has value " << temp << endl;
@ -241,7 +241,9 @@ void readOneWireSensors(bool withMQTT)
sensorTemp.setProperty(String(buf)).send(String(temp)); sensorTemp.setProperty(String(buf)).send(String(temp));
} }
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl; Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
} else { }
else
{
Serial << "OneWire sensor " << String(buf) << " could not be read " << temp << endl; Serial << "OneWire sensor " << String(buf) << " could not be read " << temp << endl;
} }
} }
@ -300,20 +302,30 @@ void readPowerSwitchedSensors()
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
} }
void copyFile(const char *source, const char *target) bool copyFile(const char *source, const char *target)
{ {
Serial << "copy started " << source << " -> " << target << endl;
byte buffer[512]; byte buffer[512];
if (!SPIFFS.begin())
{
return false;
}
File file = SPIFFS.open(source, FILE_READ); File file = SPIFFS.open(source, FILE_READ);
File file2 = SPIFFS.open(target, FILE_WRITE); File file2 = SPIFFS.open(target, FILE_WRITE);
Serial.flush();
if (!file) if (!file)
{ {
Serial << "There was an error opening " << source << " for reading" << endl; Serial << "There was an error opening " << source << " for reading" << endl;
return; SPIFFS.end();
return false;
} }
if (!file2) if (!file2)
{ {
Serial << "There was an error opening " << target << " for reading" << endl; Serial << "There was an error opening " << target << " for reading" << endl;
return; file.close();
SPIFFS.end();
return false;
} }
while (file.available()) while (file.available())
{ {
@ -321,6 +333,10 @@ void copyFile(const char *source, const char *target)
if (read < 0) if (read < 0)
{ {
Serial << "copy file is fucked" << endl; Serial << "copy file is fucked" << endl;
file.close();
file2.close();
SPIFFS.end();
return false;
} }
else else
{ {
@ -331,19 +347,28 @@ void copyFile(const char *source, const char *target)
Serial << "copy finished " << source << " -> " << target << endl; Serial << "copy finished " << source << " -> " << target << endl;
file.close(); file.close();
file2.close(); file2.close();
SPIFFS.end();
return true;
} }
void onMessage(char *incoming, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) void onMessage(char *incoming, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
{ {
getTopic if (strcmp(incoming, topic) == 0) getTopic(TEST_TOPIC, testTopic);
if (strcmp(incoming, testTopic) == 0)
{ {
mAliveWasRead = true; mAliveWasRead = true;
} }
if (strstr(incoming, "$implementation/config/set") > 0) delete testTopic;
getTopic(BACKUP_TOPIC, backupTopic);
if (strcmp(incoming, backupTopic) == 0)
{ {
copyFile("/homie/config.json", "/homie/config.old"); bool backupSucessful = copyFile("/homie/config.json", "/homie/config.old");
getTopic(BACKUP_STATUS_TOPIC, backupStatusTopic);
Homie.getMqttClient().publish(backupStatusTopic, 2, true, backupSucessful ? "true" : "false");
delete backupStatusTopic;
} }
}; delete backupTopic;
}
void onHomieEvent(const HomieEvent &event) void onHomieEvent(const HomieEvent &event)
{ {
@ -373,11 +398,15 @@ void onHomieEvent(const HomieEvent &event)
mPlants[i].postMQTTconnection(); mPlants[i].postMQTTconnection();
} }
{ {
getTopic getTopic(TEST_TOPIC, testopic)
Homie.getMqttClient() Homie.getMqttClient()
.subscribe(topic, 2); .subscribe(testopic, 2);
Homie.getMqttClient().publish(topic, 2, false, "ping"); Homie.getMqttClient().publish(testopic, 2, false, "ping");
Homie.getMqttClient().onMessage(onMessage); Homie.getMqttClient().onMessage(onMessage);
getTopic(BACKUP_TOPIC, backupTopic)
Homie.getMqttClient()
.subscribe(backupTopic, 2);
} }
mMQTTReady = true; mMQTTReady = true;