2019-08-31 22:10:07 +03:00
|
|
|
part of '../main.dart';
|
2019-03-26 00:18:30 +02:00
|
|
|
|
2019-08-31 22:10:07 +03:00
|
|
|
class ConnectionManager {
|
2019-03-26 00:18:30 +02:00
|
|
|
|
2019-08-31 22:10:07 +03:00
|
|
|
static final ConnectionManager _instance = ConnectionManager._internal();
|
2019-03-26 00:18:30 +02:00
|
|
|
|
2019-08-31 22:10:07 +03:00
|
|
|
factory ConnectionManager() {
|
2019-03-26 00:18:30 +02:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:10:07 +03:00
|
|
|
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;
|
2019-06-15 18:07:11 +03:00
|
|
|
String webhookId;
|
2019-04-05 11:48:41 +03:00
|
|
|
bool settingsLoaded = false;
|
2020-04-15 14:59:40 +03:00
|
|
|
int appIntegrationVersion;
|
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;
|
2020-03-20 01:16:59 +02:00
|
|
|
var onLovelaceUpdatedCallback;
|
2019-03-26 00:18:30 +02:00
|
|
|
|
|
|
|
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();
|
2019-06-15 14:36:11 +03:00
|
|
|
bool stopInit = false;
|
2019-04-05 11:48:41 +03:00
|
|
|
if (loadSettings) {
|
2020-02-17 21:22:38 +02:00
|
|
|
Logger.d("Loading settings...");
|
2019-04-05 11:48:41 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
_domain = prefs.getString('hassio-domain');
|
|
|
|
_port = prefs.getString('hassio-port');
|
2019-06-15 18:07:11 +03:00
|
|
|
webhookId = prefs.getString('app-webhook-id');
|
2020-04-15 14:59:40 +03:00
|
|
|
appIntegrationVersion = prefs.getInt('app-integration-version') ?? 0;
|
2019-04-05 11:48:41 +03:00
|
|
|
displayHostname = "$_domain:$_port";
|
2019-06-15 14:36:11 +03:00
|
|
|
_webSocketAPIEndpoint =
|
|
|
|
"${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket";
|
|
|
|
httpWebHost =
|
2019-06-15 18:07:11 +03:00
|
|
|
"${prefs.getString('hassio-res-protocol')}://$_domain:$_port";
|
2020-04-30 17:04:37 +03:00
|
|
|
Logger.d('$_domain$_port');
|
2019-06-15 14:36:11 +03:00
|
|
|
if ((_domain == null) || (_port == null) ||
|
|
|
|
(_domain.isEmpty) || (_port.isEmpty)) {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer.completeError(HACException.checkConnectionSettings());
|
2019-06-15 14:36:11 +03:00
|
|
|
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-02-12 15:22:26 +02:00
|
|
|
'https://ha-client.app')}&redirect_uri=${Uri
|
2019-09-04 23:40:37 +03:00
|
|
|
.encodeComponent(
|
2020-02-12 15:22:26 +02:00
|
|
|
'https://ha-client.app/service/auth_callback.html')}";
|
2019-09-04 23:40:37 +03:00
|
|
|
settingsLoaded = true;
|
2019-06-15 14:36:11 +03:00
|
|
|
} catch (e) {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer.completeError(HACException("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));
|
2019-06-15 14:36:11 +03:00
|
|
|
Logger.e("Cannt read secure storage. Need to relogin.");
|
2019-09-04 23:40:37 +03:00
|
|
|
stopInit = true;
|
2019-06-15 14:36:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((_domain == null) || (_port == null) ||
|
|
|
|
(_domain.isEmpty) || (_port.isEmpty)) {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer.completeError(HACException.checkConnectionSettings());
|
2019-06-15 14:36:11 +03:00
|
|
|
stopInit = true;
|
2019-04-05 11:48:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 14:36:11 +03:00
|
|
|
if (!stopInit) {
|
|
|
|
if (_token == null) {
|
2019-10-20 19:45:44 +03:00
|
|
|
AuthManager().start(
|
2019-06-15 14:36:11 +03:00
|
|
|
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-06-15 14:36:11 +03:00
|
|
|
}
|
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) {
|
2020-05-01 16:47:41 +03:00
|
|
|
connecting.completeError(HACException("Connection timeout"));
|
2019-11-29 14:45:59 +02:00
|
|
|
}
|
2020-05-01 16:47:41 +03:00
|
|
|
completer?.completeError(HACException("Connection timeout"));
|
|
|
|
} else if (e is HACException) {
|
2019-11-29 14:45:59 +02:00
|
|
|
completer?.completeError(e);
|
|
|
|
} else {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer?.completeError(HACException("${e.toString()}"));
|
2019-11-29 12:58:24 +02:00
|
|
|
}
|
2019-11-29 14:45:59 +02:00
|
|
|
});
|
2019-03-29 11:04:43 +02:00
|
|
|
});
|
|
|
|
});
|
2019-04-05 11:48:41 +03:00
|
|
|
} else {
|
2019-04-19 14:07:44 +03:00
|
|
|
completer?.complete();
|
2019-03-26 00:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:04:43 +02:00
|
|
|
Completer connecting;
|
|
|
|
|
2019-06-16 16:32:55 +03:00
|
|
|
Future _connect() {
|
2019-03-29 11:04:43 +02:00
|
|
|
if (connecting != null && !connecting.isCompleted) {
|
2019-04-05 12:23:31 +03:00
|
|
|
Logger.w("Previous connection attempt pending...");
|
|
|
|
return connecting.future;
|
|
|
|
} else {
|
|
|
|
connecting = Completer();
|
2019-06-16 16:32:55 +03:00
|
|
|
_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(
|
2019-06-16 16:32:55 +03:00
|
|
|
_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");
|
2020-03-20 01:16:59 +02:00
|
|
|
sendSocketMessage(
|
|
|
|
type: "subscribe_events",
|
|
|
|
additionalData: {"event_type": "lovelace_updated"},
|
|
|
|
);
|
2019-11-29 15:40:51 +02:00
|
|
|
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()}");
|
2020-05-01 16:47:41 +03:00
|
|
|
_messageResolver["auth"]?.completeError(HACException("${data["message"]}", actions: [HAErrorAction.loginAgain()]));
|
2019-11-29 13:21:45 +02:00
|
|
|
_messageResolver.remove("auth");
|
2020-05-01 16:47:41 +03:00
|
|
|
if (!connecting.isCompleted) connecting.completeError(HACException("${data["message"]}", actions: [HAErrorAction.tryAgain(title: "Retry"), HAErrorAction.loginAgain(title: "Relogin")]));
|
2019-11-29 13:21:45 +02:00
|
|
|
} else {
|
|
|
|
_handleMessage(data);
|
2019-06-16 16:32:55 +03:00
|
|
|
}
|
2019-11-29 13:21:45 +02:00
|
|
|
},
|
|
|
|
cancelOnError: true,
|
|
|
|
onDone: () => _handleSocketClose(connecting),
|
|
|
|
onError: (e) => _handleSocketError(e, connecting)
|
|
|
|
);
|
|
|
|
} catch(exeption) {
|
2020-05-01 16:47:41 +03:00
|
|
|
connecting.completeError(HACException("${exeption.toString()}"));
|
2019-11-29 13:21:45 +02:00
|
|
|
}
|
2019-06-16 16:32:55 +03:00
|
|
|
});
|
2019-03-29 11:04:43 +02:00
|
|
|
return connecting.future;
|
|
|
|
}
|
2019-03-26 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:07:44 +03: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
|
|
|
}
|
2019-04-19 14:07:44 +03: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) {
|
2019-11-08 22:14:34 +02:00
|
|
|
Logger.e("[Received] <== Error received on request id ${data['id']}: ${data['error']}");
|
2020-05-01 16:47:41 +03:00
|
|
|
_messageResolver["${data["id"]}"]?.completeError("${data["error"]["code"]}: ${data["error"]["message"]}");
|
2019-03-26 00:18:30 +02:00
|
|
|
}
|
|
|
|
_messageResolver.remove("${data["id"]}");
|
|
|
|
} else if (data["type"] == "event") {
|
2020-03-20 01:16:59 +02:00
|
|
|
if (data["event"] != null) {
|
|
|
|
if (data["event"]["event_type"] == "state_changed") {
|
|
|
|
Logger.d("[Received] <== ${data['type']}.${data["event"]["event_type"]}: ${data["event"]["data"]["entity_id"]}");
|
|
|
|
onStateChangeCallback(data["event"]["data"]);
|
|
|
|
} else if (data["event"]["event_type"] == "lovelace_updated") {
|
|
|
|
Logger.d("[Received] <== ${data['type']}.${data["event"]["event_type"]}: $data");
|
|
|
|
onLovelaceUpdatedCallback();
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 00:18:30 +02:00
|
|
|
} 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;
|
2020-05-01 16:47:41 +03:00
|
|
|
connectionCompleter.completeError(HACException("Disconnected", actions: [HAErrorAction.reconnect()]));
|
2019-11-29 13:21:45 +02:00
|
|
|
}
|
2020-05-01 16:47:41 +03:00
|
|
|
eventBus.fire(ShowErrorEvent(HACException("Unable to connect to Home Assistant")));
|
2019-11-29 13:21:45 +02:00
|
|
|
});
|
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;
|
2020-05-01 16:47:41 +03:00
|
|
|
connectionCompleter.completeError(HACException("Disconnected", actions: [HAErrorAction.reconnect()]));
|
2019-11-29 13:21:45 +02:00
|
|
|
}
|
2020-05-01 16:47:41 +03:00
|
|
|
eventBus.fire(ShowErrorEvent(HACException("Unable to connect to Home Assistant")));
|
2019-11-29 13:21:45 +02:00
|
|
|
});
|
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((_) {
|
2019-06-16 16:32:55 +03:00
|
|
|
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 {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer.completeError(HACException("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) {
|
2020-05-01 16:47:41 +03:00
|
|
|
completer.completeError(HACException("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);
|
2019-03-29 11:04:43 +02:00
|
|
|
if (!isConnected) {
|
2019-11-29 13:24:29 +02:00
|
|
|
_connect().timeout(connectTimeout).then((_) {
|
2019-08-26 18:55:12 +03:00
|
|
|
Logger.d("[Sending] ==> ${auth ? "type="+dataObject['type'] : rawMessage}");
|
2019-03-29 11:04:43 +02:00
|
|
|
_socket.sink.add(rawMessage);
|
|
|
|
}).catchError((e) {
|
2019-11-28 20:33:27 +02:00
|
|
|
if (!_completer.isCompleted) {
|
2020-05-01 16:47:41 +03:00
|
|
|
_completer.completeError(HACException("No connection to Home Assistant", actions: [HAErrorAction.reconnect()]));
|
2019-11-28 20:33:27 +02:00
|
|
|
}
|
2019-03-29 11:04:43 +02:00
|
|
|
});
|
|
|
|
} else {
|
2019-08-26 18:55:12 +03:00
|
|
|
Logger.d("[Sending] ==> ${auth ? "type="+dataObject['type'] : rawMessage}");
|
2019-03-29 11:04:43 +02:00
|
|
|
_socket.sink.add(rawMessage);
|
|
|
|
}
|
2019-03-26 00:18:30 +02:00
|
|
|
return _completer.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _incrementMessageId() {
|
|
|
|
_currentMessageId += 1;
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:08 +02:00
|
|
|
Future callService({@required String domain, @required String service, 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(
|
2019-09-14 20:08:10 +03:00
|
|
|
endPoint: "/api/services/$domain/$service",
|
|
|
|
data: json.encode(serviceData)
|
2020-05-01 16:47:41 +03:00
|
|
|
).then((data) => completer.complete(data)).catchError((e) => completer.completeError(HACException(e.toString())));
|
2019-09-14 20:08:10 +03:00
|
|
|
//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(
|
2019-09-14 20:08:10 +03:00
|
|
|
endPoint: "/api/services/$domain/$service"
|
2020-05-01 16:47:41 +03:00
|
|
|
).then((data) => completer.complete(data)).catchError((e) => completer.completeError(HACException(e.toString())));
|
2019-09-14 20:08:10 +03:00
|
|
|
//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) {
|
2019-06-15 18:07:11 +03:00
|
|
|
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}");
|
2019-11-28 21:14:50 +02:00
|
|
|
completer.completeError(response);
|
2019-03-26 00:18:30 +02:00
|
|
|
}
|
|
|
|
}).catchError((e) {
|
|
|
|
completer.completeError(e);
|
|
|
|
});
|
|
|
|
return completer.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|