From efb2404917f4d83cacde90ec6289a29779886dee Mon Sep 17 00:00:00 2001 From: Carsten Schmiemann Date: Tue, 3 May 2022 00:08:32 +0200 Subject: [PATCH] Original project changes: ServerV2: fix dropped notifications from network switching --- .../ovms_server_v2/src/ovms_server_v2.cpp | 58 +++++++++++++------ .../ovms_server_v2/src/ovms_server_v2.h | 2 +- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.cpp b/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.cpp index 4199247..49b8bd7 100644 --- a/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.cpp +++ b/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.cpp @@ -721,11 +721,11 @@ void OvmsServerV2::ProcessCommand(const char* payload) delete buffer; } -void OvmsServerV2::Transmit(const std::string& message) +bool OvmsServerV2::Transmit(const std::string& message) { OvmsMutexLock mg(&m_mgconn_mutex); if (!m_mgconn) - return; + return false; int len = message.length(); char* s = new char[(len*2)+4]; @@ -775,6 +775,7 @@ void OvmsServerV2::Transmit(const std::string& message) delete [] buf; delete [] s; + return true; } void OvmsServerV2::SetStatus(const char* status, bool fault, State newstate) @@ -1610,9 +1611,15 @@ void OvmsServerV2::TransmitNotifyInfo() buffer << "MP-0 PI" << mp_encode(e->GetValue()); - Transmit(buffer.str().c_str()); - - info->MarkRead(MyOvmsServerV2Reader, e); + if (Transmit(buffer.str().c_str())) + { + info->MarkRead(MyOvmsServerV2Reader, e); + } + else + { + m_pending_notify_info = true; + return; + } } } @@ -1634,9 +1641,15 @@ void OvmsServerV2::TransmitNotifyError() buffer << "MP-0 PE" << e->GetValue(); // no mp_encode; payload structure ",," - Transmit(buffer.str().c_str()); - - alert->MarkRead(MyOvmsServerV2Reader, e); + if (Transmit(buffer.str().c_str())) + { + alert->MarkRead(MyOvmsServerV2Reader, e); + } + else + { + m_pending_notify_error = true; + return; + } } } @@ -1658,9 +1671,15 @@ void OvmsServerV2::TransmitNotifyAlert() buffer << "MP-0 PA" << mp_encode(e->GetValue()); - Transmit(buffer.str().c_str()); - - alert->MarkRead(MyOvmsServerV2Reader, e); + if (Transmit(buffer.str().c_str())) + { + alert->MarkRead(MyOvmsServerV2Reader, e); + } + else + { + m_pending_notify_alert = true; + return; + } } } @@ -1704,7 +1723,13 @@ void OvmsServerV2::TransmitNotifyData() << -((int)(now - e->m_created) / 1000) << "," << msg; - Transmit(buffer.str().c_str()); + if (!Transmit(buffer.str().c_str())) + { + m_pending_notify_data = true; + m_pending_notify_data_last = 0; + return; + } + m_pending_notify_data_last = e->m_id; // be nice to other tasks, the network & the server: @@ -1833,8 +1858,7 @@ bool OvmsServerV2::IncomingNotification(OvmsNotifyType* type, OvmsNotifyEntry* e buffer << "MP-0 PI" << mp_encode(entry->GetValue()); - Transmit(buffer.str().c_str()); - return true; // Mark it as read, as we've managed to send it + return Transmit(buffer.str().c_str()); // Mark it as read if we've managed to send it } else if (strcmp(type->m_name,"error")==0) { @@ -1848,8 +1872,7 @@ bool OvmsServerV2::IncomingNotification(OvmsNotifyType* type, OvmsNotifyEntry* e buffer << "MP-0 PE" << entry->GetValue(); // no mp_encode; payload structure ",," - Transmit(buffer.str().c_str()); - return true; // Mark it as read, as we've managed to send it + return Transmit(buffer.str().c_str()); // Mark it as read if we've managed to send it } else if (strcmp(type->m_name,"alert")==0) { @@ -1863,8 +1886,7 @@ bool OvmsServerV2::IncomingNotification(OvmsNotifyType* type, OvmsNotifyEntry* e buffer << "MP-0 PA" << mp_encode(entry->GetValue()); - Transmit(buffer.str().c_str()); - return true; // Mark it as read, as we've managed to send it + return Transmit(buffer.str().c_str()); // Mark it as read if we've managed to send it } else if (strcmp(type->m_name,"data")==0) { diff --git a/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.h b/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.h index 5e19327..9a67a1b 100644 --- a/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.h +++ b/OVMS.V3/components/ovms_server_v2/src/ovms_server_v2.h @@ -64,7 +64,7 @@ class OvmsServerV2 : public OvmsServer protected: void ProcessServerMsg(); void ProcessCommand(const char* payload); - void Transmit(const std::string& message); + bool Transmit(const std::string& message); protected: void TransmitMsgStat(bool always = false);