Add timeout for data fetching. [#34] MDI class fixes

This commit is contained in:
estevez 2018-09-16 15:32:17 +03:00
parent f637c32000
commit 9e23b92a33
2 changed files with 28 additions and 26 deletions

View File

@ -13,6 +13,7 @@ class HassioDataModel {
Completer _fetchCompleter;
Completer _statesCompleter;
Completer _servicesCompleter;
Timer _fetchingTimer;
Map get entities => _entitiesData;
Map get services => _servicesData;
@ -27,10 +28,14 @@ class HassioDataModel {
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
debugPrint("Previous fetch is not complited");
} else {
_fetchingTimer = new Timer(new Duration(seconds: 10), () {
_fetchCompleter.completeError({"message": "Data fetching timeout."});
});
_fetchCompleter = new Completer();
_reConnectSocket().then((r) {
_getData();
}).catchError((e) {
_fetchingTimer.cancel();
_fetchCompleter.completeError(e);
});
}
@ -56,11 +61,14 @@ class HassioDataModel {
_getData() {
_getStates().then((result) {
_getServices().then((result) {
_fetchingTimer.cancel();
_fetchCompleter.complete();
}).catchError((e) {
_fetchingTimer.cancel();
_fetchCompleter.completeError(e);
});
}).catchError((e) {
_fetchingTimer.cancel();
_fetchCompleter.completeError(e);
});
}
@ -133,8 +141,6 @@ class HassioDataModel {
}).catchError((e){
debugPrint("Unable to connect for sending =(");
});
}
void _parseServices(Map data) {
@ -234,16 +240,16 @@ class HassioDataModel {
class MaterialDesignIcons {
static Map _defaultIconsByDomains = {
"light": 0xf335,
"switch": 0xf241,
"binary_sensor": 0xf130,
"group": 0xf2b1,
"sensor": 0xf208,
"automation": 0xf411,
"script": 0xf219,
"input_boolean": 0xf1de,
"input_datetime": 0xf953,
"sun": 0xf5a8
"light": "mdi:lightbulb",
"switch": "mdi:flash",
"binary_sensor": "mdi:checkbox-blank-circle-outline",
"group": "mdi:google-circles-communities",
"sensor": "mdi:eye",
"automation": "mdi:playlist-play",
"script": "mdi:file-document",
"input_boolean": "mdi:drawing",
"input_datetime": "mdi:clock",
"sun": "mdi:white-balance-sunny"
};
static Map _iconsDataMap = {
"mdi:access-point": 0xf002,

View File

@ -77,7 +77,7 @@ class _MainPageState extends State<MainPage> {
setState(() {
loading = true;
});
_dataModelErrorMessage = "";
_dataModelErrorMessage = null;
if (_dataModel != null) {
await _dataModel.fetch().then((result) {
setState(() {
@ -189,18 +189,14 @@ class _MainPageState extends State<MainPage> {
List<Widget> buildSingleView(structure) {
List<Widget> result = [];
if (_dataModelErrorMessage.length == 0) {
structure["standalone"].forEach((entityId) {
result.add(_buildCard([entityId], ""));
});
structure["groups"].forEach((group) {
result.add(_buildCard(
group["children"], group["friendly_name"].toString()));
});
} else {
//TODO
//result.add(Text(_dataModelErrorMessage));
}
structure["standalone"].forEach((entityId) {
result.add(_buildCard([entityId], ""));
});
structure["groups"].forEach((group) {
result.add(_buildCard(
group["children"], group["friendly_name"].toString()));
});
return result;
}
@ -283,7 +279,7 @@ class _MainPageState extends State<MainPage> {
title: _buildTitle()
),
drawer: _buildAppDrawer(),
body: Text("Loading... or not...\n\nPlease, restart the app in case of three dots in header starts to freaking you out."),
body: Text(_dataModelErrorMessage != null ? "Well... no. There was an error: $_dataModelErrorMessage" : "Loading... or not..."),
floatingActionButton: new FloatingActionButton(
onPressed: _refreshData,
tooltip: 'Increment',