@@ -199,108 +199,74 @@ public PowerAllCommandOperation Operation
199199 get ; private set ;
200200 }
201201
202- private List < ( LighthouseObject lighthouse , IAsyncCommand powerOn , IAsyncCommand powerDown ) > lighthouses = new ( ) ;
203-
204- private PowerDownMode powerDownMode = PowerDownMode . Sleep ;
205- private bool powerDownModeChangeQueued = false ;
206-
207- public void SetPowerDownMode ( PowerDownMode powerDownMode )
208- {
209- this . powerDownMode = powerDownMode ;
210-
211- if ( CanExecute ( ) )
212- {
213- ApplyPowerDownMode ( ) ;
214- }
215- else
216- {
217- // Wait until any outstanding commands are done to apply the mode change
218- powerDownModeChangeQueued = true ;
219- }
220- }
221-
222- private void ApplyPowerDownMode ( )
223- {
224- for ( var i = 0 ; i < lighthouses . Count ; i ++ )
225- {
226- lighthouses [ i ] . powerOn . CanExecuteChanged -= SubcommandCanExecuteChanged ;
227- lighthouses [ i ] . powerDown . CanExecuteChanged -= SubcommandCanExecuteChanged ;
228- lighthouses [ i ] = NewCommandsTuple ( lighthouses [ i ] . lighthouse ) ;
229- }
230- powerDownModeChangeQueued = false ;
231- }
232-
233- private ( LighthouseObject lighthouse , IAsyncCommand powerOn , IAsyncCommand powerOff ) NewCommandsTuple ( LighthouseObject l )
234- {
235- var powerOn = new PowerOnCommand ( ) ;
236- powerOn . CanExecuteChanged += SubcommandCanExecuteChanged ;
237-
238- IAsyncCommand powerDown = powerDownMode == PowerDownMode . Sleep || l . Lighthouse . Version == LighthouseVersion . V1 ? new SleepCommand ( ) : new StandbyCommand ( ) ;
239- powerDown . CanExecuteChanged += SubcommandCanExecuteChanged ;
240-
241- return ( l , powerOn , powerDown ) ;
242- }
202+ private readonly List < LighthouseObject > lighthouses = new ( ) ;
243203
244204 public void AddLighthouse ( LighthouseObject lighthouse )
245205 {
246- if ( ! lighthouses . Any ( x => x . lighthouse == lighthouse ) )
206+ if ( ! lighthouses . Any ( x => x == lighthouse ) )
247207 {
248- lighthouses . Add ( NewCommandsTuple ( lighthouse ) ) ;
208+ lighthouses . Add ( lighthouse ) ;
249209 }
250210 }
251211
252212 public void RemoveLighthouse ( LighthouseObject lighthouse )
253213 {
254- var i = lighthouses . FindIndex ( x => x . lighthouse == lighthouse ) ;
255- if ( i >= 0 )
256- {
257- lighthouses [ i ] . powerOn . CanExecuteChanged -= SubcommandCanExecuteChanged ;
258- lighthouses [ i ] . powerDown . CanExecuteChanged -= SubcommandCanExecuteChanged ;
259- lighthouses . RemoveAt ( i ) ;
260- CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
261- }
262- }
263-
264- private void SubcommandCanExecuteChanged ( object ? sender , EventArgs e )
265- {
266- if ( powerDownModeChangeQueued && CanExecute ( ) )
267- {
268- ApplyPowerDownMode ( ) ;
269- }
270-
271- if ( CanExecute ( ) )
272- {
273- Operation = PowerAllCommandOperation . None ;
274- }
275-
276- CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
214+ lighthouses . Remove ( lighthouse ) ;
277215 }
278216
279217 public bool CanExecute ( )
280218 {
281- return lighthouses . All ( x => x . powerOn . CanExecute ( x . lighthouse ) && x . powerDown . CanExecute ( x . lighthouse ) ) ;
219+ return Operation == PowerAllCommandOperation . None && lighthouses . Any ( l => l . IsManaged ) ;
282220 }
283221
284222 public override bool CanExecute ( object ? _ ) => CanExecute ( ) ;
285223
286224 public async override Task ExecuteAsync ( object ? parameter )
287225 {
288- var command = parameter as string ;
289-
290- Operation = command switch
226+ try
291227 {
292- "powerOn" => PowerAllCommandOperation . PowerOn ,
293- "powerDown" => PowerAllCommandOperation . PowerDown ,
294- _ => PowerAllCommandOperation . None
295- } ;
296- var on = command == "powerOn" ;
228+ var command = parameter as string ;
297229
298- foreach ( ( var lighthouse , var powerOn , var powerDown ) in lighthouses )
299- {
300- if ( lighthouse . IsManaged )
230+ Operation = command switch
301231 {
302- await ( on ? powerOn : powerDown ) . ExecuteAsync ( lighthouse ) ;
232+ "powerOn" => PowerAllCommandOperation . PowerOn ,
233+ "powerDown" => PowerAllCommandOperation . PowerDown ,
234+ _ => PowerAllCommandOperation . None
235+ } ;
236+ CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
237+
238+ var on = command == "powerOn" ;
239+
240+ var settings = App . GetService < ILighthouseSettingsService > ( ) ;
241+ foreach ( var lighthouse in lighthouses . Where ( l => l . IsManaged ) )
242+ {
243+ switch ( Operation )
244+ {
245+ case PowerAllCommandOperation . PowerOn :
246+ var powerOn = new PowerOnCommand ( ) ;
247+ await powerOn . ExecuteAsync ( lighthouse ) ;
248+ break ;
249+ case PowerAllCommandOperation . PowerDown :
250+ if ( settings . PowerDownMode == PowerDownMode . Sleep || lighthouse . Lighthouse . Version == LighthouseVersion . V1 )
251+ {
252+ var sleep = new SleepCommand ( ) ;
253+ await sleep . ExecuteAsync ( lighthouse ) ;
254+ }
255+ else
256+ {
257+ var standby = new StandbyCommand ( ) ;
258+ await standby . ExecuteAsync ( lighthouse ) ;
259+ }
260+ break ;
261+ default :
262+ continue ;
263+ }
303264 }
304265 }
266+ finally
267+ {
268+ Operation = PowerAllCommandOperation . None ;
269+ CanExecuteChanged ? . Invoke ( this , EventArgs . Empty ) ;
270+ }
305271 }
306272}
0 commit comments