diff --git a/lib/widgets/userspace_widgets/ic_blc.dart b/lib/widgets/userspace_widgets/ic_blc.dart index 31cb759..94f4d92 100644 --- a/lib/widgets/userspace_widgets/ic_blc.dart +++ b/lib/widgets/userspace_widgets/ic_blc.dart @@ -47,9 +47,11 @@ class _IcBlcState extends State { // If request made on the slider apply it after a small timer bool isRequestingPower = false; Timer? _applyPowerTimer; + List powerRequests = []; bool isRequestingCurrent = false; Timer? _applyCurrentTimer; + List currentRequests = []; /// /// @@ -116,10 +118,22 @@ class _IcBlcState extends State { case double: _powerValueEff = field.value; } - if (_powerMax != null) { + if (_powerMax != null && _powerPercentageReq == null) { _powerPercentageReq = _powerValueEff! / _powerMax! * 100; _powerPercentageEff = _powerValueEff! / _powerMax! * 100; } + + // Remove the request who has been accepted + if (powerRequests.isNotEmpty) { + powerRequests.removeAt(0); + } + + if (powerRequests.isEmpty) { + _powerValueEff = field.value.toDouble(); + _powerPercentageReq = _powerValueEff! / _powerMax! * 100; + _powerPercentageEff = _powerValueEff! / _powerMax! * 100; + } + if (sync) { _powerValueReq = _powerValueEff; } @@ -161,6 +175,14 @@ class _IcBlcState extends State { _currentValueEff = field.value; } + if (currentRequests.isNotEmpty) { + currentRequests.removeAt(0); + } + + if (currentRequests.isEmpty) { + _currentValueEff = field.value.toDouble(); + } + if (sync) { _currentValueReq = _currentValueEff; } @@ -251,10 +273,11 @@ class _IcBlcState extends State { // Send power request to broker after 50 milliseconds to not // send request while at each tick of the slider - _applyCurrentTimer = Timer(const Duration(milliseconds: 50), () { + _applyCurrentTimer = Timer(const Duration(milliseconds: 200), () { // Transform percentage in value double powerValue = (1/100) * _powerPercentageReq! * _powerMax!; + powerRequests.add(powerValue); basicSendingMqttRequest("power", "value", powerValue, widget._interfaceConnection); @@ -271,10 +294,11 @@ class _IcBlcState extends State { // Send current request to broker after 50 milliseconds to not // send request while at each tick of the slider - _applyCurrentTimer = Timer(const Duration(milliseconds: 50), () { + _applyCurrentTimer = Timer(const Duration(milliseconds: 200), () { // Send a current value approx to decimal attribute _currentValueReq = num.parse(_currentValueReq!.toStringAsFixed(_currentDecimals)).toDouble(); + currentRequests.add(_currentValueReq!); basicSendingMqttRequest("current", "value", _currentValueReq!, widget._interfaceConnection); diff --git a/lib/widgets/userspace_widgets/ic_bpc.dart b/lib/widgets/userspace_widgets/ic_bpc.dart index addff72..d8b414d 100644 --- a/lib/widgets/userspace_widgets/ic_bpc.dart +++ b/lib/widgets/userspace_widgets/ic_bpc.dart @@ -42,9 +42,11 @@ class _IcBpcState extends State { // If request made on the slider apply it after a small timer bool isRequestingVoltage = false; Timer? _applyVoltageTimer; + List voltageRequests = []; bool isRequestingCurrent = false; Timer? _applyCurrentTimer; + List currentRequests = []; /// /// @@ -73,13 +75,24 @@ class _IcBpcState extends State { case "voltage": if (field.key == "value") { - switch (field.value.runtimeType) { - case int: - _voltageValueEff = field.value.toDouble(); - case double: - _voltageValueEff = field.value; + if (_voltageValueEff == null) { + switch (field.value.runtimeType) { + case int: + _voltageValueEff = field.value.toDouble(); + case double: + _voltageValueEff = field.value; + } + } + + // Remove the request who has been accepted + if (voltageRequests.isNotEmpty) { + voltageRequests.removeAt(0); } + if (voltageRequests.isEmpty) { + _voltageValueEff = field.value.toDouble(); + } + _voltageValueReq = _voltageValueEff; } @@ -101,12 +114,24 @@ class _IcBpcState extends State { case "current": if (field.key == "value") { - switch (field.value.runtimeType) { - case int: - _currentValueEff = field.value.toDouble(); - case double: - _currentValueEff = field.value; + if (_currentValueEff == null) { + switch (field.value.runtimeType) { + case int: + _currentValueEff = field.value.toDouble(); + case double: + _currentValueEff = field.value; + } } + + // Remove the request who has been accepted + if (currentRequests.isNotEmpty) { + currentRequests.removeAt(0); + } + + if (currentRequests.isEmpty) { + _currentValueEff = field.value.toDouble(); + } + _currentValueReq = _currentValueEff; } @@ -191,10 +216,11 @@ class _IcBpcState extends State { // Send voltage request to broker after 50 milliseconds to not // send request while at each tick of the slider - _applyVoltageTimer = Timer(const Duration(milliseconds: 50), () { + _applyVoltageTimer = Timer(const Duration(milliseconds: 200), () { // Send a voltage value approx to decimal attribute _voltageValueReq = num.parse(_voltageValueReq!.toStringAsFixed(_voltageDecimal)).toDouble(); + voltageRequests.add(_voltageValueReq!); basicSendingMqttRequest("voltage", "value", _voltageValueReq!, widget._interfaceConnection); @@ -211,10 +237,11 @@ class _IcBpcState extends State { // Send current request to broker after 50 milliseconds to not // send request while at each tick of the slider - _applyVoltageTimer = Timer(const Duration(milliseconds: 50), () { + _applyVoltageTimer = Timer(const Duration(milliseconds: 200), () { // Send a current value approx to decimal attribute _currentValueReq = num.parse(_currentValueReq!.toStringAsFixed(_currentDecimal)).toDouble(); + currentRequests.add(_currentValueReq!); basicSendingMqttRequest("current", "value", _currentValueReq!, widget._interfaceConnection); @@ -265,7 +292,7 @@ class _IcBpcState extends State { children: [ Column( children: [ - cardHeadLine(widget._interfaceConnection), + cardHeadLine2(widget._interfaceConnection), ], ), const Spacer(), @@ -304,6 +331,7 @@ class _IcBpcState extends State { onChanged: (value) { setState(() { _voltageValueReq = value; + applyVoltageCurrentRequest(); }); }, min: _voltageMin, @@ -320,6 +348,7 @@ class _IcBpcState extends State { onChanged: (value) { setState(() { _currentValueReq = value; + applyVoltageCurrentRequest(); }); }, min: _currentMin, @@ -330,14 +359,16 @@ class _IcBpcState extends State { } else { return Card( child: Column(children: [ - cardHeadLine(widget._interfaceConnection), - Text( - "Wait for data...", - style: TextStyle( - color: black - ), + cardHeadLine(widget._interfaceConnection), + Text( + "Wait for data...", + style: TextStyle( + color: black + ), + ) + ] ) - ])); + ); } } }