power_clamp_batch_control

This commit is contained in:
mohammad
2024-10-25 20:06:07 +03:00
parent b8ad20b3ff
commit a594d5b93c
9 changed files with 294 additions and 15 deletions

View File

@ -18,6 +18,7 @@ import 'package:syncrow_web/pages/device_managment/main_door_sensor/view/main_do
import 'package:syncrow_web/pages/device_managment/one_g_glass_switch/view/one_gang_glass_batch_control_view.dart';
import 'package:syncrow_web/pages/device_managment/one_gang_switch/view/wall_light_batch_control.dart';
import 'package:syncrow_web/pages/device_managment/one_gang_switch/view/wall_light_device_control.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/view/power_clamp_batch_control_view.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/view/smart_power_device_control.dart';
import 'package:syncrow_web/pages/device_managment/three_g_glass_switch/view/three_gang_glass_switch_batch_control_view.dart';
import 'package:syncrow_web/pages/device_managment/three_g_glass_switch/view/three_gang_glass_switch_control_view.dart';
@ -230,7 +231,7 @@ mixin RouteControlsBasedCode {
.toList(),
);
case 'PC':
return WaterLeakBatchControlView(
return PowerClampBatchControlView(
deviceIds: devices
.where((e) => (e.productType == 'PC'))
.map((e) => e.uuid!)

View File

@ -1,11 +1,14 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/bloc/smart_power_event.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/bloc/smart_power_state.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/device_event.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_batch_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/view/power_chart.dart';
import 'package:syncrow_web/services/devices_mang_api.dart';
@ -16,13 +19,15 @@ class SmartPowerBloc extends Bloc<SmartPowerEvent, SmartPowerState> {
on<SmartPowerArrowPressedEvent>(_onArrowPressed);
on<SmartPowerFetchBatchEvent>(_onFetchBatchStatus);
on<SmartPowerPageChangedEvent>(_onPageChanged);
on<SmartPowerBatchControl>(_onBatchControl);
on<PowerBatchControlEvent>(_onBatchControl);
on<FilterRecordsByDateEvent>(_filterRecordsByDate);
on<SelectDateEvent>(checkDayMonthYearSelected);
// SelectDateEvent
on<SmartPowerFactoryReset>(_onFactoryReset);
// FetchPowerClampBatchStatusEvent
}
late PowerClampModel deviceStatus;
late PowerClampBatchModel deviceBatchStatus;
final String deviceId;
Timer? _timer;
List<Map<String, dynamic>> phaseData = [];
@ -255,29 +260,117 @@ class SmartPowerBloc extends Bloc<SmartPowerEvent, SmartPowerState> {
currentPage = event.page;
emit(SmartPowerStatusLoaded(deviceStatus, currentPage));
}
Future<void> _onFactoryReset(
SmartPowerFactoryReset event, Emitter<SmartPowerState> emit) async {
emit(SmartPowerLoading());
try {
final response = await DevicesManagementApi().factoryReset(
event.factoryReset,
event.deviceId,
);
if (response) {
emit(SmartPowerInitial());
} else {
emit(SmartPowerError('Factory reset failed'));
}
} catch (e) {
emit(SmartPowerError(e.toString()));
}
}
Future<void> _onBatchControl(
PowerBatchControlEvent event, Emitter<SmartPowerState> emit) async {
final oldValue = deviceStatus.status;
_updateLocalValue(event.code, event.value);
// emit(WaterLeakBatchStatusLoadedState(deviceStatus!));
await _runDebounce(
deviceId: event.deviceIds,
code: event.code,
value: event.value,
oldValue: oldValue,
emit: emit,
isBatch: true,
);
}
Future<void> _onFetchBatchStatus(
SmartPowerFetchBatchEvent event, Emitter<SmartPowerState> emit) async {
emit(SmartPowerLoading());
try {
await DevicesManagementApi().getBatchStatus(event.devicesIds);
emit(SmartPowerStatusLoaded(deviceStatus, currentPage));
final response =
await DevicesManagementApi().getPowerStatus(event.devicesIds);
PowerClampBatchModel deviceStatus =
PowerClampBatchModel.fromJson(response);
emit(SmartPowerLoadBatchControll(deviceStatus));
} catch (e) {
debugPrint('=========error====$e');
emit(SmartPowerError(e.toString()));
}
}
Future<void> _runDebounce({
required dynamic deviceId,
required String code,
required dynamic value,
required dynamic oldValue,
required Emitter<SmartPowerState> emit,
required bool isBatch,
}) async {
late String id;
if (deviceId is List) {
id = deviceId.first;
} else {
id = deviceId;
}
if (_timer != null) {
_timer!.cancel();
}
_timer = Timer(const Duration(milliseconds: 500), () async {
try {
late bool response;
if (isBatch) {
response = await DevicesManagementApi()
.deviceBatchControl(deviceId, code, value);
} else {
response = await DevicesManagementApi()
.deviceControl(deviceId, Status(code: code, value: value));
}
if (!response) {
_revertValueAndEmit(id, code, oldValue, emit);
}
} catch (e) {
_revertValueAndEmit(id, code, oldValue, emit);
}
});
}
void _updateLocalValue(String code, dynamic value) {
if (code == 'watersensor_state') {
deviceStatus = deviceStatus.copyWith(statusPower: value);
} else if (code == 'battery_percentage') {
deviceStatus = deviceStatus.copyWith(statusPower: value);
}
}
void _revertValueAndEmit(String deviceId, String code, dynamic oldValue,
Emitter<SmartPowerState> emit) {
_updateLocalValue(code, oldValue);
emit(SmartPowerLoadBatchControll(deviceBatchStatus));
}
@override
Future<void> close() {
_timer?.cancel();
return super.close();
}
FutureOr<void> _onBatchControl(
SmartPowerBatchControl event, Emitter<SmartPowerState> emit) async {
emit(SmartPowerStatusLoaded(deviceStatus, currentPage));
}
List<EventDevice> filteredRecords = [];
int currentIndex = 0;

View File

@ -49,11 +49,11 @@ class SmartPowerBatchControl extends SmartPowerEvent {
List<Object> get props => [devicesIds, code, value];
}
class WallLightFactoryReset extends SmartPowerEvent {
class SmartPowerFactoryReset extends SmartPowerEvent {
final String deviceId;
final FactoryResetModel factoryReset;
WallLightFactoryReset({required this.deviceId, required this.factoryReset});
SmartPowerFactoryReset({required this.deviceId, required this.factoryReset});
@override
List<Object> get props => [deviceId, factoryReset];
@ -91,3 +91,25 @@ class FilterRecordsByDateEvent extends SmartPowerEvent {
FilterRecordsByDateEvent(
{required this.selectedDate, required this.viewType});
}
class FetchPowerClampBatchStatusEvent extends SmartPowerEvent {
final List<String> deviceIds;
FetchPowerClampBatchStatusEvent(this.deviceIds);
@override
List<Object> get props => [deviceIds];
}class PowerBatchControlEvent extends SmartPowerEvent {
final List<String> deviceIds;
final String code;
final dynamic value;
PowerBatchControlEvent({
required this.deviceIds,
required this.code,
required this.value,
});
@override
List<Object> get props => [deviceIds, code, value];
}

View File

@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_batch_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/view/power_chart.dart';
@ -10,6 +11,16 @@ class SmartPowerState extends Equatable {
class SmartPowerInitial extends SmartPowerState {}
class SmartPowerLoading extends SmartPowerState {}
class SmartPowerLoadBatchControll extends SmartPowerState {
final PowerClampBatchModel status;
SmartPowerLoadBatchControll(this.status);
@override
List<Object> get props => [status];
}
class DateSelectedState extends SmartPowerState {}
class SmartPowerStatusLoaded extends SmartPowerState {
@ -18,7 +29,6 @@ class SmartPowerStatusLoaded extends SmartPowerState {
SmartPowerStatusLoaded(this.deviceStatus, this.currentPage);
}
class SmartPowerError extends SmartPowerState {
final String message;
@ -58,5 +68,5 @@ class SmartPowerBatchStatusLoaded extends SmartPowerState {
class FilterRecordsState extends SmartPowerState {
final List<EnergyData> filteredRecords;
FilterRecordsState({required this.filteredRecords});
FilterRecordsState({required this.filteredRecords});
}

View File

@ -0,0 +1,49 @@
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
abstract class PowerClampModel1 {
String get productUuid;
String get productType;
}
class PowerClampBatchModel extends PowerClampModel1 {
@override
final String productUuid;
@override
final String productType;
final List<Status> status;
PowerClampBatchModel({
required this.productUuid,
required this.productType,
required this.status,
});
factory PowerClampBatchModel.fromJson(Map<String, dynamic> json) {
String productUuid = json['productUuid'] ?? '';
String productType = json['productType'] ?? '';
List<Status> statusList = [];
if (json['status'] != null && json['status'] is List) {
statusList =
(json['status'] as List).map((e) => Status.fromJson(e)).toList();
}
return PowerClampBatchModel(
productUuid: productUuid,
productType: productType,
status: statusList,
);
}
PowerClampBatchModel copyWith({
String? productUuid,
String? productType,
List<Status>? status,
}) {
return PowerClampBatchModel(
productUuid: productUuid ?? this.productUuid,
productType: productType ?? this.productType,
status: status ?? this.status,
);
}
}

View File

@ -17,6 +17,18 @@ class PowerClampModel {
status: PowerStatus.fromJson(json['status']),
);
}
PowerClampModel copyWith({
String? productUuid,
String? productType,
PowerStatus? statusPower,
}) {
return PowerClampModel(
productUuid: productUuid ?? this.productUuid,
productType: productType ?? this.productType,
status: statusPower ?? this.status,
);
}
}
class PowerStatus {

View File

@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/bloc/smart_power_bloc.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/bloc/smart_power_event.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/bloc/smart_power_state.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_batch_model.dart';
import 'package:syncrow_web/pages/device_managment/power_clamp/models/power_clamp_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
import 'package:syncrow_web/pages/device_managment/shared/batch_control/firmware_update.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
class PowerClampBatchControlView extends StatelessWidget
with HelperResponsiveLayout {
final List<String> deviceIds;
const PowerClampBatchControlView({Key? key, required this.deviceIds})
: super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => SmartPowerBloc(deviceId: deviceIds.first)
..add(SmartPowerFetchBatchEvent(deviceIds)),
child: BlocBuilder<SmartPowerBloc, SmartPowerState>(
builder: (context, state) {
if (state is SmartPowerLoading) {
return const Center(child: CircularProgressIndicator());
} else if (state is SmartPowerLoadBatchControll) {
return _buildStatusControls(context, state.status);
} else if (state is SmartPowerError) {
return Center(child: Text('Error: ${state.message}'));
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
);
}
Widget _buildStatusControls(
BuildContext context, PowerClampBatchModel status) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 170,
height: 140,
child: FirmwareUpdateWidget(deviceId: deviceIds.first, version: 2)),
const SizedBox(
width: 12,
),
SizedBox(
width: 170,
height: 140,
child: FactoryResetWidget(
callFactoryReset: () {
context.read<SmartPowerBloc>().add(SmartPowerFactoryReset(
deviceId: deviceIds.first,
factoryReset: FactoryResetModel(devicesUuid: deviceIds)));
},
),
),
],
);
}
}

View File

@ -29,7 +29,7 @@ class SmartPowerDeviceControl extends StatelessWidget
if (state is SmartPowerLoading) {
return const Center(child: CircularProgressIndicator());
} else if (state is SmartPowerStatusLoaded) {
} else if (state is SmartPowerLoadBatchControll) {
return _buildStatusControls(
currentPage: _blocProvider.currentPage,
context: context,

View File

@ -195,6 +195,31 @@ class DevicesManagementApi {
}
}
getPowerStatus(List<String> uuids) async {
try {
final queryParameters = {
'devicesUuid': uuids.join(','),
};
final response = await HTTPService().get(
path: ApiEndpoints.getBatchStatus,
queryParameters: queryParameters,
showServerMessage: true,
expectedResponseModel: (json) {
print('object======$json');
return json;
},
);
return response;
} catch (e) {
debugPrint('Error fetching $e');
return DeviceStatus(
productUuid: '',
productType: '',
status: [],
);
}
}
Future<bool> addScheduleRecord(
ScheduleEntry sendSchedule, String uuid) async {
try {