Skip to content

Commit

Permalink
Auto finalize modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-eoh committed Dec 27, 2024
1 parent 1784ef6 commit e72c6bb
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
6 changes: 3 additions & 3 deletions linux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

ERA_MAJOR = 1
ERA_MINOR = 5
ERA_PATCH = 1
ERA_VERSION = "1.5.1"
ERA_FIRMWARE_VERSION = "1.5.1"
ERA_PATCH = 2
ERA_VERSION = "1.5.2"
ERA_FIRMWARE_VERSION = "1.5.2"

BUTTON_GPIO = 16

Expand Down
5 changes: 3 additions & 2 deletions src/Automation/ERaHelpers.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ namespace eras {
return true;
}

Mutex::Mutex() {
}
Mutex::Mutex()
: mMutex(nullptr)
{}

void Mutex::lock() {
ERaGuardLock(this->mMutex);
Expand Down
3 changes: 3 additions & 0 deletions src/Automation/ERaSmart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace eras {
void run() override;
void deleteAll() override;
void updateValue(ERaUInt_t configID, double value, bool trigger = false) override;
bool isRunning() const override;

static ERaSmart* getInstance() {
return ERaSmart::instance();
Expand Down Expand Up @@ -157,6 +158,8 @@ namespace eras {
std::vector<Smart*> mSmarts {};

Mutex mLock;

bool mInitialized {false};
};

} /* namespace eras */
Expand Down
6 changes: 6 additions & 0 deletions src/Automation/ERaSmart.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace eras {

this->calculateRunningComponents();
this->calculateValueComponents();

this->mInitialized = true;
}

void ERaSmart::run() {
Expand Down Expand Up @@ -100,6 +102,10 @@ namespace eras {
}
}

bool ERaSmart::isRunning() const {
return (this->mInitialized && !this->mSmarts.empty());
}

void ERaSmart::parseAutomations(const cJSON* const root) {
cJSON* item = root->child;
while (item != nullptr) {
Expand Down
7 changes: 7 additions & 0 deletions src/ERa/ERaApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,13 @@ class ERaApi

bool isNeedSyncConfig() const {
bool ret {false};
#if defined(ERA_MODBUS) && defined(ERA_AUTOMATION)
bool need {false};
if (this->automation != nullptr) {
need = this->automation->isConfigUpdated(true);
}
ret |= Modbus::isNeedFinalize(need);
#endif
#if defined(ERA_ZIGBEE)
ret |= Zigbee::isNeedFinalize();
#endif
Expand Down
15 changes: 15 additions & 0 deletions src/ERa/ERaAutomation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ERaAutomation
: mAuthToken(NULL)
, mBaseTopic(NULL)
, mCallback(NULL)
, mConfigUpdated(false)
{
memset(this->mHashID, 0, sizeof(this->mHashID));
}
Expand All @@ -32,6 +33,18 @@ class ERaAutomation
virtual void deleteAll() = 0;
virtual void updateValue(ERaUInt_t configID, double value, bool trigger = false) = 0;

virtual bool isRunning() const {
return false;
}

bool isConfigUpdated(bool reset = false) const {
bool ret = this->mConfigUpdated;
if (reset) {
this->mConfigUpdated = false;
}
return ret;
}

void setAuth(const char* auth) {
this->mAuthToken = auth;
}
Expand All @@ -49,6 +62,7 @@ class ERaAutomation
return false;
}
if (strcmp(this->mHashID, hash)) {
this->mConfigUpdated = true;
snprintf(this->mHashID, sizeof(this->mHashID), hash);
return true;
}
Expand All @@ -60,6 +74,7 @@ class ERaAutomation
const char* mBaseTopic;
MessageCallback_t mCallback;

mutable bool mConfigUpdated;
char mHashID[37];
};

Expand Down
4 changes: 2 additions & 2 deletions src/ERa/ERaVersion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define ERA_MAJOR 1
#define ERA_MINOR 5
#define ERA_PATCH 1
#define ERA_PATCH 2

#define ERA_VERSION_TO_STR_2(val) # val
#define ERA_VERSION_TO_STR(val) ERA_VERSION_TO_STR_2(val)
Expand All @@ -17,7 +17,7 @@
#define ERA_VERSION ERA_VERSION_TO_STR(ERA_MAJOR) "." \
ERA_VERSION_TO_STR(ERA_MINOR) "." \
ERA_VERSION_TO_STR(ERA_PATCH)
#define ERA_VERSION_1_5_1
#define ERA_VERSION_1_5_2

#if !defined(ERA_FIRMWARE_VERSION)
#define ERA_FIRMWARE_VERSION ERA_VERSION
Expand Down
10 changes: 10 additions & 0 deletions src/Modbus/ERaModbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ class ERaModbus
return (this->totalRead + this->totalWrite);
}

bool isNeedFinalize(bool need = false) const {
if (!this->initialized) {
return false;
}
if (!need) {
return false;
}
return !ModbusTransp::isNewReport();
}

void initModbusTask();

#if defined(LINUX)
Expand Down

0 comments on commit e72c6bb

Please sign in to comment.