Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
durgeshparekh committed Oct 14, 2023
1 parent 8805bc3 commit 521630e
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 111 deletions.
1 change: 1 addition & 0 deletions ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
44 changes: 44 additions & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
end
end

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
52 changes: 36 additions & 16 deletions lib/controller/dashboard_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:mqtt_flutter_web/controller/mqtt_controller.dart';

Expand All @@ -8,34 +9,45 @@ class DashboardController extends GetxController {
var passwordController = TextEditingController();
var topicController = TextEditingController();
var messageController = TextEditingController();
var publishTopicController = TextEditingController();

var brokerConnected = false.obs;
var receivedMessage = ''.obs;
var messageList = [].obs;
var mqttController = MQTTController();

// nO1cANsTOPuS
// "d\$2N@8p&1V#1"

Future<void> connectToBroker() async {
if (brokerConnected.isFalse) {
mqttController.initializeAndConnect(
hostName: "ws://test.smartnode.in/mqtt",
portNumber: 8083,
keepAliveTime: 10,
clientId: clientIdController.text,
username: usernameController.text,
password: passwordController.text,
if (clientIdController.text.isEmpty) {
Fluttertoast.showToast(
msg: 'Client Id should not be empty',
gravity: ToastGravity.CENTER,
textColor: Colors.black,
webPosition: "center",
webBgColor: "#b2dfdb",
timeInSecForIosWeb: 2,
);
brokerConnected.value = await mqttController.onConnected();
} else {
mqttController
.disconnect()
.then((value) => brokerConnected.value = false);
if (brokerConnected.isFalse) {
mqttController.initializeAndConnect(
hostName: "ws://test.smartnode.in/mqtt",
portNumber: 8083,
keepAliveTime: 10,
clientId: clientIdController.text,
username: usernameController.text,
password: passwordController.text,
);
brokerConnected.value = await mqttController.onConnected();
} else {
mqttController
.disconnect()
.then((value) => brokerConnected.value = false);
}
}
}

publishMessage() {
mqttController.publishMessage(
topic: topicController.text,
topic: publishTopicController.text,
publishMessage: messageController.text,
);
}
Expand All @@ -46,5 +58,13 @@ class DashboardController extends GetxController {

void unSubscribeToTopic() {
mqttController.unSubscribeToMQTT(topic: topicController.text);
topicController.clear();
}

void handleMessage(dynamic message) {
// Handle the received message here
debugPrint('Received message in Dashboard: $message');
// receivedMessage.value = '$topic: $message';
messageList.add(message);
}
}
35 changes: 17 additions & 18 deletions lib/controller/mqtt_controller.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mqtt_client/mqtt_browser_client.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_flutter_web/controller/dashboard_controller.dart';

class MQTTController {
MqttBrowserClient? mqttBrowserClient;
Expand All @@ -20,8 +23,6 @@ class MQTTController {
String? username,
String? password,
}) async {
debugPrint(
'clientId: $clientId, username: $username, password: $password, portNumber: $portNumber, host: $hostName, keepAlive: $keepAliveTime');
mqttBrowserClient = MqttBrowserClient(
hostName,
clientId!,
Expand All @@ -32,10 +33,6 @@ class MQTTController {

mqttBrowserClient!.websocketProtocols =
MqttClientConstants.protocolsSingleDefault;

mqttBrowserClient!.onSubscribed = onSubscribed;
mqttBrowserClient!.onUnsubscribed = onUnSubscribed;
mqttBrowserClient!.onDisconnected = onDisconnected;

MqttConnectMessage connMessage = MqttConnectMessage()
.withClientIdentifier(clientId)
Expand All @@ -45,14 +42,13 @@ class MQTTController {
mqttBrowserClient!.connectionMessage = connMessage;

try {
await mqttBrowserClient!.connect(username, password).then((value) {
var connectionStatus = value!.state;
if (connectionStatus == MqttConnectionState.connected) {
startListeningMessages();

mqttBrowserClient!.onConnected = onConnected;
}
});
await mqttBrowserClient!.connect(username, password);
mqttBrowserClient!.onConnected = onConnected;
mqttBrowserClient!.onSubscribed = onSubscribed;
mqttBrowserClient!.onUnsubscribed = onUnSubscribed;
mqttBrowserClient!.onDisconnected = onDisconnected;

startListeningMessages();
} catch (e) {
debugPrint('EXAMPLE::client exception - $e');
mqttBrowserClient!.disconnect();
Expand All @@ -70,6 +66,7 @@ class MQTTController {

Future onConnected() async {
debugPrint("connected successfully");
return true;
}

Future onSubscribed(String topic) async {
Expand All @@ -95,7 +92,7 @@ class MQTTController {
try {
mqttBrowserClient!.publishMessage(
topic,
MqttQos.exactlyOnce,
MqttQos.atMostOnce,
builder.payload!,
);
} catch (e) {
Expand All @@ -107,7 +104,6 @@ class MQTTController {
MqttSubscriptionStatus status =
mqttBrowserClient!.getSubscriptionsStatus(topic);
if (status == MqttSubscriptionStatus.doesNotExist) {
debugPrint("topic :$topic");
mqttBrowserClient!.subscribe(topic, MqttQos.atLeastOnce);
}
}
Expand All @@ -128,8 +124,11 @@ class MQTTController {
final MqttPublishMessage? recMess = c[0].payload as MqttPublishMessage?;
final String receivedMessage =
MqttPublishPayload.bytesToStringAsString(recMess!.payload.message);
// _currentState.setReceivedText(recMess.toString());
debugPrint(receivedMessage);
// Get the YourController instance
DashboardController dashboardController = Get.find<DashboardController>();
// Call the method to handle the message
dynamic object = {"topic": c[0].topic, "message": receivedMessage};
dashboardController.handleMessage(json.encode(object));
});
}
}
Loading

0 comments on commit 521630e

Please sign in to comment.