diff --git a/lib/entity.page.dart b/lib/entity.page.dart index ab5e8fe..a5f065b 100644 --- a/lib/entity.page.dart +++ b/lib/entity.page.dart @@ -32,11 +32,11 @@ class _EntityViewPageState extends State { } void _getHistory() { - widget.homeAssistant.getHistory(widget.entity.entityId).then((List history) { + /* widget.homeAssistant.getHistory(widget.entity.entityId).then((List history) { if (history != null) { } - }); + });*/ } diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index c789084..718a230 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -31,12 +31,13 @@ class HomeAssistant { StreamSubscription _socketSubscription; - int messageExpirationTime = 45; //seconds - Duration fetchTimeout = Duration(seconds: 45); + int messageExpirationTime = 30; //seconds + Duration fetchTimeout = Duration(seconds: 30); Duration connectTimeout = Duration(seconds: 15); - String get locationName => _instanceConfig["location_name"] ?? "..."; + String get locationName => _instanceConfig["location_name"] ?? ""; String get userName => _userName ?? locationName; + String get userAvatarText => userName.length > 0 ? userName[0] : ""; int get viewsCount => _entities.viewList.length ?? 0; EntityCollection get entities => _entities; @@ -59,7 +60,12 @@ class HomeAssistant { _fetchCompleter = new Completer(); _fetchTimer = Timer(fetchTimeout, () { 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) { _getData(); @@ -73,7 +79,7 @@ class HomeAssistant { disconnect() async { if ((_hassioChannel != null) && (_hassioChannel.closeCode == null) && (_hassioChannel.sink != null)) { 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(); _hassioChannel = null; @@ -85,14 +91,14 @@ class HomeAssistant { if ((_connectionCompleter != null) && (!_connectionCompleter.isCompleted)) { TheLogger.log("Debug","Previous connection is not complited"); } else { - _connectionCompleter = new Completer(); - autoReconnect = false; if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) { + _connectionCompleter = new Completer(); + autoReconnect = false; disconnect().then((_){ TheLogger.log("Debug", "Socket connecting..."); _connectionTimer = Timer(connectTimeout, () { 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) { _socketSubscription.cancel(); @@ -102,30 +108,47 @@ class HomeAssistant { _socketSubscription = _hassioChannel.stream.listen( (message) => _handleMessage(message), cancelOnError: true, - onDone: () { - TheLogger.log("Debug","Socket disconnected. Automatic reconnect is $autoReconnect"); - 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."})); - } + onDone: () => _handleSocketClose(), + onError: (e) => _handleSocketError(e) ); }); } else { - //TheLogger.log("Debug","Socket looks connected...${_hassioChannel.protocol}, ${_hassioChannel.closeCode}, ${_hassioChannel.closeReason}"); _completeConnecting(null); } } 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 { List futures = []; futures.add(_getStates()); @@ -147,8 +170,9 @@ class HomeAssistant { if (error != null) { _fetchCompleter.completeError(error); } else { - _fetchCompleter.complete(); autoReconnect = true; + TheLogger.log("Debug", "Fetch complete successful"); + _fetchCompleter.complete(); } } } diff --git a/lib/main.dart b/lib/main.dart index 0c1274b..3f3ea3a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -35,7 +35,7 @@ part 'card_class.dart'; EventBus eventBus = new EventBus(); const String appName = "HA Client"; -const appVersion = "0.2.5 .31"; +const appVersion = "0.2.5.31"; String homeAssistantWebHost; @@ -310,7 +310,7 @@ class _MainPageState extends State with WidgetsBindingObserver { }, currentAccountPicture: CircleAvatar( child: Text( - _homeAssistant.userName[0], + _homeAssistant.userAvatarText, style: TextStyle( fontSize: 32.0 ),