push sos basic device implementation

This commit is contained in:
ashrafzarkanisala
2024-10-31 11:32:34 +03:00
parent 13622c1dcf
commit 0fc709a5b1
10 changed files with 464 additions and 80 deletions

View File

@ -0,0 +1,74 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_reports.dart';
import 'package:syncrow_web/pages/device_managment/sos/models/sos_status_model.dart';
import 'package:syncrow_web/services/devices_mang_api.dart';
part 'sos_device_event.dart';
part 'sos_device_state.dart';
class SosDeviceBloc extends Bloc<SosDeviceEvent, SosDeviceState> {
SosDeviceBloc() : super(SosDeviceInitial()) {
on<GetDeviceStatus>(_getDeviceStatus);
on<GetBatchStatus>(_getBatchStatus);
on<GetDeviceRecords>(_getDeviceRecords);
on<GetDeviceAutomationRecords>(_getDeviceAutomationRecords);
on<BackToSosStatusView>(_backToSosStatusView);
}
late SosStatusModel deviceStatus;
FutureOr<void> _getDeviceStatus(GetDeviceStatus event, Emitter<SosDeviceState> emit) async {
emit(SosDeviceLoadingState());
try {
final status = await DevicesManagementApi().getDeviceStatus(event.uuid);
deviceStatus = SosStatusModel.fromJson(event.uuid, status.status);
emit(SosDeviceLoadedState(deviceStatus));
} catch (e) {
emit(SosDeviceErrorState(e.toString()));
}
}
FutureOr<void> _getBatchStatus(GetBatchStatus event, Emitter<SosDeviceState> emit) async {
emit(SosDeviceLoadingState());
try {
final status = await DevicesManagementApi().getBatchStatus(event.uuids);
deviceStatus = SosStatusModel.fromJson(event.uuids.first, status.status);
emit(SosDeviceLoadedState(deviceStatus));
} catch (e) {
emit(SosDeviceErrorState(e.toString()));
}
}
FutureOr<void> _getDeviceRecords(GetDeviceRecords event, Emitter<SosDeviceState> emit) async {
emit(SosReportLoadingState());
try {
final from = DateTime.now().subtract(const Duration(days: 30)).millisecondsSinceEpoch;
final to = DateTime.now().millisecondsSinceEpoch;
final DeviceReport records =
await DevicesManagementApi.getDeviceReportsByDate(event.uuid, 'sos', from.toString(), to.toString());
emit(SosReportLoadedState(records));
} catch (e) {
emit(SosReportErrorState(e.toString()));
}
}
FutureOr<void> _getDeviceAutomationRecords(GetDeviceAutomationRecords event, Emitter<SosDeviceState> emit) async {
emit(SosAutomationReportLoadingState());
try {
final from = DateTime.now().subtract(const Duration(days: 30)).millisecondsSinceEpoch;
final to = DateTime.now().millisecondsSinceEpoch;
final DeviceReport records = await DevicesManagementApi.getDeviceReportsByDate(
event.uuid, 'sos_automation', from.toString(), to.toString());
emit(SosAutomationReportLoadedState(records));
} catch (e) {
emit(SosAutomationReportErrorState(e.toString()));
}
}
FutureOr<void> _backToSosStatusView(BackToSosStatusView event, Emitter<SosDeviceState> emit) {
emit(SosDeviceLoadedState(deviceStatus));
}
}

View File

@ -0,0 +1,39 @@
part of 'sos_device_bloc.dart';
sealed class SosDeviceEvent extends Equatable {
const SosDeviceEvent();
}
class GetDeviceStatus extends SosDeviceEvent {
final String uuid;
const GetDeviceStatus(this.uuid);
@override
List<Object?> get props => [uuid];
}
class GetBatchStatus extends SosDeviceEvent {
final List<String> uuids;
const GetBatchStatus(this.uuids);
@override
List<Object?> get props => [uuids];
}
class GetDeviceRecords extends SosDeviceEvent {
final String uuid;
const GetDeviceRecords(this.uuid);
@override
List<Object?> get props => [uuid];
}
class GetDeviceAutomationRecords extends SosDeviceEvent {
final String uuid;
const GetDeviceAutomationRecords(this.uuid);
@override
List<Object?> get props => [uuid];
}
class BackToSosStatusView extends SosDeviceEvent {
@override
List<Object?> get props => [];
}

View File

@ -0,0 +1,82 @@
part of 'sos_device_bloc.dart';
sealed class SosDeviceState extends Equatable {
const SosDeviceState();
}
final class SosDeviceInitial extends SosDeviceState {
@override
List<Object> get props => [];
}
final class SosDeviceLoadingState extends SosDeviceState {
@override
List<Object> get props => [];
}
final class SosDeviceLoadedState extends SosDeviceState {
final SosStatusModel sosStatusModel;
const SosDeviceLoadedState(this.sosStatusModel);
@override
List<Object> get props => [sosStatusModel];
}
final class SosDeviceErrorState extends SosDeviceState {
final String message;
const SosDeviceErrorState(this.message);
@override
List<Object> get props => [message];
}
/// report state
final class SosReportLoadingState extends SosDeviceState {
@override
List<Object> get props => [];
}
final class SosReportLoadedState extends SosDeviceState {
final DeviceReport sosReport;
const SosReportLoadedState(this.sosReport);
@override
List<Object> get props => [sosReport];
}
final class SosReportErrorState extends SosDeviceState {
final String message;
const SosReportErrorState(this.message);
@override
List<Object> get props => [message];
}
/// automation reports
final class SosAutomationReportLoadingState extends SosDeviceState {
@override
List<Object> get props => [];
}
final class SosAutomationReportLoadedState extends SosDeviceState {
final DeviceReport automationReport;
const SosAutomationReportLoadedState(this.automationReport);
@override
List<Object> get props => [automationReport];
}
final class SosAutomationReportErrorState extends SosDeviceState {
final String message;
const SosAutomationReportErrorState(this.message);
@override
List<Object> get props => [message];
}