diff --git a/lib/after_setup_pages/userspace_page.dart b/lib/after_setup_pages/userspace_page.dart index 274c5cf..9c2106f 100644 --- a/lib/after_setup_pages/userspace_page.dart +++ b/lib/after_setup_pages/userspace_page.dart @@ -13,6 +13,7 @@ import 'package:panduza_sandbox_flutter/userspace_widgets/ic_not_managed.dart'; import 'package:panduza_sandbox_flutter/data/interface_connection.dart'; import 'package:panduza_sandbox_flutter/userspace_widgets/ic_relay.dart'; +import 'package:panduza_sandbox_flutter/userspace_widgets/ic_thermometer.dart'; import 'package:panduza_sandbox_flutter/utils_widgets/appBar.dart'; import 'package:panduza_sandbox_flutter/data/broker_connection_info.dart'; @@ -157,6 +158,8 @@ class _UserspacePageState extends State { return IcPlatform(ic); case "powermeter": return IcPowermeter(ic); + case "thermometer": + return IcThermometer(ic); case "relay": return IcRelay(ic); default: diff --git a/lib/userspace_widgets/ic_powermeter.dart b/lib/userspace_widgets/ic_powermeter.dart index 484c8b3..c4cd77e 100644 --- a/lib/userspace_widgets/ic_powermeter.dart +++ b/lib/userspace_widgets/ic_powermeter.dart @@ -40,34 +40,31 @@ class _IcPowermeterState extends State { var jsonObject = json.decode(pt); print(jsonObject); - - for (MapEntry atts in jsonObject.entries) { - for (MapEntry field in atts.value.entries) { - print('${atts.key} ${field.key} => ${field.value}'); - - switch (atts.key) { - case "measure": - if (field.key == "value") { - double updateVal = 0; - switch (field.value.runtimeType) { - case int: - updateVal = field.value.toDouble(); - case double: - updateVal = field.value; - } - setState(() { - _value = updateVal; - }); - } - - if (field.key == "decimals") { - } - break; + setState(() { + for (MapEntry atts in jsonObject.entries) { + for (MapEntry field in atts.value.entries) { + print('${atts.key} ${field.key} => ${field.value}'); + + switch (atts.key) { + case "measure": + if (field.key == "value") { + double update_val = 0; + switch (field.value.runtimeType) { + case int: + update_val = field.value.toDouble(); + case double: + update_val = field.value; + } + setState(() { + _value = update_val; + }); + } + break; + } } } - } - + }); } } else { // print('not good:'); diff --git a/lib/userspace_widgets/ic_thermometer.dart b/lib/userspace_widgets/ic_thermometer.dart new file mode 100644 index 0000000..e8cae01 --- /dev/null +++ b/lib/userspace_widgets/ic_thermometer.dart @@ -0,0 +1,158 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:panduza_sandbox_flutter/data/const.dart'; +import 'templates.dart'; +import '../../data/interface_connection.dart'; + +import 'dart:convert'; + +// import '../widgets/interface_control/icw_bpc.dart'; +import 'package:mqtt_client/mqtt_client.dart'; + +class IcThermometer extends StatefulWidget { + const IcThermometer(this._interfaceConnection, {super.key}); + + final InterfaceConnection _interfaceConnection; + + @override + State createState() => _IcThermometerState(); +} + +class _IcThermometerState extends State { + + double _value = 0; + + /// Init each value of the thermometer, here just the measure + /// temperature + /// + void onMqttMessage(List> c) { + print("============"); + print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} '); + + // + if (c[0].topic.startsWith(widget._interfaceConnection.topic)) { + if (!c[0].topic.endsWith('/info')) { + final recMess = c![0].payload as MqttPublishMessage; + + final pt = + MqttPublishPayload.bytesToStringAsString(recMess.payload.message); + + var jsonObject = json.decode(pt); + + print(jsonObject); + + for (MapEntry atts in jsonObject.entries) { + for (MapEntry field in atts.value.entries) { + print('${atts.key} ${field.key} => ${field.value}'); + + switch (atts.key) { + case "measure": + if (field.key == "value") { + double updateVal = 0; + switch (field.value.runtimeType) { + case int: + updateVal = field.value.toDouble(); + case double: + updateVal = field.value; + } + setState(() { + _value = updateVal; + }); + } + + if (field.key == "decimals") { + + } + break; + } + } + } + + } + } else { + // print('not good:'); + } + } + + /// Initialize MQTT Subscriptions + /// + void initializeMqttSubscription() async { + widget._interfaceConnection.client.updates!.listen(onMqttMessage); + + String attsTopic = "${widget._interfaceConnection.topic}/atts/#"; + // print(attsTopic); + Subscription? sub = widget._interfaceConnection.client + .subscribe(attsTopic, MqttQos.atLeastOnce); + + } + + late TextEditingController _freqController; + late TextEditingController _freqControllerRequested; + + /// Perform MQTT Subscriptions at the start of the component + /// + @override + void initState() { + super.initState(); + + // subscribe to info and atts ? + Future.delayed(const Duration(milliseconds: 1), initializeMqttSubscription); + + _freqController = TextEditingController(text: "1"); + } + + @override + void dispose() { + super.dispose(); + } + + /// Appearance of the widget + /// + @override + Widget build(BuildContext context) { + return Card( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + cardHeadLine(widget._interfaceConnection), + Text( + "${_value.toString()} °C", + style: TextStyle( + color: black + ), + ), + Row( + children: [ + SizedBox( + width: 100, + child: TextField( + textDirection: TextDirection.rtl, + controller: _freqController, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter.digitsOnly + ], + style: TextStyle( + color: black, + fontSize: 16 + ), + ) + ), + const SizedBox( + width: 10, + ), + Text( + "read per sec", + style: TextStyle( + color: black + ), + ), + ], + ), + const SizedBox( + height: 10, + ), + ], + )); + } +}