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/connection_manager.class.dart

428 lines
15 KiB
Dart
Raw Normal View History

part of '../main.dart';
2019-03-26 00:18:30 +02:00
class ConnectionManager {
2019-03-26 00:18:30 +02:00
static final ConnectionManager _instance = ConnectionManager._internal();
2019-03-26 00:18:30 +02:00
factory ConnectionManager() {
2019-03-26 00:18:30 +02:00
return _instance;
}
ConnectionManager._internal();
2019-03-26 00:18:30 +02:00
2019-04-05 11:48:41 +03:00
String _domain;
String _port;
2019-03-26 00:18:30 +02:00
String displayHostname;
String _webSocketAPIEndpoint;
String httpWebHost;
String _token;
String _tempToken;
String oauthUrl;
String webhookId;
2019-04-05 11:48:41 +03:00
bool useLovelace = true;
bool settingsLoaded = false;
2019-03-26 00:18:30 +02:00
bool get isAuthenticated => _token != null;
StreamSubscription _socketSubscription;
Duration connectTimeout = Duration(seconds: 15);
bool isConnected = false;
var onStateChangeCallback;
IOWebSocketChannel _socket;
int _currentMessageId = 0;
Map<String, Completer> _messageResolver = {};
2019-04-05 11:48:41 +03:00
Future init({bool loadSettings, bool forceReconnect: false}) async {
2019-03-26 00:18:30 +02:00
Completer completer = Completer();
bool stopInit = false;
2019-04-05 11:48:41 +03:00
if (loadSettings) {
Logger.e("Loading settings...");
SharedPreferences prefs = await SharedPreferences.getInstance();
useLovelace = prefs.getBool('use-lovelace') ?? true;
_domain = prefs.getString('hassio-domain');
_port = prefs.getString('hassio-port');
webhookId = prefs.getString('app-webhook-id');
2019-04-05 11:48:41 +03:00
displayHostname = "$_domain:$_port";
_webSocketAPIEndpoint =
"${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket";
httpWebHost =
"${prefs.getString('hassio-res-protocol')}://$_domain:$_port";
if ((_domain == null) || (_port == null) ||
(_domain.isEmpty) || (_port.isEmpty)) {
2019-09-04 22:46:14 +03:00
completer.completeError(HAError.checkConnectionSettings());
stopInit = true;
} else {
final storage = new FlutterSecureStorage();
try {
_token = await storage.read(key: "hacl_llt");
2019-09-04 22:46:14 +03:00
Logger.e("Long-lived token read successful");
2019-09-04 23:40:37 +03:00
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
2020-01-29 19:31:02 +02:00
'http://ha-client.estevez.dev')}&redirect_uri=${Uri
2019-09-04 23:40:37 +03:00
.encodeComponent(
2020-01-29 19:31:02 +02:00
'http://ha-client.estevez.dev/service/auth_callback.html')}";
2019-09-04 23:40:37 +03:00
settingsLoaded = true;
} catch (e) {
2019-09-04 23:40:37 +03:00
completer.completeError(HAError("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));
Logger.e("Cannt read secure storage. Need to relogin.");
2019-09-04 23:40:37 +03:00
stopInit = true;
}
}
} else {
if ((_domain == null) || (_port == null) ||
(_domain.isEmpty) || (_port.isEmpty)) {
2019-09-04 22:46:14 +03:00
completer.completeError(HAError.checkConnectionSettings());
stopInit = true;
2019-04-05 11:48:41 +03:00
}
}
if (!stopInit) {
if (_token == null) {
AuthManager().start(
oauthUrl: oauthUrl
).then((token) {
Logger.d("Token from AuthManager recived");
_tempToken = token;
_doConnect(completer: completer, forceReconnect: forceReconnect);
}).catchError((e) {
completer.completeError(e);
});
} else {
2019-04-05 11:48:41 +03:00
_doConnect(completer: completer, forceReconnect: forceReconnect);
}
2019-04-05 11:48:41 +03:00
}
return completer.future;
}
void _doConnect({Completer completer, bool forceReconnect}) {
if (forceReconnect || !isConnected) {
2019-11-29 14:45:59 +02:00
_disconnect().then((_){
_connect().timeout(connectTimeout).then((_) {
completer?.complete();
}).catchError((e) {
_disconnect().then((_) {
if (e is TimeoutException) {
if (connecting != null && !connecting.isCompleted) {
connecting.completeError(HAError("Connection timeout"));
}
completer?.completeError(HAError("Connection timeout"));
} else if (e is HAError) {
completer?.completeError(e);
} else {
completer?.completeError(HAError("${e.toString()}"));
2019-11-29 12:58:24 +02:00
}
2019-11-29 14:45:59 +02:00
});
});
});
2019-04-05 11:48:41 +03:00
} else {
completer?.complete();
2019-03-26 00:18:30 +02:00
}
}
Completer connecting;
Future _connect() {
if (connecting != null && !connecting.isCompleted) {
Logger.w("Previous connection attempt pending...");
return connecting.future;
} else {
connecting = Completer();
_disconnect().then((_) {
2019-08-26 18:55:12 +03:00
Logger.d("Socket connecting...");
2019-11-29 13:21:45 +02:00
try {
_socket = IOWebSocketChannel.connect(
_webSocketAPIEndpoint, pingInterval: Duration(seconds: 15));
2019-11-29 13:21:45 +02:00
_socketSubscription = _socket.stream.listen(
(message) {
isConnected = true;
var data = json.decode(message);
if (data["type"] == "auth_required") {
Logger.d("[Received] <== ${data.toString()}");
_authenticate().then((_) {
Logger.d('Authentication complete');
connecting.complete();
}).catchError((e) {
if (!connecting.isCompleted) connecting.completeError(e);
});
} else if (data["type"] == "auth_ok") {
Logger.d("[Received] <== ${data.toString()}");
2019-11-29 15:40:51 +02:00
Logger.d("[Connection] Subscribing to events");
sendSocketMessage(
type: "subscribe_events",
additionalData: {"event_type": "state_changed"},
).whenComplete((){
_messageResolver["auth"]?.complete();
_messageResolver.remove("auth");
if (_token != null) {
if (!connecting.isCompleted) connecting.complete();
}
});
2019-11-29 13:21:45 +02:00
} else if (data["type"] == "auth_invalid") {
Logger.d("[Received] <== ${data.toString()}");
_messageResolver["auth"]?.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()]));
_messageResolver.remove("auth");
if (!connecting.isCompleted) connecting.completeError(HAError("${data["message"]}", actions: [HAErrorAction.tryAgain(title: "Retry"), HAErrorAction.loginAgain(title: "Relogin")]));
} else {
_handleMessage(data);
}
2019-11-29 13:21:45 +02:00
},
cancelOnError: true,
onDone: () => _handleSocketClose(connecting),
onError: (e) => _handleSocketError(e, connecting)
);
} catch(exeption) {
connecting.completeError(HAError("${exeption.toString()}"));
}
});
return connecting.future;
}
2019-03-26 00:18:30 +02:00
}
Future _disconnect() {
Completer completer = Completer();
if (!isConnected) {
completer.complete();
} else {
isConnected = false;
List<Future> fl = [];
Logger.d("Socket disconnecting...");
if (_socketSubscription != null) {
fl.add(_socketSubscription.cancel());
}
if (_socket != null && _socket.sink != null &&
_socket.closeCode == null) {
fl.add(_socket.sink.close().timeout(Duration(seconds: 3)));
}
Future.wait(fl).whenComplete(() => completer.complete());
2019-03-30 00:29:52 +02:00
}
return completer.future;
2019-03-26 00:18:30 +02:00
}
_handleMessage(data) {
if (data["type"] == "result") {
if (data["id"] != null && data["success"]) {
2019-09-15 18:38:02 +03:00
//Logger.d("[Received] <== Request id ${data['id']} was successful");
2019-03-26 00:18:30 +02:00
_messageResolver["${data["id"]}"]?.complete(data["result"]);
} else if (data["id"] != null) {
Logger.e("[Received] <== Error received on request id ${data['id']}: ${data['error']}");
_messageResolver["${data["id"]}"]?.completeError("${data["error"]["code"]}");
2019-03-26 00:18:30 +02:00
}
_messageResolver.remove("${data["id"]}");
} else if (data["type"] == "event") {
if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) {
2019-10-28 22:34:43 +02:00
Logger.d("[Received] <== ${data['type']}.${data["event"]["event_type"]}: ${data["event"]["data"]["entity_id"]}");
2019-03-26 00:18:30 +02:00
onStateChangeCallback(data["event"]["data"]);
} else if (data["event"] != null) {
Logger.w("Unhandled event type: ${data["event"]["event_type"]}");
} else {
Logger.e("Event is null: $data");
}
} else {
Logger.d("[Received unhandled] <== ${data.toString()}");
}
}
void _handleSocketClose(Completer connectionCompleter) {
Logger.d("Socket disconnected.");
2019-11-29 13:21:45 +02:00
_disconnect().then((_) {
if (!connectionCompleter.isCompleted) {
isConnected = false;
connectionCompleter.completeError(HAError("Disconnected", actions: [HAErrorAction.reconnect()]));
}
eventBus.fire(ShowErrorEvent(HAError("Unable to connect to Home Assistant")));
});
2019-03-26 00:18:30 +02:00
}
void _handleSocketError(e, Completer connectionCompleter) {
Logger.e("Socket stream Error: $e");
2019-11-29 13:21:45 +02:00
_disconnect().then((_) {
if (!connectionCompleter.isCompleted) {
isConnected = false;
connectionCompleter.completeError(HAError("Disconnected", actions: [HAErrorAction.reconnect()]));
}
eventBus.fire(ShowErrorEvent(HAError("Unable to connect to Home Assistant")));
});
2019-03-26 00:18:30 +02:00
}
Future _authenticate() {
Completer completer = Completer();
if (_token != null) {
Logger.d( "Long-lived token exist");
Logger.d( "[Sending] ==> auth request");
sendSocketMessage(
type: "auth",
additionalData: {"access_token": "$_token"},
auth: true
).then((_) {
completer.complete();
}).catchError((e) => completer.completeError(e));
} else if (_tempToken != null) {
Logger.d("We have temp token. Loging in...");
sendSocketMessage(
type: "auth",
additionalData: {"access_token": "$_tempToken"},
auth: true
).then((_) {
Logger.d("Requesting long-lived token...");
_getLongLivedToken().then((_) {
Logger.d("getLongLivedToken finished");
2019-03-26 00:18:30 +02:00
completer.complete();
}).catchError((e) {
Logger.e("Can't get long-lived token: $e");
throw e;
});
}).catchError((e) => completer.completeError(e));
} else {
2019-09-04 22:46:14 +03:00
completer.completeError(HAError("General login error"));
2019-03-26 00:18:30 +02:00
}
return completer.future;
}
Future logout() {
2019-09-04 23:40:37 +03:00
Logger.d("Logging out");
2019-04-05 13:06:14 +03:00
Completer completer = Completer();
_disconnect().whenComplete(() {
_token = null;
_tempToken = null;
final storage = new FlutterSecureStorage();
storage.delete(key: "hacl_llt").whenComplete((){
completer.complete();
});
});
return completer.future;
2019-03-26 00:18:30 +02:00
}
Future _getLongLivedToken() {
Completer completer = Completer();
sendSocketMessage(type: "auth/long_lived_access_token", additionalData: {"client_name": "HA Client app ${DateTime.now().millisecondsSinceEpoch}", "lifespan": 365}).then((data) {
Logger.d("Got long-lived token.");
_token = data;
_tempToken = null;
final storage = new FlutterSecureStorage();
storage.write(key: "hacl_llt", value: "$_token").then((_) {
2019-08-26 18:04:40 +03:00
SharedPreferences.getInstance().then((prefs) {
prefs.setBool("oauth-used", true);
completer.complete();
});
2019-03-26 00:18:30 +02:00
}).catchError((e) {
throw e;
});
}).catchError((e) {
2019-09-04 23:40:37 +03:00
completer.completeError(HAError("Authentication error: $e", actions: [HAErrorAction.reload(title: "Retry"), HAErrorAction.loginAgain(title: "Relogin")]));
2019-03-26 00:18:30 +02:00
});
return completer.future;
}
Future sendSocketMessage({String type, Map additionalData, bool auth: false}) {
Completer _completer = Completer();
Map dataObject = {"type": "$type"};
String callbackName;
if (!auth) {
_incrementMessageId();
dataObject["id"] = _currentMessageId;
callbackName = "$_currentMessageId";
} else {
callbackName = "auth";
}
if (additionalData != null) {
dataObject.addAll(additionalData);
}
_messageResolver[callbackName] = _completer;
String rawMessage = json.encode(dataObject);
if (!isConnected) {
_connect().timeout(connectTimeout).then((_) {
2019-08-26 18:55:12 +03:00
Logger.d("[Sending] ==> ${auth ? "type="+dataObject['type'] : rawMessage}");
_socket.sink.add(rawMessage);
}).catchError((e) {
2019-11-28 20:33:27 +02:00
if (!_completer.isCompleted) {
_completer.completeError(HAError("No connection to Home Assistant", actions: [HAErrorAction.reconnect()]));
2019-11-28 20:33:27 +02:00
}
});
} else {
2019-08-26 18:55:12 +03:00
Logger.d("[Sending] ==> ${auth ? "type="+dataObject['type'] : rawMessage}");
_socket.sink.add(rawMessage);
}
2019-03-26 00:18:30 +02:00
return _completer.future;
}
void _incrementMessageId() {
_currentMessageId += 1;
}
2019-11-08 21:37:41 +02:00
Future callService({@required String domain, @required String service, String entityId, Map data}) {
2019-10-28 19:59:47 +02:00
eventBus.fire(NotifyServiceCallEvent(domain, service, entityId));
2019-11-08 21:37:41 +02:00
Logger.d("Service call: $domain.$service, $entityId, $data");
2019-09-15 17:29:49 +03:00
Completer completer = Completer();
2019-03-26 00:18:30 +02:00
Map serviceData = {};
if (entityId != null) {
serviceData["entity_id"] = entityId;
}
2019-11-08 21:37:41 +02:00
if (data != null && data.isNotEmpty) {
serviceData.addAll(data);
2019-03-26 00:18:30 +02:00
}
if (serviceData.isNotEmpty)
2019-09-15 17:29:49 +03:00
sendHTTPPost(
endPoint: "/api/services/$domain/$service",
data: json.encode(serviceData)
).then((data) => completer.complete(data)).catchError((e) => completer.completeError(HAError(e.toString())));
//return sendSocketMessage(type: "call_service", additionalData: {"domain": domain, "service": service, "service_data": serviceData});
2019-03-26 00:18:30 +02:00
else
2019-09-15 17:29:49 +03:00
sendHTTPPost(
endPoint: "/api/services/$domain/$service"
).then((data) => completer.complete(data)).catchError((e) => completer.completeError(HAError(e.toString())));
//return sendSocketMessage(type: "call_service", additionalData: {"domain": domain, "service": service});
2019-09-15 17:29:49 +03:00
return completer.future;
2019-03-26 00:18:30 +02:00
}
Future<List> getHistory(String entityId) async {
DateTime now = DateTime.now();
//String endTime = formatDate(now, [yyyy, '-', mm, '-', dd, 'T', HH, ':', nn, ':', ss, z]);
String startTime = formatDate(now.subtract(Duration(hours: 24)), [yyyy, '-', mm, '-', dd, 'T', HH, ':', nn, ':', ss, z]);
String url = "$httpWebHost/api/history/period/$startTime?&filter_entity_id=$entityId";
2019-08-26 18:55:12 +03:00
Logger.d("[Sending] ==> HTTP /api/history/period/$startTime?&filter_entity_id=$entityId");
2019-03-26 00:18:30 +02:00
http.Response historyResponse;
historyResponse = await http.get(url, headers: {
"authorization": "Bearer $_token",
"Content-Type": "application/json"
});
var history = json.decode(historyResponse.body);
if (history is List) {
2019-08-26 18:55:12 +03:00
Logger.d( "[Received] <== HTTP ${history.first.length} history recors");
2019-03-26 00:18:30 +02:00
return history;
} else {
return [];
}
}
2019-03-29 13:09:34 +02:00
Future sendHTTPPost({String endPoint, String data, String contentType: "application/json", bool includeAuthHeader: true}) async {
2019-03-26 00:18:30 +02:00
Completer completer = Completer();
2019-03-29 13:09:34 +02:00
String url = "$httpWebHost$endPoint";
2019-08-26 18:55:12 +03:00
Logger.d("[Sending] ==> HTTP $endPoint");
2019-03-26 00:18:30 +02:00
Map<String, String> headers = {};
if (contentType != null) {
headers["Content-Type"] = contentType;
}
if (includeAuthHeader) {
2019-03-29 13:09:34 +02:00
headers["authorization"] = "Bearer $_token";
2019-03-26 00:18:30 +02:00
}
http.post(
url,
headers: headers,
body: data
).then((response) {
if (response.statusCode >= 200 && response.statusCode < 300 ) {
2019-10-30 17:04:23 +02:00
Logger.d("[Received] <== HTTP ${response.statusCode}");
2019-03-26 00:18:30 +02:00
completer.complete(response.body);
} else {
2019-10-30 17:04:23 +02:00
Logger.d("[Received] <== HTTP ${response.statusCode}: ${response.body}");
completer.completeError(response);
2019-03-26 00:18:30 +02:00
}
}).catchError((e) {
completer.completeError(e);
});
return completer.future;
}
}