Skip to content

Commit 2f70c72

Browse files
committed
Several different changes
* Log file; * Optimization on quit; * Little Form changes; * Fix Acces Denied after fast restart; * Readme file changes; * And more code chagnes.
2 parents e90eeb9 + c3b5ce1 commit 2f70c72

5 files changed

Lines changed: 99 additions & 77 deletions

File tree

LighthouseV2PowerControl/Form1.Designer.cs

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LighthouseV2PowerControl/Form1.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ public partial class Form1 : Form
1111
public Form1()
1212
{
1313
InitializeComponent();
14+
ChangeColumnWidth();
15+
lvStatus.SizeChanged += (d, s) => ChangeColumnWidth();
1416
}
1517

1618
public void Log(object msg, LogType type = LogType.log)
1719
{
18-
if (type == LogType.error)
19-
{
20-
lvStatus.Items.Add(msg.ToString());
21-
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.DarkRed;
22-
}
23-
else if(type == LogType.log)
24-
{
25-
lvStatus.Items.Add(msg.ToString());
26-
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.Green;
27-
}
20+
ListViewItem item = new ListViewItem();
21+
item.Text = msg.ToString();
22+
item.ForeColor = (type == LogType.log) ? Color.Green : Color.DarkRed;
23+
lvStatus.Items.Add(item);
2824
}
2925

3026
public void BtnActive(bool active)
@@ -36,5 +32,13 @@ public IEnumerable<Button> GetButtons()
3632
{
3733
return Controls.OfType<Button>();
3834
}
35+
36+
private void ChangeColumnWidth()
37+
{
38+
foreach (ColumnHeader lvStatusColumn in lvStatus.Columns)
39+
{
40+
lvStatusColumn.Width = lvStatus.Width;
41+
}
42+
}
3943
}
4044
}

LighthouseV2PowerControl/LighthouseV2PowerControl.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<UseWindowsForms>true</UseWindowsForms>
77
<ApplicationIcon>newIcon.ico</ApplicationIcon>
8-
<Version>0.0.3</Version>
8+
<Version>0.0.4</Version>
99
<Authors>D0rG</Authors>
1010
<StartupObject>LighthouseV2PowerControl.Program</StartupObject>
11+
<AssemblyVersion>0.0.4.0</AssemblyVersion>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

LighthouseV2PowerControl/Program.cs

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ static class Program
2222
private static readonly byte deactivateByte = 0x00;
2323
private static List<GattCharacteristic> listGattCharacteristics = new List<GattCharacteristic>();
2424

25-
25+
private static readonly string logFileName = Path.Combine(Directory.GetCurrentDirectory(), "Logs");
2626
public delegate void LogHandler(object msg, LogType type);
2727
public static event LogHandler OnLog;
2828
private static Form1 app = null;
2929
private static CVRSystem OVRSystem;
30-
private static bool isEnabledVR = false;
3130
private static Thread onQuitThread;
3231
private static CancellationTokenSource cancellationToken = new CancellationTokenSource();
3332

@@ -38,58 +37,64 @@ static void Main(string[] args)
3837
Application.SetCompatibleTextRenderingDefault(false);
3938
Application.ApplicationExit += (obj, e) => cancellationToken.Cancel();
4039
app = new Form1();
41-
OnLog += (msg, type) => app.Log(msg, type);
40+
OnLog += (msg, type) =>
41+
{
42+
WriteToLogFileAsync(msg, LogType.error);
43+
if (app != null)
44+
{
45+
app.Log(msg, type);
46+
}
47+
};
48+
LogFileStart();
4249
if (args.Length > 0)
4350
{
4451
app.ShowInTaskbar = false;
4552
app.WindowState = FormWindowState.Minimized;
4653
UseArgumentsAsync(args);
4754
}
55+
Startup();
56+
57+
Stack<EventHandler> eventHandlers = new Stack<EventHandler>(); //Buttons from the form.
58+
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(true)));
59+
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(false)));
60+
eventHandlers.Push(new EventHandler((obj, args) => AppManifest(WithManifestTask.add)));
61+
eventHandlers.Push(new EventHandler((obj, args) => AppManifest(WithManifestTask.rm)));
62+
foreach (Button button in app.GetButtons())
63+
{
64+
button.Click += eventHandlers.Pop();
65+
}
66+
Application.Run(app);
67+
}
68+
69+
private static async Task Startup()
70+
{
71+
await GetGattCharacteristicsAsync();
72+
Log($"lighthouses found: {listGattCharacteristics.Count};");
73+
if (listGattCharacteristics.Count > 0 && app != null)
74+
{
75+
app.BtnActive(true);
76+
}
4877

4978
try
5079
{
5180
EVRInitError error = EVRInitError.None;
52-
OVRSystem = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Background); //çàïóñêàåòñÿ è âûðóáàåòñÿ ñ SteamVR
53-
if (error == EVRInitError.Init_NoServerForBackgroundApp)
81+
OVRSystem = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Background); //starts and shuts down with SteamVR.
82+
if (error == EVRInitError.Init_NoServerForBackgroundApp || error != EVRInitError.None)
5483
{
5584
Log("Init without SteamVR;");
5685
}
57-
else if(error != EVRInitError.None)
58-
{
59-
LogError(error);
60-
}
6186
else
6287
{
63-
isEnabledVR = true;
64-
OVRSystem.AcknowledgeQuit_Exiting();
88+
Log("Init with SteamVR;");
6589
onQuitThread = new Thread(new ThreadStart(QuitThreadChecker));
6690
onQuitThread.Start();
91+
await SendOnLighthouseAsync(activateByte);
6792
}
6893
}
6994
catch (Exception e)
7095
{
7196
LogError(e.Message);
7297
}
73-
74-
if (isEnabledVR)
75-
{
76-
SendOnStart();
77-
}
78-
else
79-
{
80-
GetGattCharacteristicsAsync();
81-
}
82-
83-
Stack<EventHandler> eventHandlers = new Stack<EventHandler>(); //Ïðñîòî äëÿ óäîáíîãî íàçíà÷åíèÿ êíîïîê.
84-
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(true)));
85-
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(false)));
86-
eventHandlers.Push(new EventHandler((obj, args) => AppManifest(WithManifestTask.add)));
87-
eventHandlers.Push(new EventHandler((obj, args) => AppManifest(WithManifestTask.rm)));
88-
foreach (Button button in app.GetButtons())
89-
{
90-
button.Click += eventHandlers.Pop();
91-
}
92-
Application.Run(app);
9398
}
9499

95100

@@ -115,6 +120,7 @@ private static void QuitThreadChecker()
115120
while (!cancellationToken.IsCancellationRequested)
116121
{
117122
OVRSystem.AcknowledgeQuit_Exiting();
123+
Thread.Sleep(100);
118124
}
119125
Exit();
120126
}
@@ -169,12 +175,6 @@ private static async Task GetGattCharacteristicsAsync()
169175
LogError($"Sevices {result.Status};");
170176
}
171177
}
172-
173-
Log($"lighthouses found: {listGattCharacteristics.Count};");
174-
if (listGattCharacteristics.Count > 0 && app != null)
175-
{
176-
app.BtnActive(true);
177-
}
178178
}
179179

180180
/// <summary>
@@ -208,28 +208,53 @@ public static void SendActiveStatus(bool status)
208208
{
209209
SendOnLighthouseAsync((status) ? activateByte : deactivateByte);
210210
}
211+
#endregion
212+
213+
#region Logs
211214

212-
public static async Task SendOnStart()
215+
private static void Log(object msg)
213216
{
214-
await GetGattCharacteristicsAsync();
215-
await SendOnLighthouseAsync(activateByte);
217+
OnLog.Invoke(msg, LogType.log);
218+
}
219+
220+
private static void LogError(object msg)
221+
{
222+
OnLog.Invoke(msg, LogType.error);
223+
}
224+
225+
private static void LogFileStart()
226+
{
227+
File.WriteAllText(logFileName, null);
228+
}
229+
230+
private async static Task WriteToLogFileAsync(object msg, LogType type = LogType.log)
231+
{
232+
string res = $"[{DateTime.Now}]";
233+
res += "[" + ((type == LogType.log)? "Log" : "ERROR") + "] ";
234+
res += msg.ToString();
235+
await File.AppendAllTextAsync(logFileName, res + "\n");
216236
}
237+
217238
#endregion
218239

240+
#region StartupArgs
219241
/// <summary>
220242
/// Processing arguments at the start of the application.
221243
/// </summary>
222244
/// <param name="args"></param>
223-
private static async void UseArgumentsAsync(string[] args)
245+
private static async void UseArgumentsAsync(string[] args)
224246
{
225-
await GetGattCharacteristicsAsync();
226-
if (args[0] == "--powerOn")
227-
{
228-
await SendOnLighthouseAsync(activateByte);
229-
}
230-
else if (args[0] == "--powerOff")
247+
if (args[0] == "--powerOn" || args[0] == "--powerOff")
231248
{
232-
await SendOnLighthouseAsync(deactivateByte);
249+
await GetGattCharacteristicsAsync();
250+
if (args[0] == "--powerOn")
251+
{
252+
await SendOnLighthouseAsync(activateByte);
253+
}
254+
else
255+
{
256+
await SendOnLighthouseAsync(deactivateByte);
257+
}
233258
}
234259
else if (args[0] == "--reg")
235260
{
@@ -242,18 +267,6 @@ private static async void UseArgumentsAsync(string[] args)
242267
Exit();
243268
}
244269

245-
private static void Log(object msg)
246-
{
247-
if (app == null) return;
248-
OnLog.Invoke(msg, LogType.log);
249-
}
250-
251-
private static void LogError(object msg)
252-
{
253-
if (app == null) return;
254-
OnLog.Invoke(msg, LogType.error);
255-
}
256-
257270
private static void AppManifest(WithManifestTask task)
258271
{
259272
EVRInitError evrInitError = EVRInitError.None;
@@ -265,23 +278,26 @@ private static void AppManifest(WithManifestTask task)
265278
}
266279

267280
EVRApplicationError applicationError;
281+
string manifestPath = Path.Combine(Directory.GetCurrentDirectory(), "manifest.vrmanifest");
268282
if (task == WithManifestTask.add)
269283
{
270-
applicationError = OpenVR.Applications.AddApplicationManifest(Directory.GetCurrentDirectory() + @"\manifest.vrmanifest", false);
284+
applicationError = OpenVR.Applications.AddApplicationManifest(manifestPath, false);
271285
}
272286
else
273287
{
274-
applicationError = OpenVR.Applications.RemoveApplicationManifest(Directory.GetCurrentDirectory() + @"\manifest.vrmanifest");
288+
applicationError = OpenVR.Applications.RemoveApplicationManifest(manifestPath);
275289
}
276290

277291
if (applicationError != EVRApplicationError.None)
278292
{
279293
LogError(applicationError);
280294
return;
281295
}
282-
Log($"Application manifest {((task == WithManifestTask.add)? "registered" : "removed")}");
296+
Log($"Application manifest {((task == WithManifestTask.add)? "registered;" : "removed;")}");
283297
}
284298

299+
#endregion
300+
285301
private static void Exit()
286302
{
287303
OpenVR.Shutdown();

README.MD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
* Pair the lighthouses to your pc via Bluetooth;
77
* Press the `start` or `stop` buttons to change the power of the Lighthouses;
8-
* If you want the Lighthouses to turn on and off with SteamVR click on the `SteamVR add` button;
9-
* If you no longer need to enable Lighthouses with SteamVR then delete the app manifest from SteamVR by clicking the `SteamVR del` button.
8+
* If you want the Lighthouses to turn on and off with SteamVR click on the `SteamVR add` button and change the autorun setting in SteamVR;
9+
* If you no longer need to enable Lighthouses with SteamVR then delete the app manifest from SteamVR by clicking the `SteamVR del` button or turn off the autorun in SteamVR setting.
1010
> Open the program, it will find all the paired base stations, then you can use the buttons to turn on and off the base stations. If you added the program manifest to SteamVR, the program will be turned on together with SteamVR, and then send a command to the Lighthouses to turn on, as well as a command to turn off when SteamVR is turned off.
1111
1212
Launch arguments

0 commit comments

Comments
 (0)