2024-10-02 13:07:28 +00:00
|
|
|
import 'package:agrilink_vocpro/core/state/result_state.dart';
|
|
|
|
|
import 'package:agrilink_vocpro/domain/service/mqtt_service.dart';
|
2024-10-01 03:49:55 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
class ControlProvider extends ChangeNotifier {
|
|
|
|
|
bool _control_1 = false;
|
2024-10-02 13:07:28 +00:00
|
|
|
bool _control_2 = false;
|
|
|
|
|
bool _control_3 = false;
|
|
|
|
|
bool _control_4 = false;
|
|
|
|
|
bool _control_5 = false;
|
|
|
|
|
bool _control_6 = false;
|
|
|
|
|
|
|
|
|
|
// Getters
|
2024-10-01 03:49:55 +00:00
|
|
|
bool get control_1 => _control_1;
|
2024-10-02 13:07:28 +00:00
|
|
|
bool get control_2 => _control_2;
|
|
|
|
|
bool get control_3 => _control_3;
|
|
|
|
|
bool get control_4 => _control_4;
|
|
|
|
|
bool get control_5 => _control_5;
|
|
|
|
|
bool get control_6 => _control_6;
|
2024-10-01 03:49:55 +00:00
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
ControlProvider() {
|
|
|
|
|
connectMqtt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResultState mqttState = ResultState.initial;
|
|
|
|
|
|
|
|
|
|
// Koneksi MQTT
|
|
|
|
|
Future<void> connectMqtt() async {
|
|
|
|
|
mqttState = ResultState.loading;
|
|
|
|
|
try {
|
|
|
|
|
final result = await MQTTService().setupMqtt();
|
|
|
|
|
if (result == ResultState.hasData) {
|
|
|
|
|
mqttState = result;
|
|
|
|
|
print('Connected to MQTT');
|
|
|
|
|
} else {
|
|
|
|
|
mqttState = ResultState.error;
|
|
|
|
|
print('Failed to connect to MQTT');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print(e);
|
|
|
|
|
}
|
2024-10-01 03:49:55 +00:00
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
Future<void> disconnectMqtt() async {
|
|
|
|
|
try {
|
|
|
|
|
final result = await MQTTService().disconnectMqtt();
|
|
|
|
|
if (result == ResultState.hasData) print('Disconnected from MQTT');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
2024-10-01 03:49:55 +00:00
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
Future<bool> isMqttConnected() async {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
disconnectMqtt();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void switchControl1() {
|
|
|
|
|
_control_1 = !_control_1;
|
2024-10-01 03:49:55 +00:00
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
void switchControl2() {
|
|
|
|
|
_control_2 = !_control_2;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
2024-10-01 03:49:55 +00:00
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
void switchControl3() {
|
2024-10-01 03:49:55 +00:00
|
|
|
_control_3 = !_control_3;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
void switchControl4() {
|
2024-10-01 03:49:55 +00:00
|
|
|
_control_4 = !_control_4;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
void switchControl5() {
|
2024-10-01 03:49:55 +00:00
|
|
|
_control_5 = !_control_5;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 13:07:28 +00:00
|
|
|
void switchControl6() {
|
2024-10-01 03:49:55 +00:00
|
|
|
_control_6 = !_control_6;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|