Files
abawo-bt-app/lib/model/bluetooth_device_model.dart

74 lines
2.0 KiB
Dart

import 'package:abawo_bt_app/util/constants.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart' show DeviceIdentifier;
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'bluetooth_device_model.freezed.dart';
part 'bluetooth_device_model.g.dart';
/// Enum representing the type of Bluetooth device
enum DeviceType {
/// Universal Shifters device
universalShifters,
/// Other unspecified device types
other,
}
DeviceType deviceTypeFromUuids(List<Uuid> uuids) {
if (uuids.any((uuid) => isAbawoUniversalShiftersDeviceGuid(uuid))) {
return DeviceType.universalShifters;
}
return DeviceType.other;
}
DeviceType deviceTypeFromString(String type) {
return DeviceType.values.firstWhere(
(e) => e.toString().split('.').last == type,
orElse: () => DeviceType.other,
);
}
String deviceTypeToString(DeviceType type) {
return type.toString().split('.').last;
}
/// Model representing a Bluetooth device
@freezed
abstract class BluetoothDeviceModel with _$BluetoothDeviceModel {
const factory BluetoothDeviceModel({
/// Unique identifier for the device
required String id,
/// Name of the device as advertised
String? name,
/// MAC address of the device
required String address,
/// Type of the device
@Default(DeviceType.other) DeviceType type,
/// Additional device information
Map<String, dynamic>? manufacturerData,
/// Identifier of the device
@DeviceIdentJsonConverter() required DeviceIdentifier deviceIdent,
}) = _BluetoothDeviceModel;
/// Create a BluetoothDeviceModel from JSON
factory BluetoothDeviceModel.fromJson(Map<String, dynamic> json) =>
_$BluetoothDeviceModelFromJson(json);
}
class DeviceIdentJsonConverter
implements JsonConverter<DeviceIdentifier, String> {
const DeviceIdentJsonConverter();
@override
DeviceIdentifier fromJson(String json) => DeviceIdentifier(json);
@override
String toJson(DeviceIdentifier object) => object.str;
}