Alarms, Devices and More pages
This commit is contained in:
25
lib/utils/services/device_profile_cache.dart
Normal file
25
lib/utils/services/device_profile_cache.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:thingsboard_client/thingsboard_client.dart';
|
||||
|
||||
abstract class DeviceProfileCache {
|
||||
|
||||
static final _cache = Map<String, DeviceProfileInfo>();
|
||||
|
||||
static Future<DeviceProfileInfo> getDeviceProfileInfo(ThingsboardClient tbClient, String name, String deviceId) async {
|
||||
var deviceProfile = _cache[name];
|
||||
if (deviceProfile == null) {
|
||||
var device = await tbClient.getDeviceService().getDevice(deviceId);
|
||||
deviceProfile = await tbClient.getDeviceProfileService().getDeviceProfileInfo(device.deviceProfileId!.id!);
|
||||
_cache[name] = deviceProfile;
|
||||
}
|
||||
return deviceProfile;
|
||||
}
|
||||
|
||||
static Future<PageData<DeviceProfileInfo>> getDeviceProfileInfos(ThingsboardClient tbClient, PageLink pageLink) async {
|
||||
var deviceProfileInfos = await tbClient.getDeviceProfileService().getDeviceProfileInfos(pageLink);
|
||||
deviceProfileInfos.data.forEach((deviceProfile) {
|
||||
_cache[deviceProfile.name] = deviceProfile;
|
||||
});
|
||||
return deviceProfileInfos;
|
||||
}
|
||||
|
||||
}
|
||||
61
lib/utils/services/entity_query_api.dart
Normal file
61
lib/utils/services/entity_query_api.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'package:thingsboard_client/thingsboard_client.dart';
|
||||
|
||||
abstract class EntityQueryApi {
|
||||
|
||||
static final activeDeviceKeyFilter = KeyFilter(
|
||||
key: EntityKey(type: EntityKeyType.ATTRIBUTE, key: 'active'),
|
||||
valueType: EntityKeyValueType.BOOLEAN,
|
||||
predicate: BooleanFilterPredicate(
|
||||
operation: BooleanOperation.EQUAL,
|
||||
value: FilterPredicateValue(true)));
|
||||
|
||||
static final inactiveDeviceKeyFilter = KeyFilter(
|
||||
key: EntityKey(type: EntityKeyType.ATTRIBUTE, key: 'active'),
|
||||
valueType: EntityKeyValueType.BOOLEAN,
|
||||
predicate: BooleanFilterPredicate(
|
||||
operation: BooleanOperation.EQUAL,
|
||||
value: FilterPredicateValue(false)));
|
||||
|
||||
static final defaultDeviceFields = <EntityKey>[
|
||||
EntityKey(type: EntityKeyType.ENTITY_FIELD, key: 'name'),
|
||||
EntityKey(type: EntityKeyType.ENTITY_FIELD, key: 'type'),
|
||||
EntityKey(type: EntityKeyType.ENTITY_FIELD, key: 'createdTime')
|
||||
];
|
||||
|
||||
static final defaultDeviceAttributes = <EntityKey>[
|
||||
EntityKey(type: EntityKeyType.ATTRIBUTE, key: 'active')
|
||||
];
|
||||
|
||||
static Future<int> countDevices(ThingsboardClient tbClient, {String? deviceType, bool? active}) {
|
||||
EntityFilter deviceFilter;
|
||||
if (deviceType != null) {
|
||||
deviceFilter = DeviceTypeFilter(deviceType: deviceType);
|
||||
} else {
|
||||
deviceFilter = EntityTypeFilter(entityType: EntityType.DEVICE);
|
||||
}
|
||||
EntityCountQuery deviceCountQuery = EntityCountQuery(entityFilter: deviceFilter);
|
||||
if (active != null) {
|
||||
deviceCountQuery.keyFilters = [active ? activeDeviceKeyFilter : inactiveDeviceKeyFilter];
|
||||
}
|
||||
return tbClient.getEntityQueryService().countEntitiesByQuery(deviceCountQuery);
|
||||
}
|
||||
|
||||
static EntityDataQuery createDefaultDeviceQuery({int pageSize = 10, String? searchText, String? deviceType, bool? active}) {
|
||||
EntityFilter deviceFilter;
|
||||
List<KeyFilter>? keyFilters;
|
||||
if (deviceType != null) {
|
||||
deviceFilter = DeviceTypeFilter(deviceType: deviceType);
|
||||
} else {
|
||||
deviceFilter = EntityTypeFilter(entityType: EntityType.DEVICE);
|
||||
}
|
||||
if (active != null) {
|
||||
keyFilters = [active ? activeDeviceKeyFilter : inactiveDeviceKeyFilter];
|
||||
}
|
||||
return EntityDataQuery(entityFilter: deviceFilter, keyFilters: keyFilters,
|
||||
entityFields: defaultDeviceFields, latestValues: defaultDeviceAttributes, pageLink: EntityDataPageLink(pageSize: pageSize,
|
||||
textSearch: searchText,
|
||||
sortOrder: EntityDataSortOrder(key: EntityKey(type: EntityKeyType.ENTITY_FIELD, key: 'createdTime'),
|
||||
direction: EntityDataSortOrderDirection.DESC)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user