mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 01:56:24 +00:00
connect reports to main door
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/main_door_sensor/bloc/main_door_sensor_event.dart';
|
||||
@ -63,16 +65,17 @@ class MainDoorSensorBloc
|
||||
|
||||
_timer = Timer(const Duration(milliseconds: 500), () async {
|
||||
try {
|
||||
final status = await DevicesManagementApi()
|
||||
final response = await DevicesManagementApi()
|
||||
.deviceControl(deviceId, Status(code: code, value: value));
|
||||
|
||||
if (!status) {
|
||||
if (!response) {
|
||||
_revertValueAndEmit(deviceId, code, oldValue, emit);
|
||||
}
|
||||
|
||||
emit(MainDoorSensorDeviceStatusLoaded(deviceStatus));
|
||||
} catch (e) {
|
||||
emit(MainDoorSensorFailedState(error: e.toString()));
|
||||
if (e is DioException && e.response != null) {
|
||||
debugPrint('Error response: ${e.response?.data}');
|
||||
}
|
||||
_revertValueAndEmit(deviceId, code, oldValue, emit);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -93,7 +96,6 @@ class MainDoorSensorBloc
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve the current value by code
|
||||
bool _getValueByCode(String code) {
|
||||
switch (code) {
|
||||
case 'doorcontact_state':
|
||||
@ -118,13 +120,12 @@ class MainDoorSensorBloc
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch reports related to the main door sensor
|
||||
FutureOr<void> _fetchReports(MainDoorSensorReportsEvent event,
|
||||
Emitter<MainDoorSensorState> emit) async {
|
||||
emit(MainDoorSensorLoadingState());
|
||||
try {
|
||||
final reports = await DevicesManagementApi.getDeviceReports(
|
||||
event.deviceId, event.code);
|
||||
event.deviceId, event.code, event.from, event.to);
|
||||
emit(MainDoorSensorReportLoaded(reports));
|
||||
} catch (e) {
|
||||
emit(MainDoorSensorFailedState(error: e.toString()));
|
||||
|
@ -50,6 +50,14 @@ class MainDoorSensorBatchControl extends MainDoorSensorEvent {
|
||||
class MainDoorSensorReportsEvent extends MainDoorSensorEvent {
|
||||
final String deviceId;
|
||||
final String code;
|
||||
final String from;
|
||||
final String to;
|
||||
@override
|
||||
List<Object> get props => [deviceId, code, from, to];
|
||||
|
||||
MainDoorSensorReportsEvent({required this.deviceId, required this.code});
|
||||
MainDoorSensorReportsEvent(
|
||||
{required this.deviceId,
|
||||
required this.code,
|
||||
required this.from,
|
||||
required this.to});
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import 'package:syncrow_web/pages/device_managment/main_door_sensor/bloc/main_do
|
||||
import 'package:syncrow_web/pages/device_managment/main_door_sensor/bloc/main_door_sensor_state.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/main_door_sensor/models/main_door_status_model.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/shared/device_controls_container.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/shared/table/report_table.dart';
|
||||
import 'package:syncrow_web/utils/color_manager.dart';
|
||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||
@ -25,10 +26,23 @@ class MainDoorSensorControlView extends StatelessWidget
|
||||
..add(MainDoorSensorFetchDeviceEvent(device.uuid!)),
|
||||
child: BlocBuilder<MainDoorSensorBloc, MainDoorSensorState>(
|
||||
builder: (context, state) {
|
||||
if (state is MainDoorSensorLoadingState) {
|
||||
if (state is MainDoorSensorLoadingState ||
|
||||
state is MainDoorSensorReportsLoadingState) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (state is MainDoorSensorDeviceStatusLoaded) {
|
||||
return _buildStatusControls(context, state.status);
|
||||
} else if (state is MainDoorSensorReportLoaded) {
|
||||
return ReportsTable(
|
||||
report: state.deviceReport,
|
||||
onRowTap: (index) {},
|
||||
onClose: () {
|
||||
context
|
||||
.read<MainDoorSensorBloc>()
|
||||
.add(MainDoorSensorFetchDeviceEvent(device.uuid!));
|
||||
},
|
||||
hideValueShowDescription: true,
|
||||
mainDoorSensor: true,
|
||||
);
|
||||
} else if (state is MainDoorSensorFailedState ||
|
||||
state is MainDoorSensorBatchFailedState) {
|
||||
return const Center(child: Text('Error fetching status'));
|
||||
@ -60,9 +74,15 @@ class MainDoorSensorControlView extends StatelessWidget
|
||||
),
|
||||
children: [
|
||||
IconNameStatusContainer(
|
||||
name: 'Open',
|
||||
name: status.doorContactState ? 'Open' : 'Close',
|
||||
icon: Assets.mainDoor,
|
||||
onTap: () {},
|
||||
onTap: () {
|
||||
context.read<MainDoorSensorBloc>().add(MainDoorSensorControl(
|
||||
deviceId: device.uuid!,
|
||||
code: 'doorcontact_state',
|
||||
value: !status.doorContactState,
|
||||
));
|
||||
},
|
||||
status: status.doorContactState,
|
||||
textColor: ColorsManager.red,
|
||||
paddingAmount: 8,
|
||||
@ -70,7 +90,20 @@ class MainDoorSensorControlView extends StatelessWidget
|
||||
IconNameStatusContainer(
|
||||
name: 'Open/Close\n Record',
|
||||
icon: Assets.mainDoorReports,
|
||||
onTap: () {},
|
||||
onTap: () {
|
||||
final from = DateTime.now()
|
||||
.subtract(const Duration(days: 30))
|
||||
.millisecondsSinceEpoch;
|
||||
final to = DateTime.now().millisecondsSinceEpoch;
|
||||
context.read<MainDoorSensorBloc>().add(
|
||||
MainDoorSensorReportsEvent(
|
||||
deviceId: device.uuid!,
|
||||
code: 'doorcontact_state',
|
||||
from: from.toString(),
|
||||
to: to.toString(),
|
||||
),
|
||||
);
|
||||
},
|
||||
status: false,
|
||||
textColor: ColorsManager.blackColor,
|
||||
),
|
||||
|
Reference in New Issue
Block a user