From ebcd89d2a5a85a64057913ada4bb8084872ddd84 Mon Sep 17 00:00:00 2001 From: Faris Armoush Date: Mon, 14 Apr 2025 16:24:50 +0300 Subject: [PATCH] Refactor ceiling sensor functions and update slider helper mappings for improved value handling --- .../ceiling_presence_sensor_functions.dart | 26 +++++---- .../ceiling_sensor/ceiling_sensor_dialog.dart | 53 +++++++++++++++++-- .../cps_dialog_slider_selector.dart | 18 ------- .../ceiling_sensor/cps_slider_helpers.dart | 10 ++-- 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/lib/pages/routines/models/ceiling_presence_sensor_functions.dart b/lib/pages/routines/models/ceiling_presence_sensor_functions.dart index bdec76d3..6dbe5cf6 100644 --- a/lib/pages/routines/models/ceiling_presence_sensor_functions.dart +++ b/lib/pages/routines/models/ceiling_presence_sensor_functions.dart @@ -1,7 +1,6 @@ import 'package:syncrow_web/pages/routines/models/device_functions.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; -// TODO: functions in conditions / status in actions class CpsOperationalValue { final String icon; final String description; @@ -223,7 +222,6 @@ final class CpsSpatialMotionValueFunction extends CpsFunctions { } final class CpsMaxDistanceOfDetectionFunction extends CpsFunctions { - // confirm with BE CpsMaxDistanceOfDetectionFunction({ required super.deviceId, required super.deviceName, @@ -267,7 +265,7 @@ final class CpsMaxDistanceOfStaticDetectionFunction extends CpsFunctions { max = 10.0, step = 0.5, super( - code: 'static_max_dis', // 0 / 500 + code: 'static_max_dis', operationName: 'Maximum Distance Of Static Detection', icon: Assets.currentDistanceIcon, ); @@ -302,7 +300,7 @@ final class CpsDetectionRangeFunction extends CpsFunctions { max = 25.5, step = 0.1, super( - code: 'moving_range', // just then + code: 'moving_range', operationName: 'Detection Range', icon: Assets.farDetection, ); @@ -372,7 +370,7 @@ final class CpsPresenceJudgementThrsholdFunction extends CpsFunctions { max = 255, step = 5, super( - code: 'presence_reference', // max 255 // change widget + code: 'presence_reference', operationName: 'Presence Judgement Threshold', icon: Assets.presenceJudgementThrshold, ); @@ -403,7 +401,7 @@ final class CpsMotionAmplitudeTriggerThresholdFunction extends CpsFunctions { max = 255, step = 5, super( - code: 'moving_reference', // max 255 // change widget + code: 'moving_reference', operationName: 'Motion Amplitude Trigger Threshold', icon: Assets.presenceJudgementThrshold, ); @@ -434,7 +432,7 @@ final class CpsPerpetualBoundaryFunction extends CpsFunctions { max = 5.00, step = 0.50, super( - code: 'perceptual_boundary', // 0 / 500 + code: 'perceptual_boundary', operationName: 'Perpetual Boundary', icon: Assets.boundary, ); @@ -469,7 +467,7 @@ final class CpsMotionTriggerBoundaryFunction extends CpsFunctions { max = 5.0, step = 0.5, super( - code: 'moving_boundary', // 0 / 500 / step 50 + code: 'moving_boundary', operationName: 'Motion Trigger Boundary', icon: Assets.motionMeter, ); @@ -504,7 +502,7 @@ final class CpsMotionTriggerTimeFunction extends CpsFunctions { max = 2.0, step = 0.1, super( - code: 'moving_rigger_time', // 0 / 2000 steps 10 + code: 'moving_rigger_time', operationName: 'Motion Trigger Time', icon: Assets.motionMeter, ); @@ -539,7 +537,7 @@ final class CpsMotionToStaticTimeFunction extends CpsFunctions { max = 50.0, step = 1.0, super( - code: 'moving_static_time', // 0 / 6000 steps 100 + code: 'moving_static_time', operationName: 'Motion To Static Time', icon: Assets.motionMeter, ); @@ -574,7 +572,7 @@ final class CpsEnteringNoBodyStateTimeFunction extends CpsFunctions { max = 300.0, step = 5.0, super( - code: 'none_body_time', // 0 / 300000 / steps 500 + code: 'none_body_time', operationName: 'Entering Nobody State Time', icon: Assets.motionMeter, ); @@ -606,7 +604,7 @@ final class CpsSelfTestResultFunctions extends CpsFunctions { required super.deviceName, required super.type, }) : super( - code: 'checking_result', // just in action + code: 'checking_result', operationName: 'Self-Test Result', icon: Assets.selfTestResult, ); @@ -828,7 +826,7 @@ class CpsPresenceStatusFunctions extends CpsFunctions { required super.deviceName, required super.type, }) : super( - code: 'presence_state', // just in action + code: 'presence_state', operationName: 'Presence Status', icon: Assets.presenceSensor, ); @@ -864,7 +862,7 @@ final class CpsSportsParaFunction extends CpsFunctions { max = 100, step = 1, super( - code: 'sports_para', // just in action + code: 'sports_para', operationName: 'Sports Para', icon: Assets.sportsPara, ); diff --git a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart index 1403b2ca..52665dfa 100644 --- a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart @@ -11,6 +11,7 @@ import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_senso import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_slider_selector.dart'; import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_value_selector.dart'; import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_functions_list.dart'; +import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart'; class CeilingSensorDialog extends StatefulWidget { const CeilingSensorDialog({ @@ -76,7 +77,8 @@ class _CeilingSensorDialogState extends State { ? () { context.read().add( AddFunctionToRoutine( - state.addedFunctions, + _updateValuesForAddedFunctions( + state.addedFunctions), '${widget.uniqueCustomId}', ), ); @@ -150,15 +152,58 @@ class _CeilingSensorDialogState extends State { ], ); } -List updateValuesForAddedFunctions(List addedFunctions) { + + List _updateValuesForAddedFunctions( + List addedFunctions, + ) { + const mappableSteppedFunctions = { + 'static_max_dis', + 'presence_reference', + 'moving_reference', + 'perceptual_boundary', + 'moving_boundary', + 'moving_rigger_time', + 'moving_static_time', + 'none_body_time', + 'moving_max_dis', + }; return addedFunctions.map((function) { - if (function.functionCode == 'sensitivity') { - return function.copyWith( + if (mappableSteppedFunctions.contains(function.functionCode)) { + final mappedValue = mapSteppedValue( value: function.value, + inputStep: CpsSliderHelpers.dividendOfRange(function.functionCode), + inputRange: CpsSliderHelpers.sliderRange(function.functionCode), + outputRange: CpsSliderHelpers.mappedRange(function.functionCode), + ); + return DeviceFunctionData( + value: mappedValue, + entityId: function.entityId, + functionCode: function.functionCode, + operationName: function.operationName, condition: function.condition, + actionExecutor: function.actionExecutor, + valueDescription: function.valueDescription, ); } return function; }).toList(); } + + int mapSteppedValue({ + required (double min, double max) inputRange, + required double inputStep, + required (double min, double max, double dividend) outputRange, + required double value, + }) { + final (inputMin, inputMax) = inputRange; + final (outputMin, outputMax, outputStep) = outputRange; + + final clampedValue = value.clamp(inputMin, inputMax); + + final stepsFromMin = ((clampedValue - inputMin) / inputStep).round(); + + final mappedValue = outputMin + (stepsFromMin * outputStep); + + return mappedValue.clamp(outputMin, outputMax).round(); + } } diff --git a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_slider_selector.dart b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_slider_selector.dart index 2bdd1135..1c549d9f 100644 --- a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_slider_selector.dart +++ b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_dialog_slider_selector.dart @@ -66,22 +66,4 @@ class CpsDialogSliderSelector extends StatelessWidget { ), ); } - - double reverseMappedValue(double value) { - final (inputMin, inputMax) = - CpsSliderHelpers.sliderRange(selectedFunctionData.functionCode); - final (outputMin, outputMax, outputStep) = CpsSliderHelpers.mappedRange( - selectedFunctionData.functionCode, - ); - - final clampedValue = value.clamp(outputMin, outputMax); - - final stepsFromMin = ((clampedValue - outputMin) / outputStep).round(); - - final mappedValue = inputMin + - (stepsFromMin * - CpsSliderHelpers.dividendOfRange(selectedFunctionData.functionCode)); - - return mappedValue.clamp(inputMin, inputMax); - } } diff --git a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart index 80192056..03b7b5af 100644 --- a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart +++ b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart @@ -4,13 +4,14 @@ abstract final class CpsSliderHelpers { final defaultDivdidend = dividendOfRange(functionCode); return switch (functionCode) { 'static_max_dis' => (0, 500, 50), - 'presence_reference' => (0, 255, 50), + 'presence_reference' => (0, 255, 5), 'moving_reference' => (0, 255, 5), 'perceptual_boundary' => (0, 500, 50), 'moving_boundary' => (0, 500, 50), 'moving_rigger_time' => (0, 2000, 100), - 'moving_static_time' => (0, 6000, 100), - 'none_body_time' => (0, 300000, 500), + 'moving_static_time' => (0, 60000, 1000), + 'none_body_time' => (0, 300000, 5000), + 'moving_max_dis' => (0, 500, 50), _ => (defaultMin, defaultMax, defaultDivdidend), }; } @@ -83,4 +84,7 @@ abstract final class CpsSliderHelpers { _ => '${parsedValue?.toStringAsFixed(0) ?? 0}', }; } + // TODO: Sports Para causes 400 in IF and THEN / sports_para + // TODO: Detection range causes 400 in IF / moving_range + // TODO: Distance of moving objects causes 400 in IF / presence_range }