From 3e12f4f8a43bfbef8e99769b4d9c2ec30debdab2 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Sat, 31 Aug 2019 22:06:52 +0300 Subject: [PATCH] Create MobileAppIntegrationManager --- lib/home_assistant.class.dart | 120 +---------------- lib/main.dart | 1 + .../mobile_app_integration_manager.class.dart | 121 ++++++++++++++++++ lib/panels/config_panel_widget.dart | 4 +- 4 files changed, 125 insertions(+), 121 deletions(-) create mode 100644 lib/managers/mobile_app_integration_manager.class.dart diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index f2cc39e..0675c25 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -66,7 +66,7 @@ class HomeAssistant { if (isMobileAppEnabled) { _createUI(); _fetchCompleter.complete(); - checkAppRegistration(); + MobileAppIntegrationManager.checkAppRegistration(); } else { _fetchCompleter.completeError(HAError("Mobile app component not found", actions: [HAErrorAction.tryAgain(), HAErrorAction(type: HAErrorActionType.URL ,title: "Help",url: "http://ha-client.homemade.systems/docs#mobile-app")])); } @@ -85,124 +85,6 @@ class HomeAssistant { }); } - Map _getAppRegistrationData() { - return { - "app_version": "$appVersion", - "device_name": "$userName's ${Device().model}", - "manufacturer": Device().manufacturer, - "model": Device().model, - "os_version": Device().osVersion, - "app_data": { - "push_token": "$fcmToken", - "push_url": "https://us-central1-ha-client-c73c4.cloudfunctions.net/sendPushNotification" - } - }; - } - - Future checkAppRegistration({bool forceRegister: false, bool showOkDialog: false}) { - Completer completer = Completer(); - if (Connection().webhookId == null || forceRegister) { - Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); - var registrationData = _getAppRegistrationData(); - registrationData.addAll({ - "app_id": "ha_client", - "app_name": "$appName", - "os_name": Device().osName, - "supports_encryption": false, - }); - Connection().sendHTTPPost( - endPoint: "/api/mobile_app/registrations", - includeAuthHeader: true, - data: json.encode(registrationData) - ).then((response) { - Logger.d("Processing registration responce..."); - var responseObject = json.decode(response); - SharedPreferences.getInstance().then((prefs) { - prefs.setString("app-webhook-id", responseObject["webhook_id"]); - Connection().webhookId = responseObject["webhook_id"]; - completer.complete(); - eventBus.fire(ShowPopupDialogEvent( - title: "Mobile app Integration was created", - body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant", - positiveText: "Restart now", - negativeText: "Later", - onPositive: () { - Connection().callService(domain: "homeassistant", service: "restart", entityId: null); - }, - )); - }); - }).catchError((e) { - completer.complete(); - Logger.e("Error registering the app: ${e.toString()}"); - }); - return completer.future; - } else { - Logger.d("App was previously registered. Checking..."); - var updateData = { - "type": "update_registration", - "data": _getAppRegistrationData() - }; - Connection().sendHTTPPost( - endPoint: "/api/webhook/${Connection().webhookId}", - includeAuthHeader: false, - data: json.encode(updateData) - ).then((response) { - if (response == null || response.isEmpty) { - Logger.d("No registration data in response. MobileApp integration was removed"); - _askToRegisterApp(); - } else { - Logger.d("App registration works fine"); - if (showOkDialog) { - eventBus.fire(ShowPopupDialogEvent( - title: "All good", - body: "HA Client integration with your Home Assistant server works fine", - positiveText: "Nice!", - negativeText: "Ok" - )); - } - } - completer.complete(); - }).catchError((e) { - if (e['code'] != null && e['code'] == 410) { - Logger.e("MobileApp integration was removed"); - _askToRegisterApp(); - } else { - Logger.e("Error updating app registration: ${e.toString()}"); - eventBus.fire(ShowPopupDialogEvent( - title: "App integration is not working properly", - body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.", - positiveText: "Report to GitHub", - negativeText: "Report to Discord", - onPositive: () { - HAUtils.launchURL("https://github.com/estevez-dev/ha_client/issues/new"); - }, - onNegative: () { - HAUtils.launchURL("https://discord.gg/AUzEvwn"); - }, - )); - } - completer.complete(); - }); - return completer.future; - } - } - - void _askToRegisterApp() { - eventBus.fire(ShowPopupDialogEvent( - title: "App integration was removed", - body: "Looks like app integration was removed from your Home Assistant. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and other useful stuff.", - positiveText: "Register now", - negativeText: "Cancel", - onPositive: () { - SharedPreferences.getInstance().then((prefs) { - prefs.remove("app-webhook-id"); - Connection().webhookId = null; - HomeAssistant().checkAppRegistration(); - }); - }, - )); - } - Future _getConfig() async { await Connection().sendSocketMessage(type: "get_config").then((data) { _instanceConfig = Map.from(data); diff --git a/lib/main.dart b/lib/main.dart index 6cf07b8..2a9a9ba 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -103,6 +103,7 @@ part 'mdi.class.dart'; part 'entity_collection.class.dart'; part 'managers/auth_manager.class.dart'; part 'managers/location_manager.class.dart'; +part 'managers/mobile_app_integration_manager.class.dart'; part 'connection.class.dart'; part 'device.class.dart'; part 'ui_class/ui.dart'; diff --git a/lib/managers/mobile_app_integration_manager.class.dart b/lib/managers/mobile_app_integration_manager.class.dart new file mode 100644 index 0000000..fbd2b91 --- /dev/null +++ b/lib/managers/mobile_app_integration_manager.class.dart @@ -0,0 +1,121 @@ +part of '../main.dart'; + +class MobileAppIntegrationManager { + + static final _appRegistrationData = { + "app_version": "$appVersion", + "device_name": "${HomeAssistant().userName}'s ${Device().model}", + "manufacturer": Device().manufacturer, + "model": Device().model, + "os_version": Device().osVersion, + "app_data": { + "push_token": "${HomeAssistant().fcmToken}", + "push_url": "https://us-central1-ha-client-c73c4.cloudfunctions.net/sendPushNotification" + } + }; + + static Future checkAppRegistration({bool forceRegister: false, bool showOkDialog: false}) { + Completer completer = Completer(); + if (Connection().webhookId == null || forceRegister) { + Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); + var registrationData = Map.from(_appRegistrationData); + registrationData.addAll({ + "app_id": "ha_client", + "app_name": "$appName", + "os_name": Device().osName, + "supports_encryption": false, + }); + Connection().sendHTTPPost( + endPoint: "/api/mobile_app/registrations", + includeAuthHeader: true, + data: json.encode(registrationData) + ).then((response) { + Logger.d("Processing registration responce..."); + var responseObject = json.decode(response); + SharedPreferences.getInstance().then((prefs) { + prefs.setString("app-webhook-id", responseObject["webhook_id"]); + Connection().webhookId = responseObject["webhook_id"]; + completer.complete(); + eventBus.fire(ShowPopupDialogEvent( + title: "Mobile app Integration was created", + body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant", + positiveText: "Restart now", + negativeText: "Later", + onPositive: () { + Connection().callService(domain: "homeassistant", service: "restart", entityId: null); + }, + )); + }); + }).catchError((e) { + completer.complete(); + Logger.e("Error registering the app: ${e.toString()}"); + }); + return completer.future; + } else { + Logger.d("App was previously registered. Checking..."); + var updateData = { + "type": "update_registration", + "data": _appRegistrationData + }; + Connection().sendHTTPPost( + endPoint: "/api/webhook/${Connection().webhookId}", + includeAuthHeader: false, + data: json.encode(updateData) + ).then((response) { + if (response == null || response.isEmpty) { + Logger.d("No registration data in response. MobileApp integration was removed"); + _askToRegisterApp(); + } else { + Logger.d("App registration works fine"); + if (showOkDialog) { + eventBus.fire(ShowPopupDialogEvent( + title: "All good", + body: "HA Client integration with your Home Assistant server works fine", + positiveText: "Nice!", + negativeText: "Ok" + )); + } + } + completer.complete(); + }).catchError((e) { + if (e['code'] != null && e['code'] == 410) { + Logger.e("MobileApp integration was removed"); + _askToRegisterApp(); + } else { + Logger.e("Error updating app registration: ${e.toString()}"); + eventBus.fire(ShowPopupDialogEvent( + title: "App integration is not working properly", + body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.", + positiveText: "Report to GitHub", + negativeText: "Report to Discord", + onPositive: () { + HAUtils.launchURL("https://github.com/estevez-dev/ha_client/issues/new"); + }, + onNegative: () { + HAUtils.launchURL("https://discord.gg/AUzEvwn"); + }, + )); + } + completer.complete(); + }); + return completer.future; + } + } + + static void _askToRegisterApp() { + eventBus.fire(ShowPopupDialogEvent( + title: "App integration was removed", + body: "Looks like app integration was removed from your Home Assistant. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and other useful stuff.", + positiveText: "Register now", + negativeText: "Cancel", + onPositive: () { + SharedPreferences.getInstance().then((prefs) { + prefs.remove("app-webhook-id"); + Connection().webhookId = null; + checkAppRegistration(); + }); + }, + )); + } + +} \ No newline at end of file diff --git a/lib/panels/config_panel_widget.dart b/lib/panels/config_panel_widget.dart index a18058a..1bbab44 100644 --- a/lib/panels/config_panel_widget.dart +++ b/lib/panels/config_panel_widget.dart @@ -75,7 +75,7 @@ class _ConfigPanelWidgetState extends State { } updateRegistration() { - HomeAssistant().checkAppRegistration(showOkDialog: true); + MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true); } resetRegistration() { @@ -85,7 +85,7 @@ class _ConfigPanelWidgetState extends State { positiveText: "Done it already", negativeText: "Ok, I will", onPositive: () { - HomeAssistant().checkAppRegistration(showOkDialog: true, forceRegister: true); + MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true, forceRegister: true); }, )); }