[#40] Handle app restore
This commit is contained in:
parent
9f2fcd1b42
commit
c20fa12c3d
@ -13,6 +13,7 @@ class HassioDataModel {
|
||||
int _currentMssageId = 0;
|
||||
int _statesMessageId = 0;
|
||||
int _servicesMessageId = 0;
|
||||
int _subscriptionMessageId = 0;
|
||||
Map _entitiesData = {};
|
||||
Map _servicesData = {};
|
||||
Map _uiStructure = {};
|
||||
@ -37,8 +38,7 @@ class HassioDataModel {
|
||||
//Fetch timeout timer
|
||||
_fetchingTimer = Timer(Duration(seconds: 10), () {
|
||||
_fetchCompleter.completeError({"message": "Data fetching timeout."});
|
||||
_hassioChannel.sink.close();
|
||||
_hassioChannel = null;
|
||||
closeConnection();
|
||||
});
|
||||
_fetchCompleter = new Completer();
|
||||
_reConnectSocket().then((r) {
|
||||
@ -51,6 +51,13 @@ class HassioDataModel {
|
||||
return _fetchCompleter.future;
|
||||
}
|
||||
|
||||
void closeConnection() {
|
||||
if (_hassioChannel != null) {
|
||||
_hassioChannel.sink.close();
|
||||
_hassioChannel = null;
|
||||
}
|
||||
}
|
||||
|
||||
Future _reConnectSocket() {
|
||||
var _connectionCompleter = new Completer();
|
||||
if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) {
|
||||
@ -91,7 +98,7 @@ class HassioDataModel {
|
||||
} else if (data["type"] == "auth_ok") {
|
||||
debugPrint(" auth done");
|
||||
debugPrint("Connection done.");
|
||||
sendSubscribe();
|
||||
_sendSubscribe();
|
||||
connectionCompleter.complete();
|
||||
} else if (data["type"] == "auth_invalid") {
|
||||
connectionCompleter.completeError({message: "Auth error: ${data["message"]}"});
|
||||
@ -132,9 +139,10 @@ class HassioDataModel {
|
||||
if ((_servicesCompleter != null) && (!_servicesCompleter.isCompleted)) _servicesCompleter.completeError(error);
|
||||
}
|
||||
|
||||
void sendSubscribe() {
|
||||
void _sendSubscribe() {
|
||||
_incrementMessageId();
|
||||
_sendMessageRaw('{"id": $_currentMssageId, "type": "subscribe_events"}');
|
||||
_subscriptionMessageId = _currentMssageId;
|
||||
_sendMessageRaw('{"id": $_subscriptionMessageId, "type": "subscribe_events", "event_type": "state_changed"}');
|
||||
}
|
||||
|
||||
Future _getStates() {
|
||||
@ -160,12 +168,8 @@ class HassioDataModel {
|
||||
}
|
||||
|
||||
_sendMessageRaw(message) {
|
||||
_reConnectSocket().then((r) {
|
||||
debugPrint("[Sent]$message");
|
||||
_hassioChannel.sink.add(message);
|
||||
}).catchError((e){
|
||||
debugPrint("Unable to connect for sending: $e");
|
||||
});
|
||||
}
|
||||
|
||||
void _handleEntityStateChange(Map eventData) {
|
||||
@ -263,8 +267,12 @@ class HassioDataModel {
|
||||
}
|
||||
|
||||
callService(String domain, String service, String entity_id) {
|
||||
_reConnectSocket().then((r) {
|
||||
_incrementMessageId();
|
||||
_sendMessageRaw('{"id": $_currentMssageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entity_id"}}');
|
||||
}).catchError((e){
|
||||
debugPrint("Unable to connect for service call: $entity_id $domain.$service");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import 'package:web_socket_channel/io.dart';
|
||||
import 'package:web_socket_channel/status.dart' as socketStatus;
|
||||
import 'package:progress_indicators/progress_indicators.dart';
|
||||
import 'package:event_bus/event_bus.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
part 'settings.dart';
|
||||
part 'data_model.dart';
|
||||
@ -43,7 +44,7 @@ class MainPage extends StatefulWidget {
|
||||
_MainPageState createState() => new _MainPageState();
|
||||
}
|
||||
|
||||
class _MainPageState extends State<MainPage> {
|
||||
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
HassioDataModel _dataModel;
|
||||
Map _entitiesData;
|
||||
Map _uiStructure;
|
||||
@ -61,10 +62,19 @@ class _MainPageState extends State<MainPage> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initClient();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_init();
|
||||
}
|
||||
|
||||
_initClient() async {
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
debugPrint("$state");
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
_refreshData();
|
||||
}
|
||||
}
|
||||
|
||||
_init() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
String _hassioAPIEndpoint = "wss://" +
|
||||
prefs.getString('hassio-domain') +
|
||||
@ -348,8 +358,8 @@ class _MainPageState extends State<MainPage> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
//TODO
|
||||
//_hassioChannel.sink.close();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_dataModel.closeConnection();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user