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

Commit

Permalink
Merge pull request #54 from Panduza/51-add-thermo
Browse files Browse the repository at this point in the history
51 add thermo
  • Loading branch information
Darcraytore1 authored Jun 7, 2024
2 parents a28265e + ca153bd commit f2aec43
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lib/after_setup_pages/userspace_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -157,6 +158,8 @@ class _UserspacePageState extends State<UserspacePage> {
return IcPlatform(ic);
case "powermeter":
return IcPowermeter(ic);
case "thermometer":
return IcThermometer(ic);
case "relay":
return IcRelay(ic);
default:
Expand Down
47 changes: 22 additions & 25 deletions lib/userspace_widgets/ic_powermeter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,31 @@ class _IcPowermeterState extends State<IcPowermeter> {
var jsonObject = json.decode(pt);

print(jsonObject);

for (MapEntry<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> 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<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> 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:');
Expand Down
158 changes: 158 additions & 0 deletions lib/userspace_widgets/ic_thermometer.dart
Original file line number Diff line number Diff line change
@@ -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<IcThermometer> createState() => _IcThermometerState();
}

class _IcThermometerState extends State<IcThermometer> {

double _value = 0;

/// Init each value of the thermometer, here just the measure
/// temperature
///
void onMqttMessage(List<MqttReceivedMessage<MqttMessage>> 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<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> 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: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
style: TextStyle(
color: black,
fontSize: 16
),
)
),
const SizedBox(
width: 10,
),
Text(
"read per sec",
style: TextStyle(
color: black
),
),
],
),
const SizedBox(
height: 10,
),
],
));
}
}

0 comments on commit f2aec43

Please sign in to comment.