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

Commit

Permalink
61 every interfaces didnt always appears on userspace page (#77)
Browse files Browse the repository at this point in the history
* Send approx value to decimal attribute choose to avoid discrepancies on large double

* Send publish on every topic if not received once

* increase timer to have a temporary solution

---------

Co-authored-by: Damien Albisson <[email protected]>
  • Loading branch information
Darcraytore1 and Damien Albisson authored Jun 26, 2024
1 parent c427770 commit 8084647
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
24 changes: 18 additions & 6 deletions lib/pages/after_setup_pages/userspace_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class _UserspacePageState extends State<UserspacePage> {
Map<int, InterfaceConnection> channel = {};

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


bool interfaceAlreadyRegistered(InterfaceConnection ic) {
Expand All @@ -59,22 +60,33 @@ class _UserspacePageState extends State<UserspacePage> {
@override
void dispose() {
mqttSubscription!.cancel();
timer?.cancel();

super.dispose();
}

void sendMessageToPza() {
MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
builder.addString('*');
final payload = builder.payload;
try {
widget.brokerConnectionInfo.client
.publishMessage('pza', MqttQos.atLeastOnce, payload!);
} catch(e) {
// If publish failed (means the person quitt to fastly the page)
}
}

@override
void initState() {
super.initState();

widget.brokerConnectionInfo.client
.subscribe('pza/+/+/+/atts/info', MqttQos.atLeastOnce);

MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
builder.addString('*');
final payload = builder.payload;
widget.brokerConnectionInfo.client
.publishMessage('pza', MqttQos.atLeastOnce, payload!);
sendMessageToPza();

timer = Timer.periodic(Duration(seconds: 2), (Timer t) => sendMessageToPza());

Future.delayed(const Duration(milliseconds: 1), () async {
// Run your async function here
Expand Down Expand Up @@ -130,7 +142,7 @@ class _UserspacePageState extends State<UserspacePage> {

final ic = interfaces[index];
final type = ic.info["type"];

switch (type) {
case "blc":
return IcBlc(ic);
Expand Down
5 changes: 4 additions & 1 deletion lib/widgets/userspace_widgets/ic_blc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class _IcBlcState extends State<IcBlc> {
super.initState();

// subscribe to info and atts ?
Future.delayed(const Duration(milliseconds: 1), initializeMqttSubscription);
Future.delayed(const Duration(milliseconds: 500), initializeMqttSubscription);
}

@override
Expand Down Expand Up @@ -259,6 +259,9 @@ class _IcBlcState extends State<IcBlc> {
if (_currentValueEff != _currentValueReq) {
MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();

// Send a current value approx to decimal attribute
_currentValueReq = num.parse(_currentValueReq!.toStringAsFixed(_currentDecimals)).toDouble();

Map<String, dynamic> data = {
"current": {"value": _currentValueReq!}
};
Expand Down
10 changes: 8 additions & 2 deletions lib/widgets/userspace_widgets/ic_bpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _IcBpcState extends State<IcBpc> {
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

var jsonObject = json.decode(pt);

setState(() {
for (MapEntry<String, dynamic> atts in jsonObject.entries) {
for (MapEntry<String, dynamic> field in atts.value.entries) {
Expand Down Expand Up @@ -147,7 +147,7 @@ class _IcBpcState extends State<IcBpc> {
super.initState();

// subscribe to info and atts ?
Future.delayed(const Duration(milliseconds: 1), initializeMqttSubscription);
Future.delayed(const Duration(milliseconds: 500), initializeMqttSubscription);
}

@override
Expand Down Expand Up @@ -178,6 +178,9 @@ class _IcBpcState extends State<IcBpc> {
if (_voltageValueEff != _voltageValueReq) {
MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();

// Send a voltage value approx to decimal attribute
_voltageValueReq = num.parse(_voltageValueReq!.toStringAsFixed(_voltageDecimal)).toDouble();

Map<String, dynamic> data = {
"voltage": {"value": _voltageValueReq!}
};
Expand All @@ -195,6 +198,9 @@ class _IcBpcState extends State<IcBpc> {
if (_currentValueEff != _currentValueReq) {
MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();

// Send a current value approx to decimal attribute
_currentValueReq = num.parse(_currentValueReq!.toStringAsFixed(_currentDecimal)).toDouble();

Map<String, dynamic> data = {
"current": {"value": _currentValueReq!}
};
Expand Down

0 comments on commit 8084647

Please sign in to comment.