mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 00:21:19 +00:00
Add mqtt (as helper) + fix languages config init
This commit is contained in:
parent
7a80a6ff8d
commit
9657240cb0
@ -2,6 +2,7 @@
|
||||
package="be.musee.de.la.fraise.tablet_app">
|
||||
<application
|
||||
android:label="tablet_app"
|
||||
android:usesCleartextTraffic="true"
|
||||
android:icon="@mipmap/ic_launcher">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
|
||||
74
lib/Helpers/MQTTHelper.dart
Normal file
74
lib/Helpers/MQTTHelper.dart
Normal file
@ -0,0 +1,74 @@
|
||||
import 'package:mqtt_client/mqtt_client.dart';
|
||||
import 'package:mqtt_client/mqtt_server_client.dart';
|
||||
import 'package:unique_identifier/unique_identifier.dart';
|
||||
|
||||
class MQTTHelper {
|
||||
MQTTHelper._privateConstructor();
|
||||
static final MQTTHelper instance = MQTTHelper._privateConstructor();
|
||||
|
||||
void onConnected(dynamic tabletAppContext) {
|
||||
print('Connected !!!!!!!!!!!! ----------------------------------');
|
||||
|
||||
tabletAppContext.clientMQTT.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
|
||||
final MqttPublishMessage message = c[0].payload;
|
||||
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
|
||||
print('Received message:$payload from topic: ${c[0].topic}');
|
||||
});
|
||||
}
|
||||
|
||||
// unconnected
|
||||
void onDisconnected() {
|
||||
print('Disconnected');
|
||||
}
|
||||
|
||||
// subscribe to topic succeeded
|
||||
void onSubscribed(String topic) {
|
||||
print('Subscribed topic: $topic');
|
||||
}
|
||||
|
||||
// subscribe to topic failed
|
||||
void onSubscribeFail(String topic) {
|
||||
print('Failed to subscribe $topic');
|
||||
}
|
||||
|
||||
// unsubscribe succeeded
|
||||
void onUnsubscribed(String topic) {
|
||||
print('Unsubscribed topic: $topic');
|
||||
}
|
||||
|
||||
Future<MqttServerClient> connect(dynamic tabletAppContext) async {
|
||||
var identifier = await UniqueIdentifier.serial;
|
||||
tabletAppContext.clientMQTT = MqttServerClient.withPort(tabletAppContext.host.replaceAll('http://', ''), 'tablet_app_'+identifier, 1883);
|
||||
|
||||
tabletAppContext.clientMQTT.logging(on: false);
|
||||
tabletAppContext.clientMQTT.keepAlivePeriod = 20;
|
||||
tabletAppContext.clientMQTT.onDisconnected = onDisconnected;
|
||||
tabletAppContext.clientMQTT.onConnected = () => onConnected(tabletAppContext);
|
||||
tabletAppContext.clientMQTT.onSubscribed = onSubscribed;
|
||||
|
||||
final connMessage = MqttConnectMessage().authenticateAs('admin', 'mdlf2021!') // TODO ONLINE
|
||||
.keepAliveFor(60)
|
||||
.withWillTopic('willtopic')
|
||||
.withWillMessage('Will message')
|
||||
.withClientIdentifier('tablet_app_'+identifier)
|
||||
.startClean()
|
||||
.withWillQos(MqttQos.atLeastOnce);
|
||||
|
||||
tabletAppContext.clientMQTT.connectionMessage = connMessage;
|
||||
tabletAppContext.clientMQTT.autoReconnect = true;
|
||||
try {
|
||||
await tabletAppContext.clientMQTT.connect();
|
||||
tabletAppContext.clientMQTT.subscribe("#", MqttQos.atLeastOnce);
|
||||
|
||||
const pubTopic = 'player/status';
|
||||
final builder = MqttClientPayloadBuilder();
|
||||
builder.addString('tablet_app_'+identifier);
|
||||
tabletAppContext.clientMQTT.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload);
|
||||
} catch (e) {
|
||||
print('Exception: $e');
|
||||
tabletAppContext.clientMQTT.disconnect();
|
||||
}
|
||||
|
||||
return tabletAppContext.clientMQTT;
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:managerapi/api.dart';
|
||||
import 'package:mqtt_client/mqtt_server_client.dart';
|
||||
import 'package:tablet_app/client.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
|
||||
class TabletAppContext with ChangeNotifier{
|
||||
Client clientAPI;
|
||||
MqttServerClient clientMQTT;
|
||||
String id;
|
||||
String host;
|
||||
ConfigurationDTO configuration;
|
||||
|
||||
@ -3,19 +3,19 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:managerapi/api.dart';
|
||||
import 'package:mqtt_client/mqtt_server_client.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tablet_app/Components/Buttons/rounded_button.dart';
|
||||
import 'package:tablet_app/Components/loading.dart';
|
||||
import 'package:tablet_app/Components/rounded_input_field.dart';
|
||||
import 'package:tablet_app/Helpers/DatabaseHelper.dart';
|
||||
import 'package:tablet_app/Models/map-marker.dart';
|
||||
import 'package:tablet_app/Helpers/MQTTHelper.dart';
|
||||
import 'package:tablet_app/Models/tabletContext.dart';
|
||||
import 'package:tablet_app/Screens/MainView/dropDown_configuration.dart';
|
||||
import 'package:tablet_app/Screens/MainView/main_view.dart';
|
||||
import 'package:tablet_app/app_context.dart';
|
||||
import 'package:tablet_app/client.dart';
|
||||
import 'package:tablet_app/constants.dart';
|
||||
import 'package:auto_size_text/auto_size_text.dart';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:unique_identifier/unique_identifier.dart';
|
||||
@ -153,13 +153,13 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
textColor: Colors.white,
|
||||
fontSize: 16.0
|
||||
);
|
||||
TabletAppContext tabletAppContext = new TabletAppContext();
|
||||
tabletAppContext.host = url;
|
||||
tabletAppContext.clientAPI = client;
|
||||
tabletAppContext.clientMQTT = await MQTTHelper.instance.connect(tabletAppContext);
|
||||
setState(() {
|
||||
TabletAppContext tabletAppContext = new TabletAppContext();
|
||||
tabletAppContext.host = url;
|
||||
tabletAppContext.clientAPI = client;
|
||||
appContext.setContext(tabletAppContext);
|
||||
|
||||
configOk = true;
|
||||
appContext.setContext(tabletAppContext);
|
||||
configOk = true;
|
||||
});
|
||||
} else {
|
||||
Fluttertoast.showToast(
|
||||
|
||||
@ -30,6 +30,8 @@ class _MainViewWidget extends State<MainViewWidget> {
|
||||
Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution
|
||||
SectionDTO sectionSelected;
|
||||
|
||||
int rowCount = 4;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final appContext = Provider.of<AppContext>(context);
|
||||
@ -176,7 +178,7 @@ class _MainViewWidget extends State<MainViewWidget> {
|
||||
color: kBackgroundGrey,
|
||||
child: Stack(
|
||||
children: [
|
||||
LanguageSelection(),
|
||||
if (appContext.getContext().configuration != null) LanguageSelection(),
|
||||
Center(
|
||||
child: Container(
|
||||
height: size.height * 0.85,
|
||||
@ -191,7 +193,7 @@ class _MainViewWidget extends State<MainViewWidget> {
|
||||
else {
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
|
||||
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: rowCount),
|
||||
itemCount: snapshot.data?.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return InkWell(
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:managerapi/api.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tablet_app/Helpers/MQTTHelper.dart';
|
||||
import 'package:tablet_app/client.dart';
|
||||
import 'Helpers/DatabaseHelper.dart';
|
||||
import 'Models/tabletContext.dart';
|
||||
@ -60,6 +63,8 @@ class _MyAppState extends State<MyApp> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.tabletAppContext != null)
|
||||
MQTTHelper.instance.connect(widget.tabletAppContext);
|
||||
return ChangeNotifierProvider<AppContext>(
|
||||
create: (_) => AppContext(widget.tabletAppContext),
|
||||
child: MaterialApp(
|
||||
|
||||
14
pubspec.lock
14
pubspec.lock
@ -85,6 +85,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.1"
|
||||
event_bus:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: event_bus
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.1"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -205,6 +212,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.9.7"
|
||||
mqtt_client:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: mqtt_client
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "8.2.0"
|
||||
nested:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@ -35,6 +35,7 @@ dependencies:
|
||||
enum_to_string: ^2.0.1
|
||||
carousel_slider: ^4.0.0
|
||||
youtube_player_flutter: ^7.0.0+7
|
||||
mqtt_client: ^8.1.0
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user