mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 00:51:19 +00:00
115 lines
3.6 KiB
Dart
115 lines
3.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:mqtt_client/mqtt_client.dart';
|
|
import 'package:mqtt_client/mqtt_server_client.dart';
|
|
import 'package:myhomie_app/Models/homieContext.dart';
|
|
import 'package:myhomie_app/app_context.dart';
|
|
|
|
|
|
class MQTTHelper {
|
|
MQTTHelper._privateConstructor();
|
|
bool isInstantiated = false;
|
|
static final MQTTHelper instance = MQTTHelper._privateConstructor();
|
|
|
|
void onConnected(AppContext appContext) {
|
|
print('Connected !!!!!!!!!!!! ----------------------------------');
|
|
HomieAppContext homieAppContext = appContext.getContext();
|
|
|
|
homieAppContext.clientMQTT.subscribe("#", MqttQos.atLeastOnce);
|
|
|
|
print(homieAppContext.clientMQTT);
|
|
|
|
homieAppContext.clientMQTT.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) async {
|
|
print("IN Received message");
|
|
final MqttPublishMessage message = c[0].payload;
|
|
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
|
|
print('Received message:$payload from topic: ${c[0].topic}');
|
|
|
|
appContext.setLastMessage('Received message:$payload from topic: ${c[0].topic}');
|
|
|
|
var topic = c[0].topic.split('/')[0];
|
|
|
|
switch(topic) {
|
|
case "config":
|
|
print('Get message in topic config = $payload');
|
|
break;
|
|
case "player":
|
|
print('Get message in topic player = $payload');
|
|
// refresh device info
|
|
try {
|
|
}
|
|
catch(e) {
|
|
print('Error = $e');
|
|
}
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
|
|
// 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 appContext) async {
|
|
HomieAppContext homieAppContext = appContext.getContext();
|
|
|
|
if (homieAppContext == null) {
|
|
homieAppContext = new HomieAppContext(host: '192.168.31.140', userId: 'TODO');
|
|
} // TO DEBUG
|
|
|
|
if(homieAppContext.host != null) {
|
|
homieAppContext.clientMQTT = MqttServerClient.withPort(homieAppContext.host.replaceAll('http://', ''), 'homie_app_'+homieAppContext.userId, 1883);
|
|
isInstantiated = true;
|
|
|
|
homieAppContext.clientMQTT.logging(on: false);
|
|
homieAppContext.clientMQTT.keepAlivePeriod = 20;
|
|
homieAppContext.clientMQTT.onDisconnected = onDisconnected;
|
|
homieAppContext.clientMQTT.onConnected = () => onConnected(appContext);
|
|
homieAppContext.clientMQTT.onSubscribed = onSubscribed;
|
|
|
|
var message = {
|
|
"userId": homieAppContext.userId,
|
|
"connected": false
|
|
};
|
|
|
|
final connMessage = MqttConnectMessage().authenticateAs('user', 'user') // TODO ONLINE
|
|
.keepAliveFor(60)
|
|
.withWillTopic('player/status')
|
|
.withWillMessage(jsonEncode(message))
|
|
.withClientIdentifier('tablet_app_'+homieAppContext.userId)
|
|
.startClean()
|
|
.withWillQos(MqttQos.atLeastOnce);
|
|
|
|
homieAppContext.clientMQTT.connectionMessage = connMessage;
|
|
homieAppContext.clientMQTT.autoReconnect = true;
|
|
try {
|
|
await homieAppContext.clientMQTT.connect();
|
|
|
|
} catch (e) {
|
|
print('Exception: $e');
|
|
homieAppContext.clientMQTT.disconnect();
|
|
}
|
|
appContext.setContext(homieAppContext); // TODO CLEAN
|
|
|
|
return homieAppContext.clientMQTT;
|
|
}
|
|
return null;
|
|
}
|
|
} |