Skip to content

Commit 06cb55e

Browse files
fixup! ArduinoIoTCloudTCP: switch to messages
1 parent 08874c8 commit 06cb55e

File tree

2 files changed

+20
-65
lines changed

2 files changed

+20
-65
lines changed

src/ArduinoIoTCloudTCP.cpp

+10-65
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7575
, _password("")
7676
#endif
7777
, _mqttClient{nullptr}
78+
, _deviceTopicOut("")
79+
, _deviceTopicIn("")
80+
, _shadowTopicOut("")
81+
, _shadowTopicIn("")
82+
, _dataTopicOut("")
83+
, _dataTopicIn("")
7884
#if OTA_ENABLED
7985
, _ota_cap{false}
8086
, _ota_error{static_cast<int>(OTAError::None)}
@@ -182,9 +188,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
182188
_mqttClient.setConnectionTimeout(1500);
183189
_mqttClient.setId(getDeviceId().c_str());
184190

185-
getTopic_devicein() = getTopic_devicein();
186-
187-
_messageTopicIn = getTopic_messagein();
191+
_deviceTopicOut = getTopic_deviceout();
192+
_deviceTopicIn = getTopic_devicein();
188193

189194
_thing.begin();
190195
_device.begin();
@@ -430,7 +435,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
430435
}
431436

432437
/* Topic for OTA properties and device configuration */
433-
if (getTopic_devicein() == topic) {
438+
if (_deviceTopicIn == topic) {
434439
CBORDecoder::decode(_device.getPropertyContainer(), (uint8_t*)bytes, length);
435440

436441
/* Temporary check to avoid flooding device state machine with usless messages */
@@ -507,28 +512,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
507512

508513
void ArduinoIoTCloudTCP::sendMessage(Message * msg)
509514
{
510-
switch (msg->id)
511-
{
512-
case DeviceBeginCmdId:
513-
sendDevicePropertiesToCloud();
514-
break;
515-
516-
case ThingBeginCmdId:
517-
requestThingId();
518-
break;
519-
520-
case LastValuesBeginCmdId:
521-
requestLastValue();
522-
break;
523-
524-
case PropertiesUpdateCmdId:
525-
sendThingPropertiesToCloud();
526-
break;
527-
528-
default:
529-
break;
530-
}
531-
532515
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
533516
size_t bytes_encoded = sizeof(data);
534517
CBORMessageEncoder encoder;
@@ -566,24 +549,6 @@ void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
566549
sendPropertyContainerToCloud(_dataTopicOut, _thing.getPropertyContainer(), _thing.getPropertyContainerIndex());
567550
}
568551

569-
void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud()
570-
{
571-
PropertyContainer ro_device_property_container;
572-
unsigned int last_device_property_index = 0;
573-
574-
std::list<String> ro_device_property_list {"LIB_VERSION", "OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
575-
std::for_each(ro_device_property_list.begin(),
576-
ro_device_property_list.end(),
577-
[this, &ro_device_property_container ] (String const & name)
578-
{
579-
Property* p = getProperty(this->_device.getPropertyContainer(), name);
580-
if(p != nullptr)
581-
addPropertyToContainer(ro_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
582-
}
583-
);
584-
sendPropertyContainerToCloud(getTopic_deviceout(), ro_device_property_container, last_device_property_index);
585-
}
586-
587552
#if OTA_ENABLED
588553
void ArduinoIoTCloudTCP::sendDevicePropertyToCloud(String const name)
589554
{
@@ -594,31 +559,11 @@ void ArduinoIoTCloudTCP::sendDevicePropertyToCloud(String const name)
594559
if(p != nullptr)
595560
{
596561
addPropertyToContainer(temp_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
597-
sendPropertyContainerToCloud(getTopic_deviceout(), temp_device_property_container, last_device_property_index);
562+
sendPropertyContainerToCloud(_deviceTopicOut, temp_device_property_container, last_device_property_index);
598563
}
599564
}
600565
#endif
601566

602-
void ArduinoIoTCloudTCP::requestLastValue()
603-
{
604-
// Send the getLastValues CBOR message to the cloud
605-
// [{0: "r:m", 3: "getLastValues"}] = 81 A2 00 63 72 3A 6D 03 6D 67 65 74 4C 61 73 74 56 61 6C 75 65 73
606-
// Use http://cbor.me to easily generate CBOR encoding
607-
const uint8_t CBOR_REQUEST_LAST_VALUE_MSG[] = { 0x81, 0xA2, 0x00, 0x63, 0x72, 0x3A, 0x6D, 0x03, 0x6D, 0x67, 0x65, 0x74, 0x4C, 0x61, 0x73, 0x74, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x73 };
608-
write(_shadowTopicOut, CBOR_REQUEST_LAST_VALUE_MSG, sizeof(CBOR_REQUEST_LAST_VALUE_MSG));
609-
}
610-
611-
void ArduinoIoTCloudTCP::requestThingId()
612-
{
613-
if (!_mqttClient.subscribe(getTopic_devicein()))
614-
{
615-
/* If device_id is wrong the board can't connect to the broker so this condition
616-
* should never happen.
617-
*/
618-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, getTopic_devicein().c_str());
619-
}
620-
}
621-
622567
void ArduinoIoTCloudTCP::attachThing()
623568
{
624569

src/ArduinoIoTCloudTCP.h

+10
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
166166

167167
MqttClient _mqttClient;
168168

169+
String _deviceTopicOut;
170+
String _deviceTopicIn;
171+
String _shadowTopicOut;
172+
String _shadowTopicIn;
169173
String _messageTopicOut;
170174
String _messageTopicIn;
171175
String _dataTopicOut;
@@ -184,6 +188,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
184188
inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");}
185189
inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");}
186190

191+
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
192+
inline String getTopic_shadowin () { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/i"); }
193+
187194
inline String getTopic_messageout() { return String("/a/d/" + getDeviceId() + "/c/up");}
188195
inline String getTopic_messagein () { return String("/a/d/" + getDeviceId() + "/c/dw");}
189196

@@ -206,6 +213,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
206213
void detachThing();
207214
int write(String const topic, byte const data[], int const length);
208215

216+
#if OTA_ENABLED
217+
void sendDevicePropertyToCloud(String const name);
218+
#endif
209219
};
210220

211221
/******************************************************************************

0 commit comments

Comments
 (0)