import 'dart:math'; import 'package:agrilink_vocpro/core/state/result_state.dart'; import 'package:agrilink_vocpro/domain/service/app_service.dart'; import 'package:flutter/material.dart'; class ControlProvider extends ChangeNotifier { final AppService _appService = AppService(); bool _control_1 = false; bool get control_1 => _control_1; bool _control_2 = false; bool get control_2 => _control_2; ControlProvider() { getRelayStatus(); } // ResultState mqttState = ResultState.initial; // ResultState subscribeState = ResultState.initial; ResultState relayState = ResultState.initial; // Future connectMqtt() async { // mqttState = ResultState.loading; // subscribeState = ResultState.loading; // notifyListeners(); // try { // final result = await _mqttService.setupMqtt(); // if (result == ResultState.hasData) { // mqttState = result; // final result2 = await _mqttService.subscribeToRelayStatus(); // // if (result2 == true) { // // subscribeState = ResultState.hasData; // // _control_1 = true; // // } else { // // subscribeState = ResultState.hasData; // // _control_1 = false; // // } // } else { // mqttState = ResultState.error; // } // } catch (e) { // mqttState = ResultState.error; // print(e); // } // notifyListeners(); // } Future getRelayStatus() async { relayState = ResultState.loading; notifyListeners(); try { final result = await _appService.getRelayStatus(); if (result.success == true) { for (var element in result.data!) { if (element.number == 1) { switchControl1(element.currentStatus ?? false); } if (element.number == 2) { switchControl2(element.currentStatus ?? false); } } relayState = ResultState.hasData; notifyListeners(); } else { relayState = ResultState.error; notifyListeners(); } } catch (e) { relayState = ResultState.error; notifyListeners(); print(e); rethrow; } } // Future disconnectMqtt() async { // try { // await _mqttService.disconnectMqtt(); // } catch (e) { // print(e); // rethrow; // } // notifyListeners(); // } // @override // void dispose() { // disconnectMqtt(); // super.dispose(); // } void switchControl1(bool value) { _control_1 = value; notifyListeners(); } void switchControl2(bool value) { _control_2 = value; notifyListeners(); } // Future publishMessage(String topic, String message) async { // try { // final result = await _mqttService.publishMessage(topic, message); // return result; // } catch (e) { // print(e); // rethrow; // } // } // Future subscribeToTopic(String topic) async { // try { // await _mqttService.subscribeToTopic(topic); // } catch (e) { // print(e); // rethrow; // } // } }