25-6-13/1
This commit is contained in:
@@ -4,10 +4,12 @@ import 'dart:math';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:location/location.dart';
|
||||
import 'package:sefer_driver/constant/table_names.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../home/captin/home_captain_controller.dart';
|
||||
import '../home/payment/captain_wallet_controller.dart';
|
||||
import 'crud.dart';
|
||||
@@ -72,6 +74,8 @@ class LocationController extends GetxController {
|
||||
}
|
||||
|
||||
int _insertCounter = 0;
|
||||
double? _lastSpeed;
|
||||
DateTime? _lastSpeedTime;
|
||||
|
||||
Future<void> startLocationUpdates() async {
|
||||
if (box.read(BoxName.driverID) != null) {
|
||||
@@ -111,13 +115,17 @@ class LocationController extends GetxController {
|
||||
if (_lastSavedPosition == null ||
|
||||
_calculateDistanceInMeters(_lastSavedPosition!, myLocation) >=
|
||||
10) {
|
||||
double currentSpeed = speed; // m/s
|
||||
double? acceleration = _calculateAcceleration(currentSpeed);
|
||||
|
||||
await sql.insertData({
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
'latitude': myLocation.latitude,
|
||||
'longitude': myLocation.longitude,
|
||||
'acceleration': acceleration ?? 0.0,
|
||||
'created_at': DateTime.now().toIso8601String(),
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
}, 'car_locations');
|
||||
}, TableName.behavior);
|
||||
|
||||
_lastSavedPosition = myLocation;
|
||||
}
|
||||
@@ -125,6 +133,7 @@ class LocationController extends GetxController {
|
||||
|
||||
// ✅ إدخال للسيرفر كل دقيقة
|
||||
_insertCounter++;
|
||||
Log.print('_insertCounter: ${_insertCounter}');
|
||||
if (_insertCounter >= 12) {
|
||||
_insertCounter = 0;
|
||||
await CRUD().post(
|
||||
@@ -199,4 +208,22 @@ class LocationController extends GetxController {
|
||||
2;
|
||||
return 12742 * 1000 * asin(sqrt(a)); // meters
|
||||
}
|
||||
|
||||
double? _calculateAcceleration(double currentSpeed) {
|
||||
final now = DateTime.now();
|
||||
if (_lastSpeed != null && _lastSpeedTime != null) {
|
||||
final deltaTime =
|
||||
now.difference(_lastSpeedTime!).inMilliseconds / 1000.0; // seconds
|
||||
if (deltaTime > 0) {
|
||||
final acceleration = (currentSpeed - _lastSpeed!) / deltaTime;
|
||||
_lastSpeed = currentSpeed;
|
||||
_lastSpeedTime = now;
|
||||
return double.parse(acceleration.toStringAsFixed(2));
|
||||
}
|
||||
}
|
||||
|
||||
_lastSpeed = currentSpeed;
|
||||
_lastSpeedTime = now;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user