From 8db24dac3c394ca4a363d6c2ee334d07ebe6f594 Mon Sep 17 00:00:00 2001 From: Darcraytore1 <57347517+Darcraytore1@users.noreply.github.com> Date: Wed, 22 May 2024 15:33:31 +0200 Subject: [PATCH] 11 append widget to control a relay (#30) * add basic widget relay * test with voxpower work but with delay (too much log) * fix bug button stuck --------- Co-authored-by: Damien Albisson --- lib/after_setup_pages/userspace_page.dart | 5 +- lib/userspace_widgets/ic_bpc.dart | 4 +- lib/userspace_widgets/ic_relay.dart | 171 ++++++++++++++++++++++ 3 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 lib/userspace_widgets/ic_relay.dart diff --git a/lib/after_setup_pages/userspace_page.dart b/lib/after_setup_pages/userspace_page.dart index fedcc4a..7726f2d 100644 --- a/lib/after_setup_pages/userspace_page.dart +++ b/lib/after_setup_pages/userspace_page.dart @@ -12,6 +12,7 @@ import 'package:panduza_sandbox_flutter/userspace_widgets/ic_not_managed.dart'; // import '../widgets/interface_control/icw_bpc.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/utils_widgets/appBar.dart'; import 'package:panduza_sandbox_flutter/data/broker_connection_info.dart'; @@ -77,7 +78,7 @@ class _UserspacePageState extends State { // print(message.toString()); // pza/*/atts/info - + print(c[0].topic); if (c![0].topic.startsWith("pza") & c![0].topic.endsWith("atts/info")) { final recMess = c![0].payload as MqttPublishMessage; @@ -147,6 +148,8 @@ class _UserspacePageState extends State { return IcPlatform(ic); case "powermeter": return IcPowermeter(ic); + case "relay": + return IcRelay(ic); default: print("!!!! $type"); return IcNotManaged(ic); diff --git a/lib/userspace_widgets/ic_bpc.dart b/lib/userspace_widgets/ic_bpc.dart index b49ca9d..aeabef8 100644 --- a/lib/userspace_widgets/ic_bpc.dart +++ b/lib/userspace_widgets/ic_bpc.dart @@ -34,6 +34,7 @@ class _IcBpcState extends State { // if (c[0].topic.startsWith(widget._interfaceConnection.topic)) { + print(c[0].topic); if (!c[0].topic.endsWith('/info')) { final recMess = c![0].payload as MqttPublishMessage; @@ -41,7 +42,7 @@ class _IcBpcState extends State { MqttPublishPayload.bytesToStringAsString(recMess.payload.message); var jsonObject = json.decode(pt); - + print(jsonObject); // Map updateAtts = Map.from(_attsEffective); @@ -50,7 +51,6 @@ class _IcBpcState extends State { for (MapEntry atts in jsonObject.entries) { for (MapEntry field in atts.value.entries) { print('${atts.key} ${field.key} => ${field.value}'); - switch (atts.key) { case "enable": if (field.key == "value") { diff --git a/lib/userspace_widgets/ic_relay.dart b/lib/userspace_widgets/ic_relay.dart new file mode 100644 index 0000000..5b0b66f --- /dev/null +++ b/lib/userspace_widgets/ic_relay.dart @@ -0,0 +1,171 @@ +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +// import '../widgets/interface_control/icw_bpc.dart'; +import 'package:mqtt_client/mqtt_client.dart'; +import 'package:panduza_sandbox_flutter/data/const.dart'; +import 'package:panduza_sandbox_flutter/userspace_widgets/templates.dart'; +import 'package:panduza_sandbox_flutter/data/interface_connection.dart'; + +class IcRelay extends StatefulWidget { + const IcRelay(this._interfaceConnection, { + super.key + }); + + final InterfaceConnection _interfaceConnection; + + @override + State createState() => _IcRelayState(); +} + +class _IcRelayState extends State { + bool? _enableValueReq = false; + bool? _enableValueEff = false; + + /// + /// + void onMqttMessage(List> c) { + print("============"); + print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} '); + + print(widget._interfaceConnection.topic); + + if (c[0].topic.startsWith(widget._interfaceConnection.topic)) { + print("test = ${c[0].topic}"); + if (!c[0].topic.endsWith('/info')) { + print("success = ${c[0].topic}"); + final recMess = c![0].payload as MqttPublishMessage; + + final pt = + MqttPublishPayload.bytesToStringAsString(recMess.payload.message); + + var jsonObject = json.decode(pt); + + print(jsonObject); + + // Map updateAtts = Map.from(_attsEffective); + + setState(() { + for (MapEntry atts in jsonObject.entries) { + for (MapEntry field in atts.value.entries) { + + if (field.key == "open") { + _enableValueEff = field.value; + + if (_enableValueEff != null) { + _enableValueReq = _enableValueEff; + } + } + } + } + }); + } + } 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); + + // if (sub != null) { + // print("coool !!"); + // } else { + // print("nullllll"); + // } + } + + /// 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); + } + + /// When Switch tap send a mqtt request to the platform + /// + void Function(bool)? enableValueSwitchOnChanged() { + + if (_enableValueReq != _enableValueEff) { + return null; + } else { + return (value) { + enableValueToggleRequest(); + }; + } + } + + /// Publish a request to make the relay take the value of the switch widget + /// + void enableValueToggleRequest() { + if (_enableValueEff == null) { + return; + } + bool target = _enableValueEff! ? false : true; + + MqttClientPayloadBuilder builder = MqttClientPayloadBuilder(); + + // Example JSON object + Map data = { + "state": {"open": target} + }; + + // Convert JSON object to string + String jsonString = jsonEncode(data); + + builder.addString(jsonString); + final payload = builder.payload; + + String cmdsTopic = "${widget._interfaceConnection.topic}/cmds/set"; + + widget._interfaceConnection.client + .publishMessage(cmdsTopic, MqttQos.atLeastOnce, payload!); + + setState(() { + _enableValueReq = target; + }); + } + + /// Simple widget of + /// + @override + Widget build(BuildContext context) { + if (_enableValueEff != null) { + return Card( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + cardHeadLine(widget._interfaceConnection), + Switch( + value: _enableValueEff!, + onChanged: enableValueSwitchOnChanged(), + activeColor: blue, + ), + /* + Switch( + value: _enableValueEff, + onChanged: (value) { + }, + ) + */ + ], + ) + ); + } else { + return const Card(); + } + } +} \ No newline at end of file