Implemented WPS device

This commit is contained in:
Abdullah Alassaf
2024-08-26 03:28:18 +03:00
parent d94ec25003
commit 929b72d11a
11 changed files with 561 additions and 58 deletions

View File

@ -3,22 +3,25 @@ import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/ac_device_control.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/living_room_switch/view/living_room_device_control.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/view/wall_sensor_conrtols.dart';
mixin RouteControlsBasedCode {
Widget routeControlsWidgets({required AllDevicesModel device}) {
switch (device.categoryName) {
case 'Switch':
switch (device.productType) {
case '3G':
return LivingRoomDeviceControl(
device: device,
);
case 'Gateway':
case 'GW':
return const SizedBox();
case 'Residential Lock PRO':
case 'DL':
return const SizedBox();
case 'Human Presence Sensor':
case 'WPS':
return WallSensorControls(device: device);
case 'CPS':
return const SizedBox();
case 'Thermostat':
return AcDeviceControl(device: device);
case 'AC':
return AcDeviceControl(device: device);
default:
return const SizedBox();
}

View File

@ -0,0 +1,87 @@
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/wall_sensor/bloc/event.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/state.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/model/wall_sensor_model.dart';
import 'package:syncrow_web/services/devices_mang_api.dart';
class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
final String deviceId;
late WallSensorModel deviceStatus;
WallSensorBloc({required this.deviceId}) : super(InitialState()) {
on<WallSensorInitialEvent>(_fetchCeilingSensorStatus);
on<ChangeIndicatorEvent>(_changeIndicator);
on<ChangeValueEvent>(_changeValue);
on<WallSensorUpdatedEvent>(_wallSensorUpdated);
}
void _fetchCeilingSensorStatus(
WallSensorInitialEvent event, Emitter<WallSensorState> emit) async {
emit(LoadingInitialState());
try {
var response = await DevicesManagementApi().getDeviceStatus(deviceId);
deviceStatus = WallSensorModel.fromJson(response.status);
emit(UpdateState(wallSensorModel: deviceStatus));
// _listenToChanges();
} catch (e) {
emit(FailedState(error: e.toString()));
return;
}
}
// _listenToChanges() {
// try {
// DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
// Stream<DatabaseEvent> stream = ref.onValue;
// stream.listen((DatabaseEvent event) {
// Map<dynamic, dynamic> usersMap = event.snapshot.value as Map<dynamic, dynamic>;
// List<StatusModel> statusList = [];
// usersMap['status'].forEach((element) {
// statusList.add(StatusModel(code: element['code'], value: element['value']));
// });
// deviceStatus = WallSensorModel.fromJson(statusList);
// add(WallSensorUpdatedEvent());
// });
// } catch (_) {}
// }
_wallSensorUpdated(WallSensorUpdatedEvent event, Emitter<WallSensorState> emit) {
emit(UpdateState(wallSensorModel: deviceStatus));
}
void _changeIndicator(ChangeIndicatorEvent event, Emitter<WallSensorState> emit) async {
emit(LoadingNewSate(wallSensorModel: deviceStatus));
try {
final response = await DevicesManagementApi()
.deviceControl(deviceId, Status(code: 'indicator', value: !event.value));
if (response) {
deviceStatus.indicator = !event.value;
}
} catch (_) {}
emit(UpdateState(wallSensorModel: deviceStatus));
}
void _changeValue(ChangeValueEvent event, Emitter<WallSensorState> emit) async {
emit(LoadingNewSate(wallSensorModel: deviceStatus));
try {
final response = await DevicesManagementApi()
.deviceControl(deviceId, Status(code: event.code, value: event.value));
if (response) {
if (event.code == 'far_detection') {
deviceStatus.farDetection = event.value;
} else if (event.code == 'motionless_sensitivity') {
deviceStatus.motionlessSensitivity = event.value;
} else if (event.code == 'motion_sensitivity_value') {
deviceStatus.motionSensitivity = event.value;
}
}
} catch (_) {}
emit(UpdateState(wallSensorModel: deviceStatus));
}
}

View File

@ -0,0 +1,31 @@
import 'package:equatable/equatable.dart';
abstract class WallSensorEvent extends Equatable {
const WallSensorEvent();
@override
List<Object> get props => [];
}
class WallSensorLoadingEvent extends WallSensorEvent {}
class WallSensorInitialEvent extends WallSensorEvent {}
class WallSensorUpdatedEvent extends WallSensorEvent {}
class ChangeIndicatorEvent extends WallSensorEvent {
final bool value;
const ChangeIndicatorEvent({required this.value});
@override
List<Object> get props => [value];
}
class ChangeValueEvent extends WallSensorEvent {
final int value;
final String code;
const ChangeValueEvent({required this.value, required this.code});
@override
List<Object> get props => [value, code];
}

View File

@ -0,0 +1,38 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/model/wall_sensor_model.dart';
class WallSensorState extends Equatable {
const WallSensorState();
@override
List<Object> get props => [];
}
class InitialState extends WallSensorState {}
class LoadingInitialState extends WallSensorState {}
class UpdateState extends WallSensorState {
final WallSensorModel wallSensorModel;
const UpdateState({required this.wallSensorModel});
@override
List<Object> get props => [wallSensorModel];
}
class LoadingNewSate extends WallSensorState {
final WallSensorModel wallSensorModel;
const LoadingNewSate({required this.wallSensorModel});
@override
List<Object> get props => [wallSensorModel];
}
class FailedState extends WallSensorState {
final String error;
const FailedState({required this.error});
@override
List<Object> get props => [error];
}

View File

@ -0,0 +1,63 @@
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
class WallSensorModel {
String presenceState;
int farDetection;
int presenceTime;
int motionSensitivity;
int motionlessSensitivity;
int currentDistance;
int illuminance;
bool indicator;
WallSensorModel({
required this.presenceState,
required this.farDetection,
required this.presenceTime,
required this.motionSensitivity,
required this.motionlessSensitivity,
required this.currentDistance,
required this.illuminance,
required this.indicator,
});
factory WallSensorModel.fromJson(List<Status> jsonList) {
late String _presenceState;
late int _farDetection;
late int _presenceTime;
late int _motionSensitivity;
late int _motionlessSensitivity;
late int _currentDistance;
late int _illuminance;
late bool _indicator;
for (int i = 0; i < jsonList.length; i++) {
if (jsonList[i].code == 'presence_state') {
_presenceState = jsonList[i].value ?? 'none';
} else if (jsonList[i].code == 'far_detection') {
_farDetection = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'presence_time') {
_presenceTime = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'motion_sensitivity_value') {
_motionSensitivity = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'motionless_sensitivity') {
_motionlessSensitivity = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'dis_current') {
_currentDistance = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'illuminance_value') {
_illuminance = jsonList[i].value ?? 0;
} else if (jsonList[i].code == 'indicator') {
_indicator = jsonList[i].value ?? false;
}
}
return WallSensorModel(
presenceState: _presenceState,
farDetection: _farDetection,
presenceTime: _presenceTime,
motionSensitivity: _motionSensitivity,
motionlessSensitivity: _motionlessSensitivity,
currentDistance: _currentDistance,
illuminance: _illuminance,
indicator: _indicator);
}
}

View File

@ -0,0 +1,96 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/bloc.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/event.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/state.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/view/widgets/presence_display_data.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/view/widgets/presence_status.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/view/widgets/presence_update_data.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
class WallSensorControls extends StatelessWidget with HelperResponsiveLayout {
const WallSensorControls({super.key, required this.device});
final AllDevicesModel device;
@override
Widget build(BuildContext context) {
final isLarge = isLargeScreenSize(context);
final isMedium = isMediumScreenSize(context);
return BlocProvider(
create: (context) => WallSensorBloc(deviceId: device.uuid!)..add(WallSensorInitialEvent()),
child: BlocBuilder<WallSensorBloc, WallSensorState>(
builder: (context, state) {
if (state is LoadingInitialState) {
return const Center(child: CircularProgressIndicator());
} else if (state is UpdateState) {
return GridView(
padding: const EdgeInsets.symmetric(horizontal: 50),
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: isLarge
? 3
: isMedium
? 2
: 1,
mainAxisExtent: 133,
crossAxisSpacing: 12,
mainAxisSpacing: 12,
),
children: [
PresenceState(
value: state.wallSensorModel.presenceState,
),
PresenceDisplayValue(
value: state.wallSensorModel.presenceTime.toString(),
postfix: 'min',
description: 'Presence Time',
),
PresenceDisplayValue(
value: state.wallSensorModel.currentDistance.toString(),
postfix: 'cm',
description: 'Current Distance',
),
PresenceDisplayValue(
value: state.wallSensorModel.illuminance.toString(),
postfix: 'Lux',
description: 'Illuminance Value',
),
PresenceUpdateData(
value: state.wallSensorModel.motionSensitivity.toDouble(),
title: 'Motion Detection Sensitivity:',
minValue: 1,
maxValue: 10,
action: (int value) {
context.read<WallSensorBloc>().add(
ChangeValueEvent(
code: 'motion_sensitivity_value',
value: value,
),
);
},
),
PresenceUpdateData(
value: state.wallSensorModel.motionlessSensitivity.toDouble(),
title: 'Motionless Detection Sensitivity:',
minValue: 1,
maxValue: 10,
action: (int value) => context.read<WallSensorBloc>().add(
ChangeValueEvent(
code: 'motionless_sensitivity',
value: value,
),
),
),
],
);
} else {
return const Center(child: Text('Error fetching status'));
}
},
),
);
}
}

View File

@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class PresenceDisplayValue extends StatelessWidget {
const PresenceDisplayValue(
{super.key, required this.value, required this.postfix, required this.description});
final String value;
final String postfix;
final String description;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: ColorsManager.greyColor.withOpacity(0.2),
border: Border.all(color: ColorsManager.boxDivider),
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
value,
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: ColorsManager.dialogBlueTitle,
fontSize: 40,
fontWeight: FontWeight.w400),
),
Text(
postfix,
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: ColorsManager.blackColor),
),
],
),
Text(
description,
style: Theme.of(context).textTheme.bodySmall!.copyWith(
color: ColorsManager.blackColor, fontWeight: FontWeight.w400, fontSize: 16),
),
],
),
);
}
}

View File

@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
class PresenceState extends StatelessWidget {
const PresenceState({
super.key,
required this.value,
});
final String value;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: ColorsManager.greyColor.withOpacity(0.2),
border: Border.all(color: ColorsManager.boxDivider),
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Status:',
style: Theme.of(context).textTheme.bodySmall!.copyWith(color: ColorsManager.blackColor),
),
const SizedBox(
height: 5,
),
SvgPicture.asset(
value.toLowerCase() == 'motion' ? Assets.sensorMotion : Assets.sensorPresence,
width: 20,
height: 20,
),
Text(
value,
style: Theme.of(context).textTheme.bodySmall!.copyWith(
color: ColorsManager.blackColor, fontWeight: FontWeight.w400, fontSize: 16),
),
],
),
);
}
}

View File

@ -0,0 +1,111 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/shared/increament_decreament.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class PresenceUpdateData extends StatefulWidget {
const PresenceUpdateData({
super.key,
required this.title,
required this.value,
required this.action,
required this.minValue,
required this.maxValue,
this.description,
});
final String title;
final double value;
final double minValue;
final double maxValue;
final Function action;
final String? description;
@override
State<PresenceUpdateData> createState() => _CurrentTempState();
}
class _CurrentTempState extends State<PresenceUpdateData> {
late double _adjustedValue;
Timer? _debounce;
@override
void initState() {
super.initState();
_adjustedValue = _initialAdjustedValue(widget.value);
}
double _initialAdjustedValue(dynamic value) {
if (value is int || value is double) {
return value;
} else {
throw ArgumentError('Invalid value type: Expected int or double');
}
}
void _onValueChanged(double newValue) {
if (_debounce?.isActive ?? false) {
_debounce?.cancel();
}
_debounce = Timer(const Duration(milliseconds: 500), () {
widget.action(newValue.toInt());
});
}
@override
void dispose() {
_debounce?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: ColorsManager.greyColor.withOpacity(0.2),
border: Border.all(color: ColorsManager.boxDivider),
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: ColorsManager.blackColor),
),
],
),
IncrementDecrementWidget(
value: widget.value.toString(),
description: widget.description ?? '',
descriptionColor: ColorsManager.dialogBlueTitle,
onIncrement: () {
if (_adjustedValue < widget.maxValue) {
return;
}
setState(() {
_adjustedValue++;
});
_onValueChanged(_adjustedValue);
},
onDecrement: () {
if (_adjustedValue > widget.minValue) {
return;
}
setState(() {
_adjustedValue--;
});
_onValueChanged(_adjustedValue);
}),
],
),
);
}
}