This commit is contained in:
Yegor Vialov 2018-10-08 23:11:56 +03:00
parent 84d283de2b
commit 164800951d
3 changed files with 52 additions and 28 deletions

View File

@ -32,11 +32,11 @@ class _EntityViewPageState extends State<EntityViewPage> {
} }
void _getHistory() { void _getHistory() {
widget.homeAssistant.getHistory(widget.entity.entityId).then((List history) { /* widget.homeAssistant.getHistory(widget.entity.entityId).then((List history) {
if (history != null) { if (history != null) {
} }
}); });*/
} }

View File

@ -31,12 +31,13 @@ class HomeAssistant {
StreamSubscription _socketSubscription; StreamSubscription _socketSubscription;
int messageExpirationTime = 45; //seconds int messageExpirationTime = 30; //seconds
Duration fetchTimeout = Duration(seconds: 45); Duration fetchTimeout = Duration(seconds: 30);
Duration connectTimeout = Duration(seconds: 15); Duration connectTimeout = Duration(seconds: 15);
String get locationName => _instanceConfig["location_name"] ?? "..."; String get locationName => _instanceConfig["location_name"] ?? "";
String get userName => _userName ?? locationName; String get userName => _userName ?? locationName;
String get userAvatarText => userName.length > 0 ? userName[0] : "";
int get viewsCount => _entities.viewList.length ?? 0; int get viewsCount => _entities.viewList.length ?? 0;
EntityCollection get entities => _entities; EntityCollection get entities => _entities;
@ -59,7 +60,12 @@ class HomeAssistant {
_fetchCompleter = new Completer(); _fetchCompleter = new Completer();
_fetchTimer = Timer(fetchTimeout, () { _fetchTimer = Timer(fetchTimeout, () {
TheLogger.log("Error", "Data fetching timeout"); TheLogger.log("Error", "Data fetching timeout");
_completeFetching({"errorCode" : 9,"errorMessage": "Couldn't get data from server"}); disconnect().then((_) {
_completeFetching({
"errorCode": 9,
"errorMessage": "Couldn't get data from server"
});
});
}); });
_connection().then((r) { _connection().then((r) {
_getData(); _getData();
@ -73,7 +79,7 @@ class HomeAssistant {
disconnect() async { disconnect() async {
if ((_hassioChannel != null) && (_hassioChannel.closeCode == null) && (_hassioChannel.sink != null)) { if ((_hassioChannel != null) && (_hassioChannel.closeCode == null) && (_hassioChannel.sink != null)) {
await _hassioChannel.sink.close().timeout(Duration(seconds: 3), await _hassioChannel.sink.close().timeout(Duration(seconds: 3),
onTimeout: () => TheLogger.log("Warning", "Socket sink closing timeout") onTimeout: () => TheLogger.log("Debug", "Socket sink closed")
); );
await _socketSubscription.cancel(); await _socketSubscription.cancel();
_hassioChannel = null; _hassioChannel = null;
@ -85,14 +91,14 @@ class HomeAssistant {
if ((_connectionCompleter != null) && (!_connectionCompleter.isCompleted)) { if ((_connectionCompleter != null) && (!_connectionCompleter.isCompleted)) {
TheLogger.log("Debug","Previous connection is not complited"); TheLogger.log("Debug","Previous connection is not complited");
} else { } else {
_connectionCompleter = new Completer();
autoReconnect = false;
if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) { if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) {
_connectionCompleter = new Completer();
autoReconnect = false;
disconnect().then((_){ disconnect().then((_){
TheLogger.log("Debug", "Socket connecting..."); TheLogger.log("Debug", "Socket connecting...");
_connectionTimer = Timer(connectTimeout, () { _connectionTimer = Timer(connectTimeout, () {
TheLogger.log("Error", "Socket connection timeout"); TheLogger.log("Error", "Socket connection timeout");
_completeConnecting({"errorCode" : 1,"errorMessage": "Couldn't connect to Home Assistant. Check network connection or connection settings."}); _handleSocketError(null);
}); });
if (_socketSubscription != null) { if (_socketSubscription != null) {
_socketSubscription.cancel(); _socketSubscription.cancel();
@ -102,30 +108,47 @@ class HomeAssistant {
_socketSubscription = _hassioChannel.stream.listen( _socketSubscription = _hassioChannel.stream.listen(
(message) => _handleMessage(message), (message) => _handleMessage(message),
cancelOnError: true, cancelOnError: true,
onDone: () { onDone: () => _handleSocketClose(),
TheLogger.log("Debug","Socket disconnected. Automatic reconnect is $autoReconnect"); onError: (e) => _handleSocketError(e)
if (autoReconnect) {
disconnect().then((_) {
_connection().catchError((e){
_completeConnecting(e);
});
});
}
},
onError: (e) {
TheLogger.log("Error","Socket stream Error: $e");
disconnect().then((_) => _completeConnecting({"errorCode" : 1,"errorMessage": "Couldn't connect to Home Assistant. Check network connection or connection settings."}));
}
); );
}); });
} else { } else {
//TheLogger.log("Debug","Socket looks connected...${_hassioChannel.protocol}, ${_hassioChannel.closeCode}, ${_hassioChannel.closeReason}");
_completeConnecting(null); _completeConnecting(null);
} }
} }
return _connectionCompleter.future; return _connectionCompleter.future;
} }
void _handleSocketClose() {
TheLogger.log("Debug","Socket disconnected. Automatic reconnect is $autoReconnect");
if (autoReconnect) {
_reconnect();
}
}
void _handleSocketError(e) {
TheLogger.log("Error","Socket stream Error: $e");
TheLogger.log("Debug","Automatic reconnect is $autoReconnect");
if (autoReconnect) {
_reconnect();
} else {
disconnect().then((_) {
_completeConnecting({
"errorCode": 1,
"errorMessage": "Couldn't connect to Home Assistant. Check network connection or connection settings."
});
});
}
}
void _reconnect() {
disconnect().then((_) {
_connection().catchError((e){
_completeConnecting(e);
});
});
}
_getData() async { _getData() async {
List<Future> futures = []; List<Future> futures = [];
futures.add(_getStates()); futures.add(_getStates());
@ -147,8 +170,9 @@ class HomeAssistant {
if (error != null) { if (error != null) {
_fetchCompleter.completeError(error); _fetchCompleter.completeError(error);
} else { } else {
_fetchCompleter.complete();
autoReconnect = true; autoReconnect = true;
TheLogger.log("Debug", "Fetch complete successful");
_fetchCompleter.complete();
} }
} }
} }

View File

@ -35,7 +35,7 @@ part 'card_class.dart';
EventBus eventBus = new EventBus(); EventBus eventBus = new EventBus();
const String appName = "HA Client"; const String appName = "HA Client";
const appVersion = "0.2.5 .31"; const appVersion = "0.2.5.31";
String homeAssistantWebHost; String homeAssistantWebHost;
@ -310,7 +310,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
}, },
currentAccountPicture: CircleAvatar( currentAccountPicture: CircleAvatar(
child: Text( child: Text(
_homeAssistant.userName[0], _homeAssistant.userAvatarText,
style: TextStyle( style: TextStyle(
fontSize: 32.0 fontSize: 32.0
), ),