Skip to content

Commit 5562923

Browse files
OATMobile - Updates
- Added AutoDiscovery and connection to Wifi OAT.
1 parent 6befcb3 commit 5562923

File tree

3 files changed

+104
-44
lines changed

3 files changed

+104
-44
lines changed

Software/OATMobile/OATMobile/OATMobile/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using OATMobile.Services;
33
using OATMobile.Views;
44

5+
using OATCommunications.CommunicationHandlers;
6+
57
namespace OATMobile
68
{
79
public partial class App : Application
@@ -17,6 +19,7 @@ public App()
1719

1820
protected override void OnStart()
1921
{
22+
CommunicationHandlerFactory.DiscoverDevices();
2023
}
2124

2225
protected override void OnSleep()

Software/OATMobile/OATMobile/OATMobile/ViewModels/MountControlViewModel.cs

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,109 @@
33
using OATCommunications.TelescopeCommandHandlers;
44
using Xamarin.Forms;
55
using Xamarin.Essentials;
6+
using System.Linq;
7+
using OATCommunications;
8+
using OATCommunications.CommunicationHandlers;
9+
using System.Net;
10+
using System.Timers;
611

7-
namespace OATMobile.ViewModels {
8-
public class MountControlViewModel : ViewModelBase {
12+
namespace OATMobile.ViewModels
13+
{
14+
public class MountControlViewModel : ViewModelBase
15+
{
16+
private ITelescopeCommandHandler _cmdHandler;
917

10-
private ITelescopeCommandHandler _cmdHandler;
18+
public string OppositeParkStateText { get; private set; } = "Unpark";
19+
private Timer discoveryTimer = new Timer(500);
1120

12-
public string OppositeParkStateText { get; private set; } = "Unpark";
21+
public MountControlViewModel()
22+
{
23+
Title = "Control";
1324

14-
public MountControlViewModel(ITelescopeCommandHandler handler) {
15-
_cmdHandler = handler;
16-
Title = "Control";
25+
if (CommunicationHandlerFactory.AvailableDevices.Any())
26+
{
27+
ConnectToDevice();
28+
}
29+
else
30+
{
31+
discoveryTimer.Elapsed += (s, e) =>
32+
{
33+
if (CommunicationHandlerFactory.AvailableDevices.Any())
34+
{
35+
discoveryTimer.Stop();
1736

18-
_cmdHandler.MountState.PropertyChanged += MountStateOnPropertyChanged;
37+
// This is probably needed for Windows. Seems to work OK on Android.
38+
// RunOnUiThread() is platform specific :-(
1939

20-
Commands.Add("GoHome", new Command(async () => {
21-
await _cmdHandler.GoHome();
22-
await _cmdHandler.RefreshMountState();
23-
}));
40+
//Activity.RunOnUiThread(() =>
41+
//{
42+
ConnectToDevice();
43+
discoveryTimer.Dispose();
44+
discoveryTimer = null;
45+
//});
46+
}
47+
};
2448

25-
Commands.Add("StartMoveDirection", new Command<string>(x => { _cmdHandler.StartMoving(x); }));
49+
discoveryTimer.Start();
50+
}
51+
}
2652

27-
Commands.Add("StopMoveDirection", new Command<string>(x => { _cmdHandler.StopMoving(x); }));
53+
public void ConnectToDevice()
54+
{
55+
var device = CommunicationHandlerFactory.ConnectToDevice(CommunicationHandlerFactory.AvailableDevices.First());
56+
_cmdHandler = new OatmealTelescopeCommandHandlers(device);
2857

29-
Commands.Add("SetTracking", new Command<bool>(x => { _cmdHandler.SetTracking(x); }));
58+
_cmdHandler.MountState.PropertyChanged += MountStateOnPropertyChanged;
3059

31-
Commands.Add("SetLocation", new Command(async () => {
32-
var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Default));
33-
await _cmdHandler.SetLocation(location.Latitude, location.Longitude, 0, 0);
34-
}));
60+
Commands.Add("GoHome", new Command(async () =>
61+
{
62+
await _cmdHandler?.GoHome();
63+
await _cmdHandler?.RefreshMountState();
64+
}));
3565

36-
Commands.Add("ToggleParkedState", new Command(async () => {
37-
if (_cmdHandler.MountState.IsTracking) {
38-
await _cmdHandler.SetTracking(false);
39-
}
40-
else {
41-
await _cmdHandler.SetTracking(true);
42-
}
43-
}));
66+
Commands.Add("StartMoveDirection", new Command<string>(x => { _cmdHandler?.StartMoving(x); }));
4467

45-
_cmdHandler.RefreshMountState();
46-
}
68+
Commands.Add("StopMoveDirection", new Command<string>(x => { _cmdHandler?.StopMoving(x); }));
4769

48-
private void MountStateOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
49-
switch (e.PropertyName) {
50-
case nameof(MountState.IsTracking):
51-
OppositeParkStateText = _cmdHandler.MountState.IsTracking ? "Park" : "Unpark";
52-
break;
53-
}
54-
}
70+
Commands.Add("SetTracking", new Command<bool>(x => { _cmdHandler?.SetTracking(x); }));
5571

56-
public bool IsConnected { get; private set; }
57-
}
72+
Commands.Add("SetLocation", new Command(async () =>
73+
{
74+
var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Default));
75+
await _cmdHandler?.SetLocation(location.Latitude, location.Longitude, 0, 0);
76+
}));
77+
78+
Commands.Add("ToggleParkedState", new Command(async () =>
79+
{
80+
if (_cmdHandler != null)
81+
{
82+
if (_cmdHandler.MountState.IsTracking)
83+
{
84+
await _cmdHandler?.SetTracking(false);
85+
}
86+
else
87+
{
88+
await _cmdHandler?.SetTracking(true);
89+
}
90+
}
91+
}));
92+
93+
_cmdHandler?.RefreshMountState();
94+
}
95+
96+
private void MountStateOnPropertyChanged(object sender, PropertyChangedEventArgs e)
97+
{
98+
if (_cmdHandler != null)
99+
{
100+
switch (e.PropertyName)
101+
{
102+
case nameof(MountState.IsTracking):
103+
OppositeParkStateText = _cmdHandler.MountState.IsTracking ? "Park" : "Unpark";
104+
break;
105+
}
106+
}
107+
}
108+
109+
public bool IsConnected { get; private set; }
110+
}
58111
}

Software/OATMobile/OATMobile/OATMobile/Views/MountControlView.xaml.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using OATCommunications.CommunicationHandlers;
66
using Xamarin.Forms;
77
using OATMobile.ViewModels;
8+
using System.Linq;
89

910
namespace OATMobile.Views {
1011
// Learn more about making custom code visible in the Xamarin.Forms previewer
@@ -15,20 +16,23 @@ public partial class MountControlView : ContentPage {
1516

1617
public MountControlView() {
1718
InitializeComponent();
18-
var handler =
19-
new OatmealTelescopeCommandHandlers(
20-
new TcpCommunicationHandler(new IPAddress(new byte[] {192, 168, 86, 57}), 4030));
21-
BindingContext = viewModel = new MountControlViewModel(handler);
19+
BindingContext = viewModel = new MountControlViewModel();
2220
}
2321

2422
private void Move_Button_Pressed(object sender, EventArgs e) {
2523
var dir = (sender as ImageButton).CommandParameter.ToString();
26-
viewModel.Commands["StartMoveDirection"].Execute(dir);
24+
if (viewModel.Commands.Any())
25+
{
26+
viewModel.Commands["StartMoveDirection"].Execute(dir);
27+
}
2728
}
2829

2930
private void Move_Button_Released(object sender, EventArgs e) {
3031
var dir = (sender as ImageButton).CommandParameter.ToString();
31-
viewModel.Commands["StopMoveDirection"].Execute(dir);
32+
if (viewModel.Commands.Any())
33+
{
34+
viewModel.Commands["StopMoveDirection"].Execute(dir);
35+
}
3236
}
3337
}
3438
}

0 commit comments

Comments
 (0)