Hi,
I’m trying out the Ledger system and have create three Ledgers at each scope level. I’ve attached the three ledgers to an onSync callback If I Update an Instance in the cloud and the device connects the callback is called as expected.
However If I then disconnect and sleep the device and then wake and reconnect the callback is called again, but I have not changed the ledger in the cloud.
If I reset the device then the callback is not called after it connects (and if I make a change it is called).
Is there something that I need to do when I disconnect to tell it that the current Ledger is current and don’t need to update? Below are the log output and extracts of the code I’m using.
0000650240|cloud|INFO|Connectivity status: CONNECTED
0000650241|comm.coap|TRACE|Received CoAP message
0000650241|comm.coap|TRACE|CON POST /E/particle/device/updates/pending size=47 token=01 id=50899
0000650243|comm.coap|TRACE|Sending CoAP message
0000650243|comm.coap|TRACE|ACK 0.00 size=4 token= id=50899
0000650247|comm.coap|TRACE|Received CoAP message
0000650248|comm.coap|TRACE|ACK 0.00 size=4 token= id=190
0000650685|comm.coap|TRACE|Received CoAP message
0000650685|comm.coap|TRACE|ACK 2.04 size=35 token=4e id=191
0000650963|messages|INFO|Cloud ledger synced [monitor-device]*
void CloudHandler::setup(int (*callback)(String)) {
bool success = Particle.function("callback", callback);
if (!success) lg(log,log_msg_failed_callback);
_dataLedger_device = Particle.ledger("monitor-device");
_dataLedger_product = Particle.ledger("monitor-product");
_dataLedger_global = Particle.ledger("monitor-global");
_dataLedger_device.onSync([](Ledger ledger, void* arg) {
MessageManager::instance().notifyCloudLedgerSynced(ledger);
});
_dataLedger_product.onSync([](Ledger ledger, void* arg) {
MessageManager::instance().notifyCloudLedgerSynced(ledger);
});
_dataLedger_global.onSync([](Ledger ledger, void* arg) {
MessageManager::instance().notifyCloudLedgerSynced(ledger);
});
if (_dataLedger_device.isValid()) {
lg(log,"Device Ledger is valid last update %llu, last synced %llu",_dataLedger_device.lastUpdated(),_dataLedger_device.lastSynced());
} else {
lg(warn,"Device Ledger is not valid");
}
if (_dataLedger_product.isValid()) {
lg(log,"Product Ledger is valid last update %llu, last synced %llu",_dataLedger_product.lastUpdated(),_dataLedger_product.lastSynced());
} else {
lg(warn,"Product Ledger is not valid");
}
if (_dataLedger_global.isValid()) {
lg(log,"Global Ledger is valid last update %llu, last synced %llu",_dataLedger_global.lastUpdated(),_dataLedger_global.lastSynced());
} else {
lg(warn,"Global Ledger is not valid");
}
}
void MessageManager::notifyCloudLedgerSynced(Ledger ledger) {
lg(info,"*******************Cloud ledger synced [%s]********************", ledger.name());
LedgerData data = ledger.get();
for (const LedgerData::Entry& entry: data.entries()) {
const String& name = entry.first;
const Variant& value = entry.second;
Log.info("%s: %s", name.c_str(), value.toString().c_str());
}
}
void CloudHandler::connect() {
lg(log,log_msg_connection, "Cloud");
Particle.connect();
waitFor(Particle.connected, 20000);
connectivityState = TM_CONNECTING;
}
void CloudHandler::disconnect() {
Particle.disconnect(CloudDisconnectOptions().graceful(true).timeout(20s));
waitFor(Particle.disconnected, 20000);
lg(info,log_msg_cloud_turn_off);
Cellular.disconnect();
Cellular.off();
waitFor(Cellular.isOff, 20000);
lg(info,log_msg_modem_is_off);
connectivityState = TM_NOT_CONNECTED;
}