Ledger onSync constantly being called after reconnect

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;
}

I am wondering if all three of your ledger instances are being created with the same name, “ledger”, instead of unique names, such as: “ledger1”, “ledger2” & “ledger3” for example. The .onSync() statement should synchronize only one particular Ledger after connection but it looks to me that all of them: “.onSync([](Ledger ledger,” - name an identical instance. Hope this helps.

Not sure I understand what you mean. I’ve created 3 Seperate ledger variables with 3 seperate ledger names so they all should be seperate. I can see from the output that each ledger calls the onsync correctly when I update the ledgers in the cloud. Just that it continually calls it whenever I re connect to the cloud. So if I only update 1 ledger then that ledger is called again and again on reconnect, but if I update 2 or 3 ledgers then both or all three are called again and again.