Resolves #61: Prevent second connection opening
This commit is contained in:
@ -20,7 +20,8 @@ class HomeAssistant {
|
|||||||
Completer _statesCompleter;
|
Completer _statesCompleter;
|
||||||
Completer _servicesCompleter;
|
Completer _servicesCompleter;
|
||||||
Completer _configCompleter;
|
Completer _configCompleter;
|
||||||
Timer _fetchingTimer;
|
Completer _connectionCompleter;
|
||||||
|
Timer _connectionTimer;
|
||||||
|
|
||||||
String get locationName => _instanceConfig["location_name"] ?? "";
|
String get locationName => _instanceConfig["location_name"] ?? "";
|
||||||
int get viewsCount => _entities.viewList.length ?? 0;
|
int get viewsCount => _entities.viewList.length ?? 0;
|
||||||
@ -40,11 +41,6 @@ class HomeAssistant {
|
|||||||
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
|
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
|
||||||
TheLogger.log("Warning","Previous fetch is not complited");
|
TheLogger.log("Warning","Previous fetch is not complited");
|
||||||
} else {
|
} else {
|
||||||
//TODO: Fetch timeout timer. Should be removed after #21 fix
|
|
||||||
_fetchingTimer = Timer(Duration(seconds: 15), () {
|
|
||||||
closeConnection();
|
|
||||||
_fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"});
|
|
||||||
});
|
|
||||||
_fetchCompleter = new Completer();
|
_fetchCompleter = new Completer();
|
||||||
_reConnectSocket().then((r) {
|
_reConnectSocket().then((r) {
|
||||||
_getData();
|
_getData();
|
||||||
@ -63,7 +59,15 @@ class HomeAssistant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future _reConnectSocket() {
|
Future _reConnectSocket() {
|
||||||
var _connectionCompleter = new Completer();
|
if ((_connectionCompleter != null) && (!_connectionCompleter.isCompleted)) {
|
||||||
|
TheLogger.log("Warning","Previous connection is not complited");
|
||||||
|
} else {
|
||||||
|
_connectionCompleter = new Completer();
|
||||||
|
//TODO: Connection timeout timer. Should be removed after #21 fix
|
||||||
|
_connectionTimer = Timer(Duration(seconds: 10), () {
|
||||||
|
closeConnection();
|
||||||
|
_connectionCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout or connection issues"});
|
||||||
|
});
|
||||||
if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) {
|
if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) {
|
||||||
TheLogger.log("Debug", "Socket connecting...");
|
TheLogger.log("Debug", "Socket connecting...");
|
||||||
_hassioChannel = IOWebSocketChannel.connect(_hassioAPIEndpoint);
|
_hassioChannel = IOWebSocketChannel.connect(_hassioAPIEndpoint);
|
||||||
@ -75,6 +79,7 @@ class HomeAssistant {
|
|||||||
} else {
|
} else {
|
||||||
_connectionCompleter.complete();
|
_connectionCompleter.complete();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return _connectionCompleter.future;
|
return _connectionCompleter.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +100,7 @@ class HomeAssistant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_finishFetching(error) {
|
_finishFetching(error) {
|
||||||
_fetchingTimer.cancel();
|
_connectionTimer.cancel();
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
_fetchCompleter.completeError(error);
|
_fetchCompleter.completeError(error);
|
||||||
} else {
|
} else {
|
||||||
@ -183,7 +188,7 @@ class HomeAssistant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _handleEntityStateChange(Map eventData) {
|
void _handleEntityStateChange(Map eventData) {
|
||||||
TheLogger.log("Debug", "New state for ${eventData['entity_id']}");
|
//TheLogger.log("Debug", "New state for ${eventData['entity_id']}");
|
||||||
_entities.updateState(eventData);
|
_entities.updateState(eventData);
|
||||||
eventBus.fire(new StateChangedEvent(eventData["entity_id"], null, false));
|
eventBus.fire(new StateChangedEvent(eventData["entity_id"], null, false));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user