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 #58 from Panduza/widget_video
Browse files Browse the repository at this point in the history
add widget video and fix close connection broker properly
  • Loading branch information
Darcraytore1 authored Jun 12, 2024
2 parents 58e2c57 + 66e9a74 commit 6342a69
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 167 deletions.
83 changes: 19 additions & 64 deletions lib/after_setup_pages/userspace_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
Expand All @@ -8,6 +10,7 @@ import 'package:panduza_sandbox_flutter/userspace_widgets/ic_bpc.dart';
import 'package:panduza_sandbox_flutter/userspace_widgets/ic_platform.dart';
import 'package:panduza_sandbox_flutter/userspace_widgets/ic_powermeter.dart';
import 'package:panduza_sandbox_flutter/userspace_widgets/ic_not_managed.dart';
import 'package:panduza_sandbox_flutter/userspace_widgets/ic_video.dart';

// import '../widgets/interface_control/icw_bpc.dart';

Expand All @@ -23,21 +26,22 @@ class UserspacePage extends StatefulWidget {
final BrokerConnectionInfo broker_connection_info;

@override
_UserspacePageState createState() => _UserspacePageState();
State<UserspacePage> createState() => _UserspacePageState();
}

class _UserspacePageState extends State<UserspacePage> {
List<InterfaceConnection> interfaces = [];
Map<int, InterfaceConnection> channel = {};

StreamSubscription<List<MqttReceivedMessage<MqttMessage>>>? mqttSubscription;


bool interfaceAlreadyRegistered(InterfaceConnection ic) {
for (var interface in interfaces) {
if (interface.topic == ic.topic) {
return true;
}
}
// print(ic.topic);
return false;
}

Expand All @@ -52,6 +56,13 @@ class _UserspacePageState extends State<UserspacePage> {
return null;
}

@override
void dispose() {
mqttSubscription!.cancel();

super.dispose();
}

@override
void initState() {
super.initState();
Expand All @@ -69,17 +80,9 @@ class _UserspacePageState extends State<UserspacePage> {
// Run your async function here
// await myAsyncFunction();

widget.broker_connection_info.client.updates!
mqttSubscription = widget.broker_connection_info.client.updates!
.listen((List<MqttReceivedMessage<MqttMessage>> c) {
// final MqttMessage message = c[0].payload;

// print('Received from userspace from topic: ${c[0].topic}>');

// final string = binascii.b2a_base64(bytearray(data)).decode('utf-8');
// 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 All @@ -92,8 +95,11 @@ class _UserspacePageState extends State<UserspacePage> {

var jsonObject = json.decode(pt);

// print(jsonObject);

InterfaceConnection ic = InterfaceConnection(
widget.broker_connection_info.client, topic, jsonObject["info"]);

if (!interfaceAlreadyRegistered(ic)) {
if (ic.getType() != "device") {
setState(() {
Expand All @@ -106,36 +112,12 @@ class _UserspacePageState extends State<UserspacePage> {
}
return compareResult;
});
// interfaces = [...interfaces, ic];
});
}
}
}
// final payload =
// MqttPublishPayload.bytesToStringAsString(message.);

// print('Received message:$payload from topic: ${c[0].topic}>');

// sort and put in my map

});
});

/*
Future.delayed(Duration(seconds: 2), () async {
List<InterfaceConnection> newInterfaces = [];
newInterfaces.add(findPlatform() as InterfaceConnection);
for (var interface in interfaces) {
if (interface.info["type"] != "platform") {
newInterfaces.add(interface);
}
}
interfaces = newInterfaces;
});
*/
}

// Build item of the grid that control the interfaces
Expand All @@ -162,6 +144,8 @@ class _UserspacePageState extends State<UserspacePage> {
return IcThermometer(ic);
case "relay":
return IcRelay(ic);
case "video":
return IcVideo(ic);
default:
print("!!!! $type");
return IcNotManaged(ic);
Expand All @@ -179,35 +163,6 @@ class _UserspacePageState extends State<UserspacePage> {
// appBar: getAppBar("UserSpace"),
appBar: getAppBarUserSpace("Userspace", context),
body:
/*
ListView.separated(
padding: const EdgeInsets.all(20),
itemCount: interfaces.length,
itemBuilder: (BuildContext context, int index) {
return interfaceControlItemBuiler(context, index);
},
separatorBuilder: (context, index) => const Divider(),
)
*/
/*
Column(
// Start with the platform then display every device
children: <Widget>[
Center(
child: Container(
height: MediaQuery.sizeOf(context).height / 3,
width: MediaQuery.sizeOf(context).width / 3,
child: IcPlatform(findPlatform() as InterfaceConnection),
)
),
]
)
*/




MasonryGridView.count(
crossAxisCount: columns,
mainAxisSpacing: 4,
Expand Down
23 changes: 2 additions & 21 deletions lib/userspace_widgets/ic_blc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class IcBlc extends StatefulWidget {
final InterfaceConnection _interfaceConnection;

@override
_IcBlcState createState() => _IcBlcState();
State<IcBlc> createState() => _IcBlcState();

}

double value_to_double(dynamic value) {
Expand Down Expand Up @@ -53,8 +54,6 @@ class _IcBlcState extends State<IcBlc> {
///
///
void onMqttMessage(List<MqttReceivedMessage<MqttMessage>> c) {
// print("============");
// print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} ');

//
if (c[0].topic.startsWith(widget._interfaceConnection.topic)) {
Expand All @@ -66,14 +65,9 @@ class _IcBlcState extends State<IcBlc> {

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) {
// print('${atts.key} ${field.key} => ${field.value}');

switch (atts.key) {
case "mode":
Expand Down Expand Up @@ -186,14 +180,6 @@ class _IcBlcState extends State<IcBlc> {
}
}
});
// print(updateAtts);

// setState(() {
// _attsEffective = updateAtts;
// });
// _attsEffective

// print(jsonObject.runtimeType);
}
} else {
// print('not good:');
Expand All @@ -210,11 +196,6 @@ class _IcBlcState extends State<IcBlc> {
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
Expand Down
34 changes: 8 additions & 26 deletions lib/userspace_widgets/ic_bpc.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:panduza_sandbox_flutter/data/const.dart';
import 'templates.dart';
Expand All @@ -14,7 +16,8 @@ class IcBpc extends StatefulWidget {
final InterfaceConnection _interfaceConnection;

@override
_IcBpcState createState() => _IcBpcState();
State<IcBpc> createState() => _IcBpcState();

}

class _IcBpcState extends State<IcBpc> {
Expand All @@ -27,15 +30,14 @@ class _IcBpcState extends State<IcBpc> {
double? _currentValueReq;
double? _currentValueEff;

StreamSubscription<List<MqttReceivedMessage<MqttMessage>>>? mqttSubscription;

///
///
void onMqttMessage(List<MqttReceivedMessage<MqttMessage>> c) {
// print("============");
// print('Received ${c[0].topic} from ${widget._interfaceConnection.topic} ');

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

Expand All @@ -44,14 +46,9 @@ class _IcBpcState extends State<IcBpc> {

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) {
// print('${atts.key} ${field.key} => ${field.value}');
switch (atts.key) {
case "enable":
if (field.key == "value") {
Expand All @@ -62,7 +59,6 @@ class _IcBpcState extends State<IcBpc> {

case "voltage":
if (field.key == "value") {
// print("pokkk !! ${field.value.runtimeType}");
switch (field.value.runtimeType) {
case int:
_voltageValueEff = field.value.toDouble();
Expand All @@ -75,7 +71,6 @@ class _IcBpcState extends State<IcBpc> {

case "current":
if (field.key == "value") {
// print("pokkk !! ${field.value.runtimeType}");
switch (field.value.runtimeType) {
case int:
_currentValueEff = field.value.toDouble();
Expand All @@ -89,14 +84,6 @@ class _IcBpcState extends State<IcBpc> {
}
}
});
// print(updateAtts);

// setState(() {
// _attsEffective = updateAtts;
// });
// _attsEffective

// print(jsonObject.runtimeType);
}
} else {
// print('not good:');
Expand All @@ -106,18 +93,12 @@ class _IcBpcState extends State<IcBpc> {
/// Initialize MQTT Subscriptions
///
void initializeMqttSubscription() async {
widget._interfaceConnection.client.updates!.listen(onMqttMessage);
mqttSubscription = 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
Expand All @@ -132,6 +113,7 @@ class _IcBpcState extends State<IcBpc> {

@override
void dispose() {
mqttSubscription!.cancel();
super.dispose();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/userspace_widgets/ic_not_managed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class IcNotManaged extends StatefulWidget {
InterfaceConnection _interfaceConnection;

@override
_IcNotManagedState createState() => _IcNotManagedState();
State<IcNotManaged> createState() => _IcNotManagedState();
}

class _IcNotManagedState extends State<IcNotManaged> {
Expand Down
7 changes: 6 additions & 1 deletion lib/userspace_widgets/ic_powermeter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:panduza_sandbox_flutter/data/const.dart';
Expand All @@ -22,6 +24,8 @@ class _IcPowermeterState extends State<IcPowermeter> {

double _value = 0;

StreamSubscription<List<MqttReceivedMessage<MqttMessage>>>? mqttSubscription;

/// Init each value of the powermeter, here just the measure
/// powermeter
///
Expand Down Expand Up @@ -74,7 +78,7 @@ class _IcPowermeterState extends State<IcPowermeter> {
/// Initialize MQTT Subscriptions
///
void initializeMqttSubscription() async {
widget._interfaceConnection.client.updates!.listen(onMqttMessage);
mqttSubscription = widget._interfaceConnection.client.updates!.listen(onMqttMessage);

String attsTopic = "${widget._interfaceConnection.topic}/atts/#";
// print(attsTopic);
Expand All @@ -100,6 +104,7 @@ class _IcPowermeterState extends State<IcPowermeter> {

@override
void dispose() {
mqttSubscription!.cancel();
super.dispose();
}

Expand Down
Loading

0 comments on commit 6342a69

Please sign in to comment.