feat: add logic implementation controlling relay via API
This commit is contained in:
parent
a2dc5c4b3f
commit
0e89909893
18
agrilink_vocpro/lib/data/model/switch_relay_response.dart
Normal file
18
agrilink_vocpro/lib/data/model/switch_relay_response.dart
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
class SwitchRelayResponse {
|
||||||
|
bool? success;
|
||||||
|
String? message;
|
||||||
|
|
||||||
|
SwitchRelayResponse({this.success, this.message});
|
||||||
|
|
||||||
|
SwitchRelayResponse.fromJson(Map<String, dynamic> json) {
|
||||||
|
success = json['success'];
|
||||||
|
message = json['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
final Map<String, dynamic> data = <String, dynamic>{};
|
||||||
|
data['success'] = success;
|
||||||
|
data['message'] = message;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,8 +9,8 @@ import 'package:agrilink_vocpro/data/model/npk1_graphic_response.dart';
|
||||||
import 'package:agrilink_vocpro/data/model/npk2_graphic_response.dart';
|
import 'package:agrilink_vocpro/data/model/npk2_graphic_response.dart';
|
||||||
import 'package:agrilink_vocpro/data/model/relay_response.dart';
|
import 'package:agrilink_vocpro/data/model/relay_response.dart';
|
||||||
import 'package:agrilink_vocpro/data/model/latest_data_response.dart';
|
import 'package:agrilink_vocpro/data/model/latest_data_response.dart';
|
||||||
|
import 'package:agrilink_vocpro/data/model/switch_relay_response.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
|
@ -70,7 +70,7 @@ class AppService {
|
||||||
try {
|
try {
|
||||||
await Future.delayed(const Duration(seconds: 3));
|
await Future.delayed(const Duration(seconds: 3));
|
||||||
final result = await _dioWithoutInterceptor.get(
|
final result = await _dioWithoutInterceptor.get(
|
||||||
'get-relay',
|
'/api/get-relay',
|
||||||
options: Options(
|
options: Options(
|
||||||
headers: {'Authorization': auth},
|
headers: {'Authorization': auth},
|
||||||
),
|
),
|
||||||
|
|
@ -82,10 +82,39 @@ class AppService {
|
||||||
throw Exception('Failed to load data');
|
throw Exception('Failed to load data');
|
||||||
}
|
}
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
if (kDebugMode) {
|
final errorMessage = e.response?.data['message'];
|
||||||
print(e);
|
throw (errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// switch relay
|
||||||
|
|
||||||
|
Future<SwitchRelayResponse> switchRelay(
|
||||||
|
{required int relayNumber, required int state}) async {
|
||||||
|
final SharedPreferences pref = await SharedPreferences.getInstance();
|
||||||
|
final String auth = 'Bearer ${pref.getString('token')}';
|
||||||
|
|
||||||
|
try {
|
||||||
|
final response = await _dioWithoutInterceptor.post(
|
||||||
|
'/api/set-relay',
|
||||||
|
data: {
|
||||||
|
'id': relayNumber,
|
||||||
|
'state': state,
|
||||||
|
},
|
||||||
|
options: Options(
|
||||||
|
headers: {'Authorization': auth},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||||
|
final data = SwitchRelayResponse.fromJson(response.data);
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
throw Exception('Failed to load data');
|
||||||
}
|
}
|
||||||
rethrow;
|
} on DioException catch (e) {
|
||||||
|
final errorMessage = e.response?.data['message'];
|
||||||
|
throw (errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,22 +83,4 @@ class LoginScreen extends StatelessWidget {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showErrorDialog(BuildContext context, String errorMessage) {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) => AlertDialog(
|
|
||||||
title: const Text('Error'),
|
|
||||||
content: Text(errorMessage),
|
|
||||||
actions: [
|
|
||||||
TextButton(
|
|
||||||
onPressed: () {
|
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
|
||||||
child: const Text('OK'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,37 @@ class ControlProvider extends ChangeNotifier {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print(e);
|
print(e);
|
||||||
}
|
}
|
||||||
rethrow;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> switchRelay(int relayNumber, bool state) async {
|
||||||
|
relayState = ResultState.loading;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final int stateConverted;
|
||||||
|
if (state == true) {
|
||||||
|
stateConverted = 1;
|
||||||
|
} else {
|
||||||
|
stateConverted = 0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final result = await _appService.switchRelay(
|
||||||
|
relayNumber: relayNumber, state: stateConverted);
|
||||||
|
if (result.success == true) {
|
||||||
|
relayState = ResultState.hasData;
|
||||||
|
notifyListeners();
|
||||||
|
print(result.message);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (kDebugMode) {
|
||||||
|
print(e);
|
||||||
|
}
|
||||||
|
relayState = ResultState.error;
|
||||||
|
notifyListeners();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,10 +75,13 @@ class ControlScreen extends StatelessWidget {
|
||||||
title: 'Katup Air',
|
title: 'Katup Air',
|
||||||
subtitle: 'Relay 1',
|
subtitle: 'Relay 1',
|
||||||
isActive: provider.control_1,
|
isActive: provider.control_1,
|
||||||
onTap: () {
|
onTap: () async {
|
||||||
provider.control_1 != true
|
final result = provider.control_1 != true
|
||||||
? provider.switchControl1(true)
|
? await provider.switchRelay(1, true)
|
||||||
: provider.switchControl1(false);
|
: await provider.switchRelay(1, false);
|
||||||
|
result == true
|
||||||
|
? provider.switchControl1(!provider.control_1)
|
||||||
|
: provider.switchControl1(provider.control_1);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
ControlButtonWidget(
|
ControlButtonWidget(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user