diff --git a/lib/forms/add_bench_to_config_form.dart b/lib/forms/add_bench_to_config_form.dart index 557f732..0a5bbfe 100644 --- a/lib/forms/add_bench_to_config_form.dart +++ b/lib/forms/add_bench_to_config_form.dart @@ -52,7 +52,7 @@ class AddBenchToConfigForm extends StatelessWidget { }, // Show error message if unsuccessful connection style: ButtonStyle( - backgroundColor: WidgetStateProperty.all(blue) + backgroundColor: MaterialStateProperty.all(blue) ), child: Text( 'SAVE', diff --git a/lib/utils/utils_functions.dart b/lib/utils/utils_functions.dart index 689e651..2278187 100644 --- a/lib/utils/utils_functions.dart +++ b/lib/utils/utils_functions.dart @@ -276,4 +276,15 @@ Future checkIfConnectionValid(BuildContext context, String name, String ho } return true; +} + +// Convert dynamic to double if possible +double valueToDouble(dynamic value) { + switch (value.runtimeType) { + case int: + return value.toDouble(); + case double: + return value; + } + return 0.0; } \ No newline at end of file diff --git a/lib/widgets/userspace_widgets/ic_blc.dart b/lib/widgets/userspace_widgets/ic_blc.dart index bad526e..5fd5e19 100644 --- a/lib/widgets/userspace_widgets/ic_blc.dart +++ b/lib/widgets/userspace_widgets/ic_blc.dart @@ -1,11 +1,12 @@ import 'dart:async'; +import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:panduza_sandbox_flutter/utils/const.dart'; import 'templates.dart'; import '../../utils/utils_objects/interface_connection.dart'; +import 'package:panduza_sandbox_flutter/utils/utils_functions.dart'; -import 'dart:convert'; // import '../widgets/interface_control/icw_bpc.dart'; import 'package:mqtt_client/mqtt_client.dart'; @@ -20,16 +21,6 @@ class IcBlc extends StatefulWidget { } -double value_to_double(dynamic value) { - switch (value.runtimeType) { - case int: - return value.toDouble(); - case double: - return value; - } - return 0.0; -} - class _IcBlcState extends State { bool? _enableValueReq; bool? _enableValueEff; @@ -68,8 +59,8 @@ class _IcBlcState extends State { setState(() { for (MapEntry atts in jsonObject.entries) { for (MapEntry field in atts.value.entries) { - switch (atts.key) { + case "mode": if (field.key == "value") { bool sync = false; @@ -110,8 +101,6 @@ class _IcBlcState extends State { sync = true; } - - switch (field.value.runtimeType) { case int: _powerValueEff = field.value.toDouble(); @@ -124,10 +113,10 @@ class _IcBlcState extends State { } if (field.key == "min") { - _powerMin = value_to_double(field.value); + _powerMin = valueToDouble(field.value); } if (field.key == "max") { - _powerMax = value_to_double(field.value); + _powerMax = valueToDouble(field.value); } if (field.key == "decimals") { switch (field.value.runtimeType) { @@ -161,10 +150,10 @@ class _IcBlcState extends State { } if (field.key == "min") { - _currentMin = value_to_double(field.value); + _currentMin = valueToDouble(field.value); } if (field.key == "max") { - _currentMax = value_to_double(field.value); + _currentMax = valueToDouble(field.value); } if (field.key == "decimals") { switch (field.value.runtimeType) { diff --git a/lib/widgets/userspace_widgets/ic_bpc.dart b/lib/widgets/userspace_widgets/ic_bpc.dart index 123c00d..e06adc7 100644 --- a/lib/widgets/userspace_widgets/ic_bpc.dart +++ b/lib/widgets/userspace_widgets/ic_bpc.dart @@ -1,12 +1,11 @@ import 'dart:async'; +import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:panduza_sandbox_flutter/utils/const.dart'; import 'package:panduza_sandbox_flutter/utils/utils_functions.dart'; +import 'package:panduza_sandbox_flutter/utils/utils_objects/interface_connection.dart'; import 'templates.dart'; -import '../../utils/utils_objects/interface_connection.dart'; - -import 'dart:convert'; // import '../widgets/interface_control/icw_bpc.dart'; import 'package:mqtt_client/mqtt_client.dart'; @@ -77,10 +76,10 @@ class _IcBpcState extends State { } if (field.key == "min") { - _voltageMin = value_to_double(field.value); + _voltageMin = valueToDouble(field.value); } if (field.key == "max") { - _voltageMax = value_to_double(field.value); + _voltageMax = valueToDouble(field.value); } if (field.key == "decimals") { switch (field.value.runtimeType) { @@ -104,10 +103,10 @@ class _IcBpcState extends State { } if (field.key == "min") { - _currentMin = value_to_double(field.value); + _currentMin = valueToDouble(field.value); } if (field.key == "max") { - _currentMax = value_to_double(field.value); + _currentMax = valueToDouble(field.value); } if (field.key == "decimals") { switch (field.value.runtimeType) { diff --git a/lib/widgets/userspace_widgets/ic_powermeter.dart b/lib/widgets/userspace_widgets/ic_powermeter.dart index 537ec3d..0941dbe 100644 --- a/lib/widgets/userspace_widgets/ic_powermeter.dart +++ b/lib/widgets/userspace_widgets/ic_powermeter.dart @@ -20,6 +20,7 @@ class IcPowermeter extends StatefulWidget { class _IcPowermeterState extends State { + int _measureDecimal = 5; double _value = 0; StreamSubscription>>? mqttSubscription; @@ -28,8 +29,8 @@ class _IcPowermeterState extends State { /// powermeter /// void onMqttMessage(List> c) { - print("============"); - print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} '); + // print("============"); + // print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} '); // if (c[0].topic.startsWith(widget._interfaceConnection.topic)) { @@ -41,12 +42,12 @@ class _IcPowermeterState extends State { var jsonObject = json.decode(pt); - print(jsonObject); + // print(jsonObject); setState(() { for (MapEntry atts in jsonObject.entries) { for (MapEntry field in atts.value.entries) { - print('${atts.key} ${field.key} => ${field.value}'); + // print('${atts.key} ${field.key} => ${field.value}'); switch (atts.key) { case "measure": @@ -62,6 +63,15 @@ class _IcPowermeterState extends State { _value = updateVal; }); } + + if (field.key == "decimals") { + switch (field.value.runtimeType) { + case int: + _measureDecimal = field.value; + case double: + _measureDecimal = (field.value as double).toInt(); + } + } break; } } @@ -115,7 +125,7 @@ class _IcPowermeterState extends State { children: [ cardHeadLine(widget._interfaceConnection), Text( - "${_value.toString()} W", + "${double.parse(_value.toStringAsFixed(_measureDecimal))} W", style: TextStyle( color: black ), diff --git a/lib/widgets/userspace_widgets/ic_thermometer.dart b/lib/widgets/userspace_widgets/ic_thermometer.dart index 058e286..96a0ed8 100644 --- a/lib/widgets/userspace_widgets/ic_thermometer.dart +++ b/lib/widgets/userspace_widgets/ic_thermometer.dart @@ -20,6 +20,7 @@ class IcThermometer extends StatefulWidget { class _IcThermometerState extends State { + int _measureDecimal = 1; double _value = 0; StreamSubscription>>? mqttSubscription; @@ -58,7 +59,12 @@ class _IcThermometerState extends State { } if (field.key == "decimals") { - + switch (field.value.runtimeType) { + case int: + _measureDecimal = field.value; + case double: + _measureDecimal = (field.value as double).toInt(); + } } break; } @@ -114,7 +120,7 @@ class _IcThermometerState extends State { children: [ cardHeadLine(widget._interfaceConnection), Text( - "${_value.toString()} °C", + "${double.parse(_value.toStringAsFixed(_measureDecimal))} °C", style: TextStyle( color: black ),