201 lines
5.9 KiB
Dart
201 lines
5.9 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:mobile_monitoring/data/models/service_result.dart';
|
|
|
|
/// Model untuk data monitoring
|
|
class MonitoringData {
|
|
final String ph;
|
|
final String nutrisi;
|
|
final String phStatus;
|
|
final String nutrientStatus;
|
|
final bool pumpPhUp;
|
|
final bool pumpPhDown;
|
|
final bool pumpNutrient;
|
|
final bool pumpWater;
|
|
final bool isPhAbnormal;
|
|
final bool isNutrientAbnormal;
|
|
|
|
MonitoringData({
|
|
required this.ph,
|
|
required this.nutrisi,
|
|
required this.phStatus,
|
|
required this.nutrientStatus,
|
|
required this.pumpPhUp,
|
|
required this.pumpPhDown,
|
|
required this.pumpNutrient,
|
|
required this.pumpWater,
|
|
required this.isPhAbnormal,
|
|
required this.isNutrientAbnormal,
|
|
});
|
|
}
|
|
|
|
/// Model untuk status error
|
|
class HomeDataError {
|
|
final String displayMessage;
|
|
final String detailMessage;
|
|
final ErrorType errorType;
|
|
|
|
HomeDataError({
|
|
required this.displayMessage,
|
|
required this.detailMessage,
|
|
required this.errorType,
|
|
});
|
|
|
|
factory HomeDataError.fromException(Object error) {
|
|
final errorMsg = error.toString();
|
|
debugPrint('[HomeService] Error: $errorMsg');
|
|
|
|
bool isPermissionError = errorMsg.contains('permission') || errorMsg.contains('Permission');
|
|
bool isNotAuthError = errorMsg.contains('terautentikasi');
|
|
|
|
if (isPermissionError) {
|
|
return HomeDataError(
|
|
displayMessage: 'Akses Ditolak',
|
|
detailMessage: 'Anda tidak memiliki izin untuk melihat data ini',
|
|
errorType: ErrorType.permission,
|
|
);
|
|
} else if (isNotAuthError) {
|
|
return HomeDataError(
|
|
displayMessage: 'Silakan Login',
|
|
detailMessage: 'Sesi Anda telah berakhir, login kembali',
|
|
errorType: ErrorType.authentication,
|
|
);
|
|
} else {
|
|
return HomeDataError(
|
|
displayMessage: 'Gagal memuat data',
|
|
detailMessage: 'Pastikan koneksi internet stabil',
|
|
errorType: ErrorType.network,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
enum ErrorType {
|
|
permission,
|
|
authentication,
|
|
network,
|
|
unknown,
|
|
}
|
|
|
|
/// Service untuk Home Page - Backend Logic
|
|
class HomeService {
|
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
|
|
|
ServiceResult<String?> getCurrentUserIdResult() {
|
|
final uid = _auth.currentUser?.uid;
|
|
debugPrint('[HomeService] getCurrentUserIdResult()');
|
|
debugPrint(' Current user email: ${_auth.currentUser?.email}');
|
|
debugPrint(' Retrieved UID: "$uid"');
|
|
debugPrint(' UID length: ${uid?.length ?? 0}');
|
|
debugPrint(' UID is empty: ${uid?.isEmpty ?? true}');
|
|
if (uid == null || uid.isEmpty) {
|
|
return ServiceResult.failure(
|
|
message: 'User tidak terautentikasi',
|
|
errorCode: 'unauthenticated',
|
|
);
|
|
}
|
|
return ServiceResult.success(
|
|
message: 'User ID berhasil diambil',
|
|
data: uid,
|
|
);
|
|
}
|
|
|
|
/// Get current user
|
|
User? getCurrentUser() {
|
|
return _auth.currentUser;
|
|
}
|
|
|
|
/// Get user display name
|
|
String getUserDisplayName() {
|
|
final name = _auth.currentUser?.displayName;
|
|
return name?.isNotEmpty == true ? name! : 'Petani';
|
|
}
|
|
|
|
/// Get user initials untuk avatar
|
|
String getUserInitials(String? name) {
|
|
if (name == null || name.isEmpty) return 'U';
|
|
final parts = name.trim().split(' ');
|
|
if (parts.length >= 2) {
|
|
return '${parts[0][0]}${parts[1][0]}'.toUpperCase();
|
|
}
|
|
return name[0].toUpperCase();
|
|
}
|
|
|
|
MonitoringData _parseMonitoringData(Map<String, dynamic> data) {
|
|
debugPrint('[HomeService] parseMonitoringData()');
|
|
debugPrint(' Raw data: ${data.toString()}');
|
|
|
|
// Extract pump status
|
|
final pumpPhUp = data['pump_ph_up_status'] ?? false;
|
|
final pumpPhDown = data['pump_ph_down_status'] ?? false;
|
|
final pumpNutrient = data['pump_nutrient_status'] ?? false;
|
|
final pumpWater = data['pump_water_status'] ?? false;
|
|
|
|
// Determine abnormality status
|
|
final bool isPhAbnormal = pumpPhUp || pumpPhDown;
|
|
final bool isNutrientAbnormal = pumpNutrient || pumpWater;
|
|
|
|
// Determine user-friendly status
|
|
final String phStatus = isPhAbnormal ? 'Abnormal' : 'Normal';
|
|
final String nutrientStatus = isNutrientAbnormal ? 'Abnormal' : 'Normal';
|
|
|
|
// Extract and format values
|
|
final ph = data['pH_value']?.toString() ?? '-';
|
|
final nutrisi = data['nutrient_level']?.toString() ?? '-';
|
|
|
|
debugPrint(' Parsed pH: $ph, status: $phStatus');
|
|
debugPrint(' Parsed nutrient: $nutrisi, status: $nutrientStatus');
|
|
debugPrint(' Pump status - PhUp: $pumpPhUp, PhDown: $pumpPhDown, Nutrient: $pumpNutrient, Water: $pumpWater');
|
|
|
|
return MonitoringData(
|
|
ph: ph,
|
|
nutrisi: nutrisi,
|
|
phStatus: phStatus,
|
|
nutrientStatus: nutrientStatus,
|
|
pumpPhUp: pumpPhUp,
|
|
pumpPhDown: pumpPhDown,
|
|
pumpNutrient: pumpNutrient,
|
|
pumpWater: pumpWater,
|
|
isPhAbnormal: isPhAbnormal,
|
|
isNutrientAbnormal: isNutrientAbnormal,
|
|
);
|
|
}
|
|
|
|
ServiceResult<MonitoringData> parseMonitoringDataResult(Map<String, dynamic> data) {
|
|
try {
|
|
final parsed = _parseMonitoringData(data);
|
|
return ServiceResult.success(
|
|
message: 'Data monitoring berhasil diproses',
|
|
data: parsed,
|
|
);
|
|
} catch (e) {
|
|
return ServiceResult.failure(
|
|
message: 'Gagal memproses data monitoring',
|
|
errorCode: 'parse_error',
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Validate user authentication
|
|
bool isUserAuthenticated() {
|
|
final user = _auth.currentUser;
|
|
final isAuth = user != null && user.uid.isNotEmpty;
|
|
debugPrint('[HomeService] isUserAuthenticated: $isAuth');
|
|
return isAuth;
|
|
}
|
|
|
|
/// Check if user ID is valid
|
|
bool isValidUserId(String? userId) {
|
|
final isValid = userId != null && userId.isNotEmpty;
|
|
debugPrint('[HomeService] isValidUserId("$userId"): $isValid');
|
|
return isValid;
|
|
}
|
|
|
|
ServiceResult<HomeDataError> getErrorInfoResult(Object error) {
|
|
return ServiceResult.success(
|
|
message: 'Error info berhasil dipetakan',
|
|
data: HomeDataError.fromException(error),
|
|
);
|
|
}
|
|
}
|