// ignore_for_file: use_build_context_synchronously import 'dart:async'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import '../product/services/api_services.dart'; import '../product/utils/date_time_utils.dart'; import '../feature/device_log/device_logs_model.dart'; import '../feature/devices/device_model.dart'; import '../product/base/bloc/base_bloc.dart'; import '../product/utils/device_utils.dart'; class DetailDeviceBloc extends BlocBase { APIServices apiServices = APIServices(); final deviceInfo = StreamController.broadcast(); StreamSink get sinkDeviceInfo => deviceInfo.sink; Stream get streamDeviceInfo => deviceInfo.stream; final deviceSensor = StreamController>.broadcast(); StreamSink> get sinkDeviceSensor => deviceSensor.sink; Stream> get streamDeviceSensor => deviceSensor.stream; final deviceLocation = StreamController.broadcast(); StreamSink get sinkDeviceLocation => deviceLocation.sink; Stream get streamDeviceLocation => deviceLocation.stream; final sensorTemps = StreamController>.broadcast(); StreamSink> get sinkSensorTemps => sensorTemps.sink; Stream> get streamSensorTemps => sensorTemps.stream; @override void dispose() {} void getDeviceDetail( BuildContext context, String thingID, Completer controller, ) async { await apiServices.execute(context, () async { Device device = await apiServices.getDeviceInformation(thingID); sinkDeviceInfo.add(device); if (device.areaPath != null) { String fullLocation = await DeviceUtils.instance .getFullDeviceLocation(context, device.areaPath!, ""); log("Location: $fullLocation"); sinkDeviceLocation.add(fullLocation); } Map sensorMap = {}; if (device.status!.sensors != null) { sensorMap = DeviceUtils.instance .getDeviceSensors(context, device.status!.sensors!); } else { sensorMap = DeviceUtils.instance.getDeviceSensors(context, []); } sinkDeviceSensor.add(sensorMap); if (device.settings!.latitude! != "" && device.settings!.longitude! != "") { final CameraPosition cameraPosition = CameraPosition( target: LatLng( double.parse(device.settings!.latitude!), double.parse(device.settings!.longitude!), ), zoom: 13, ); final GoogleMapController mapController = await controller.future; mapController .animateCamera(CameraUpdate.newCameraPosition(cameraPosition)); } }); // try { // Device device = await apiServices.getDeviceInformation(thingID); // sinkDeviceInfo.add(device); // if (device.areaPath != null) { // String fullLocation = await DeviceUtils.instance // .getFullDeviceLocation(context, device.areaPath!, ""); // log("Location: $fullLocation"); // sinkDeviceLocation.add(fullLocation); // } // Map sensorMap = {}; // if (device.status!.sensors != null) { // sensorMap = DeviceUtils.instance // .getDeviceSensors(context, device.status!.sensors!); // } else { // sensorMap = DeviceUtils.instance.getDeviceSensors(context, []); // } // sinkDeviceSensor.add(sensorMap); // if (device.settings!.latitude! != "" && // device.settings!.longitude! != "") { // final CameraPosition cameraPosition = CameraPosition( // target: LatLng( // double.parse(device.settings!.latitude!), // double.parse(device.settings!.longitude!), // ), // zoom: 13, // ); // final GoogleMapController mapController = await controller.future; // mapController // .animateCamera(CameraUpdate.newCameraPosition(cameraPosition)); // } // } catch (e) { // if (!context.mounted) return; // showErrorTopSnackBarCustom(context, e.toString()); // } } void findLocation(BuildContext context, String areaPath) async { String fullLocation = await DeviceUtils.instance.getFullDeviceLocation(context, areaPath, ""); sinkDeviceLocation.add(fullLocation); } void getNearerSensorValue(BuildContext context, String thingID) async { apiServices.execute(context, () async { List sensorTemps = []; DateTime twoDaysAgo = DateTime.now().subtract(const Duration(days: 2)); String from = DateTimeUtils.instance.formatDateTimeToString(twoDaysAgo); String now = DateTimeUtils.instance.formatDateTimeToString(DateTime.now()); Map params = { 'thing_id': thingID, 'from': from, 'to': now, 'limit': '100', 'n': '7', }; DeviceLog devicesListLog = await apiServices.getLogsOfDevice(thingID, params); if (devicesListLog.sensors!.isNotEmpty) { for (var sensor in devicesListLog.sensors!) { sensorTemps.add(sensor); } sensorTemps = sensorTemps.reversed.toList(); sinkSensorTemps.add(sensorTemps); } else { sinkSensorTemps.add([]); } }); // try { // List sensorTemps = []; // DateTime twoDaysAgo = DateTime.now().subtract(const Duration(days: 2)); // String from = DateTimeUtils.instance.formatDateTimeToString(twoDaysAgo); // String now = // DateTimeUtils.instance.formatDateTimeToString(DateTime.now()); // Map params = { // 'thing_id': thingID, // 'from': from, // 'to': now, // 'limit': '100', // 'n': '7', // }; // DeviceLog devicesListLog = // await apiServices.getLogsOfDevice(thingID, params); // if (devicesListLog.sensors!.isNotEmpty) { // for (var sensor in devicesListLog.sensors!) { // sensorTemps.add(sensor); // } // sensorTemps = sensorTemps.reversed.toList(); // sinkSensorTemps.add(sensorTemps); // } else { // sinkSensorTemps.add([]); // } // } catch (e) { // if (!context.mounted) return; // showErrorTopSnackBarCustom(context, e.toString()); // sinkSensorTemps.add([]); // } } }