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

Commit

Permalink
Add value_to_double function
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Albisson authored and Damien Albisson committed Jun 17, 2024
1 parent 3e4a3c0 commit 858cf96
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/forms/add_bench_to_config_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AddBenchToConfigForm extends StatelessWidget {
},
// Show error message if unsuccessful connection
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all<Color>(blue)
backgroundColor: MaterialStateProperty.all<Color>(blue)
),
child: Text(
'SAVE',
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/utils_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,15 @@ Future<bool> 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;
}
25 changes: 7 additions & 18 deletions lib/widgets/userspace_widgets/ic_blc.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<IcBlc> {
bool? _enableValueReq;
bool? _enableValueEff;
Expand Down Expand Up @@ -68,8 +59,8 @@ class _IcBlcState extends State<IcBlc> {
setState(() {
for (MapEntry<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> field in atts.value.entries) {

switch (atts.key) {

case "mode":
if (field.key == "value") {
bool sync = false;
Expand Down Expand Up @@ -110,8 +101,6 @@ class _IcBlcState extends State<IcBlc> {
sync = true;
}



switch (field.value.runtimeType) {
case int:
_powerValueEff = field.value.toDouble();
Expand All @@ -124,10 +113,10 @@ class _IcBlcState extends State<IcBlc> {
}

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) {
Expand Down Expand Up @@ -161,10 +150,10 @@ class _IcBlcState extends State<IcBlc> {
}

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) {
Expand Down
13 changes: 6 additions & 7 deletions lib/widgets/userspace_widgets/ic_bpc.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -77,10 +76,10 @@ class _IcBpcState extends State<IcBpc> {
}

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) {
Expand All @@ -104,10 +103,10 @@ class _IcBpcState extends State<IcBpc> {
}

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) {
Expand Down
20 changes: 15 additions & 5 deletions lib/widgets/userspace_widgets/ic_powermeter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class IcPowermeter extends StatefulWidget {

class _IcPowermeterState extends State<IcPowermeter> {

int _measureDecimal = 5;
double _value = 0;

StreamSubscription<List<MqttReceivedMessage<MqttMessage>>>? mqttSubscription;
Expand All @@ -28,8 +29,8 @@ class _IcPowermeterState extends State<IcPowermeter> {
/// powermeter
///
void onMqttMessage(List<MqttReceivedMessage<MqttMessage>> 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)) {
Expand All @@ -41,12 +42,12 @@ class _IcPowermeterState extends State<IcPowermeter> {

var jsonObject = json.decode(pt);

print(jsonObject);
// print(jsonObject);

setState(() {
for (MapEntry<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> field in atts.value.entries) {
print('${atts.key} ${field.key} => ${field.value}');
// print('${atts.key} ${field.key} => ${field.value}');

switch (atts.key) {
case "measure":
Expand All @@ -62,6 +63,15 @@ class _IcPowermeterState extends State<IcPowermeter> {
_value = updateVal;
});
}

if (field.key == "decimals") {
switch (field.value.runtimeType) {
case int:
_measureDecimal = field.value;
case double:
_measureDecimal = (field.value as double).toInt();
}
}
break;
}
}
Expand Down Expand Up @@ -115,7 +125,7 @@ class _IcPowermeterState extends State<IcPowermeter> {
children: [
cardHeadLine(widget._interfaceConnection),
Text(
"${_value.toString()} W",
"${double.parse(_value.toStringAsFixed(_measureDecimal))} W",
style: TextStyle(
color: black
),
Expand Down
10 changes: 8 additions & 2 deletions lib/widgets/userspace_widgets/ic_thermometer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class IcThermometer extends StatefulWidget {

class _IcThermometerState extends State<IcThermometer> {

int _measureDecimal = 1;
double _value = 0;

StreamSubscription<List<MqttReceivedMessage<MqttMessage>>>? mqttSubscription;
Expand Down Expand Up @@ -58,7 +59,12 @@ class _IcThermometerState extends State<IcThermometer> {
}

if (field.key == "decimals") {

switch (field.value.runtimeType) {
case int:
_measureDecimal = field.value;
case double:
_measureDecimal = (field.value as double).toInt();
}
}
break;
}
Expand Down Expand Up @@ -114,7 +120,7 @@ class _IcThermometerState extends State<IcThermometer> {
children: [
cardHeadLine(widget._interfaceConnection),
Text(
"${_value.toString()} °C",
"${double.parse(_value.toStringAsFixed(_measureDecimal))} °C",
style: TextStyle(
color: black
),
Expand Down

0 comments on commit 858cf96

Please sign in to comment.