2026-03-25 13:56:23 +08:00

74 lines
1.9 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:freezed_annotation/freezed_annotation.dart';
part 'device.freezed.dart';
part 'device.g.dart';
/// API 有时返回完整对象,有时只返回整数 ID需要容错
class _SafeDeviceTypeConverter
implements JsonConverter<DeviceType?, Object?> {
const _SafeDeviceTypeConverter();
@override
DeviceType? fromJson(Object? json) {
if (json is Map<String, dynamic>) {
return DeviceType.fromJson(json);
}
return null; // 整数 ID 或 null → 忽略
}
@override
Object? toJson(DeviceType? object) => object?.toJson();
}
@freezed
abstract class DeviceType with _$DeviceType {
const factory DeviceType({
required int id,
required String brand,
required String productCode,
required String name,
@Default(true) bool isNetworkRequired,
@Default(true) bool isActive,
String? createdAt,
}) = _DeviceType;
factory DeviceType.fromJson(Map<String, dynamic> json) =>
_$DeviceTypeFromJson(json);
}
@freezed
abstract class DeviceInfo with _$DeviceInfo {
const factory DeviceInfo({
required int id,
required String sn,
@_SafeDeviceTypeConverter() DeviceType? deviceType,
@_SafeDeviceTypeConverter() DeviceType? deviceTypeInfo,
String? macAddress,
@Default('') String name,
@Default('in_stock') String status,
@Default(false) bool isOnline,
@Default('') String firmwareVersion,
String? lastOnlineAt,
String? createdAt,
}) = _DeviceInfo;
factory DeviceInfo.fromJson(Map<String, dynamic> json) =>
_$DeviceInfoFromJson(json);
}
@freezed
abstract class UserDevice with _$UserDevice {
const factory UserDevice({
required int id,
required DeviceInfo device,
int? spirit,
String? spiritName,
@Default('owner') String bindType,
String? bindTime,
@Default(true) bool isActive,
}) = _UserDevice;
factory UserDevice.fromJson(Map<String, dynamic> json) =>
_$UserDeviceFromJson(json);
}