diff --git a/MainForm.cs b/MainForm.cs index d28a95b..adfe280 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -15,9 +15,12 @@ namespace robospot_camera_finder public partial class MainForm : Form { public string VERSION = "1.1.0"; + // Version + // Main list containing all cameras private BindingList all_cameras = new(); + // Camera credentials public static string cam_username = "admin"; public static string cam_password = "RoboSpot10"; @@ -28,10 +31,12 @@ namespace robospot_camera_finder private const byte DEF_REQ_SCAN = 1; private const byte RES_REQ_SCAN = 11; + // UDP clients private UdpClient sendClient; private UdpClient receiveClient; private bool isDiscovering = false; + // Camera class public class Camera { public Camera(string name, string ip, string location, string serial) @@ -139,9 +144,10 @@ namespace robospot_camera_finder InitializeUdpClients(); // Discover cameras - discover_cameras(); + DiscoverCameras(); } + // Inits UDP clients private void InitializeUdpClients() { try @@ -157,6 +163,7 @@ namespace robospot_camera_finder } } + // Add cameras to the main list private void add_camera(Camera new_camera) { if (all_cameras.Count == 0) @@ -179,67 +186,6 @@ namespace robospot_camera_finder } } - private void lbMain_MouseDoubleClick(object sender, MouseEventArgs e) - { - if (lbMain.SelectedItem != null) - { - toolStripStatusLabel.Text = "Loading camera..."; - Camera camera = all_cameras.FirstOrDefault(cam => cam.camera_ip.ToString() == lbMain.SelectedValue.ToString()); - Form viewer = new StreamViewer(lbMain.GetItemText(lbMain.SelectedItem), lbMain.SelectedValue.ToString(), camera, this); - viewer.Show(); - toolStripStatusLabel.Text = ""; - } - } - - private void loadToolStripMenuItem_Click(object sender, EventArgs e) - { - string json = ""; - OpenFileDialog openFile = new OpenFileDialog(); - openFile.Filter = "json file (*.json)|*.json"; - openFile.RestoreDirectory = true; - - if (openFile.ShowDialog() == DialogResult.OK) - { - json = File.ReadAllText(openFile.FileName); - - List cameras_in_file = JsonSerializer.Deserialize>(json); - - foreach (Camera new_camera in cameras_in_file) - { - add_camera(new_camera); - } - } - } - - private void saveToolStripMenuItem_Click(object sender, EventArgs e) - { - string json = JsonSerializer.Serialize>(all_cameras, new JsonSerializerOptions - { - WriteIndented = true - }); - SaveFileDialog saveFile = new SaveFileDialog(); - saveFile.Filter = "json file (*.json)|*.json"; - saveFile.RestoreDirectory = true; - - if (saveFile.ShowDialog() == DialogResult.OK) - { - File.WriteAllText(saveFile.FileName, json); - } - } - - private void manualAddToolStripMenuItem_Click(object sender, EventArgs e) - { - Manual_IP_Form manualForm = new Manual_IP_Form(); - - if (manualForm.ShowDialog(this) == DialogResult.OK) - { - string ipAddress = manualForm.ipAddress; - - Camera camera_to_add = new Camera("Camera " + ipAddress, ipAddress, "", "CAM_FINDER_" + DateTime.Now.Ticks.ToString()); - add_camera(camera_to_add); - } - } - // Helper method to convert structure to byte array private byte[] StructureToByteArray(DataPacketIPv4 packet) { @@ -430,7 +376,7 @@ namespace robospot_camera_finder // Add camera to list (must be done on UI thread) this.Invoke(new Action(() => { - add_camera(discoveredCamera); + AddCamera(discoveredCamera); toolStripStatusLabel.Text = $"Found camera: {cameraName} ({cameraIP})"; })); } @@ -446,7 +392,7 @@ namespace robospot_camera_finder } // Main discovery method - private async void discover_cameras() + private async void DiscoverCameras() { if (isDiscovering) { @@ -494,6 +440,67 @@ namespace robospot_camera_finder } } + private void lbMain_MouseDoubleClick(object sender, MouseEventArgs e) + { + if (lbMain.SelectedItem != null) + { + toolStripStatusLabel.Text = "Loading camera..."; + Camera camera = all_cameras.FirstOrDefault(cam => cam.camera_ip.ToString() == lbMain.SelectedValue.ToString()); + Form viewer = new StreamViewer(lbMain.GetItemText(lbMain.SelectedItem), lbMain.SelectedValue.ToString(), camera, this); + viewer.Show(); + toolStripStatusLabel.Text = ""; + } + } + + private void loadToolStripMenuItem_Click(object sender, EventArgs e) + { + string json = ""; + OpenFileDialog openFile = new OpenFileDialog(); + openFile.Filter = "json file (*.json)|*.json"; + openFile.RestoreDirectory = true; + + if (openFile.ShowDialog() == DialogResult.OK) + { + json = File.ReadAllText(openFile.FileName); + + List cameras_in_file = JsonSerializer.Deserialize>(json); + + foreach (Camera new_camera in cameras_in_file) + { + AddCamera(new_camera); + } + } + } + + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { + string json = JsonSerializer.Serialize>(all_cameras, new JsonSerializerOptions + { + WriteIndented = true + }); + SaveFileDialog saveFile = new SaveFileDialog(); + saveFile.Filter = "json file (*.json)|*.json"; + saveFile.RestoreDirectory = true; + + if (saveFile.ShowDialog() == DialogResult.OK) + { + File.WriteAllText(saveFile.FileName, json); + } + } + + private void manualAddToolStripMenuItem_Click(object sender, EventArgs e) + { + Manual_IP_Form manualForm = new Manual_IP_Form(); + + if (manualForm.ShowDialog(this) == DialogResult.OK) + { + string ipAddress = manualForm.ipAddress; + + Camera camera_to_add = new Camera("Camera " + ipAddress, ipAddress, "", "CAM_FINDER_" + DateTime.Now.Ticks.ToString()); + add_camera(camera_to_add); + } + } + // Clean up resources when form is closing protected override void OnFormClosing(FormClosingEventArgs e) {