Files
syncrow-app/lib/features/devices/model/device_model.dart
Mohammad Salameh 81625cd50e Add device icons based on device type and update network exception handling
- Add logic to set device icons based on device type in DeviceModel.fromJson method
- Update network exception handling to parse HTML response in ServerFailure class
- Add html package as a dependency for parsing HTML responses
-Added Devices Icons by updateing device types mapping
2024-04-02 15:11:20 +03:00

142 lines
3.6 KiB
Dart

import 'package:syncrow_app/features/devices/model/function_model.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
class DeviceModel {
int? activeTime;
String? category;
String? categoryName;
int? createTime;
String? gatewayId;
String? icon;
String? id;
String? ip;
double? lat;
String? localKey;
double? lon;
String? model;
String? name;
String? nodeId; //rmeove
bool? isOnline;
String? ownerId; //remove
String? productId; //remove
String? productName;
bool? isSub;
String? timeZone;
int? updateTime;
String? uuid;
DeviceType? productType;
bool isSelected = false;
late List<FunctionModel> functions;
DeviceModel({
this.activeTime,
this.category,
this.categoryName,
this.createTime,
this.gatewayId,
this.icon,
this.id,
this.ip,
this.lat,
this.localKey,
this.lon,
this.model,
this.name,
this.nodeId,
this.isOnline,
this.ownerId,
this.productId,
this.productName,
this.isSub,
this.timeZone,
this.updateTime,
this.uuid,
this.productType,
}) {
functions = getFunctions(productType!);
}
factory DeviceModel.fromJson(Map<String, dynamic> json) {
print(
'type : ${json['productId']} => ${devicesTypesMap[json['productId']]}');
String icon = '';
DeviceType type = devicesTypesMap[json['productId']] ?? DeviceType.Other;
if (type == DeviceType.LightBulb) {
icon = Assets.iconsLight;
} else if (type == DeviceType.CeilingSensor ||
type == DeviceType.WallSensor) {
icon = Assets.iconsSensors;
} else if (type == DeviceType.AC) {
icon = Assets.iconsAC;
} else if (type == DeviceType.DoorLock) {
icon = Assets.iconsDoorLock;
} else if (type == DeviceType.Curtain) {
icon = Assets.iconsCurtain;
} else if (type == DeviceType.ThreeGang) {
icon = Assets.iconsLight;
} else if (type == DeviceType.Gateway) {
icon = Assets.iconsGateway;
} else {
icon = Assets.iconsLogo;
}
return DeviceModel(
activeTime: json['activeTime'],
category: json['category'],
categoryName: json['categoryName'],
createTime: json['createTime'],
gatewayId: json['gatewayId'],
icon: icon,
id: json['id'],
ip: json['ip'],
lat: double.tryParse(json['lat']),
localKey: json['localKey'],
lon: double.tryParse(json['lon']),
model: json['model'],
name: json['name'],
nodeId: json['nodeId'],
isOnline: json['online'],
ownerId: json['ownerId'],
productId: json['productId'],
productName: json['productName'],
isSub: json['sub'],
timeZone: json['timeZone'],
updateTime: json['updateTime'],
uuid: json['uuid'],
productType: devicesTypesMap[json['productName']] ?? DeviceType.Other,
);
}
Map<String, dynamic> toJson() {
return {
'activeTime': activeTime,
'category': category,
'categoryName': categoryName,
'createTime': createTime,
'gatewayId': gatewayId,
'icon': icon,
'id': id,
'ip': ip,
'lat': lat,
'localKey': localKey,
'lon': lon,
'model': model,
'name': name,
'nodeId': nodeId,
'online': isOnline,
'ownerId': ownerId,
'productId': productId,
'productName': productName,
'sub': isSub,
'timeZone': timeZone,
'updateTime': updateTime,
'uuid': uuid,
'productType': productType,
};
}
List<FunctionModel> getFunctions(DeviceType type) =>
devicesFunctionsMap[productType] ?? [];
}