25-5-30/1
This commit is contained in:
115
lib/controller/home/captin/behavior_controller.dart
Normal file
115
lib/controller/home/captin/behavior_controller.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:sefer_driver/constant/box_name.dart';
|
||||
import 'package:sefer_driver/constant/links.dart';
|
||||
import 'package:sefer_driver/controller/functions/crud.dart';
|
||||
|
||||
import '../../../constant/table_names.dart';
|
||||
import '../../../main.dart';
|
||||
|
||||
class DriverBehaviorController extends GetxController {
|
||||
Future<List<Map<String, dynamic>>> getAllData() async {
|
||||
return await sql.getAllData(TableName.behavior);
|
||||
}
|
||||
|
||||
var isLoading = false.obs;
|
||||
var overallScore = 100.0.obs;
|
||||
var lastTrips = [].obs;
|
||||
|
||||
Future<void> fetchDriverBehavior() async {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
final response = await CRUD().get(
|
||||
link: AppLink.get_driver_behavior,
|
||||
payload: {"driver_id": box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
final json = jsonDecode(response.body);
|
||||
|
||||
overallScore.value =
|
||||
double.parse(json['data']['overall_behavior_score'].toString());
|
||||
lastTrips.value = json['data']['last_10_trips'];
|
||||
} else {
|
||||
// Get.snackbar("Error", json['message'] ?? "Unknown error");
|
||||
}
|
||||
} catch (e) {
|
||||
Get.snackbar("Error", "Exception: $e");
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> analyzeData() async {
|
||||
final data = await getAllData();
|
||||
if (data.isEmpty) return {};
|
||||
|
||||
double maxSpeed = 0;
|
||||
double totalSpeed = 0;
|
||||
int hardBrakes = 0;
|
||||
double totalDistance = 0;
|
||||
double? prevLat, prevLng;
|
||||
|
||||
for (var item in data) {
|
||||
double speed = item['speed'] ?? 0;
|
||||
double lat = item['lat'] ?? 0;
|
||||
double lng = item['lng'] ?? 0;
|
||||
double acc = item['acceleration'] ?? 0;
|
||||
|
||||
if (speed > maxSpeed) maxSpeed = speed;
|
||||
totalSpeed += speed;
|
||||
|
||||
// ✅ Hard brake threshold
|
||||
if (acc.abs() > 3.0) hardBrakes++;
|
||||
|
||||
// ✅ Distance between points
|
||||
if (prevLat != null && prevLng != null) {
|
||||
totalDistance += _calculateDistance(prevLat, prevLng, lat, lng);
|
||||
}
|
||||
prevLat = lat;
|
||||
prevLng = lng;
|
||||
}
|
||||
|
||||
double avgSpeed = totalSpeed / data.length;
|
||||
double behaviorScore = 100 - (hardBrakes * 5) - ((maxSpeed > 100) ? 10 : 0);
|
||||
behaviorScore = behaviorScore.clamp(0, 100);
|
||||
|
||||
return {
|
||||
'max_speed': maxSpeed,
|
||||
'avg_speed': avgSpeed,
|
||||
'hard_brakes': hardBrakes,
|
||||
'total_distance': totalDistance,
|
||||
'behavior_score': behaviorScore,
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> sendSummaryToServer(String driverId, String tripId) async {
|
||||
final summary = await analyzeData();
|
||||
if (summary.isEmpty) return;
|
||||
|
||||
final body = {
|
||||
'driver_id': driverId,
|
||||
'trip_id': tripId,
|
||||
...summary,
|
||||
};
|
||||
|
||||
CRUD().post(link: AppLink.saveBehavior, payload: (body));
|
||||
|
||||
await clearData();
|
||||
}
|
||||
|
||||
Future<void> clearData() async {
|
||||
await sql.deleteAllData(TableName.behavior);
|
||||
}
|
||||
|
||||
double _calculateDistance(
|
||||
double lat1, double lon1, double lat2, double lon2) {
|
||||
const p = 0.017453292519943295;
|
||||
final a = 0.5 -
|
||||
cos((lat2 - lat1) * p) / 2 +
|
||||
cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2;
|
||||
return 12742 * asin(sqrt(a)); // distance in km
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,7 @@ class HelpController extends GetxController {
|
||||
update();
|
||||
var res = await CRUD().post(link: AppLink.addhelpCenter, payload: {
|
||||
'driverID': box.read(BoxName.driverID).toString(),
|
||||
'helpQuestion':
|
||||
EncryptionHelper.instance.encryptData(helpQuestionController.text)
|
||||
'helpQuestion': (helpQuestionController.text)
|
||||
});
|
||||
var d = jsonDecode(res);
|
||||
isLoading = false;
|
||||
|
||||
@@ -198,9 +198,7 @@ class HomeCaptainController extends GetxController {
|
||||
controller.animateCamera(
|
||||
CameraUpdate.newLatLng(Get.find<LocationController>().myLocation),
|
||||
);
|
||||
} else {
|
||||
print("Controller is null, cannot proceed.");
|
||||
}
|
||||
} else {}
|
||||
}
|
||||
|
||||
void savePeriod(Duration period) {
|
||||
@@ -299,7 +297,6 @@ class HomeCaptainController extends GetxController {
|
||||
// List<Map<String, dynamic>> results =
|
||||
// await sql.getCustomQuery(customQuery);
|
||||
// countRefuse = results[0]['count'].toString();
|
||||
// print(countRefuse);
|
||||
// update();
|
||||
// if (int.parse(countRefuse) > 3) {
|
||||
// box.write(BoxName.statusDriverLocation, 'on');
|
||||
@@ -324,34 +321,34 @@ class HomeCaptainController extends GetxController {
|
||||
|
||||
addToken() async {
|
||||
String? fingerPrint = await storage.read(key: BoxName.fingerPrint);
|
||||
await CRUD().post(link: AppLink.addTokensDriver, payload: {
|
||||
CRUD().post(link: AppLink.addTokensDriver, payload: {
|
||||
'token': (box.read(BoxName.tokenDriver)),
|
||||
'captain_id': (box.read(BoxName.driverID)).toString(),
|
||||
'fingerPrint': (fingerPrint).toString()
|
||||
});
|
||||
|
||||
await CRUD().post(
|
||||
link: "${AppLink.seferAlexandriaServer}/ride/firebase/addDriver.php",
|
||||
payload: {
|
||||
'token': box.read(BoxName.tokenDriver),
|
||||
'captain_id': box.read(BoxName.driverID).toString(),
|
||||
'fingerPrint': (fingerPrint).toString()
|
||||
});
|
||||
await CRUD().post(
|
||||
link: "${AppLink.seferGizaServer}/ride/firebase/addDriver.php",
|
||||
payload: {
|
||||
'token': box.read(BoxName.tokenDriver),
|
||||
'captain_id': box.read(BoxName.driverID).toString(),
|
||||
'fingerPrint': (fingerPrint).toString()
|
||||
});
|
||||
await CRUD().post(
|
||||
// CRUD().post(
|
||||
// link: "${AppLink.seferAlexandriaServer}/ride/firebase/addDriver.php",
|
||||
// payload: {
|
||||
// 'token': box.read(BoxName.tokenDriver),
|
||||
// 'captain_id': box.read(BoxName.driverID).toString(),
|
||||
// 'fingerPrint': (fingerPrint).toString()
|
||||
// });
|
||||
// CRUD().post(
|
||||
// link: "${AppLink.seferGizaServer}/ride/firebase/addDriver.php",
|
||||
// payload: {
|
||||
// 'token': box.read(BoxName.tokenDriver),
|
||||
// 'captain_id': box.read(BoxName.driverID).toString(),
|
||||
// 'fingerPrint': (fingerPrint).toString()
|
||||
// });
|
||||
await CRUD().postWallet(
|
||||
link: "${AppLink.seferPaymentServer}/ride/firebase/addDriver.php",
|
||||
payload: {
|
||||
'token': box.read(BoxName.tokenDriver),
|
||||
'captain_id': box.read(BoxName.driverID).toString(),
|
||||
'fingerPrint': (fingerPrint).toString()
|
||||
});
|
||||
MapDriverController().driverCallPassenger();
|
||||
// MapDriverController().driverCallPassenger();
|
||||
// box.write(BoxName.statusDriverLocation, 'off');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:sefer_driver/controller/home/captin/behavior_controller.dart';
|
||||
import 'package:sefer_driver/controller/home/captin/home_captain_controller.dart';
|
||||
import 'package:sefer_driver/views/widgets/mydialoug.dart';
|
||||
import 'package:bubble_head/bubble.dart';
|
||||
@@ -183,9 +184,7 @@ class MapDriverController extends GetxController {
|
||||
await CRUD().post(
|
||||
link: "${AppLink.seferCairoServer}/ride/rides/update.php",
|
||||
payload: {
|
||||
"id": EncryptionHelper.instance
|
||||
.encryptData(rideId)
|
||||
.toString(), // Convert to String
|
||||
"id": (rideId).toString(), // Convert to String
|
||||
"status": 'CancelFromDriverAfterApply'
|
||||
});
|
||||
CRUD().postFromDialogue(
|
||||
@@ -193,28 +192,22 @@ class MapDriverController extends GetxController {
|
||||
payload: {
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
// box.read(BoxName.driverID).toString(),
|
||||
'order_id':
|
||||
EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'CancelFromDriverAfterApply'
|
||||
});
|
||||
await CRUD().post(
|
||||
link:
|
||||
"${AppLink.seferCairoServer}/ride/cancelRide/addCancelTripFromDriverAfterApplied.php",
|
||||
payload: {
|
||||
"order_id":
|
||||
EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
"order_id": (rideId).toString(),
|
||||
"driver_id": box.read(BoxName.driverID).toString(),
|
||||
"status": 'reject After Applied',
|
||||
"notes": EncryptionHelper.instance
|
||||
.encryptData(cancelTripCotroller.text)
|
||||
.toString()
|
||||
"notes": (cancelTripCotroller.text).toString()
|
||||
});
|
||||
if (AppLink.endPoint != AppLink.seferCairoServer) {
|
||||
CRUD()
|
||||
.post(link: "${AppLink.endPoint}/ride/rides/update.php", payload: {
|
||||
"id": EncryptionHelper.instance
|
||||
.encryptData(rideId)
|
||||
.toString(), // Convert to String
|
||||
"id": (rideId).toString(), // Convert to String
|
||||
"status": 'CancelFromDriverAfterApply'
|
||||
});
|
||||
CRUD().postFromDialogue(
|
||||
@@ -222,26 +215,22 @@ class MapDriverController extends GetxController {
|
||||
payload: {
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
// box.read(BoxName.driverID).toString(),
|
||||
'order_id':
|
||||
EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'CancelFromDriverAfterApply'
|
||||
});
|
||||
CRUD().post(
|
||||
link:
|
||||
"${AppLink.endPoint}/ride/cancelRide/addCancelTripFromDriverAfterApplied.php",
|
||||
payload: {
|
||||
"order_id":
|
||||
EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
"order_id": (rideId).toString(),
|
||||
"driver_id": box.read(BoxName.driverID).toString(),
|
||||
"status": 'reject After Applied',
|
||||
"notes": EncryptionHelper.instance
|
||||
.encryptData(cancelTripCotroller.text)
|
||||
.toString()
|
||||
"notes": (cancelTripCotroller.text).toString()
|
||||
});
|
||||
}
|
||||
|
||||
sql.insertData({
|
||||
'order_id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'order_id': (rideId),
|
||||
'created_at': DateTime.now().toString(),
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
}, TableName.driverOrdersRefuse);
|
||||
@@ -312,13 +301,13 @@ class MapDriverController extends GetxController {
|
||||
await CRUD().post(
|
||||
link: "${AppLink.seferCairoServer}/ride/rides/update.php",
|
||||
payload: {
|
||||
'id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'id': (rideId),
|
||||
'driverGoToPassengerTime': DateTime.now().toString(),
|
||||
'status': 'Applied'
|
||||
});
|
||||
if (AppLink.endPoint != AppLink.seferCairoServer) {
|
||||
CRUD().post(link: "${AppLink.endPoint}/ride/rides/update.php", payload: {
|
||||
'id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'id': (rideId),
|
||||
'driverGoToPassengerTime': DateTime.now().toString(),
|
||||
'status': 'Applied'
|
||||
});
|
||||
@@ -533,35 +522,35 @@ class MapDriverController extends GetxController {
|
||||
: (distanceBetweenDriverAndPassengerWhenConfirm * .06) +
|
||||
(5 * .06); //for Eygpt other like jordan .06 per minute
|
||||
await CRUD().post(link: AppLink.updateRides, payload: {
|
||||
'id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'id': (rideId),
|
||||
'rideTimeStart': DateTime.now().toString(),
|
||||
'status': 'CancelAfterWait',
|
||||
});
|
||||
CRUD().post(link: AppLink.addDriverOrder, payload: {
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
'order_id': EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'CancelAfterWait'
|
||||
});
|
||||
|
||||
if (AppLink.endPoint != AppLink.seferCairoServer) {
|
||||
CRUD().post(link: "${AppLink.endPoint}/rides/update.php", payload: {
|
||||
'id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'id': (rideId),
|
||||
'rideTimeStart': DateTime.now().toString(),
|
||||
'status': 'CancelAfterWait',
|
||||
});
|
||||
CRUD().post(link: "${AppLink.endPoint}/rides/update.php", payload: {
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
'order_id': EncryptionHelper.instance.encryptData(rideId).toString(),
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'CancelAfterWait'
|
||||
});
|
||||
}
|
||||
var paymentTokenWait =
|
||||
await generateTokenDriver(costOfWaiting5Minute.toString());
|
||||
var res = await CRUD().post(link: AppLink.addDrivePayment, payload: {
|
||||
'rideId': EncryptionHelper.instance.encryptData(rideId),
|
||||
'rideId': (rideId),
|
||||
'amount': (costOfWaiting5Minute.toString()),
|
||||
'payment_method': 'wait-cancel',
|
||||
'passengerID': EncryptionHelper.instance.encryptData(passengerId),
|
||||
'passengerID': (passengerId),
|
||||
'token': paymentTokenWait,
|
||||
'driverID': box.read(BoxName.driverID).toString(),
|
||||
});
|
||||
@@ -569,7 +558,7 @@ class MapDriverController extends GetxController {
|
||||
await generateTokenDriver(costOfWaiting5Minute.toString());
|
||||
var res1 =
|
||||
await CRUD().post(link: AppLink.addDriversWalletPoints, payload: {
|
||||
'paymentID': 'rideId${EncryptionHelper.instance.encryptData(rideId)}',
|
||||
'paymentID': 'rideId${(rideId)}',
|
||||
'amount': (costOfWaiting5Minute).toStringAsFixed(0),
|
||||
'paymentMethod': 'wait',
|
||||
'token': paymentTokenWait1,
|
||||
@@ -585,7 +574,7 @@ class MapDriverController extends GetxController {
|
||||
var paymentTokenWaitPassenger1 =
|
||||
await generateTokenPassenger((costOfWaiting5Minute * -1).toString());
|
||||
await CRUD().post(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': EncryptionHelper.instance.encryptData(passengerId),
|
||||
'passenger_id': (passengerId),
|
||||
'balance': (costOfWaiting5Minute * -1).toString(),
|
||||
'token': paymentTokenWaitPassenger1,
|
||||
});
|
||||
@@ -671,13 +660,13 @@ class MapDriverController extends GetxController {
|
||||
// Prepare data for API calls
|
||||
final nowString = DateTime.now().toString();
|
||||
final basePayload = {
|
||||
'id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'id': (rideId),
|
||||
'rideTimeFinish': nowString,
|
||||
'status': 'Finished',
|
||||
'price': totalCost,
|
||||
};
|
||||
final driverOrderPayload = {
|
||||
'order_id': EncryptionHelper.instance.encryptData(rideId.toString()),
|
||||
'order_id': (rideId.toString()),
|
||||
'status': 'Finished'
|
||||
};
|
||||
|
||||
@@ -710,7 +699,7 @@ class MapDriverController extends GetxController {
|
||||
paymentToken = await generateTokenPassenger(
|
||||
((-1) * double.parse(paymentAmount)).toString());
|
||||
futures.add(CRUD().post(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': EncryptionHelper.instance.encryptData(passengerId),
|
||||
'passenger_id': (passengerId),
|
||||
'balance': ((-1) * double.parse(paymentAmount)).toString(),
|
||||
'token': paymentToken,
|
||||
}));
|
||||
@@ -718,11 +707,11 @@ class MapDriverController extends GetxController {
|
||||
|
||||
paymentToken = await generateTokenDriver(paymentAmount.toString());
|
||||
futures.add(CRUD().post(link: AppLink.addDrivePayment, payload: {
|
||||
'rideId': EncryptionHelper.instance.encryptData(rideId),
|
||||
'rideId': (rideId),
|
||||
'amount': paymentAmount,
|
||||
'payment_method':
|
||||
walletChecked == 'true' ? "${paymentMethod}Ride" : paymentMethod,
|
||||
'passengerID': EncryptionHelper.instance.encryptData(passengerId),
|
||||
'passengerID': (passengerId),
|
||||
'token': paymentToken,
|
||||
'driverID': box.read(BoxName.driverID).toString(),
|
||||
}));
|
||||
@@ -731,7 +720,7 @@ class MapDriverController extends GetxController {
|
||||
final paymentToken1 = await generateTokenPassenger(
|
||||
((-1) * double.parse(passengerWalletBurc)).toString());
|
||||
futures.add(CRUD().post(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': EncryptionHelper.instance.encryptData(passengerId),
|
||||
'passenger_id': (passengerId),
|
||||
'token': paymentToken1,
|
||||
'balance': ((-1) * double.parse(passengerWalletBurc)).toString()
|
||||
}));
|
||||
@@ -741,7 +730,7 @@ class MapDriverController extends GetxController {
|
||||
final paymentToken2 =
|
||||
await generateTokenDriver((pointsSubtraction).toStringAsFixed(0));
|
||||
futures.add(CRUD().post(link: AppLink.addDriversWalletPoints, payload: {
|
||||
'paymentID': 'rideId${EncryptionHelper.instance.encryptData(rideId)}',
|
||||
'paymentID': 'rideId${(rideId)}',
|
||||
'amount': (pointsSubtraction).toStringAsFixed(0),
|
||||
'paymentMethod': paymentMethod,
|
||||
'token': paymentToken2,
|
||||
@@ -750,6 +739,7 @@ class MapDriverController extends GetxController {
|
||||
|
||||
// Wait for all independent API calls to complete
|
||||
await Future.wait(futures);
|
||||
Get.put(DriverBehaviorController()).sendSummaryToServer(driverId, rideId);
|
||||
|
||||
// Send notification (this likely depends on previous steps)
|
||||
FirebaseMessagesController().sendNotificationToDriverMAP(
|
||||
@@ -898,7 +888,7 @@ class MapDriverController extends GetxController {
|
||||
var res = await CRUD().get(
|
||||
link: "${AppLink.endPoint}/ride/driver_order/getOrderCancelStatus.php",
|
||||
payload: {
|
||||
'order_id': EncryptionHelper.instance.encryptData(rideId),
|
||||
'order_id': (rideId),
|
||||
}); //.then((value) {
|
||||
var response = jsonDecode(res);
|
||||
canelString = response['data']['status'];
|
||||
|
||||
Reference in New Issue
Block a user