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

Commit

Permalink
11 append widget to control a relay (#30)
Browse files Browse the repository at this point in the history
* add basic widget relay

* test with voxpower work but with delay (too much log)

* fix bug button stuck

---------

Co-authored-by: Damien Albisson <[email protected]>
  • Loading branch information
Darcraytore1 and Damien Albisson authored May 22, 2024
1 parent 3d1945b commit 8db24da
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/after_setup_pages/userspace_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -77,7 +78,7 @@ class _UserspacePageState extends State<UserspacePage> {
// 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;

Expand Down Expand Up @@ -147,6 +148,8 @@ class _UserspacePageState extends State<UserspacePage> {
return IcPlatform(ic);
case "powermeter":
return IcPowermeter(ic);
case "relay":
return IcRelay(ic);
default:
print("!!!! $type");
return IcNotManaged(ic);
Expand Down
4 changes: 2 additions & 2 deletions lib/userspace_widgets/ic_bpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class _IcBpcState extends State<IcBpc> {

//
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;

final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

var jsonObject = json.decode(pt);

print(jsonObject);

// Map<String, dynamic> updateAtts = Map.from(_attsEffective);
Expand All @@ -50,7 +51,6 @@ class _IcBpcState extends State<IcBpc> {
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 "enable":
if (field.key == "value") {
Expand Down
171 changes: 171 additions & 0 deletions lib/userspace_widgets/ic_relay.dart
Original file line number Diff line number Diff line change
@@ -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<IcRelay> createState() => _IcRelayState();
}

class _IcRelayState extends State<IcRelay> {
bool? _enableValueReq = false;
bool? _enableValueEff = false;

///
///
void onMqttMessage(List<MqttReceivedMessage<MqttMessage>> 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<String, dynamic> updateAtts = Map.from(_attsEffective);

setState(() {
for (MapEntry<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> 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<String, dynamic> 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();
}
}
}

0 comments on commit 8db24da

Please sign in to comment.