sendLocation method

Future<void> sendLocation({
  1. required String deviceId,
  2. required double latitude,
  3. required double longitude,
  4. int? timestamp,
})

Sends a location point to the GeoEngine system.

The data is first stored in a persistent local buffer to ensure it is not lost if there is no internet connection. Then, it attempts to automatically synchronize the buffer.

Parameters:

  • deviceId: Unique identifier for the device.
  • latitude: Latitude in decimal degrees.
  • longitude: Longitude in decimal degrees.
  • timestamp: (Optional) Time of the reading in seconds since Unix epoch. If omitted, DateTime.now() is used.

Implementation

Future<void> sendLocation({
  required String deviceId,
  required double latitude,
  required double longitude,
  int? timestamp,
}) async {
  final ts = timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;

  final data = {
    'device_id': deviceId,
    'latitude': latitude,
    'longitude': longitude,
    'timestamp':
        DateTime.fromMillisecondsSinceEpoch(ts * 1000).toIso8601String(),
  };

  if (_bufferBox != null) {
    await _bufferBox!.add(data);
    if (debug) {
      print(
          '[GeoEngine] Ping guardado. Buffer: ${_bufferBox!.length} items.');
    }
  }

  _flushBuffer();
}