1+ @page " /HardwareSelect"
2+ @using System .Net
3+ @using System .Timers
4+ @using System .Numerics
5+ @using ArduinoUploader .Hardware
6+ @using NFTLock .Data
7+ @using System .IO .Ports
8+ @using System .Diagnostics
9+ @using SYNCWallet .Services
10+ @using SYNCWallet .Services .Definitions
11+ @using static SYNCWallet .Models .Enums
12+
13+ @inject NavigationManager NavigationManager
14+ @inject IJSRuntime JS
15+
16+
17+
18+ <div class =" container FixCenter" id =" LoginPanel" style =" height :100vh ; display :flex ;" >
19+ <div class =" row" style =" width : 100% ;display : flex ;justify-content : center ;align-items : center ; padding :0 ; margin :0 ;" >
20+ <div class =" col-md-4 col-sm-12 " style =" min-width : 500px ; " >
21+ <div class =" row" style =" display : flex ;flex-direction : row ;align-items : center ;justify-content : center ;" >
22+ <img src =" /logo.png" style =" width : 250px ;height : 200px ;" alt =" homepage" class =" light-logo" />
23+
24+ </div >
25+ <div class =" row" style =" margin-top : 50px ;" >
26+ <h2 style =" color :#EA7080 ; text-align :center ; font-size :25pt ;padding : 50px ;" >
27+ Connect and select ATmega328 compatable device.
28+ </h2 >
29+ </div >
30+ <a class =" nav-link dropdown-toggle waves-effect waves-dark" href =" #" data-bs-toggle =" dropdown" aria-haspopup =" true" aria-expanded =" false" style =" color : black ;font-weight : 400 ; text-align :center ;" >
31+ <i class =" me-2 mdi mdi-access-point-network" ></i >
32+ <span id =" network" style =" text-align :center ; width :100% ;" >@DeviceModel.ToString() </span >
33+
34+ </a >
35+ <div id =" dropdownNetworks" class =" dropdown-menu dropdown-menu-end user-dd animated flipInY" style =" left : 68% ;top : 4% ; position :absolute !important ; height :150px ; overflow-y :scroll ;background : #282931 ;" >
36+ @if (Devices != null )
37+ {
38+ @foreach ( var device in Devices )
39+ {
40+ <a class =" dropdown-item" @onclick =" (() => DeviceChanged(device))" >
41+ <i class =" me-2 mdi mdi-access-point-network" ></i >
42+ @device.ToString()
43+ </a >
44+ }
45+
46+ }
47+ </div >
48+ </div >
49+ </div >
50+ </div >
51+
52+ @code {
53+ private ICommunication Communication { get ; set ; }
54+ private IAuthenicationService AuthenicationHandler { get ; set ; }
55+ private IHardwareService HardwareService { get ; set ; }
56+ private string Address { get ; set ; }
57+ private string Port { get ; set ; }
58+
59+ public ArduinoModel DeviceModel { get ; set ; }
60+ public List <ArduinoModel > Devices { get ; set ; }
61+
62+ System .Timers .Timer aTimer { get ; set ; }
63+
64+ protected override Task OnAfterRenderAsync (bool firstRender )
65+ {
66+
67+ return base .OnAfterRenderAsync (firstRender );
68+ }
69+
70+ protected override async Task OnInitializedAsync ()
71+ {
72+
73+ AuthenicationHandler = ServiceHelper .GetService <IAuthenicationService >();
74+ HardwareService = ServiceHelper .GetService <IHardwareService >();
75+ Communication = ServiceHelper .GetService <ICommunication >();
76+
77+
78+ Communication .Init ();
79+ // Get Supported Devices
80+ Devices = HardwareService .GetSupportedDevices ();
81+
82+ Port = HardwareService .DeviceConnected ();
83+ aTimer = new System .Timers .Timer ();
84+ aTimer .Elapsed += new ElapsedEventHandler (OnTimedEvent );
85+ aTimer .Interval = 5000 ;
86+ aTimer .Start ();
87+
88+ }
89+
90+ // Specify what you want to happen when the Elapsed event is raised.
91+ private void OnTimedEvent (object source , ElapsedEventArgs e )
92+ {
93+
94+ Port = HardwareService .DeviceConnected ();
95+ if (! string .IsNullOrEmpty (Port ))
96+ {
97+ aTimer .Stop ();
98+ aTimer .Dispose ();
99+ }
100+ }
101+
102+ private async Task <bool > CheckDeviceConnected (string port )
103+ {
104+
105+ try
106+ {
107+ if (! string .IsNullOrEmpty (port ))
108+ {
109+ var firmwareUpdated = HardwareService .CreateNewDevice (port );
110+
111+ var configStatus = Communication .CheckConfigured (ConfigMode .ColdWallet );
112+
113+ if (configStatus && firmwareUpdated )
114+ {
115+
116+ KillTimer ();
117+ NavigationManager .NavigateTo (" LoginPanel" );
118+ }
119+ else
120+ {
121+ KillTimer ();
122+ NavigationManager .NavigateTo (" Create" );
123+
124+ }
125+ }
126+
127+ }
128+ catch (Exception e )
129+ {
130+
131+ throw ;
132+ }
133+
134+ return true ;
135+ }
136+
137+
138+ private void KillTimer ()
139+ {
140+ if (aTimer != null )
141+ {
142+ aTimer .Stop ();
143+ aTimer .Dispose ();
144+ }
145+ }
146+
147+ private void DeviceChanged (ArduinoModel deviceType )
148+ {
149+ InvokeAsync (async () =>
150+ {
151+ DeviceModel = deviceType ;
152+ Communication .DeviceType = deviceType ;
153+ await CheckDeviceConnected (Port );
154+ StateHasChanged ();
155+
156+ });
157+
158+
159+
160+ }
161+ }
0 commit comments