Skip to content

Commit

Permalink
Merge pull request #31 from KoiFresh/fix/mqtt-password
Browse files Browse the repository at this point in the history
fix: show error on mqtt connection error
  • Loading branch information
KoiFresh authored Aug 22, 2023
2 parents 2a83cf0 + 2cb7805 commit 35f4c84
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 179 deletions.
Binary file modified android/app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
40 changes: 20 additions & 20 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
PODS:
- audio_session (0.0.1):
- Flutter
- Firebase/CoreOnly (10.7.0):
- FirebaseCore (= 10.7.0)
- Firebase/Messaging (10.7.0):
- Firebase/CoreOnly (10.12.0):
- FirebaseCore (= 10.12.0)
- Firebase/Messaging (10.12.0):
- Firebase/CoreOnly
- FirebaseMessaging (~> 10.7.0)
- firebase_core (2.9.0):
- Firebase/CoreOnly (= 10.7.0)
- FirebaseMessaging (~> 10.12.0)
- firebase_core (2.15.1):
- Firebase/CoreOnly (= 10.12.0)
- Flutter
- firebase_messaging (14.4.0):
- Firebase/Messaging (= 10.7.0)
- firebase_messaging (14.6.6):
- Firebase/Messaging (= 10.12.0)
- firebase_core
- Flutter
- FirebaseCore (10.7.0):
- FirebaseCore (10.12.0):
- FirebaseCoreInternal (~> 10.0)
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/Logger (~> 7.8)
- FirebaseCoreInternal (10.13.0):
- FirebaseCoreInternal (10.14.0):
- "GoogleUtilities/NSData+zlib (~> 7.8)"
- FirebaseInstallations (10.13.0):
- FirebaseInstallations (10.14.0):
- FirebaseCore (~> 10.0)
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/UserDefaults (~> 7.8)
- PromisesObjC (~> 2.1)
- FirebaseMessaging (10.7.0):
- FirebaseMessaging (10.12.0):
- FirebaseCore (~> 10.0)
- FirebaseInstallations (~> 10.0)
- GoogleDataTransport (~> 9.2)
Expand Down Expand Up @@ -117,20 +117,20 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
audio_session: 4f3e461722055d21515cf3261b64c973c062f345
Firebase: 0219acf760880eeec8ce479895bd7767466d9f81
firebase_core: d85432877e814811e040e7659f9c82faeab66e04
firebase_messaging: 45c0514ca78426630338a42fb7b55af962c7ccd2
FirebaseCore: e317665b9d744727a97e623edbbed009320afdd7
FirebaseCoreInternal: b342e37cd4f5b4454ec34308f073420e7920858e
FirebaseInstallations: b28af1b9f997f1a799efe818c94695a3728c352f
FirebaseMessaging: ac9062bcc35ed56e15a0241d8fd317022499baf8
Firebase: 07150e75d142fb9399f6777fa56a187b17f833a0
firebase_core: 4a3246a02f828a01c74a2c26427037786d90f17f
firebase_messaging: 13b378c8449cae7ec96c79570170943dd73d4738
FirebaseCore: f86a1394906b97ac445ae49c92552a9425831bed
FirebaseCoreInternal: d558159ee6cc4b823c2296ecc193de9f6d9a5bb3
FirebaseInstallations: f672b1eda64e6381c21d424a2f680a943fd83f3b
FirebaseMessaging: bb2c4f6422a753038fe137d90ae7c1af57251316
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743
flutter_webrtc: 1944895d4e908c4bc722929dc4b9f8620d8e1b2f
GoogleDataTransport: 54dee9d48d14580407f8f5fbf2f496e92437a2f2
GoogleUtilities: 13e2c67ede716b8741c7989e26893d151b2b2084
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
path_provider_foundation: c68054786f1b4f3343858c1e1d0caaded73f0be9
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
url_launcher_ios: 08a3dfac5fb39e8759aeb0abbd5d9480f30fc8b4
WebRTC-SDK: dd913fd31cfbf1d43b9a22d83f4c6354c960c623
Expand Down
36 changes: 25 additions & 11 deletions lib/blocs/call_view_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:mqtt/mqtt.dart';
import 'package:mqtt_client/mqtt_client.dart' as mqtt;
import 'package:uuid/uuid.dart';
import 'package:path/path.dart' as path;

Expand Down Expand Up @@ -51,10 +52,15 @@ class CallViewBloc extends Bloc<CallEvent, CallState> {
emit(CallInitatedState());

final MqttClient client = MqttClient(home.uri);
await client.connect(
username: home.username ?? "",
password: home.password ?? "",
);
try {
await client.connect(
username: home.username ?? "",
password: home.password ?? "",
);
} on mqtt.NoConnectionException catch (exception) {
emit(CallCancelState(exception.toString()));
return;
}

String uuid = const Uuid().v4();

Expand Down Expand Up @@ -118,12 +124,14 @@ class CallViewBloc extends Bloc<CallEvent, CallState> {

String answerChannel = const Uuid().v4();
final operation = CancelableOperation.fromFuture(
client.once(
path.normalize(
"./${home.uri.path}/rtc/connections/create/$uuid/$answerChannel",
),
timeout: const Duration(seconds: 15),
),
client
.once(
path.normalize(
"./${home.uri.path}/rtc/connections/create/$uuid/$answerChannel",
),
timeout: const Duration(seconds: 15),
)
.catchError((error) => ""),
);
client.publish(
path.normalize("./${home.uri.path}/rtc/connections/create/$uuid"),
Expand All @@ -135,9 +143,15 @@ class CallViewBloc extends Bloc<CallEvent, CallState> {
_requestOperation = operation;

await operation.value.then((value) async {
if (value.isEmpty) {
emit(CallCancelState("the doorunit did not send a repsonse"));
return;
}

Response response = Response.fromMap(jsonDecode(value));
if (response.statusCode != 201) {
add(CallHangup());
return;
}

final Map<String, dynamic> answer = jsonDecode(response.body);
Expand Down Expand Up @@ -166,7 +180,7 @@ class CallViewBloc extends Bloc<CallEvent, CallState> {
);
}).onError<TimeoutException>((exception, stackTrace) async {
emit(CallCancelState(
"could not connect to the given doorunit",
"the doorunit did not respond to the message",
));
rtcclient?.ressource.close();
await rtcclient?.dispose();
Expand Down
33 changes: 22 additions & 11 deletions lib/blocs/home_add_view_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,43 @@ class HomeAddViewBloc extends Bloc<HomeAddEvent, HomeAddState> {
home.username = event.username;
home.password = event.password;

emit(HomeAddLoadingState());
final client = MqttClient(home.uri);
try {
await client.connect(
username: home.username ?? "",
password: home.password ?? "",
);
} catch (e) {
emit(
HomeAddErrorState(
"Could not conenct the the server ${uri.scheme}://${uri.host}:${uri.port} because ${e.toString()}",
),
);
return;
}

await homeRepository.add(home);
await homeRepository.select(home);
emit(HomeAddSuccessfulState());

Box settingsBox = Hive.box("settings");
String? token = settingsBox.get("token");

if (token == null) {
emit(HomeAddSuccessfulState());
return;
}

final client = MqttClient(home.uri);
await client.connect(
username: home.username ?? "",
password: home.password ?? "",
);

client.publish(
await client.publish(
path.normalize("./${home.uri.path}/devices/save"),
Request.fromMap(
Request.withJsonBody(
"GET",
Device(
token,
signs: [home.uri.fragment],
).toMap(),
).toJsonString(),
);

emit(HomeAddSuccessfulState());
client.disconnect();
}
}
28 changes: 24 additions & 4 deletions lib/handlers/notification_handler.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:math';
import 'package:http/http.dart' as http;

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Expand All @@ -11,6 +12,7 @@ Future<void> onBackgroundNotificationReceived(RemoteMessage message) async {
Future<void> _notification(RemoteMessage message) async {
String? title = message.data["title"] ?? message.notification?.title;
String? body = message.data["body"] ?? message.notification?.body;
int id = int.tryParse(message.data["id"] ?? "") ?? Random().nextInt(100);

if (null == title || null == body) return;

Expand All @@ -21,14 +23,32 @@ Future<void> _notification(RemoteMessage message) async {
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
),
);
int id = Random().nextInt(100);

StyleInformation? styleInformation;

if (message.data["image"] != null) {
final http.Response response = await http.get(
Uri.parse(message.data["imageUrl"]),
);

styleInformation = BigPictureStyleInformation(
ByteArrayAndroidBitmap(response.bodyBytes),
);
}

await plugin.show(
id,
title,
body,
const NotificationDetails(
iOS: DarwinNotificationDetails(),
android: AndroidNotificationDetails("dieklingel", "dieklingle"),
NotificationDetails(
iOS: const DarwinNotificationDetails(
sound: "ringtone.wav",
),
android: AndroidNotificationDetails(
"dieklingel",
"dieklingle",
styleInformation: styleInformation,
),
),
);
}
Expand Down
15 changes: 10 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ class _App extends State<App> {
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');

if (settings.authorizationStatus != AuthorizationStatus.authorized) {
return;
}
String? token = await FirebaseMessaging.instance.getToken();
if (null == token) return;
print("Token: $token");
if (null == token) {
return;
}

Box settingsBox = Hive.box("settings");
settingsBox.put("token", token);
Expand All @@ -110,9 +114,10 @@ class _App extends State<App> {
username: home.username ?? "",
password: home.password ?? "",
);
client.publish(
await client.publish(
path.normalize("./${home.uri.path}/devices/save"),
Request.fromMap(
Request.withJsonBody(
"GET",
Device(
token,
signs: [home.uri.fragment],
Expand Down
8 changes: 8 additions & 0 deletions lib/states/home_add_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class HomeAddInitialState extends HomeAddState {
});
}

class HomeAddErrorState extends HomeAddState {
final String errorMessage;

HomeAddErrorState(this.errorMessage);
}

class HomeAddLoadingState extends HomeAddState {}

class HomeAddFormErrorState extends HomeAddState {
final String? nameError;
final String? serverError;
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/rtc_client_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class RtcClientWrapper {
}

_applyMicrophoneSettings();
_applySpeakerSettings();
_applySpeakerSettings(media: event.streams);

renderer.srcObject = event.streams.first;
}
Expand All @@ -193,8 +193,8 @@ class RtcClientWrapper {
}
}

void _applySpeakerSettings() async {
final streams = connection.getRemoteStreams();
void _applySpeakerSettings({List<MediaStream> media = const []}) async {
final streams = [...connection.getRemoteStreams(), ...media];
for (final stream in streams) {
if (stream == null) {
continue;
Expand Down
Loading

0 comments on commit 35f4c84

Please sign in to comment.