migrate mqtt config to struct

This commit is contained in:
Siwat Sirichai 2023-12-29 23:43:12 +07:00
parent 2dee25276c
commit 71475ef2f1
7 changed files with 125 additions and 82 deletions

View File

@ -13,8 +13,48 @@
"*.tcc": "cpp",
"memory": "cpp",
"random": "cpp",
"functional": "cpp"
"functional": "cpp",
"atomic": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
},
"cmake.configureOnOpen": true,
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO"
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO",
"editor.tokenColorCustomizations": {
"comments": "",
"textMateRules": []
}
}

View File

@ -17,10 +17,6 @@ ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
ESPMegaIoT::~ESPMegaIoT()
{
// Delete the mqtt server
delete[] mqtt_server;
delete[] mqtt_user;
delete[] mqtt_password;
}
void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length)
@ -248,13 +244,13 @@ void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *payload)
bool ESPMegaIoT::mqttReconnect()
{
if (mqtt_useauth)
if (this->mqtt_config.mqtt_useauth)
{
return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password);
return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port, this->mqtt_config.mqtt_user, this->mqtt_config.mqtt_password);
}
else
{
return this->connectToMqtt(client_id, mqtt_server, mqtt_port);
return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port);
}
}
@ -300,7 +296,6 @@ void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void))
void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config)
{
this->network_config = network_config;
client_id = network_config.hostname;
}
void ESPMegaIoT::loadNetworkConfig()
@ -323,38 +318,21 @@ void ESPMegaIoT::ethernetBegin()
void ESPMegaIoT::loadMqttConfig()
{
// Load the mqtt config from FRAM
MqttConfig mqtt_config;
fram->read(sizeof(NetworkConfig), (uint8_t *)&mqtt_config, sizeof(MqttConfig));
fram->read(sizeof(NetworkConfig), (uint8_t *)&this->mqtt_config, sizeof(MqttConfig));
// Populate the mqtt connection parameters
strcpy(mqtt_server, mqtt_config.mqtt_server);
mqtt_port = mqtt_config.mqtt_port;
strcpy(mqtt_user, mqtt_config.mqtt_user);
strcpy(mqtt_password, mqtt_config.mqtt_password);
mqtt_useauth = mqtt_config.mqtt_useauth;
}
void ESPMegaIoT::saveMqttConfig()
{
// Save the mqtt config to FRAM
MqttConfig mqtt_config;
strcpy(mqtt_config.mqtt_server, mqtt_server);
mqtt_config.mqtt_port = mqtt_port;
strcpy(mqtt_config.mqtt_user, mqtt_user);
strcpy(mqtt_config.mqtt_password, mqtt_password);
mqtt_config.mqtt_useauth = mqtt_useauth;
fram->write(MQTT_CONFIG_ADDRESS, (uint8_t *)&mqtt_config, sizeof(MqttConfig));
}
void ESPMegaIoT::connectToMqtt()
{
if (mqtt_useauth)
{
this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password);
}
if (mqtt_config.mqtt_useauth)
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port, mqtt_config.mqtt_user, mqtt_config.mqtt_password);
else
{
this->connectToMqtt(client_id, mqtt_server, mqtt_port);
}
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port);
}
void ESPMegaIoT::connectNetwork()
@ -380,24 +358,7 @@ void ESPMegaIoT::connectNetwork()
void ESPMegaIoT::setMqttConfig(MqttConfig mqtt_config)
{
if (mqtt_server != nullptr) {
delete[] mqtt_server;
}
this->mqtt_server = new char[32];
strcpy(mqtt_server, mqtt_config.mqtt_server);
mqtt_port = mqtt_config.mqtt_port;
if (mqtt_user != nullptr) {
delete[] mqtt_user;
}
this->mqtt_user = new char[32];
strcpy(mqtt_user, mqtt_config.mqtt_user);
if (mqtt_password != nullptr) {
delete[] mqtt_password;
}
this->mqtt_password = new char[32];
strcpy(mqtt_password, mqtt_config.mqtt_password);
mqtt_useauth = mqtt_config.mqtt_useauth;
strcpy(base_topic, mqtt_config.base_topic);
this->mqtt_config = mqtt_config;
}
void ESPMegaIoT::bindEthernetInterface(ETHClass *ethernetIface)

View File

@ -74,6 +74,7 @@ public:
void registerSubscribeCallback(void (*callback)(void));
void setBaseTopic(char *base_topic);
void bindEthernetInterface(ETHClass *ethernetIface);
IoTComponent* getComponent(uint8_t card_id);
IPAddress getETHIp();
@ -101,13 +102,8 @@ private:
uint8_t base_topic_length;
ExpansionCard **cards; // Points to card array in ESPMegaPRO Core
// MQTT Connection Parameters
char *mqtt_server;
uint16_t mqtt_port;
char *mqtt_user;
char *mqtt_password;
char *client_id;
bool mqtt_useauth;
bool mqtt_connected;
NetworkConfig network_config;
MqttConfig mqtt_config;
ETHClass *ethernetIface;
};

View File

@ -9,15 +9,7 @@
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <time.h>
struct rtctime_t {
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t day;
uint8_t month;
uint16_t year;
};
#include <TimeStructure.hpp>
#define FRAM_ADDRESS 0x56
#define INPUT_BANK_A_ADDRESS 0x21

View File

@ -1,8 +1,7 @@
#include <InternalDisplay.hpp>
void InternalDisplay::begin(ESPMegaPRO *espmega) {
this->espmega = espmega;
void InternalDisplay::begin() {
}
void InternalDisplay::loop() {
@ -26,17 +25,23 @@ void InternalDisplay::bindOutputCard(uint8_t card_id) {
void InternalDisplay::handleInputStateChange(uint8_t pin, bool state) {
// If the input card is binded to the display and the current page is the input page
// then update the respective input component
// TODO: implementation
if (!this->bindedInputCard || this->currentPage != INTERNAL_DISPLAY_INPUT_PAGE) return;
// Update the input state
this->setInputMarker(pin, state);
}
void InternalDisplay::handlePwmStateChange(uint8_t pin, uint16_t value) {
void InternalDisplay::handlePwmStateChange(uint8_t pin, bool state, uint16_t value) {
// If the output card is binded to the display and the current page is the output page
// then update the respective output component
// TODO: implementation
if (!this->bindedOutputCard || this->currentPage != INTERNAL_DISPLAY_OUTPUT_PAGE) return;
// Update the output state
this->setOutputBar(pin, value);
this->setOutputStateColor(pin, state);
}
void InternalDisplay::handlePageChange(uint8_t page) {
// TODO: implementation
// Refresh the page
this->refreshPage(page);
}
void InternalDisplay::saveNetworkConfig() {
@ -47,24 +52,51 @@ void InternalDisplay::saveMQTTConfig() {
// TODO: implementation
}
void InternalDisplay::updateStatusIcons() {
// TODO: implementation
void InternalDisplay::updateStatusIcons(bool networkStatus, bool mqttStatus) {
this->setNumber("server.pic", mqttStatus ? PIC_MQTT_CONNECTED : PIC_MQTT_DISCONNECTED);
this->setNumber("lan.pic", networkStatus ? PIC_LAN_CONNECTED : PIC_LAN_DISCONNECTED);
}
void InternalDisplay::updateClock() {
// TODO: implementation
rtctime_t time = this->getRtcTime();
this->displayAdapter->print("time.txt=");
this->displayAdapter->print(time.hours%12);
this->displayAdapter->print(":");
this->displayAdapter->print(time.minutes);
this->displayAdapter->print(" ");
this->displayAdapter->print(time.hours/12 ? "PM" : "AM");
this->sendStopBytes();
}
void InternalDisplay::refreshPage() {
// TODO: implementation
this->refreshPage(this->currentPage);
}
void InternalDisplay::refreshPage(uint8_t page) {
// TODO: implementation
switch (page) {
case INTERNAL_DISPLAY_DASHBOARD_PAGE:
this->refreshDashboard();
break;
case INTERNAL_DISPLAY_INPUT_PAGE:
this->refreshInput();
break;
case INTERNAL_DISPLAY_OUTPUT_PAGE:
this->refreshOutput();
break;
case INTERNAL_DISPLAY_AC_PAGE:
this->refreshAC();
break;
}
}
void InternalDisplay::refreshDashboard() {
// TODO: implementation
// The dashboard have the following components:
// 1. Hostname
// 2. IP Address
// 3. MQTT Server with port
// 4. MQTT Connection status
this->iot->
}
void InternalDisplay::refreshInput() {

View File

@ -1,24 +1,32 @@
#include <ESPMegaPRO_OOP.hpp>
#include <ESPMegaDisplay.hpp>
#include <TimeStructure.hpp>
#include <ESPMegaIoT.hpp>
#define INTERNAL_DISPLAY_DASHBOARD_PAGE 0
#define INTERNAL_DISPLAY_INPUT_PAGE 1
#define INTERNAL_DISPLAY_OUTPUT_PAGE 2
#define INTERNAL_DISPLAY_AC_PAGE 3
// Picture IDs
#define PIC_LAN_DISCONNECTED 2
#define PIC_LAN_CONNECTED 3
#define PIC_MQTT_DISCONNECTED 4
#define PIC_MQTT_CONNECTED 5
class InternalDisplay : public ESPMegaDisplay {
public:
InternalDisplay(HardwareSerial *displayAdapter);
void begin(ESPMegaPRO *espmega);
void begin(ESPMegaIoT *iot, std::function<rtctime_t()> getRtcTime);
void loop();
void bindInputCard(uint8_t card_id);
void bindOutputCard(uint8_t card_id);
private:
uint8_t bindedInputCard;
uint8_t bindedOutputCard;
ESPMegaPRO *espmega;
void handleInputStateChange(uint8_t pin, bool state);
void handlePwmStateChange(uint8_t pin, uint16_t value);
void handlePwmStateChange(uint8_t pin, bool state, uint16_t value);
void handlePageChange(uint8_t page);
void setOutputBar(uint8_t pin, uint16_t value);
void setOutputStateColor(uint8_t pin, bool state);
@ -28,7 +36,7 @@ class InternalDisplay : public ESPMegaDisplay {
void setPWMAdjustmentButton(bool state);
void saveNetworkConfig();
void saveMQTTConfig();
void updateStatusIcons();
void updateStatusIcons(bool networkStatus, bool mqttStatus);
void updateClock();
void refreshPage();
void refreshPage(uint8_t page);
@ -36,4 +44,7 @@ class InternalDisplay : public ESPMegaDisplay {
void refreshInput();
void refreshOutput();
void refreshAC();
// Pointers to various data
ESPMegaIoT *iot;
std::function<rtctime_t()> getRtcTime;
};

View File

@ -0,0 +1,11 @@
#pragma once
#include <stdint.h>
struct rtctime_t {
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t day;
uint8_t month;
uint16_t year;
};