Resolves #353 Show error if connection drops
This commit is contained in:
@ -100,12 +100,12 @@ class Connection {
|
|||||||
|
|
||||||
Future _connect() async {
|
Future _connect() async {
|
||||||
if (connecting != null && !connecting.isCompleted) {
|
if (connecting != null && !connecting.isCompleted) {
|
||||||
Logger.w("");
|
Logger.w("Previous connection attempt pending...");
|
||||||
return connecting.future;
|
return connecting.future;
|
||||||
}
|
} else {
|
||||||
connecting = Completer();
|
connecting = Completer();
|
||||||
await _disconnect();
|
await _disconnect();
|
||||||
Logger.d( "Socket connecting...");
|
Logger.d("Socket connecting...");
|
||||||
_socket = IOWebSocketChannel.connect(
|
_socket = IOWebSocketChannel.connect(
|
||||||
_webSocketAPIEndpoint, pingInterval: Duration(seconds: 15));
|
_webSocketAPIEndpoint, pingInterval: Duration(seconds: 15));
|
||||||
_socketSubscription = _socket.stream.listen(
|
_socketSubscription = _socket.stream.listen(
|
||||||
@ -114,7 +114,8 @@ class Connection {
|
|||||||
var data = json.decode(message);
|
var data = json.decode(message);
|
||||||
if (data["type"] == "auth_required") {
|
if (data["type"] == "auth_required") {
|
||||||
Logger.d("[Received] <== ${data.toString()}");
|
Logger.d("[Received] <== ${data.toString()}");
|
||||||
_authenticate().then((_) => connecting.complete()).catchError((e) {
|
_authenticate().then((_) => connecting.complete()).catchError((
|
||||||
|
e) {
|
||||||
if (!connecting.isCompleted) connecting.completeError(e);
|
if (!connecting.isCompleted) connecting.completeError(e);
|
||||||
});
|
});
|
||||||
} else if (data["type"] == "auth_ok") {
|
} else if (data["type"] == "auth_ok") {
|
||||||
@ -124,10 +125,12 @@ class Connection {
|
|||||||
if (!connecting.isCompleted) connecting.complete();
|
if (!connecting.isCompleted) connecting.complete();
|
||||||
} else if (data["type"] == "auth_invalid") {
|
} else if (data["type"] == "auth_invalid") {
|
||||||
Logger.d("[Received] <== ${data.toString()}");
|
Logger.d("[Received] <== ${data.toString()}");
|
||||||
_messageResolver["auth"]?.completeError({"errorCode": 62, "errorMessage": "${data["message"]}"});
|
_messageResolver["auth"]?.completeError(
|
||||||
|
{"errorCode": 62, "errorMessage": "${data["message"]}"});
|
||||||
_messageResolver.remove("auth");
|
_messageResolver.remove("auth");
|
||||||
logout().then((_) {
|
logout().then((_) {
|
||||||
if (!connecting.isCompleted) connecting.completeError({"errorCode": 62, "errorMessage": "${data["message"]}"});
|
if (!connecting.isCompleted) connecting.completeError(
|
||||||
|
{"errorCode": 62, "errorMessage": "${data["message"]}"});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
_handleMessage(data);
|
_handleMessage(data);
|
||||||
@ -139,6 +142,7 @@ class Connection {
|
|||||||
);
|
);
|
||||||
return connecting.future;
|
return connecting.future;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_disconnect() async {
|
_disconnect() async {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
@ -147,7 +151,7 @@ class Connection {
|
|||||||
await _socketSubscription?.cancel();
|
await _socketSubscription?.cancel();
|
||||||
}
|
}
|
||||||
if (_socket != null && _socket.sink != null) {
|
if (_socket != null && _socket.sink != null) {
|
||||||
await _socket.sink.close().timeout(Duration(seconds: 5),
|
await _socket.sink.close().timeout(Duration(seconds: 3),
|
||||||
onTimeout: () => Logger.d("Socket sink close timeout")
|
onTimeout: () => Logger.d("Socket sink close timeout")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -187,7 +191,9 @@ class Connection {
|
|||||||
_disconnect().then((_) {
|
_disconnect().then((_) {
|
||||||
Timer(Duration(seconds: 5), () {
|
Timer(Duration(seconds: 5), () {
|
||||||
Logger.d("Trying to reconnect...");
|
Logger.d("Trying to reconnect...");
|
||||||
_connect();
|
_connect().catchError((e) {
|
||||||
|
eventBus.fire(ShowErrorEvent("Unable to connect to Home Assistant", 81));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -202,7 +208,9 @@ class Connection {
|
|||||||
_disconnect().then((_) {
|
_disconnect().then((_) {
|
||||||
Timer(Duration(seconds: 5), () {
|
Timer(Duration(seconds: 5), () {
|
||||||
Logger.d("Trying to reconnect...");
|
Logger.d("Trying to reconnect...");
|
||||||
_connect();
|
_connect().catchError((e) {
|
||||||
|
eventBus.fire(ShowErrorEvent("Unable to connect to Home Assistant", 81));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -283,16 +291,17 @@ class Connection {
|
|||||||
}
|
}
|
||||||
_messageResolver[callbackName] = _completer;
|
_messageResolver[callbackName] = _completer;
|
||||||
String rawMessage = json.encode(dataObject);
|
String rawMessage = json.encode(dataObject);
|
||||||
Logger.d("[Sending] ==> $rawMessage");
|
|
||||||
if (!isConnected) {
|
if (!isConnected) {
|
||||||
_connect().timeout(connectTimeout, onTimeout: (){
|
_connect().timeout(connectTimeout, onTimeout: (){
|
||||||
_completer.completeError({"errorCode": 8, "errorMessage": "No connection to Home Assistant"});
|
_completer.completeError({"errorCode": 8, "errorMessage": "No connection to Home Assistant"});
|
||||||
}).then((_) {
|
}).then((_) {
|
||||||
|
Logger.d("[Sending] ==> $rawMessage");
|
||||||
_socket.sink.add(rawMessage);
|
_socket.sink.add(rawMessage);
|
||||||
}).catchError((e) {
|
}).catchError((e) {
|
||||||
_completer.completeError(e);
|
_completer.completeError(e);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
Logger.d("[Sending] ==> $rawMessage");
|
||||||
_socket.sink.add(rawMessage);
|
_socket.sink.add(rawMessage);
|
||||||
}
|
}
|
||||||
return _completer.future;
|
return _completer.future;
|
||||||
|
Reference in New Issue
Block a user