33using OATCommunications . TelescopeCommandHandlers ;
44using Xamarin . Forms ;
55using 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}
0 commit comments