feat: align Sensor model with API

This commit is contained in:
2026-05-12 16:35:44 +02:00
parent aedd8ad00c
commit fd0eaa1ddf
+35 -42
View File
@@ -1,56 +1,49 @@
import 'position.dart';
enum SensorStatus { online, offline, provisioning }
class Sensor { class Sensor {
const Sensor({ const Sensor({
required this.id, required this.id,
required this.name, required this.sensorId,
required this.floorId, this.name,
required this.position, this.roomId,
required this.status, this.x,
this.lastSeen, this.y,
this.rssiRef,
}); });
final String id; final int id;
final String name; final String sensorId; // BLE MAC / provisioning device ID
final String floorId; final String? name; // human-readable label
final Position position; final int? roomId;
final SensorStatus status; final double? x; // floor_x from API
final DateTime? lastSeen; final double? y; // floor_y from API
final double? rssiRef;
bool get isPlaced => roomId != null;
String get displayName => name ?? sensorId;
factory Sensor.fromJson(Map<String, dynamic> json) => Sensor(
id: json['id'] as int,
sensorId: json['sensor_id'] as String,
name: json['name'] as String?,
roomId: json['room_id'] as int?,
x: (json['floor_x'] as num?)?.toDouble(),
y: (json['floor_y'] as num?)?.toDouble(),
rssiRef: (json['rssi_ref'] as num?)?.toDouble(),
);
Sensor copyWith({ Sensor copyWith({
String? name, String? name,
Position? position, int? roomId,
SensorStatus? status, double? x,
DateTime? lastSeen, double? y,
double? rssiRef,
}) => }) =>
Sensor( Sensor(
id: id, id: id,
sensorId: sensorId,
name: name ?? this.name, name: name ?? this.name,
floorId: floorId, roomId: roomId ?? this.roomId,
position: position ?? this.position, x: x ?? this.x,
status: status ?? this.status, y: y ?? this.y,
lastSeen: lastSeen ?? this.lastSeen, rssiRef: rssiRef ?? this.rssiRef,
);
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'floor_id': floorId,
'position': position.toJson(),
'status': status.name,
'last_seen': lastSeen?.toIso8601String(),
};
factory Sensor.fromJson(Map<String, dynamic> json) => Sensor(
id: json['id'] as String,
name: json['name'] as String,
floorId: json['floor_id'] as String,
position: Position.fromJson(json['position'] as Map<String, dynamic>),
status: SensorStatus.values.byName(json['status'] as String),
lastSeen: json['last_seen'] == null
? null
: DateTime.parse(json['last_seen'] as String),
); );
} }