Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Fix slider, send less request to platform to not overload it #98

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/widgets/userspace_widgets/ic_blc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class _IcBlcState extends State<IcBlc> {
// If request made on the slider apply it after a small timer
bool isRequestingPower = false;
Timer? _applyPowerTimer;
List<double> powerRequests = [];

bool isRequestingCurrent = false;
Timer? _applyCurrentTimer;
List<double> currentRequests = [];

///
///
Expand Down Expand Up @@ -116,10 +118,22 @@ class _IcBlcState extends State<IcBlc> {
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;
}
Expand Down Expand Up @@ -161,6 +175,14 @@ class _IcBlcState extends State<IcBlc> {
_currentValueEff = field.value;
}

if (currentRequests.isNotEmpty) {
currentRequests.removeAt(0);
}

if (currentRequests.isEmpty) {
_currentValueEff = field.value.toDouble();
}

if (sync) {
_currentValueReq = _currentValueEff;
}
Expand Down Expand Up @@ -251,10 +273,11 @@ class _IcBlcState extends State<IcBlc> {

// 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);

Expand All @@ -271,10 +294,11 @@ class _IcBlcState extends State<IcBlc> {

// 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);

Expand Down
71 changes: 51 additions & 20 deletions lib/widgets/userspace_widgets/ic_bpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ class _IcBpcState extends State<IcBpc> {
// If request made on the slider apply it after a small timer
bool isRequestingVoltage = false;
Timer? _applyVoltageTimer;
List<double> voltageRequests = [];

bool isRequestingCurrent = false;
Timer? _applyCurrentTimer;
List<double> currentRequests = [];

///
///
Expand Down Expand Up @@ -73,13 +75,24 @@ class _IcBpcState extends State<IcBpc> {

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;
}

Expand All @@ -101,12 +114,24 @@ class _IcBpcState extends State<IcBpc> {

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;
}

Expand Down Expand Up @@ -191,10 +216,11 @@ class _IcBpcState extends State<IcBpc> {

// 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);

Expand All @@ -211,10 +237,11 @@ class _IcBpcState extends State<IcBpc> {

// 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);

Expand Down Expand Up @@ -265,7 +292,7 @@ class _IcBpcState extends State<IcBpc> {
children: [
Column(
children: [
cardHeadLine(widget._interfaceConnection),
cardHeadLine2(widget._interfaceConnection),
],
),
const Spacer(),
Expand Down Expand Up @@ -304,6 +331,7 @@ class _IcBpcState extends State<IcBpc> {
onChanged: (value) {
setState(() {
_voltageValueReq = value;
applyVoltageCurrentRequest();
});
},
min: _voltageMin,
Expand All @@ -320,6 +348,7 @@ class _IcBpcState extends State<IcBpc> {
onChanged: (value) {
setState(() {
_currentValueReq = value;
applyVoltageCurrentRequest();
});
},
min: _currentMin,
Expand All @@ -330,14 +359,16 @@ class _IcBpcState extends State<IcBpc> {
} 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
),
)
]
)
]));
);
}
}
}