Skip to content

Commit 4f04664

Browse files
committed
Add simultaneous way option
1 parent be4ab1e commit 4f04664

File tree

8 files changed

+161
-36
lines changed

8 files changed

+161
-36
lines changed

OVRLighthouseManager/Contracts/Services/ILighthouseSettingsService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public PowerDownMode PowerDownMode
1414
get;
1515
}
1616

17+
public bool SendSimultaneously
18+
{
19+
get;
20+
}
21+
1722
public List<Lighthouse> Devices
1823
{
1924
get;
@@ -22,5 +27,6 @@ public List<Lighthouse> Devices
2227
public Task InitializeAsync();
2328
public Task SetPowerManagementAsync(bool powerManagement);
2429
public Task SetPowerDownModeAsync(PowerDownMode powerDownMode);
30+
public Task SetSendSimultaneously(bool sendSimultaneously);
2531
public Task SetDevicesAsync(Lighthouse[] device);
2632
}

OVRLighthouseManager/Helpers/LighthouseCommands.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,30 +225,44 @@ public async override Task ExecuteAsync(object? parameter)
225225
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
226226

227227
var settings = App.GetService<ILighthouseSettingsService>();
228+
var tasks = new List<Func<Task>>();
228229
foreach (var lighthouse in param.Lighthouses.Where(l => l.IsManaged))
229230
{
230231
switch (Operation)
231232
{
232233
case PowerAllCommandOperation.PowerOn:
233234
var powerOn = new PowerOnCommand();
234-
await powerOn.ExecuteAsync(lighthouse);
235+
tasks.Add(() => powerOn.ExecuteAsync(lighthouse));
235236
break;
236237
case PowerAllCommandOperation.PowerDown:
237238
if (settings.PowerDownMode == PowerDownMode.Sleep || lighthouse.Lighthouse.Version == LighthouseVersion.V1)
238239
{
239240
var sleep = new SleepCommand();
240-
await sleep.ExecuteAsync(lighthouse);
241+
tasks.Add(() => sleep.ExecuteAsync(lighthouse));
241242
}
242243
else
243244
{
244245
var standby = new StandbyCommand();
245-
await standby.ExecuteAsync(lighthouse);
246+
tasks.Add(() => standby.ExecuteAsync(lighthouse));
246247
}
247248
break;
248249
default:
249250
continue;
250251
}
251252
}
253+
254+
if (settings.SendSimultaneously)
255+
{
256+
await Task.WhenAll(tasks.Select(t => t()));
257+
}
258+
else
259+
{
260+
foreach (var task in tasks)
261+
{
262+
await task();
263+
await Task.Delay(200);
264+
}
265+
}
252266
}
253267
finally
254268
{

OVRLighthouseManager/Services/AppLifeCycleService.cs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,35 @@ private async Task OnVRMonitorConnected()
8686
}
8787

8888
var managedDevices = _lighthouseSettingsService.Devices.Where(d => d.IsManaged).ToArray();
89+
var tasks = new List<Func<Task>>();
8990
foreach (var d in managedDevices)
90-
{
91-
Log.Information($"Power On {d.Name}");
92-
try
91+
{
92+
tasks.Add(async () =>
9393
{
94-
await _lighthouseGattService.PowerOnAsync(d);
95-
Log.Information($"Done {d.Name}");
96-
}
97-
catch (Exception e)
94+
Log.Information($"Power On {d.Name}");
95+
try
96+
{
97+
await _lighthouseGattService.PowerOnAsync(d);
98+
Log.Information($"Done {d.Name}");
99+
}
100+
catch (Exception e)
101+
{
102+
Log.Error(e, $"Failed to power on {d.Name}");
103+
}
104+
});
105+
}
106+
107+
if (_lighthouseSettingsService.SendSimultaneously)
108+
{
109+
await Task.WhenAll(tasks.Select(t => t()));
110+
}
111+
else
112+
{
113+
foreach (var task in tasks)
98114
{
99-
Log.Error(e, $"Failed to power on {d.Name}");
115+
await task();
116+
await Task.Delay(200);
100117
}
101-
await Task.Delay(200);
102118
}
103119

104120
Log.Information("OnVRMonitorConnected Done");
@@ -119,40 +135,56 @@ private async Task OnVRSystemQuit()
119135
}
120136

121137
var managedDevices = _lighthouseSettingsService.Devices.Where(d => d.IsManaged).ToArray();
138+
var tasks = new List<Func<Task>>();
122139
foreach (var d in managedDevices)
123140
{
124-
var powerDownMode = _lighthouseSettingsService.PowerDownMode;
125-
if (powerDownMode == PowerDownMode.Sleep || d.Version == LighthouseVersion.V1)
141+
tasks.Add(async () =>
126142
{
127-
Log.Information($"Sleep {d.Name}");
128-
try
129-
{
130-
await _lighthouseGattService.SleepAsync(d);
131-
Log.Information($"Done {d.Name}");
132-
}
133-
catch (Exception e)
143+
var powerDownMode = _lighthouseSettingsService.PowerDownMode;
144+
if (powerDownMode == PowerDownMode.Sleep || d.Version == LighthouseVersion.V1)
134145
{
135-
Log.Error(e, $"Failed to sleep {d.Name}");
146+
Log.Information($"Sleep {d.Name}");
147+
try
148+
{
149+
await _lighthouseGattService.SleepAsync(d);
150+
Log.Information($"Done {d.Name}");
151+
}
152+
catch (Exception e)
153+
{
154+
Log.Error(e, $"Failed to sleep {d.Name}");
155+
}
136156
}
137-
}
138-
else if (powerDownMode == PowerDownMode.Standby)
139-
{
140-
Log.Information($"Standby {d.Name}");
141-
try
157+
else if (powerDownMode == PowerDownMode.Standby)
142158
{
143-
await _lighthouseGattService.StandbyAsync(d);
144-
Log.Information($"Done {d.Name}");
159+
Log.Information($"Standby {d.Name}");
160+
try
161+
{
162+
await _lighthouseGattService.StandbyAsync(d);
163+
Log.Information($"Done {d.Name}");
164+
}
165+
catch (Exception e)
166+
{
167+
Log.Error(e, $"Failed to standby {d.Name}");
168+
}
145169
}
146-
catch (Exception e)
170+
else
147171
{
148-
Log.Error(e, $"Failed to standby {d.Name}");
172+
throw new InvalidProgramException("Unknown PowerDownMode");
149173
}
150-
}
151-
else
174+
});
175+
}
176+
177+
if (_lighthouseSettingsService.SendSimultaneously)
178+
{
179+
await Task.WhenAll(tasks.Select(t => t()));
180+
}
181+
else
182+
{
183+
foreach(var task in tasks)
152184
{
153-
throw new InvalidProgramException("Unknown PowerDownMode");
185+
await task();
186+
await Task.Delay(200);
154187
}
155-
await Task.Delay(200);
156188
}
157189

158190
await _scanCommand.StopScan();

OVRLighthouseManager/Services/LighthouseSettingsService.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class LighthouseSettingsService : ILighthouseSettingsService
1212
{
1313
private const string SettingsKey_PowerManagement = "PowerManagement";
1414
private const string SettingsKey_PowerDownMode = "PowerDownMode";
15+
private const string SettingsKey_SendSimultaneously = "SendSimultaneously";
1516
private const string SettingsKey_Devices = "Devices";
1617

1718
public bool PowerManagement
@@ -24,6 +25,11 @@ public PowerDownMode PowerDownMode
2425
get; set;
2526
}
2627

28+
public bool SendSimultaneously
29+
{
30+
get; set;
31+
}
32+
2733
public List<Lighthouse> Devices
2834
{
2935
get; set;
@@ -40,6 +46,7 @@ public async Task InitializeAsync()
4046
{
4147
PowerManagement = await LoadPowerManagementFromSettingsAsync();
4248
PowerDownMode = await LoadPowerDownModeFromSettingsAsync();
49+
SendSimultaneously = await LoadSendSimultaneouslyAsync();
4350
Devices = await LoadDevicesFromSettingsAsync();
4451
await Task.CompletedTask;
4552
}
@@ -56,6 +63,12 @@ public async Task SetPowerDownModeAsync(PowerDownMode powerDownMode)
5663
await SavePowerDownModeInSettingsAsync(PowerDownMode);
5764
}
5865

66+
public async Task SetSendSimultaneously(bool sendSimultaneously)
67+
{
68+
SendSimultaneously = sendSimultaneously;
69+
await SaveSendSendSimultaneouslyAsync(SendSimultaneously);
70+
}
71+
5972
public async Task SetDevicesAsync(Lighthouse[] devices)
6073
{
6174
Devices = devices.ToList();
@@ -100,6 +113,20 @@ private async Task<PowerDownMode> LoadPowerDownModeFromSettingsAsync()
100113

101114
#endregion
102115

116+
#region SendSimultaneously
117+
118+
private async Task SaveSendSendSimultaneouslyAsync(bool sendSimultaneously)
119+
{
120+
await _localSettingsService.SaveSettingAsync(SettingsKey_SendSimultaneously, sendSimultaneously);
121+
}
122+
123+
private async Task<bool> LoadSendSimultaneouslyAsync()
124+
{
125+
return await _localSettingsService.ReadSettingAsync<bool>(SettingsKey_SendSimultaneously);
126+
}
127+
128+
#endregion
129+
103130
#region Devices
104131

105132
private async Task SaveDevicesInSettingsAsync(List<Lighthouse> devices)

OVRLighthouseManager/Strings/en-us/Resources.resw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,10 @@
258258
<data name="Shell_Main_PowerAll_AllOn.Text" xml:space="preserve">
259259
<value>All On</value>
260260
</data>
261+
<data name="Settings_SendSimultaneously.Text" xml:space="preserve">
262+
<value>Simultaneously send messages to multiple base stations</value>
263+
</data>
264+
<data name="Settings_SendSimultaneouslyWarning.Text" xml:space="preserve">
265+
<value>Possibly may cause unstable communication.</value>
266+
</data>
261267
</root>

OVRLighthouseManager/Strings/ja-jp/Resources.resw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,10 @@
258258
<data name="Shell_Main_PowerAll_AllOn.Text" xml:space="preserve">
259259
<value>全てオン</value>
260260
</data>
261+
<data name="Settings_SendSimultaneously.Text" xml:space="preserve">
262+
<value>複数のベースステーションと同時に通信する</value>
263+
</data>
264+
<data name="Settings_SendSimultaneouslyWarning.Text" xml:space="preserve">
265+
<value>通信が不安定になる可能性があります。</value>
266+
</data>
261267
</root>

OVRLighthouseManager/ViewModels/SettingsViewModel.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public partial class SettingsViewModel : ObservableRecipient, INavigationAware
1818
{
1919
private readonly IThemeSelectorService _themeSelectorService;
2020
private readonly IMiscSettingsService _miscSettingsService;
21+
private readonly ILighthouseSettingsService _lighthouseSettingsService;
2122

2223
[ObservableProperty]
2324
private ElementTheme _elementTheme;
@@ -28,6 +29,9 @@ public partial class SettingsViewModel : ObservableRecipient, INavigationAware
2829
[ObservableProperty]
2930
private bool _minimizeToTray;
3031

32+
[ObservableProperty]
33+
private bool _sendSimultaneously;
34+
3135
[ObservableProperty]
3236
private bool _outputDebug;
3337

@@ -44,12 +48,16 @@ public ICommand SwitchThemeCommand
4448
get;
4549
}
4650

47-
public SettingsViewModel(IThemeSelectorService themeSelectorService, IMiscSettingsService miscSettingsService)
51+
public SettingsViewModel(IThemeSelectorService themeSelectorService, IMiscSettingsService miscSettingsService, ILighthouseSettingsService lighthouseSettingsService)
4852
{
4953
_themeSelectorService = themeSelectorService;
5054
_elementTheme = _themeSelectorService.Theme;
55+
5156
_miscSettingsService = miscSettingsService;
57+
_lighthouseSettingsService = lighthouseSettingsService;
58+
5259
_minimizeOnLaunchedByOpenVR = _miscSettingsService.MinimizeOnLaunchedByOpenVR;
60+
_sendSimultaneously = _lighthouseSettingsService.SendSimultaneously;
5361
_outputDebug = _miscSettingsService.OutputDebug;
5462
_versionDescription = GetVersionDescription();
5563

@@ -103,6 +111,15 @@ public async void OnToggleMinimizeToTray(object sender, RoutedEventArgs e)
103111
}
104112
}
105113

114+
public async void OnToggleSendSimultaneously(object sender, RoutedEventArgs e)
115+
{
116+
if(sender is ToggleSwitch toggleSwitch)
117+
{
118+
SendSimultaneously = toggleSwitch.IsOn;
119+
await _lighthouseSettingsService.SetSendSimultaneously(SendSimultaneously);
120+
}
121+
}
122+
106123
public async void OnToggleOutputDebug(object sender, RoutedEventArgs e)
107124
{
108125
if (sender is ToggleSwitch toggleSwitch)

OVRLighthouseManager/Views/SettingsPage.xaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@
7373
</ToggleSwitch.Header>
7474
</ToggleSwitch>
7575

76+
<ToggleSwitch
77+
Margin="{StaticResource XXSmallTopMargin}"
78+
IsOn="{x:Bind ViewModel.MinimizeToTray, Mode=OneWay}"
79+
Toggled="{x:Bind ViewModel.OnToggleSendSimultaneously}"
80+
>
81+
<ToggleSwitch.Header>
82+
<StackPanel>
83+
<TextBlock x:Uid="Settings_SendSimultaneously" />
84+
<TextBlock
85+
x:Uid="Settings_SendSimultaneouslyWarning"
86+
Foreground="{ThemeResource SystemControlErrorTextForegroundBrush}"
87+
FontSize="12"
88+
/>
89+
</StackPanel>
90+
</ToggleSwitch.Header>
91+
</ToggleSwitch>
92+
7693
<ToggleSwitch
7794
Margin="{StaticResource XXSmallTopMargin}"
7895
IsOn="{x:Bind ViewModel.OutputDebug, Mode=OneWay}"

0 commit comments

Comments
 (0)