146 lines
5.0 KiB
Dart
146 lines
5.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:sfm_app/product/extension/context_extension.dart';
|
|
|
|
import '../product/services/api_services.dart';
|
|
import '../feature/home/device_alias_model.dart';
|
|
import '../product/base/bloc/base_bloc.dart';
|
|
import '../product/services/language_services.dart';
|
|
import '../product/utils/device_utils.dart';
|
|
|
|
class HomeBloc extends BlocBase {
|
|
APIServices apiServices = APIServices();
|
|
|
|
final allDevicesAliasMap =
|
|
StreamController<Map<String, List<DeviceWithAlias>>?>.broadcast();
|
|
StreamSink<Map<String, List<DeviceWithAlias>>?> get sinkAllDevicesAliasMap =>
|
|
allDevicesAliasMap.sink;
|
|
Stream<Map<String, List<DeviceWithAlias>>?> get streamAllDevicesAliasMap =>
|
|
allDevicesAliasMap.stream;
|
|
|
|
// final allDevicesAliasJoinedMap =
|
|
// StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
|
// StreamSink<Map<String, List<DeviceWithAlias>>>
|
|
// get sinkAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.sink;
|
|
// Stream<Map<String, List<DeviceWithAlias>>>
|
|
// get streamAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.stream;
|
|
|
|
final countNotification = StreamController<int>.broadcast();
|
|
StreamSink<int> get sinkCountNotification => countNotification.sink;
|
|
Stream<int> get streamCountNotification => countNotification.stream;
|
|
|
|
final hasJoinedDevice = StreamController<bool?>.broadcast();
|
|
StreamSink<bool?> get sinkHasJoinedDevice => hasJoinedDevice.sink;
|
|
Stream<bool?> get streamHasJoinedDevice => hasJoinedDevice.stream;
|
|
|
|
final ownerDevicesStatus =
|
|
StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
|
StreamSink<Map<String, List<DeviceWithAlias>>> get sinkOwnerDevicesStatus =>
|
|
ownerDevicesStatus.sink;
|
|
Stream<Map<String, List<DeviceWithAlias>>> get streamOwnerDevicesStatus =>
|
|
ownerDevicesStatus.stream;
|
|
|
|
|
|
final aliasDevices = StreamController<List<DeviceWithAlias>?>.broadcast();
|
|
StreamSink<List<DeviceWithAlias>?> get sinkAliasDevices => aliasDevices.sink;
|
|
Stream<List<DeviceWithAlias>?> get streamAliasDevices => aliasDevices.stream;
|
|
|
|
void getOwnerAndJoinedDevices(BuildContext context) async {
|
|
await apiServices.execute(context, () async {
|
|
List<DeviceWithAlias> devices = await apiServices.getDashBoardDevices().handleApiError();
|
|
List<DeviceWithAlias> publicDevices = [];
|
|
for (var device in devices) {
|
|
if (device.visibility == "PUBLIC") {
|
|
publicDevices.add(device);
|
|
}
|
|
}
|
|
// getDeviceStatusAliasMap(publicDevices);
|
|
sinkAllDevicesAliasMap.add(null);
|
|
sinkAliasDevices.add(publicDevices);
|
|
if (!context.mounted) return;
|
|
getOwnerDeviceState(context, publicDevices);
|
|
});
|
|
}
|
|
void getOwnerDeviceState(BuildContext context,List<DeviceWithAlias> allDevices) async {
|
|
// int notificationCount = 0;
|
|
Map<String, List<DeviceWithAlias>> ownerDevicesStatus = {};
|
|
List<String> ownerDevicesState = [];
|
|
|
|
if (!context.mounted) return;
|
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
|
|
int count = 0;
|
|
for (var device in allDevices) {
|
|
// if (device.isOwner != true) continue;
|
|
|
|
if (!context.mounted) return;
|
|
Map<String, dynamic> sensorMap = DeviceUtils.instance
|
|
.getDeviceSensors(context, device.status?.sensors ?? []);
|
|
|
|
if (device.state == 1 || device.state == 3) {
|
|
ownerDevicesStatus["state"] ??= [];
|
|
ownerDevicesStatus["state"]!.add(device);
|
|
if (!context.mounted) return;
|
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
count++;
|
|
}
|
|
|
|
final noDataMessage = appLocalization(context).no_data_message;
|
|
if (sensorMap['sensorBattery'] != noDataMessage) {
|
|
if (double.parse(sensorMap['sensorBattery']) <= 20) {
|
|
ownerDevicesStatus['battery'] ??= [];
|
|
ownerDevicesStatus['battery']!.add(device);
|
|
if (!context.mounted) return;
|
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
if (!context.mounted) return;
|
|
sinkCountNotification.add(count);
|
|
}
|
|
|
|
void getDeviceStatusAliasMap(List<DeviceWithAlias> devices) {
|
|
Map<String, List<DeviceWithAlias>> allDevicesAliasMap = {};
|
|
for (var key in ['all', 'online', 'offline', 'warning', 'not-use']) {
|
|
allDevicesAliasMap[key] = [];
|
|
}
|
|
|
|
for (DeviceWithAlias device in devices) {
|
|
allDevicesAliasMap['all']!.add(device);
|
|
if (device.state == 0 || device.state == 1) {
|
|
allDevicesAliasMap['online']!.add(device);
|
|
}
|
|
if (device.state == -1) {
|
|
allDevicesAliasMap['offline']!.add(device);
|
|
}
|
|
if (device.state == 1) {
|
|
allDevicesAliasMap['warning']!.add(device);
|
|
}
|
|
if (device.state == -2) {
|
|
allDevicesAliasMap['not-use']!.add(device);
|
|
}
|
|
|
|
}
|
|
sinkAllDevicesAliasMap.add(allDevicesAliasMap);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
void dispose() {
|
|
allDevicesAliasMap.close();
|
|
// allDevicesAliasJoinedMap.close();
|
|
countNotification.close();
|
|
hasJoinedDevice.close();
|
|
ownerDevicesStatus.close();
|
|
aliasDevices.close();
|
|
}
|
|
|
|
|
|
}
|