Files
ignis_app/lib/models/home_config.dart
Artem Kokos 8198ea09ae Geofence
2026-04-14 00:02:02 +07:00

71 lines
2.8 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.
/// Модель "дома" -- один физический сервер Ignis.
/// Каждый дом имеет свой URL и API-ключ.
class HomeConfig {
final String id; // уникальный идентификатор (uuid или timestamp)
final String name; // человекочитаемое название ("Квартира", "Дача")
final String url; // адрес сервера (например ignis.akokos.ru)
final String apiKey; // ключ авторизации
final double? latitude; // GPS-широта дома (для гео-автоматизации)
final double? longitude; // GPS-долгота дома (для гео-автоматизации)
final bool geofenceEnabled; // автовыключение при уходе из дома
HomeConfig({
required this.id,
required this.name,
required this.url,
required this.apiKey,
this.latitude,
this.longitude,
this.geofenceEnabled = false,
});
/// Есть ли координаты у дома
bool get hasCoordinates => latitude != null && longitude != null;
/// Готов ли геофенс к работе: включён + координаты заданы
bool get geofenceReady => geofenceEnabled && hasCoordinates;
/// Сериализация в JSON для хранения в SharedPreferences
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'url': url,
'apiKey': apiKey,
if (latitude != null) 'latitude': latitude,
if (longitude != null) 'longitude': longitude,
'geofenceEnabled': geofenceEnabled,
};
factory HomeConfig.fromJson(Map<String, dynamic> json) => HomeConfig(
id: json['id'] as String,
name: json['name'] as String,
url: json['url'] as String,
apiKey: json['apiKey'] as String,
latitude: (json['latitude'] as num?)?.toDouble(),
longitude: (json['longitude'] as num?)?.toDouble(),
geofenceEnabled: json['geofenceEnabled'] as bool? ?? false,
);
/// Копирование с изменениями
HomeConfig copyWith({
String? name,
String? url,
String? apiKey,
double? latitude,
double? longitude,
bool? geofenceEnabled,
bool clearCoordinates = false,
}) =>
HomeConfig(
id: id,
name: name ?? this.name,
url: url ?? this.url,
apiKey: apiKey ?? this.apiKey,
latitude: clearCoordinates ? null : (latitude ?? this.latitude),
longitude: clearCoordinates ? null : (longitude ?? this.longitude),
// Если очищаем координаты -- геофенс тоже выключается
geofenceEnabled: clearCoordinates
? false
: (geofenceEnabled ?? this.geofenceEnabled),
);
}