MobileApp integration force update

This commit is contained in:
Yegor Vialov 2020-04-15 11:59:40 +00:00
parent c376c0e952
commit 7c2cfe3215
3 changed files with 46 additions and 16 deletions

View File

@ -149,8 +149,8 @@ EventBus eventBus = new EventBus();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
const String appName = "HA Client"; const String appName = "HA Client";
const appVersionNumber = "0.8.3"; const appVersionNumber = "0.8.4";
const appVersionAdd = ""; const appVersionAdd = "alpha1";
const appVersion = "$appVersionNumber$appVersionAdd"; const appVersion = "$appVersionNumber$appVersionAdd";
Future<void> _reportError(dynamic error, dynamic stackTrace) async { Future<void> _reportError(dynamic error, dynamic stackTrace) async {

View File

@ -20,6 +20,7 @@ class ConnectionManager {
String oauthUrl; String oauthUrl;
String webhookId; String webhookId;
bool settingsLoaded = false; bool settingsLoaded = false;
int appIntegrationVersion;
bool get isAuthenticated => _token != null; bool get isAuthenticated => _token != null;
StreamSubscription _socketSubscription; StreamSubscription _socketSubscription;
Duration connectTimeout = Duration(seconds: 15); Duration connectTimeout = Duration(seconds: 15);
@ -43,6 +44,7 @@ class ConnectionManager {
_domain = prefs.getString('hassio-domain'); _domain = prefs.getString('hassio-domain');
_port = prefs.getString('hassio-port'); _port = prefs.getString('hassio-port');
webhookId = prefs.getString('app-webhook-id'); webhookId = prefs.getString('app-webhook-id');
appIntegrationVersion = prefs.getInt('app-integration-version') ?? 0;
displayHostname = "$_domain:$_port"; displayHostname = "$_domain:$_port";
_webSocketAPIEndpoint = _webSocketAPIEndpoint =
"${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket"; "${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket";

View File

@ -2,9 +2,11 @@ part of '../main.dart';
class MobileAppIntegrationManager { class MobileAppIntegrationManager {
static const INTEGRATION_VERSION = 3;
static final _appRegistrationData = { static final _appRegistrationData = {
"app_version": "$appVersion",
"device_name": "", "device_name": "",
"app_version": "$appVersion",
"manufacturer": DeviceInfoManager().manufacturer, "manufacturer": DeviceInfoManager().manufacturer,
"model": DeviceInfoManager().model, "model": DeviceInfoManager().model,
"os_version": DeviceInfoManager().osVersion, "os_version": DeviceInfoManager().osVersion,
@ -22,6 +24,7 @@ class MobileAppIntegrationManager {
Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); Logger.d("Mobile app was not registered yet or need to be reseted. Registering...");
var registrationData = Map.from(_appRegistrationData); var registrationData = Map.from(_appRegistrationData);
registrationData.addAll({ registrationData.addAll({
"device_id": "${DeviceInfoManager().unicDeviceId}",
"app_id": "ha_client", "app_id": "ha_client",
"app_name": "$appName", "app_name": "$appName",
"os_name": DeviceInfoManager().osName, "os_name": DeviceInfoManager().osName,
@ -34,9 +37,11 @@ class MobileAppIntegrationManager {
).then((response) { ).then((response) {
Logger.d("Processing registration responce..."); Logger.d("Processing registration responce...");
var responseObject = json.decode(response); var responseObject = json.decode(response);
ConnectionManager().webhookId = responseObject["webhook_id"];
ConnectionManager().appIntegrationVersion = INTEGRATION_VERSION;
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
prefs.setString("app-webhook-id", responseObject["webhook_id"]); prefs.setString("app-webhook-id", responseObject["webhook_id"]);
ConnectionManager().webhookId = responseObject["webhook_id"]; prefs.setInt('app-integration-version', INTEGRATION_VERSION);
completer.complete(); completer.complete();
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupDialogEvent(
@ -65,11 +70,21 @@ class MobileAppIntegrationManager {
includeAuthHeader: false, includeAuthHeader: false,
data: json.encode(updateData) data: json.encode(updateData)
).then((response) { ).then((response) {
if (response == null || response.isEmpty) { var registrationData;
Logger.d("No registration data in response. MobileApp integration was removed"); try {
registrationData = json.decode(response);
} catch (e) {
registrationData = null;
}
if (registrationData == null || registrationData.isEmpty) {
Logger.d("No registration data in response. MobileApp integration was removed or broken");
_askToRegisterApp(); _askToRegisterApp();
} else { } else {
Logger.d("App registration works fine"); if (INTEGRATION_VERSION > ConnectionManager().appIntegrationVersion) {
Logger.d('App registration needs to be updated');
_askToRemoveAndRegisterApp();
} else {
Logger.d('App registration works fine');
if (showOkDialog) { if (showOkDialog) {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupDialogEvent(
title: "All good", title: "All good",
@ -79,6 +94,7 @@ class MobileAppIntegrationManager {
)); ));
} }
} }
}
completer.complete(); completer.complete();
}).catchError((e) { }).catchError((e) {
if (e is http.Response && e.statusCode == 410) { if (e is http.Response && e.statusCode == 410) {
@ -105,10 +121,22 @@ class MobileAppIntegrationManager {
} }
} }
static void _askToRemoveAndRegisterApp() {
eventBus.fire(ShowPopupDialogEvent(
title: "Mobile app integration needs to be updated",
body: "You need to update HA Client integration to continue using notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant and restart Home Assistant. Then go back to HA Client to create app integration again.",
positiveText: "Ok",
negativeText: "Report an issue",
onNegative: () {
Launcher.launchURL("https://github.com/estevez-dev/ha_client/issues/new");
},
));
}
static void _askToRegisterApp() { static void _askToRegisterApp() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupDialogEvent(
title: "App integration was removed", title: "App integration is broken",
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.", body: "Looks like app integration was removed from your Home Assistant or it needs to be updated. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant before registering and restart Home Assistant. Then go back here.",
positiveText: "Register now", positiveText: "Register now",
negativeText: "Cancel", negativeText: "Cancel",
onPositive: () { onPositive: () {