Implement fetch timer with 30 timeout along with connection timer

This commit is contained in:
estevez 2018-10-02 16:00:55 +03:00
parent b14b248f2f
commit 41297150c2
2 changed files with 14 additions and 3 deletions

View File

@ -22,6 +22,7 @@ class HomeAssistant {
Completer _configCompleter;
Completer _connectionCompleter;
Timer _connectionTimer;
Timer _fetchTimer;
String get locationName => _instanceConfig["location_name"] ?? "";
int get viewsCount => _entities.viewList.length ?? 0;
@ -42,6 +43,10 @@ class HomeAssistant {
TheLogger.log("Warning","Previous fetch is not complited");
} else {
_fetchCompleter = new Completer();
_fetchTimer = Timer(Duration(seconds: 30), () {
closeConnection();
_finishFetching({"errorCode" : 9,"errorMessage": "Connection timeout or connection issues"});
});
_reConnectSocket().then((r) {
_getData();
}).catchError((e) {
@ -100,9 +105,13 @@ class HomeAssistant {
}
void _finishFetching(error) {
_fetchTimer.cancel();
_finishConnecting(error);
if (error != null) {
if (!_fetchCompleter.isCompleted)
_fetchCompleter.completeError(error);
} else {
if (!_fetchCompleter.isCompleted)
_fetchCompleter.complete();
}
}
@ -110,6 +119,7 @@ class HomeAssistant {
void _finishConnecting(error) {
_connectionTimer.cancel();
if (error != null) {
if (!_connectionCompleter.isCompleted)
_connectionCompleter.completeError(error);
} else {
if (!_connectionCompleter.isCompleted)

View File

@ -505,6 +505,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
String message = _lastErrorMessage;
SnackBarAction action;
switch (_errorCodeToBeShown) {
case 9:
case 1: {
action = SnackBarAction(
label: "Retry",