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 {
const Sensor({
required this.id,
required this.name,
required this.floorId,
required this.position,
required this.status,
this.lastSeen,
required this.sensorId,
this.name,
this.roomId,
this.x,
this.y,
this.rssiRef,
});
final String id;
final String name;
final String floorId;
final Position position;
final SensorStatus status;
final DateTime? lastSeen;
final int id;
final String sensorId; // BLE MAC / provisioning device ID
final String? name; // human-readable label
final int? roomId;
final double? x; // floor_x from API
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({
String? name,
Position? position,
SensorStatus? status,
DateTime? lastSeen,
int? roomId,
double? x,
double? y,
double? rssiRef,
}) =>
Sensor(
id: id,
sensorId: sensorId,
name: name ?? this.name,
floorId: floorId,
position: position ?? this.position,
status: status ?? this.status,
lastSeen: lastSeen ?? this.lastSeen,
);
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),
roomId: roomId ?? this.roomId,
x: x ?? this.x,
y: y ?? this.y,
rssiRef: rssiRef ?? this.rssiRef,
);
}