import 'dart:convert'; import 'package:http/http.dart' as http; import '../models/sensor_data.dart'; class ApiService { // Singleton pattern static final ApiService _instance = ApiService._internal(); factory ApiService() => _instance; ApiService._internal(); // Base URL Railway (Updated account) static const String baseUrl = 'https://backendaplikasinovil-production.up.railway.app'; // Timeout static const Duration timeout = Duration(seconds: 10); // ===================================================== // GET LATEST DATA // ===================================================== Future getLatestData() async { try { final response = await http .get( Uri.parse('$baseUrl/api/data/latest'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true && jsonData['data'] != null) { return SensorData.fromApiJson(jsonData['data']); } } print('❌ Failed to get latest data: ${response.statusCode}'); return null; } catch (e) { print('❌ Error getting latest data: $e'); return null; } } // ===================================================== // GET DATA HISTORY // ===================================================== Future> getDataHistory({int limit = 50}) async { try { final response = await http .get( Uri.parse('$baseUrl/api/data/history?limit=$limit'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true && jsonData['data'] != null) { final List dataList = jsonData['data']; return dataList .map((item) => SensorData.fromApiJson(item)) .toList(); } } print('❌ Failed to get data history: ${response.statusCode}'); return []; } catch (e) { print('❌ Error getting data history: $e'); return []; } } // ===================================================== // GET STATUS HISTORY // ===================================================== Future>> getStatusHistory({int limit = 50}) async { try { final response = await http .get( Uri.parse('$baseUrl/api/status/history?limit=$limit'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true && jsonData['data'] != null) { return List>.from(jsonData['data']); } } print('❌ Failed to get status history: ${response.statusCode}'); return []; } catch (e) { print('❌ Error getting status history: $e'); return []; } } // ===================================================== // GET STATISTICS // ===================================================== Future?> getStatistics() async { try { final response = await http .get( Uri.parse('$baseUrl/api/stats'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true && jsonData['stats'] != null) { return jsonData['stats']; } } print('❌ Failed to get statistics: ${response.statusCode}'); return null; } catch (e) { print('❌ Error getting statistics: $e'); return null; } } // ===================================================== // SEND CONTROL COMMAND // ===================================================== Future sendCommand(String command) async { try { final response = await http .post( Uri.parse('$baseUrl/api/control'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({'command': command}), ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true) { print('✅ Command sent: $command'); return true; } } print('❌ Failed to send command: ${response.statusCode}'); return false; } catch (e) { print('❌ Error sending command: $e'); return false; } } // ===================================================== // CONTROL COMMANDS // ===================================================== // System commands Future startPengeringan() => sendCommand('START'); Future resetPengeringan() => sendCommand('RESET'); // Heater commands Future heaterOn() => sendCommand('HEATER_ON'); Future heaterOff() => sendCommand('HEATER_OFF'); // Fan commands Future fanOn() => sendCommand('FAN_ON'); Future fanOff() => sendCommand('FAN_OFF'); // Lamp commands Future lampOn() => sendCommand('LAMP_ON'); Future lampOff() => sendCommand('LAMP_OFF'); // Exhaust commands Future exhaustOn() => sendCommand('EXHAUST_ON'); Future exhaustOff() => sendCommand('EXHAUST_OFF'); // ===================================================== // CLEAR HISTORY // ===================================================== Future clearHistory() async { try { final response = await http .delete( Uri.parse('$baseUrl/api/history/clear'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['success'] == true) { print('✅ History cleared'); return true; } } print('❌ Failed to clear history: ${response.statusCode}'); return false; } catch (e) { print('❌ Error clearing history: $e'); return false; } } // ===================================================== // TEST CONNECTION // ===================================================== Future testConnection() async { try { final response = await http .get( Uri.parse('$baseUrl/'), headers: {'Content-Type': 'application/json'}, ) .timeout(timeout); if (response.statusCode == 200) { final jsonData = jsonDecode(response.body); if (jsonData['status'] == 'OK') { print('✅ API connection successful'); return true; } } print('❌ API connection failed: ${response.statusCode}'); return false; } catch (e) { print('❌ Error testing API connection: $e'); return false; } } }