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

124 lines
4.8 KiB
Dart
Raw Normal View History

2019-08-31 22:06:52 +03:00
part of '../main.dart';
class MobileAppIntegrationManager {
static final _appRegistrationData = {
"app_version": "$appVersion",
2019-10-23 21:22:52 +03:00
"device_name": "",
"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 Future checkAppRegistration({bool forceRegister: false, bool showOkDialog: false}) {
Completer completer = Completer();
2019-10-23 21:22:52 +03:00
_appRegistrationData["device_name"] = "${HomeAssistant().userName}'s ${DeviceInfoManager().model}";
(_appRegistrationData["app_data"] as Map)["push_token"] = "${HomeAssistant().fcmToken}";
if (ConnectionManager().webhookId == null || forceRegister) {
2019-08-31 22:06:52 +03:00
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": DeviceInfoManager().osName,
2019-08-31 22:06:52 +03:00
"supports_encryption": false,
});
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);
SharedPreferences.getInstance().then((prefs) {
prefs.setString("app-webhook-id", responseObject["webhook_id"]);
ConnectionManager().webhookId = responseObject["webhook_id"];
2019-10-23 21:22:52 +03:00
2019-08-31 22:06:52 +03:00
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: () {
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: "homeassistant", service: "restart");
2019-08-31 22:06:52 +03:00
},
));
});
}).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
};
ConnectionManager().sendHTTPPost(
endPoint: "/api/webhook/${ConnectionManager().webhookId}",
2019-08-31 22:06:52 +03:00
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 is http.Response && e.statusCode == 410) {
2019-08-31 22:06:52 +03:00
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: () {
Launcher.launchURL("https://github.com/estevez-dev/ha_client/issues/new");
2019-08-31 22:06:52 +03:00
},
onNegative: () {
Launcher.launchURL("https://discord.gg/AUzEvwn");
2019-08-31 22:06:52 +03:00
},
));
}
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");
ConnectionManager().webhookId = null;
2019-08-31 22:06:52 +03:00
checkAppRegistration();
});
},
));
}
}