@@ -258,3 +258,48 @@ public class PowerAllCommandParameter
258258 public string Command { get ; set ; } = string . Empty ;
259259 public IEnumerable < LighthouseObject > Lighthouses { get ; set ; } = Enumerable . Empty < LighthouseObject > ( ) ;
260260}
261+
262+ public class IdentifyCommand : AsyncCommandBase
263+ {
264+ public override event EventHandler ? CanExecuteChanged ;
265+ private Task ? _task ;
266+ private readonly ILogger _log = LogHelper . ForContext < IdentifyCommand > ( ) ;
267+
268+ public override bool CanExecute ( object ? parameter )
269+ {
270+ var isV1 = parameter is LighthouseObject lighthouse && new Lighthouse { Name = lighthouse . Name } . Version == LighthouseVersion . V1 ;
271+ if ( isV1 )
272+ {
273+ return false ;
274+ }
275+ return _task == null || _task . IsCompleted ;
276+ }
277+
278+ public async override Task ExecuteAsync ( object ? parameter )
279+ {
280+ if ( parameter is LighthouseObject lighthouse )
281+ {
282+ var notification = App . GetService < INotificationService > ( ) ;
283+ try
284+ {
285+ _log . Information ( $ "{ lighthouse . Name } Identify") ;
286+ var l = new Lighthouse { Name = lighthouse . Name , BluetoothAddress = lighthouse . BluetoothAddress , Id = lighthouse . Id } ;
287+ _task = App . GetService < ILighthouseGattService > ( ) . IdentifyAsync ( l ) ;
288+ CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
289+ await _task ;
290+ _task = null ;
291+ CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
292+ notification . Success ( string . Format ( "Notification_Identify" . GetLocalized ( ) , lighthouse . Name ) ) ;
293+ }
294+ catch ( Exception ex )
295+ {
296+ _log . Error ( ex , $ "{ lighthouse . Name } Failed to identify") ;
297+ notification . Error ( string . Format ( "Notification_CommunicationError" . GetLocalized ( ) , lighthouse . Name ) + "\n " + ex . Message ) ;
298+ }
299+ }
300+ else
301+ {
302+ throw new ArgumentException ( "Invalid parameter" ) ;
303+ }
304+ }
305+ }
0 commit comments