|
1 | 1 | #include "MqttManager.h" |
2 | 2 |
|
| 3 | +#include "ArduinoJson.h" |
| 4 | + |
3 | 5 | MqttManager::MqttManager() |
4 | 6 | { |
5 | 7 | m_connected = false; |
@@ -30,32 +32,35 @@ void MqttManager::setup(std::string mqttServer, std::string mqttPort, std::strin |
30 | 32 | m_checkConnectivityTimer.start(); |
31 | 33 | } |
32 | 34 |
|
33 | | -void MqttManager::setDeviceData(std::string deviceName, std::string deviceType, std::string deviceIP, std::string fw, std::string fwVersion) |
| 35 | +void MqttManager::setDeviceData(std::string deviceName, std::string hardware, std::string deviceIP, std::string firmware, std::string firmwareVersion) |
34 | 36 | { |
35 | 37 | m_deviceName = deviceName; |
36 | | - m_deviceType = deviceType; |
37 | 38 | m_deviceIP = deviceIP; |
38 | | - m_fw = fw; |
39 | | - m_fwVersion = fwVersion; |
| 39 | + m_hardware = hardware; |
| 40 | + m_firmware = firmware; |
| 41 | + m_firmwareVersion = firmwareVersion; |
40 | 42 |
|
41 | 43 | m_mqttClient.setClientId(m_deviceName.c_str()); |
42 | 44 |
|
43 | | - m_deviceNameTopic = "/" + m_deviceName; |
44 | | - m_deviceMacTopic = m_deviceNameTopic + "/mac"; |
45 | | - m_deviceIpTopic = m_deviceNameTopic + "/ip"; |
46 | | - m_deviceTypeTopic = m_deviceNameTopic + "/type"; |
47 | | - m_fwTopic = m_deviceNameTopic + "/fw"; |
48 | | - m_fwVersionTopic = m_fwTopic + "/version"; |
| 45 | + m_deviceDataTopic = "/" + m_deviceName; |
49 | 46 | } |
50 | 47 |
|
51 | 48 | void MqttManager::publishDeviceStatusInfo() |
52 | 49 | { |
53 | | - m_mqttClient.publish(m_deviceNameTopic.c_str(), 1, true, m_deviceName.c_str()); |
54 | | - m_mqttClient.publish(m_deviceMacTopic.c_str(), 1, true, m_deviceMac.c_str()); |
55 | | - m_mqttClient.publish(m_deviceIpTopic.c_str(), 1, true, m_deviceIP.c_str()); |
56 | | - m_mqttClient.publish(m_deviceTypeTopic.c_str(), 1, true, m_deviceType.c_str()); |
57 | | - m_mqttClient.publish(m_fwTopic.c_str(), 1, true, m_fw.c_str()); |
58 | | - m_mqttClient.publish(m_fwVersionTopic.c_str(), 1, true, m_fwVersion.c_str()); |
| 50 | + StaticJsonBuffer<200> deviceDataBuffer; |
| 51 | + JsonObject& deviceDataObject = deviceDataBuffer.createObject(); |
| 52 | + String deviceDataString; |
| 53 | + |
| 54 | + deviceDataObject["name"] = m_deviceName.c_str(); |
| 55 | + deviceDataObject["mac"] = m_deviceMac.c_str(); |
| 56 | + deviceDataObject["ip"] = m_deviceIP.c_str(); |
| 57 | + deviceDataObject["hardware"] = m_hardware.c_str(); |
| 58 | + deviceDataObject["firmware"] = m_firmware.c_str(); |
| 59 | + deviceDataObject["firmware_version"] = m_firmwareVersion.c_str(); |
| 60 | + |
| 61 | + deviceDataObject.printTo(deviceDataString); |
| 62 | + |
| 63 | + m_mqttClient.publish(m_deviceDataTopic.c_str(), 1, true, deviceDataString.c_str()); |
59 | 64 |
|
60 | 65 | this->refreshStatusTopics(); |
61 | 66 | } |
|
0 commit comments