WIP #49 Add location update interval settings

This commit is contained in:
estevez-dev 2019-08-30 21:45:34 +03:00
parent 5a3b57c28e
commit b07ff6fe71
8 changed files with 278 additions and 395 deletions

View File

@ -408,53 +408,4 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
super.dispose();
}
}
class TemperatureControlWidget extends StatelessWidget {
final double value;
final double fontSize;
final Color fontColor;
final onInc;
final onDec;
TemperatureControlWidget(
{Key key,
@required this.value,
@required this.onInc,
@required this.onDec,
this.fontSize,
this.fontColor})
: super(key: key);
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"$value",
style: TextStyle(
fontSize: fontSize ?? 24.0,
color: fontColor ?? Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
'mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => onInc(),
),
IconButton(
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
'mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => onDec(),
)
],
)
],
);
}
}

View File

@ -0,0 +1,50 @@
part of '../../../main.dart';
class TemperatureControlWidget extends StatelessWidget {
final double value;
final double fontSize;
final Color fontColor;
final onInc;
final onDec;
TemperatureControlWidget(
{Key key,
@required this.value,
@required this.onInc,
@required this.onDec,
this.fontSize,
this.fontColor})
: super(key: key);
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"$value",
style: TextStyle(
fontSize: fontSize ?? 24.0,
color: fontColor ?? Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
'mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => onInc(),
),
IconButton(
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
'mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => onDec(),
)
],
)
],
);
}
}

View File

@ -83,6 +83,7 @@ part 'entities/cover/widgets/cover_state.dart';
part 'entities/date_time/widgets/date_time_state.dart';
part 'entities/lock/widgets/lock_state.dart';
part 'entities/climate/widgets/climate_controls.dart';
part 'entities/climate/widgets/temperature_control_widget.dart';
part 'entities/cover/widgets/cover_controls.widget.dart';
part 'entities/light/widgets/light_controls.dart';
part 'entities/media_player/widgets/media_player_widgets.dart';
@ -112,7 +113,8 @@ part 'ui_class/panel_class.dart';
part 'ui_widgets/view.dart';
part 'ui_widgets/card_widget.dart';
part 'ui_widgets/card_header_widget.dart';
part 'ui_widgets/config_panel_widget.dart';
part 'panels/config_panel_widget.dart';
part 'panels/widgets/link_to_web_config.dart';
EventBus eventBus = new EventBus();

View File

@ -3,9 +3,9 @@ part of '../main.dart';
class LocationManager {
static void updateDeviceLocation() {
print("[Test isolate #${Isolate.current.hashCode}] alarm service callback");
print("[Location isolate #${Isolate.current.hashCode}] started");
SharedPreferences.getInstance().then((prefs){
print("[Test isolate #${Isolate.current.hashCode}] loading settings");
print("[Location isolate #${Isolate.current.hashCode}] loading settings");
String webhookId = prefs.getString('app-webhook-id');
String domain = prefs.getString('hassio-domain');
String port = prefs.getString('hassio-port');
@ -15,7 +15,7 @@ class LocationManager {
DateTime currentTime = DateTime.now();
String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}";
try {
print("[Test isolate #${Isolate.current.hashCode}] Sending test time data home...");
print("[Location isolate #${Isolate.current.hashCode}] Sending test time data home...");
String url = "$httpWebHost/api/webhook/$webhookId";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
@ -36,11 +36,11 @@ class LocationManager {
body: json.encode(data)
);
} catch (e) {
print("[Test isolate #${Isolate.current.hashCode}] Error: ${e.toString()}");
print("[Location isolate #${Isolate.current.hashCode}] Error: ${e.toString()}");
}
Logger.d("[Test isolate #${Isolate.current.hashCode}] Getting device location...");
Logger.d("[Location isolate #${Isolate.current.hashCode}] Getting device location...");
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
Logger.d("[Test isolate #${Isolate.current.hashCode}] Got location: ${location.latitude} ${location.longitude}. Sending home...");
Logger.d("[Location isolate #${Isolate.current.hashCode}] Got location: ${location.latitude} ${location.longitude}. Sending home...");
int battery = DateTime.now().hour;
try {
String url = "$httpWebHost/api/webhook/$webhookId";
@ -60,12 +60,12 @@ class LocationManager {
body: json.encode(data)
);
} catch (e) {
print("[Test isolate #${Isolate.current.hashCode}] Error sending location: ${e.toString()}");
print("[Location isolate #${Isolate.current.hashCode}] Error sending location: ${e.toString()}");
}
});
} else {
print("[Test isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
print("[Location isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
}
});
}
@ -78,14 +78,18 @@ class LocationManager {
}
LocationManager._internal() {
_registerLocationListener();
_startLocationService();
}
final int alarmId = 34901199;
final Duration locationUpdateInterval = Duration(minutes: 5);
void _registerLocationListener() async {
Logger.d("Activating alarm service test");
void _startLocationService() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? 10);
Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId);
Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes...");
await AndroidAlarmManager.periodic(
locationUpdateInterval,
alarmId,

View File

@ -12,8 +12,6 @@ class PanelPage extends StatefulWidget {
class _PanelPageState extends State<PanelPage> {
List<ConfigurationItem> _items;
@override
void initState() {
super.initState();

View File

@ -0,0 +1,182 @@
part of '../main.dart';
class ConfigPanelWidget extends StatefulWidget {
ConfigPanelWidget({Key key}) : super(key: key);
@override
_ConfigPanelWidgetState createState() => new _ConfigPanelWidgetState();
}
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
int locationInterval;
bool needToRestartLocationTracking = false;
@override
void initState() {
super.initState();
_loadSettings();
}
_loadSettings() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
SharedPreferences.getInstance().then((prefs) {
setState(() {
locationInterval = prefs.getInt("location-interval") ?? 10;
});
});
}
void incLocationInterval() {
needToRestartLocationTracking = true;
if (locationInterval < 720) {
setState(() {
locationInterval = locationInterval + 1;
});
SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", locationInterval));
}
}
void decLocationInterval() {
needToRestartLocationTracking = true;
if (locationInterval > 1) {
setState(() {
locationInterval = locationInterval - 1;
});
SharedPreferences.getInstance().then((prefs) {
prefs.setInt("location-interval", locationInterval);
});
}
}
restart() {
eventBus.fire(ShowPopupDialogEvent(
title: "Are you sure you want to restart Home Assistant?",
body: "This will restart your Home Assistant server.",
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
Connection().callService(domain: "homeassistant", service: "restart", entityId: null);
},
));
}
stop() {
eventBus.fire(ShowPopupDialogEvent(
title: "Are you sure you wanr to STOP Home Assistant?",
body: "This will STOP your Home Assistant server. It means that your web interface as well as HA Client will not work untill you'll find a way to start your server using ssh or something.",
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
Connection().callService(domain: "homeassistant", service: "stop", entityId: null);
},
));
}
updateRegistration() {
HomeAssistant().checkAppRegistration(showOkDialog: true);
}
resetRegistration() {
eventBus.fire(ShowPopupDialogEvent(
title: "Waaaait",
body: "If you don't whant to have duplicate integrations and entities in your HA for your current device, first you need to remove MobileApp integration from Integration settings in HA and restart server.",
positiveText: "Done it already",
negativeText: "Ok, I will",
onPositive: () {
HomeAssistant().checkAppRegistration(showOkDialog: true, forceRegister: true);
},
));
}
@override
Widget build(BuildContext context) {
return ListView(
children: [
Card(
child: Padding(
padding: EdgeInsets.only(left: Sizes.leftWidgetPadding, right: Sizes.rightWidgetPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(
title: Text("Mobile app integration",
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: Sizes.largeFontSize))
),
Text("Registration", style: TextStyle(fontSize: Sizes.largeFontSize-2)),
Container(height: Sizes.rowPadding,),
Text("${HomeAssistant().userName}'s ${Device().model}, ${Device().osName} ${Device().osVersion}"),
Container(height: 6.0,),
Text("Here you can manually check if HA Client integration with your Home Assistant works fine. As mobileApp integration in Home Assistant is still in development, this is not 100% correct check."),
//Divider(),
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton(
color: Colors.blue,
onPressed: () => updateRegistration(),
child: Text("Check registration", style: TextStyle(color: Colors.white))
),
Container(width: 10.0,),
RaisedButton(
color: Colors.redAccent,
onPressed: () => resetRegistration(),
child: Text("Reset registration", style: TextStyle(color: Colors.white))
)
],
),
Divider(),
Text("Location tracking", style: TextStyle(fontSize: Sizes.largeFontSize-2)),
Container(height: Sizes.rowPadding,),
Text("Location update interval in minutes:"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//Expanded(child: Container(),),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)),
onPressed: () => incLocationInterval(),
),
Text("$locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)),
onPressed: () => decLocationInterval(),
),
],
)
],
),
),
),
LinkToWebConfig(name: "Home Assistant Cloud", url: Connection().httpWebHost+"/config/cloud/account"),
Container(height: 8.0,),
LinkToWebConfig(name: "Integrations", url: Connection().httpWebHost+"/config/integrations/dashboard"),
LinkToWebConfig(name: "Users", url: Connection().httpWebHost+"/config/users/picker"),
Container(height: 8.0,),
LinkToWebConfig(name: "General", url: Connection().httpWebHost+"/config/core"),
LinkToWebConfig(name: "Server Control", url: Connection().httpWebHost+"/config/server_control"),
LinkToWebConfig(name: "Persons", url: Connection().httpWebHost+"/config/person"),
LinkToWebConfig(name: "Entity Registry", url: Connection().httpWebHost+"/config/entity_registry"),
LinkToWebConfig(name: "Area Registry", url: Connection().httpWebHost+"/config/area_registry"),
LinkToWebConfig(name: "Automation", url: Connection().httpWebHost+"/config/automation"),
LinkToWebConfig(name: "Script", url: Connection().httpWebHost+"/config/script"),
LinkToWebConfig(name: "Customization", url: Connection().httpWebHost+"/config/customize"),
],
);
}
@override
void dispose() {
if (needToRestartLocationTracking) {
Logger.d("Location tracking interval was changed. Rescheduling location service...");
LocationManager()._startLocationService();
}
super.dispose();
}
}

View File

@ -0,0 +1,27 @@
part of '../../main.dart';
class LinkToWebConfig extends StatelessWidget {
final String name;
final String url;
const LinkToWebConfig({Key key, @required this.name, @required this.url}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: <Widget>[
ListTile(
title: Text("${this.name}",
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: Sizes.largeFontSize)),
subtitle: Text("Tap to opne web version"),
onTap: () => HAUtils.launchURLInCustomTab(context: context, url: this.url),
)
],
),
);
}
}

View File

@ -1,331 +0,0 @@
part of '../main.dart';
class ConfigPanelWidget extends StatefulWidget {
ConfigPanelWidget({Key key}) : super(key: key);
@override
_ConfigPanelWidgetState createState() => new _ConfigPanelWidgetState();
}
class ConfigurationItem {
ConfigurationItem({ this.isExpanded: false, this.header, this.body });
bool isExpanded;
final String header;
final Widget body;
}
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
List<ConfigurationItem> _items;
@override
void initState() {
super.initState();
_items = <ConfigurationItem>[
ConfigurationItem(
header: 'Home Assistant Cloud',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/cloud/account");
},
)
],
),
)
),
ConfigurationItem(
header: 'Integrations',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/integrations/dashboard");
},
)
],
),
)
),
ConfigurationItem(
header: 'Users',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/users/picker");
},
)
],
),
)
),
ConfigurationItem(
header: 'General',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/core");
},
),
Container(height: Sizes.rowPadding,),
Text("Server management", style: TextStyle(fontSize: Sizes.largeFontSize)),
Container(height: Sizes.rowPadding,),
Text("Control your Home Assistant server from HA Client."),
Divider(),
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Restart', style: TextStyle(color: Colors.blue)),
onPressed: () => restart(),
),
FlatButton(
child: Text("Stop", style: TextStyle(color: Colors.blue)),
onPressed: () => stop(),
),
],
)
],
),
)
),
ConfigurationItem(
header: 'Persons',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/person");
},
)
],
),
)
),
ConfigurationItem(
header: 'Entity Registry',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/entity_registry");
},
)
],
),
)
),
ConfigurationItem(
header: 'Area Registry',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/area_registry");
},
)
],
),
)
),
ConfigurationItem(
header: 'Automation',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/automation");
},
)
],
),
)
),
ConfigurationItem(
header: 'Script',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/script");
},
)
],
),
)
),
ConfigurationItem(
header: 'Customization',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('Open web version', style: TextStyle(color: Colors.blue)),
onPressed: () {
HAUtils.launchURLInCustomTab(context: context, url: Connection().httpWebHost+"/config/customize");
},
)
],
),
)
),
ConfigurationItem(
header: 'Mobile app',
body: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, 0.0, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Registration", style: TextStyle(fontSize: Sizes.largeFontSize)),
Container(height: Sizes.rowPadding,),
Text("${HomeAssistant().userName}'s ${Device().model}, ${Device().osName} ${Device().osVersion}"),
Container(height: 6.0,),
Text("Here you can manually check if HA Client integration with your Home Assistant works fine. As mobileApp integration in Home Assistant is still in development, this is not 100% correct check."),
Divider(),
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
onPressed: () => updateRegistration(),
child: Text("Check registration", style: TextStyle(color: Colors.blue))
),
FlatButton(
onPressed: () => resetRegistration(),
child: Text("Reset registration", style: TextStyle(color: Colors.red))
)
],
)
],
),
)
)
];
}
restart() {
eventBus.fire(ShowPopupDialogEvent(
title: "Are you sure you want to restart Home Assistant?",
body: "This will restart your Home Assistant server.",
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
Connection().callService(domain: "homeassistant", service: "restart", entityId: null);
},
));
}
stop() {
eventBus.fire(ShowPopupDialogEvent(
title: "Are you sure you wanr to STOP Home Assistant?",
body: "This will STOP your Home Assistant server. It means that your web interface as well as HA Client will not work untill you'll find a way to start your server using ssh or something.",
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
Connection().callService(domain: "homeassistant", service: "stop", entityId: null);
},
));
}
updateRegistration() {
HomeAssistant().checkAppRegistration(showOkDialog: true);
}
resetRegistration() {
eventBus.fire(ShowPopupDialogEvent(
title: "Waaaait",
body: "If you don't whant to have duplicate integrations and entities in your HA for your current device, first you need to remove MobileApp integration from Integration settings in HA and restart server.",
positiveText: "Done it already",
negativeText: "Ok, I will",
onPositive: () {
HomeAssistant().checkAppRegistration(showOkDialog: true, forceRegister: true);
},
));
}
@override
Widget build(BuildContext context) {
return ListView(
children: [
new ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
_items[index].isExpanded = !_items[index].isExpanded;
});
},
children: _items.map((ConfigurationItem item) {
return new ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return CardHeaderWidget(
name: item.header,
);
},
isExpanded: item.isExpanded,
body: new Container(
child: item.body,
),
);
}).toList(),
),
],
);
}
@override
void dispose() {
super.dispose();
}
}