This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/managers/mobile_app_integration_manager.class.dart

166 lines
6.2 KiB
Dart
Raw Normal View History

2019-08-31 22:06:52 +03:00
part of '../main.dart';
class MobileAppIntegrationManager {
2020-04-15 14:59:40 +03:00
static const INTEGRATION_VERSION = 3;
2019-08-31 22:06:52 +03:00
static final _appRegistrationData = {
2019-10-23 21:22:52 +03:00
"device_name": "",
2020-04-15 14:59:40 +03:00
"app_version": "$appVersion",
"manufacturer": DeviceInfoManager().manufacturer,
"model": DeviceInfoManager().model,
"os_version": DeviceInfoManager().osVersion,
2019-08-31 22:06:52 +03:00
"app_data": {
2019-10-23 21:22:52 +03:00
"push_token": "",
2019-08-31 22:06:52 +03:00
"push_url": "https://us-central1-ha-client-c73c4.cloudfunctions.net/sendPushNotification"
}
};
static String getDefaultDeviceName() {
if (HomeAssistant().userName.isEmpty) {
return '${DeviceInfoManager().model}';
}
return '${HomeAssistant().userName}\'s ${DeviceInfoManager().model}';
}
2020-05-03 18:26:44 +03:00
static Future checkAppRegistration() {
2019-08-31 22:06:52 +03:00
Completer completer = Completer();
_appRegistrationData["device_name"] = ConnectionManager().mobileAppDeviceName ?? getDefaultDeviceName();
2019-10-23 21:22:52 +03:00
(_appRegistrationData["app_data"] as Map)["push_token"] = "${HomeAssistant().fcmToken}";
if (ConnectionManager().webhookId == null) {
Logger.d("Mobile app was not registered yet. Registering...");
2019-08-31 22:06:52 +03:00
var registrationData = Map.from(_appRegistrationData);
registrationData.addAll({
"app_id": "ha_client",
"app_name": "$appName",
"os_name": DeviceInfoManager().osName,
2019-08-31 22:06:52 +03:00
"supports_encryption": false,
});
2020-05-06 19:40:19 +03:00
if (ConnectionManager().haVersion >= 104) {
registrationData.addAll({
"device_id": "${DeviceInfoManager().unicDeviceId}"
});
}
ConnectionManager().sendHTTPPost(
2019-08-31 22:06:52 +03:00
endPoint: "/api/mobile_app/registrations",
includeAuthHeader: true,
data: json.encode(registrationData)
).then((response) {
Logger.d("Processing registration responce...");
var responseObject = json.decode(response);
2020-04-15 14:59:40 +03:00
ConnectionManager().webhookId = responseObject["webhook_id"];
ConnectionManager().appIntegrationVersion = INTEGRATION_VERSION;
2019-08-31 22:06:52 +03:00
SharedPreferences.getInstance().then((prefs) {
prefs.setString("app-webhook-id", responseObject["webhook_id"]);
2020-04-15 14:59:40 +03:00
prefs.setInt('app-integration-version', INTEGRATION_VERSION);
2019-10-23 21:22:52 +03:00
2019-08-31 22:06:52 +03:00
completer.complete();
2020-05-03 13:30:51 +03:00
eventBus.fire(ShowPopupEvent(
2020-05-03 18:26:44 +03:00
popup: Popup(
2020-05-03 13:30:51 +03:00
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: () {
ConnectionManager().callService(domain: "homeassistant", service: "restart");
},
)
2019-08-31 22:06:52 +03:00
));
});
}).catchError((e) {
completer.complete();
2020-05-05 15:20:51 +03:00
if (e is http.Response) {
Logger.e("Error registering the app: ${e.statusCode}: ${e.body}");
} else {
Logger.e("Error registering the app: ${e?.toString()}");
}
2020-05-05 15:25:34 +03:00
_showError();
2019-08-31 22:06:52 +03:00
});
return completer.future;
} else {
Logger.d("App was previously registered. Checking...");
var updateData = {
"type": "update_registration",
"data": _appRegistrationData
};
ConnectionManager().sendHTTPPost(
endPoint: "/api/webhook/${ConnectionManager().webhookId}",
2019-08-31 22:06:52 +03:00
includeAuthHeader: false,
data: json.encode(updateData)
).then((response) {
2020-04-15 14:59:40 +03:00
var registrationData;
try {
registrationData = json.decode(response);
} catch (e) {
registrationData = null;
}
if (registrationData == null || registrationData.isEmpty) {
2020-05-05 14:33:04 +03:00
Logger.w("No registration data in response. MobileApp integration was removed or broken");
2019-08-31 22:06:52 +03:00
_askToRegisterApp();
} else {
2020-04-15 14:59:40 +03:00
if (INTEGRATION_VERSION > ConnectionManager().appIntegrationVersion) {
Logger.d('App registration needs to be updated');
_askToRemoveAndRegisterApp();
} else {
Logger.d('App registration works fine');
2019-08-31 22:06:52 +03:00
}
}
completer.complete();
}).catchError((e) {
if (e is http.Response && e.statusCode == 410) {
2020-05-05 14:33:04 +03:00
Logger.w("MobileApp integration was removed");
2019-08-31 22:06:52 +03:00
_askToRegisterApp();
2020-05-05 15:25:34 +03:00
} else if (e is http.Response) {
Logger.w("Error updating app registration: ${e.statusCode}: ${e.body}");
_showError();
2019-08-31 22:06:52 +03:00
} else {
2020-05-05 15:25:34 +03:00
Logger.w("Error updating app registration: ${e?.toString()}");
_showError();
2019-08-31 22:06:52 +03:00
}
completer.complete();
});
return completer.future;
}
}
static void _showError() {
eventBus.fire(ShowPopupEvent(
2020-05-03 18:26:44 +03:00
popup: Popup(
title: "App integration is not working properly",
body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue. You can try to remove Mobile App integration from Home Assistant and restart server to fix this issue.",
positiveText: "Report to GitHub",
negativeText: "Report to Discord",
onPositive: () {
Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new");
},
onNegative: () {
2020-05-03 21:56:28 +03:00
Launcher.launchURLInBrowser("https://discord.gg/u9vq7QE");
},
)
));
}
2020-04-15 14:59:40 +03:00
static void _askToRemoveAndRegisterApp() {
2020-05-03 13:30:51 +03:00
eventBus.fire(ShowPopupEvent(
2020-05-03 18:26:44 +03:00
popup: Popup(
2020-05-03 13:30:51 +03:00
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.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new");
},
)
2020-04-15 14:59:40 +03:00
));
}
2019-08-31 22:06:52 +03:00
static void _askToRegisterApp() {
2020-05-03 13:30:51 +03:00
eventBus.fire(ShowPopupEvent(
2020-05-03 18:26:44 +03:00
popup: RegisterAppPopup(
title: "Mobile App integration is missing",
body: "Looks like mobile app integration was removed from your Home Assistant or it needs to be updated.",
2020-05-03 13:30:51 +03:00
)
2019-08-31 22:06:52 +03:00
));
}
}