processToDeviceQueue method

Future<void> processToDeviceQueue()

Processes the to_device queue and tries to send every entry. This function MAY throw an error, which just means the to_device queue wasn't proccessed all the way.

Implementation

Future<void> processToDeviceQueue() async {
  final database = this.database;
  if (database == null || !_toDeviceQueueNeedsProcessing) {
    return;
  }
  final entries = await database.getToDeviceEventQueue();
  if (entries.isEmpty) {
    _toDeviceQueueNeedsProcessing = false;
    return;
  }
  for (final entry in entries) {
    // Convert the Json Map to the correct format regarding
    // https: //sdn.org/docs/spec/client_server/r0.6.1#put-sdn-client-r0-sendtodevice-eventtype-txnid
    final data = entry.content.map((k, v) =>
        MapEntry<String, Map<String, Map<String, dynamic>>>(
            k,
            (v as Map).map((k, v) => MapEntry<String, Map<String, dynamic>>(
                k, Map<String, dynamic>.from(v)))));

    try {
      await super.sendToDevice(entry.type, entry.txnId, data);
    } on SDNException catch (e) {
      Logs().w(
          '[To-Device] failed to to_device message from the queue to the server. Ignoring error: $e');
      Logs().w('Payload: $data');
    }
    await database.deleteFromToDeviceQueue(entry.id);
  }
}