[#56] Reconnect with timeout on service call sending

This commit is contained in:
estevez 2018-09-21 00:01:53 +03:00
parent eceece5a90
commit ea855b96dd
2 changed files with 45 additions and 10 deletions

View File

@ -45,7 +45,7 @@ class HassioDataModel {
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) { if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
debugPrint("Previous fetch is not complited"); debugPrint("Previous fetch is not complited");
} else { } else {
//Fetch timeout timer //TODO: Fetch timeout timer. Should be removed after #21 fix
_fetchingTimer = Timer(Duration(seconds: 10), () { _fetchingTimer = Timer(Duration(seconds: 10), () {
closeConnection(); closeConnection();
_fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"}); _fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"});
@ -306,13 +306,22 @@ class HassioDataModel {
_statesCompleter.complete(); _statesCompleter.complete();
} }
callService(String domain, String service, String entity_id) { Future callService(String domain, String service, String entity_id) {
var sendCompleter = Completer();
//TODO: Send service call timeout timer. Should be removed after #21 fix
Timer _sendTimer = Timer(Duration(seconds: 7), () {
sendCompleter.completeError({"errorCode" : 8,"errorMessage": "Connection timeout"});
});
_reConnectSocket().then((r) { _reConnectSocket().then((r) {
_incrementMessageId(); _incrementMessageId();
_sendMessageRaw('{"id": $_currentMssageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entity_id"}}'); _sendMessageRaw('{"id": $_currentMssageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entity_id"}}');
_sendTimer.cancel();
sendCompleter.complete();
}).catchError((e){ }).catchError((e){
debugPrint("Unable to connect for service call: $entity_id $domain.$service"); _sendTimer.cancel();
sendCompleter.completeError(e);
}); });
return sendCompleter.future;
} }
} }

View File

@ -130,15 +130,30 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_isLoading = false; _isLoading = false;
}); });
}).catchError((e) { }).catchError((e) {
setState(() { _setErrorState(e);
_errorCodeToBeShown = e["errorCode"] != null ? e["errorCode"] : 99;
_lastErrorMessage = e["errorMessage"] ?? "Unknown error";
_isLoading = false;
});
}); });
} }
} }
_setErrorState(e) {
setState(() {
_errorCodeToBeShown = e["errorCode"] != null ? e["errorCode"] : 99;
_lastErrorMessage = e["errorMessage"] ?? "Unknown error";
_isLoading = false;
});
}
void _callService(String domain, String service, String entityId) {
setState(() {
_isLoading = true;
});
_dataModel.callService(domain, service, entityId).then((r) {
setState(() {
_isLoading = false;
});
}).catchError((e) => _setErrorState(e));
}
Widget _buildEntityAction(String entityId) { Widget _buildEntityAction(String entityId) {
var entity = _entitiesData[entityId]; var entity = _entitiesData[entityId];
Widget result; Widget result;
@ -146,7 +161,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
result = Switch( result = Switch(
value: (entity["state"] == "on"), value: (entity["state"] == "on"),
onChanged: ((state) { onChanged: ((state) {
_dataModel.callService( _callService(
entity["domain"], state ? "turn_on" : "turn_off", entityId); entity["domain"], state ? "turn_on" : "turn_off", entityId);
setState(() { setState(() {
_entitiesData[entityId]["state"] = state ? "on" : "off"; _entitiesData[entityId]["state"] = state ? "on" : "off";
@ -158,7 +173,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
width: 60.0, width: 60.0,
child: FlatButton( child: FlatButton(
onPressed: (() { onPressed: (() {
_dataModel.callService(entity["domain"], "turn_on", entityId); _callService(entity["domain"], "turn_on", entityId);
}), }),
child: Text( child: Text(
"Run", "Run",
@ -369,6 +384,17 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
); );
break; break;
} }
case 8: {
action = SnackBarAction(
label: "Reconnect",
onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData();
},
);
break;
}
} }
Timer(Duration(seconds: 1), () { Timer(Duration(seconds: 1), () {
_scaffoldKey.currentState.hideCurrentSnackBar(); _scaffoldKey.currentState.hideCurrentSnackBar();