Files
smarthelmet_app/lib/services/locker_api.dart

59 lines
1.6 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
class LockerApi {
// 안드로이드 에뮬레이터: "http://10.0.2.2:8182"
// iOS 시뮬레이터: "http://127.0.0.1:8182"
// "http://192.168.0.82:8182"
static const String baseUrl = "http://192.168.0.81:8182";
// 명령 전송 공통 함수
Future<bool> sendCommand({
required int addr,
required int value,
String target = "*"
}) async {
final url = Uri.parse('$baseUrl/modbus/write');
try {
final response = await http.post(
url,
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"target": target,
"unit": 1,
"addr": addr,
"value": value
}),
);
if (response.statusCode == 200) {
print("명령 성공: Addr($addr) -> Val($value)");
return true;
} else {
print("명령 실패: ${response.body}");
return false;
}
} catch (e) {
print("서버 연결 오류: $e");
return false;
}
}
// --- [기능별 동작 함수] ---
// 1. 잠금 해제 (주소: 0x0016 / 값: 1)
Future<bool> unlock() async {
return await sendCommand(addr: 0x0016, value: 1);
}
// 2. 살균 (UV) 켜기/끄기 (주소: 0x0014 / 값: 1 or 0)
Future<bool> setUV(bool isOn) async {
return await sendCommand(addr: 0x0014, value: isOn ? 1 : 0);
}
// 3. 건조 (FAN) 켜기/끄기 (주소: 0x0015 / 값: 1 or 0)
Future<bool> setFan(bool isOn) async {
return await sendCommand(addr: 0x0015, value: isOn ? 1 : 0);
}
}