Files
ignis_app/lib/services/api_client.dart

86 lines
3.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:dio/dio.dart';
/// HTTP-клиент для одного сервера Ignis.
/// Покрывает все эндпоинты из openapi.json.
class IgnisApi {
final Dio _dio = Dio();
Dio get dioInstance => _dio;
/// Инициализация базового URL и API-ключа
void init(String baseUrl, String apiKey) {
String url = baseUrl.trim();
if (!url.startsWith('http')) {
url = 'https://$url';
}
// Убираем trailing slash
if (url.endsWith('/')) url = url.substring(0, url.length - 1);
_dio.options.baseUrl = url;
_dio.options.headers['X-API-Key'] = apiKey;
// Бэкенд WiZ ламп тормозит -- даём запас
_dio.options.connectTimeout = const Duration(seconds: 15);
_dio.options.receiveTimeout = const Duration(seconds: 15);
}
// ─── Устройства и группы ───────────────────────────────────
/// Все устройства (лампы)
Future<Response> getDevices() => _dio.get('/devices');
/// Все группы
Future<Response> getGroups() => _dio.get('/devices/groups');
/// Все доступные сцены
Future<Response> getScenes() => _dio.get('/devices/scenes');
/// Создать группу
Future<Response> createGroup(String id, String name, List<String> macs) =>
_dio.post('/devices/groups', data: {'id': id, 'name': name, 'macs': macs});
/// Удалить группу
Future<Response> deleteGroup(String groupId) =>
_dio.delete('/devices/groups/$groupId');
/// Пересканировать сеть (найти новые лампы)
Future<Response> rescanNetwork() => _dio.post('/devices/rescan');
// ─── Управление ────────────────────────────────────────────
/// Управление группой: state, brightness, temp, scene, r/g/b
Future<Response> controlGroup(String id, Map<String, dynamic> params) =>
_dio.post('/control/group/$id', queryParameters: params);
/// Управление одной лампой
Future<Response> controlDevice(String id, Map<String, dynamic> params) =>
_dio.post('/control/device/$id', queryParameters: params);
/// Мигнуть лампой (для идентификации)
Future<Response> blinkDevice(String id) =>
_dio.post('/control/device/$id/blink');
/// Статус группы (реальный опрос ламп)
Future<Response> getGroupStatus(String id) =>
_dio.get('/control/group/$id/status');
/// Статус одной лампы
Future<Response> getDeviceStatus(String id) =>
_dio.get('/control/device/$id/status');
// ─── Расписания ────────────────────────────────────────────
/// Одноразовое расписание (таймер)
Future<Response> scheduleOnce(Map<String, dynamic> params) =>
_dio.post('/schedules/once', queryParameters: params);
/// Cron-расписание (повторяющееся)
Future<Response> scheduleCron(Map<String, dynamic> params) =>
_dio.post('/schedules/cron', queryParameters: params);
/// Все активные задачи расписания
Future<Response> getTasks() => _dio.get('/schedules/tasks');
/// Отменить задачу расписания
Future<Response> cancelTask(String jobId) =>
_dio.delete('/schedules/$jobId');
}