Skip to content

Commit

Permalink
Fix trayicon behavior then clicking with right mouse button, focuses …
Browse files Browse the repository at this point in the history
…all cameras, it should focus only when click with right button

Reimplemented LogWindow and MQTT Service threads so it can cleanly shutdown, no crash dumps anymore, no long waiting for exit, just clean shutdown
Some code formatting and cleanup
Bump to version 0.3 in version tag
  • Loading branch information
e1z0 committed Mar 12, 2024
1 parent 1e89b28 commit 011d95f
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 77 deletions.
12 changes: 8 additions & 4 deletions Src/Client/Classes/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,16 @@ public void SpawnCameraWindow()

cameraForm.Click += (sender, e) =>
{
if (Settings.Advanced.FocusAllWindowsOnClick && cameraForm.WindowState != FormWindowState.Maximized)
MouseEventArgs mouseEvent = e as MouseEventArgs;
if (mouseEvent != null && mouseEvent.Button == MouseButtons.Left)
{
// Get all open forms and bring each one to the front
foreach (Form form in Application.OpenForms)
if (Settings.Advanced.FocusAllWindowsOnClick && cameraForm.WindowState != FormWindowState.Maximized)
{
form.Focus();
// Get all open forms and bring each one to the front
foreach (Form form in Application.OpenForms)
{
form.Focus();
}
}
}
};
Expand Down
9 changes: 5 additions & 4 deletions Src/Client/CustomUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ private void InitializeTrayIcon()
{
var mouseArgs = e as MouseEventArgs;
// left mouse button click activates all windows
if (mouseArgs.Button == MouseButtons.Left)
if (mouseArgs != null && mouseArgs.Button == MouseButtons.Left)
{
}
if (Settings.Advanced.FocusAllWindowsOnClick)
{
// Get all open forms and bring each one to the front
Expand All @@ -241,7 +241,7 @@ private void InitializeTrayIcon()
Win32Func.SetForegroundWindow(form.Handle);
}
}
}
};
// Make the tray icon visible
trayIcon.Visible = true;
Expand Down Expand Up @@ -290,11 +290,12 @@ public void Init()
MainContextMenu.Items.Insert(0,menuItem);
trayIcon.ContextMenuStrip = MainContextMenu;

/*
if (Settings.Logging > 0 && Settings.LogWindow > 0)
{
Settings.ShowOrActivateForm<LogForm>();
}

*/


if (Settings.MqttEnabled > 0 && Settings.LedsCount > 0)
Expand Down
51 changes: 37 additions & 14 deletions Src/Client/LogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace AnotherRTSP
public partial class LogForm : Form
{
private string filePath;
private Thread logThread;

public LogForm()
{
Expand All @@ -23,14 +24,43 @@ public LogForm()

}

public void StartService()
{
if (Settings.Logging > 0 && File.Exists(Settings.LogPath))
{
logThread = new Thread(ReadLogFile);
logThread.Start();
Logger.WriteLog("Log Window Thread is running...");
if (logThread.IsAlive)
{
Settings.LogWindowRunning = true;
Settings.LogWindow = 1;
CustomUI.logmenuItem.Checked = true;
}
}
}

public void StopService()
{
logThread.Abort();
Settings.LogWindowRunning = false;
CustomUI.logmenuItem.Checked = false;
Logger.WriteLog("Log Window Thread is done.");
}

public void WaitForCompletion()
{
logThread.Join();
}



private void ReadLogFile()
{
var initialFileSize = new FileInfo(filePath).Length;
var lastReadLength = initialFileSize - 1024;
if (lastReadLength < 0) lastReadLength = 0;
while (Settings.LogWindowRunning)
while (logThread.ThreadState == ThreadState.Running)
{
try
{
Expand Down Expand Up @@ -60,7 +90,7 @@ private void ReadLogFile()
catch { }
Thread.Sleep(1000);
}
Logger.WriteLog("Log Window Thread is done.");


}

Expand Down Expand Up @@ -96,8 +126,9 @@ private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.LogWindow = 0;
}
Settings.LogWindowRunning = false;
CustomUI.logmenuItem.Checked = false;
StopService();
//Settings.LogWindowRunning = false;
//CustomUI.logmenuItem.Checked = false;

}
}
Expand All @@ -108,16 +139,8 @@ private void LogForm_Load(object sender, EventArgs e)
if (Settings.LogWindowHeight > 0 && Settings.LogWindowWidth > 0)
this.Size = new Size(Settings.LogWindowWidth, Settings.LogWindowHeight);
// if (Settings.LogWindowX > 0 && Settings.LogWindowY > 0)
this.Location = new Point(Settings.LogWindowX, Settings.LogWindowY);
if (Settings.Logging > 0 && File.Exists(Settings.LogPath))
{
Thread logThread = new Thread(ReadLogFile);
logThread.IsBackground = true;
Settings.LogWindowRunning = true;
logThread.Start();
Settings.LogWindow = 1;
CustomUI.logmenuItem.Checked = true;
}
this.Location = new Point(Settings.LogWindowX, Settings.LogWindowY);
StartService();
}

private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
Expand Down
101 changes: 57 additions & 44 deletions Src/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,84 @@ namespace AnotherRTSP
{
static class Program
{
private static MqttService mqttService;
private static LogForm logService;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// read the embedded version information
// Read the embedded version information
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("AnotherRTSP.version.info");
byte[] data = new byte[stream.Length];
stream.Position = 0; // Set the stream position to the beginning
stream.Read(data, 0, data.Length); // Read the entire stream
Settings.VERSION = System.Text.Encoding.UTF8.GetString(data);
// continue loading
using (Stream stream = assembly.GetManifestResourceStream("AnotherRTSP.version.info"))
using (StreamReader reader = new StreamReader(stream))
{
Settings.VERSION = reader.ReadToEnd();
}
// Enable visual styles and set text rendering default
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
//AppDomain.CurrentDomain.ProcessExit += new EventHandler(Application_ApplicationExit);
Application.ApplicationExit += Application_ApplicationExit;
// Load settings
Settings.Load();
if (!Settings.FirstRun)

// Check for first run
if (Settings.FirstRun)
{
if (Settings.CustomLayout > 0)
Application.Run(new FirstRun());
return;
}


// Initialize MQTT service and UI
if (Settings.CustomLayout > 0)
{
CustomUI ui = new CustomUI();
ui.Init();

// Start Log service if enabled
if (Settings.Logging > 0 && Settings.LogWindow > 0)
{
MqttService mqttsvc = new MqttService();
Thread thread = new Thread(mqttsvc.ServiceStart);
thread.IsBackground = true;
CustomUI ui = new CustomUI();
ui.Init();

// start mqtt service if enabled
if (Settings.MqttEnabled > 0)
{
thread.Start();
}
Application.Run();
thread.Join();
logService = new LogForm();
logService.Show();
}
else

// Start MQTT service if enabled
if (Settings.MqttEnabled > 0)
{
Application.Run(new PlayerForm());
mqttService = new MqttService();
mqttService.StartService();
}
Application.Run();
}
else
{
Application.Run(new FirstRun());
Application.Run(new PlayerForm());
}

}


// application exit event, we should stop all threads on this event
static void Application_ApplicationExit(object sender, EventArgs e)
{
Settings.Save();
if (mqttService != null)
{
mqttService.StopService(); // Stop MQTT service
mqttService.WaitForCompletion(); // Wait for MQTT service to stop
}
if (logService != null)
{
logService.StopService();
logService.WaitForCompletion();
}
// clean exit
Logger.WriteLog("Program gracefully closed!");
Application.Exit();
}

// for some reason i left it, maybe it be useful someday
static void TerminateAllThreads()
{
// Get the current process
Expand All @@ -85,22 +114,6 @@ static void TerminateAllThreads()
}
}
}
// bind application exit event and stop all threads on event
static void Application_ApplicationExit(object sender, EventArgs e)
{
Settings.MqttServiceRunning = false;
Settings.LogWindowRunning = false;
Settings.Save();
// wait for all threads to terminate
Thread.Sleep(500);
Logger.WriteLog("Program gracefully closed!");
// forcefully terminate threads if remaining
TerminateAllThreads();
// clean exit
Application.Exit();
// if application is still active, then terminate it
Environment.Exit(0);

}
}
}
52 changes: 43 additions & 9 deletions Src/Client/Services/MqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ namespace AnotherRTSP.Services
{
class MqttService
{
public void ServiceStart()
private MqttClient client;
private Thread mqttThread;

private void ServiceStart()
{
Logger.WriteLog("Mqtt service Thread is running...");

var client = new MqttClient(Settings.MqttSettings.Server);
client = new MqttClient(Settings.MqttSettings.Server);

// register handler to a received message
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
Expand All @@ -38,13 +41,44 @@ public void ServiceStart()
}
Settings.MqttServiceRunning = true;

while (Settings.MqttServiceRunning)
while (mqttThread.ThreadState == ThreadState.Running)
{
Thread.Sleep(500);
}
}


public void StartService()
{
mqttThread = new Thread(ServiceStart);
mqttThread.Start();
if (mqttThread.IsAlive)
Settings.MqttServiceRunning = true;
}

public void StopService()
{
mqttThread.Abort();
Settings.MqttServiceRunning = false;
if (client != null)
{
try
{
client.Disconnect();
}
catch (Exception ex)
{
Logger.WriteLog("Unable to disconnect from mqtt server on mqtt thread service end: {0}", ex.StackTrace.ToString());
}
}
Logger.WriteLog("Mqtt service Thread is done.");
}

public void WaitForCompletion()
{
mqttThread.Join();
}



static bool ContainsPropertyWithValue(Dictionary<string, string> dictionary, string propertyName, string propertyValue)
Expand Down Expand Up @@ -79,7 +113,7 @@ static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs
// raw message
if (rule.Type == 0)
{
Logger.WriteLog("MQTT alert type: {0} topic: {1} msg: {2}", rule.Type, e.Topic,mqttMessage);
Logger.WriteLog("MQTT alert type: {0} topic: {1} msg: {2}", rule.Type, e.Topic, mqttMessage);
// log message
if (rule.Action == 0)
{
Expand Down Expand Up @@ -107,7 +141,7 @@ static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs
LedStateManager.UpdateLedState(rule.Name, 0);
}
}

}
// json message
else if (rule.Type == 1)
Expand All @@ -134,7 +168,7 @@ static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs
// all true
if (containsAll)
{
LedStateManager.UpdateLedState(rule.Name,1);
LedStateManager.UpdateLedState(rule.Name, 1);
//Logger.WriteLog("LED [{0}] = {1}", rule.Name, 1);
if (Settings.Advanced.LedsSoundAlert)
SystemSounds.Exclamation.Play();
Expand All @@ -153,11 +187,11 @@ static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs
}
else
{
Logger.WriteLog("MQTT topic invalid: "+e.Topic);
Logger.WriteLog("MQTT topic invalid: " + e.Topic);
}


}

}

}
}
Loading

0 comments on commit 011d95f

Please sign in to comment.