Commit d5f52070 authored by Dio Maulana's avatar Dio Maulana

add mqtt to reload viewbill

parent 45828f6e
// ignore_for_file: unused_catch_clause
import 'dart:io';
import 'dart:js';
import 'package:byod/bloc/view_bill.dart';
import 'package:byod/helper/logger.dart';
import 'package:byod/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mqtt_client/mqtt_browser_client.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:uuid/uuid.dart';
String mqttUrl = 'ws://mqtt.ravku.com';
final mqClient = MqttBrowserClient(mqttUrl, '');
const int mqttPort = 1883;
const String queueTopic = 'romi/byod/payment';
// String queueMessage = getMqMessage();
const String usernameMq = 'jasadigital';
const String passwordMq = 'jasadigital';
const uuidInit = Uuid();
var uuid = uuidInit.v4();
Future<int> runMqTT(BuildContext context) async {
mqClient.logging(on: false);
mqClient.setProtocolV311();
mqClient.keepAlivePeriod = 86400;
mqClient.port = mqttPort;
mqClient.onDisconnected;
mqClient.onConnected;
mqClient.onSubscribed;
// mqClient.pongCallback = pong;
final connMess = MqttConnectMessage()
.withClientIdentifier(uuid)
.withWillTopic('willtopic') // If you set this you must set a will message
.withWillMessage('My Will message')
.startClean() // Non persistent session for testing
.withWillQos(MqttQos.atLeastOnce);
// print('EXAMPLE::Mosquitto client connecting....');
mqClient.connectionMessage = connMess;
try {
await mqClient.connect();
} on NoConnectionException catch (e) {
if (debug) {
logd('MQ CONFIG::client exception', '$e');
}
mqClient.disconnect();
} on SocketException catch (e) {
if (debug) {
logd('MQ CONFIG::socket exception', '$e');
}
mqClient.disconnect();
}
if (mqClient.connectionStatus!.state == MqttConnectionState.connected) {
if (debug) {
logd('CHECK CONNECTION', 'CONNECTED');
}
} else {
/// Use status here rather than state if you also want the broker return code.
// print(
// 'EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, status is ${mqClient.connectionStatus}');
mqClient.disconnect();
exit(-1);
}
/// Ok, lets try a subscription
// print('EXAMPLE::Subscribing to the queuetest/1 topic');
var topic = queueTopic; // Not a wildcard topic
mqClient.subscribe(topic, MqttQos.atMostOnce);
/// The client has a change notifier object(see the Observable class) which we then listen to to get
/// notifications of published updates to each subscribed topic.
mqClient.updates!.listen((List<MqttReceivedMessage<MqttMessage?>>? c) {
final recMess = c![0].payload as MqttPublishMessage;
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);
// print('Notifikasi masuk dari topic <${c[0].topic}>, isinya adalah: $pt ');
// print('');
if (c[0].topic == topic && pt.contains("ORDER-ID")) {
// update bill disini
String sessionID = prefs.getString('sessionId') ?? '';
if (sessionID != '' && pt.contains(sessionID)) {
context.read<ViewBillBloc>().getBill();
}
if (debug) {
logd('CHECK TOPIC', 'TOPIC MASUK::${c[0].topic} , MESSAGE::$pt');
}
} else {
if (debug) {
logd('CHECK TOPIC',
'TOPIC TIDAK SAMA, YANG MASUK::$topic , MESSAGE::$pt');
}
}
});
// final builder = MqttClientPayloadBuilder();
// builder.addString('Hello from mqtt_client');
// mqClient.publishMessage(topic, MqttQos.exactlyOnce, builder.payload!);
return 0;
}
...@@ -18,6 +18,7 @@ import 'package:scrollable_positioned_list/scrollable_positioned_list.dart'; ...@@ -18,6 +18,7 @@ import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:sliver_tools/sliver_tools.dart'; import 'package:sliver_tools/sliver_tools.dart';
import '../../bloc/filter_menu.dart'; import '../../bloc/filter_menu.dart';
import '../../helper/mq.dart';
import '../../helper/widget/button_dialog.dart'; import '../../helper/widget/button_dialog.dart';
import '../../main.dart'; import '../../main.dart';
import '../../models/fav_group.dart'; import '../../models/fav_group.dart';
...@@ -29,7 +30,6 @@ import 'cat_list.dart'; ...@@ -29,7 +30,6 @@ import 'cat_list.dart';
import 'fav_grid_menu.dart'; import 'fav_grid_menu.dart';
import 'fav_list.dart'; import 'fav_list.dart';
import 'package:byod/models/category_list.dart'; import 'package:byod/models/category_list.dart';
import 'shimmer_menu.dart';
import 'shimmer_menu_new.dart'; import 'shimmer_menu_new.dart';
class NewHome2 extends StatefulWidget { class NewHome2 extends StatefulWidget {
...@@ -107,6 +107,8 @@ class _NewHome2State extends State<NewHome2> { ...@@ -107,6 +107,8 @@ class _NewHome2State extends State<NewHome2> {
// } // }
}, },
); );
runMqTT(context);
_scrollFavItem.addListener(() { _scrollFavItem.addListener(() {
double positionMenuFavScroll = _scrollFavItem.position.pixels; double positionMenuFavScroll = _scrollFavItem.position.pixels;
double positionMenuFavMax = _scrollFavItem.position.maxScrollExtent; double positionMenuFavMax = _scrollFavItem.position.maxScrollExtent;
......
...@@ -12,6 +12,7 @@ import 'package:flutter/material.dart'; ...@@ -12,6 +12,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/branch_exist.dart'; import '../bloc/branch_exist.dart';
import '../helper/mq.dart';
import '../main.dart'; import '../main.dart';
import 'package:flutter/services.dart' as bundle_root; import 'package:flutter/services.dart' as bundle_root;
import 'home/new_home2.dart'; import 'home/new_home2.dart';
......
...@@ -23,6 +23,7 @@ import 'package:uuid/uuid.dart'; ...@@ -23,6 +23,7 @@ import 'package:uuid/uuid.dart';
import '../../api/api.dart'; import '../../api/api.dart';
import '../../bloc/branch_exist.dart'; import '../../bloc/branch_exist.dart';
import '../../bloc/feedback_option.dart'; import '../../bloc/feedback_option.dart';
import '../../helper/mq.dart';
import '../../helper/widget/button_dialog.dart'; import '../../helper/widget/button_dialog.dart';
import '../../helper/widget/emoticon_rate.dart'; import '../../helper/widget/emoticon_rate.dart';
import '../../helper/widget/open_url.dart'; import '../../helper/widget/open_url.dart';
......
...@@ -71,6 +71,13 @@ packages: ...@@ -71,6 +71,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.1.0" version: "3.1.0"
event_bus:
dependency: transitive
description:
name: event_bus
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
...@@ -205,6 +212,13 @@ packages: ...@@ -205,6 +212,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0" version: "1.8.0"
mqtt_client:
dependency: "direct main"
description:
name: mqtt_client
url: "https://pub.dartlang.org"
source: hosted
version: "9.7.2"
nested: nested:
dependency: transitive dependency: transitive
description: description:
......
...@@ -35,6 +35,7 @@ dependencies: ...@@ -35,6 +35,7 @@ dependencies:
http: ^0.13.4 http: ^0.13.4
intl: ^0.17.0 intl: ^0.17.0
loading_animations: ^2.2.0 loading_animations: ^2.2.0
mqtt_client: ^9.7.2
scrollable_positioned_list: ^0.3.4 scrollable_positioned_list: ^0.3.4
shared_preferences: ^2.0.15 shared_preferences: ^2.0.15
shimmer: ^2.0.0 shimmer: ^2.0.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment