Skip to content

Commit

Permalink
Added bUseTopicForEventDefaults to be able to use topic for filling i…
Browse files Browse the repository at this point in the history
…n absent event data
  • Loading branch information
grodansparadis committed Feb 10, 2025
1 parent 2075b05 commit 74963f1
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/vscp/common/vscp-client-mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ vscpClientMqtt::vscpClientMqtt(void)
m_keepAlive = 30; // 30 seconds for keepalive
// m_bCleanSession = false; // Do not start with a clean session

m_bUseTopicForEventDefaults = false; // Do not use topic for event defaults

// Defaults for timing
m_timeoutConnection = 5000; // 5 seconds
m_timeoutResponse = 200;
Expand Down Expand Up @@ -1531,6 +1533,32 @@ vscpClientMqtt::handleMessage(const struct mosquitto_message *pmsg)
return false;
}

// If standard topic format is used, that is
// vscp/<vscp-guid>/<vscp-class>/<vscp-type>/index/zone/subzone
// and instructed to do so we can find the GUID, class and type from the topic
if (m_bUseTopicForEventDefaults) {
std::deque<std::string> vec;
vscp_split(vec, pmsg->topic, "/");
if (vec.size() >= 4) { // at least "vscp/<vscp-guid>/<vscp-class>/<vscp-type>"

// Assigne GUID from topic if all nills from event.
cguid guid(ex.GUID);
if (guid.isNULL()) {
vscp_getGuidFromStringToArray(ex.GUID, vec[1]);
}

// Assign class from topic if set to zeror in event.
if (!ex.vscp_class) {
ex.vscp_class = vscp_readStringValue(vec[2]);
}

// Assign type from topic if set to zero in event.
if (!ex.vscp_type) {
ex.vscp_type = vscp_readStringValue(vec[3]);
}
}
}

// If callback is defined send event
if (isCallbackEvActive()) {
m_callbackev(ev, getCallbackObj());
Expand Down
22 changes: 21 additions & 1 deletion src/vscp/common/vscp-client-mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ class vscpClientMqtt : public CVscpClient {
void setKeepAlive(uint16_t keepAlive) { m_keepAlive = keepAlive; };
uint32_t getKeepAlive(void) { return m_keepAlive; };

/*!
Getter/setter for bUseTopicForEventDefaults
*/
void setUseTopicForEventDefaults(bool b) { m_bUseTopicForEventDefaults = b; };
bool getUseTopicForEventDefaults(void) { return m_bUseTopicForEventDefaults; };
bool isUseTopicForEventDefaults(void) { return m_bUseTopicForEventDefaults; };

/*!
Getter for remote port
@return remote host port.
Expand Down Expand Up @@ -658,7 +665,7 @@ class vscpClientMqtt : public CVscpClient {
/*!
Set parent disconnect callback
*/
void setFuncParentCallbackDisconnet(LPFN_PARENT_CALLBACK_DISCONNECT func) { m_parentCallbackDisconnect = func; };
void setFuncParentCallbackDisconnect(LPFN_PARENT_CALLBACK_DISCONNECT func) { m_parentCallbackDisconnect = func; };

/*!
Set parent publish callback
Expand Down Expand Up @@ -736,6 +743,19 @@ class vscpClientMqtt : public CVscpClient {
*/
bool m_bJsonMeasurementAdd;

/*!
Use topic for defaults.
If the standard format for topics is used, that is
vscp/guid/class/type/index/zone/subzone
the by enabling this guid, class and type information that
is absent from the received event can be filled in from the
MQTT topic of the message. This allows for very compact
MQTT messages while still preserving the full VSCP event
information.
*/
bool m_bUseTopicForEventDefaults;

/*!
Mutex that protect CANAL interface when callbacks are defined
*/
Expand Down
6 changes: 6 additions & 0 deletions src/vscp/common/vscphelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4479,6 +4479,9 @@ vscp_convertJSONToEvent(vscpEvent *pEvent, std::string &strJSON)
return false;
}

// Initialize event
memset(pEvent, 0, sizeof(vscpEvent));

try {
auto j = json::parse(strJSON);

Expand Down Expand Up @@ -4632,6 +4635,9 @@ vscp_convertJSONToEventEx(vscpEventEx *pEventEx, std::string &strJSON)
return false;
}

// Initialize event
memset(pEventEx, 0, sizeof(vscpEventEx));

try {

auto j = json::parse(strJSON);
Expand Down
14 changes: 14 additions & 0 deletions src/vscp/common/vscphelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -1621,12 +1621,26 @@ vscp_convertEventExToJSON(std::string &strJSON, vscpEventEx *pEventEx);

/*!
* Convert JSON string to event
*
* If a value is not found in the JSON string
* it will be set to zero. This means a value
* can be omitted in the JSON string. The only
* values that must be in the JSON string is
* class and type as long as the GUID is known
* beforehand
*/
bool
vscp_convertJSONToEvent(vscpEvent *pEvent, std::string &strJSON);

/*!
* Convert JSON string to eventex
*
* If a value is not found in the JSON string
* it will be set to zero. This means a value
* can be omitted in the JSON string. The only
* values that must be in the JSON string is
* class and type as long as the GUID is known
* beforehand
*/
bool
vscp_convertJSONToEventEx(vscpEventEx *pEventEx, std::string &strJSONx);
Expand Down
2 changes: 1 addition & 1 deletion third_party/mongoose
Submodule mongoose updated 380 files
2 changes: 1 addition & 1 deletion third_party/nlohmann
Submodule nlohmann updated 757 files
2 changes: 1 addition & 1 deletion third_party/spdlog
Submodule spdlog updated 42 files
+2 −12 .github/workflows/macos.yml
+122 −139 CMakeLists.txt
+0 −3 README.md
+0 −3 example/example.cpp
+2 −2 include/spdlog/cfg/env.h
+6 −1 include/spdlog/common.h
+1 −2 include/spdlog/details/file_helper-inl.h
+0 −12 include/spdlog/details/os-inl.h
+0 −4 include/spdlog/details/os.h
+1 −1 include/spdlog/fmt/bin_to_hex.h
+28 −20 include/spdlog/fmt/bundled/args.h
+1,886 −1,770 include/spdlog/fmt/bundled/base.h
+470 −376 include/spdlog/fmt/bundled/chrono.h
+22 −20 include/spdlog/fmt/bundled/color.h
+24 −46 include/spdlog/fmt/bundled/compile.h
+44 −65 include/spdlog/fmt/bundled/format-inl.h
+1,282 −1,089 include/spdlog/fmt/bundled/format.h
+2 −0 include/spdlog/fmt/bundled/locale.h
+55 −43 include/spdlog/fmt/bundled/os.h
+88 −43 include/spdlog/fmt/bundled/ostream.h
+130 −107 include/spdlog/fmt/bundled/printf.h
+130 −98 include/spdlog/fmt/bundled/ranges.h
+47 −74 include/spdlog/fmt/bundled/std.h
+46 −97 include/spdlog/fmt/bundled/xchar.h
+6 −12 include/spdlog/sinks/ansicolor_sink-inl.h
+3 −4 include/spdlog/sinks/ansicolor_sink.h
+0 −6 include/spdlog/sinks/basic_file_sink-inl.h
+0 −1 include/spdlog/sinks/basic_file_sink.h
+1 −1 include/spdlog/sinks/null_sink.h
+0 −6 include/spdlog/sinks/rotating_file_sink-inl.h
+0 −1 include/spdlog/sinks/rotating_file_sink.h
+4 −5 include/spdlog/sinks/stdout_sinks-inl.h
+0 −7 include/spdlog/tweakme.h
+1 −1 include/spdlog/version.h
+1 −5 src/bundled_fmtlib_format.cpp
+5 −2 tests/CMakeLists.txt
+0 −9 tests/test_cfg.cpp
+1 −2 tests/test_custom_callbacks.cpp
+5 −3 tests/test_daily_logger.cpp
+0 −40 tests/test_file_logging.cpp
+0 −35 tests/test_misc.cpp
+1 −2 tests/test_sink.h

0 comments on commit 74963f1

Please sign in to comment.