using System.ComponentModel; using System.Diagnostics; using System.Net; using System.Net.NetworkInformation; using Tmds.MDns; namespace robospot_camera_finder { public partial class MainForm : Form { private readonly ServiceBrowser browser = new(); private BindingList all_cameras = new(); public static string cam_username = "admin"; public static string cam_password = "RoboSpot10"; public class Camera { private IPAddress ip; private string name; private string location; private string serial; public Camera(string name, IPAddress ip, string location, string serial) { this.name = name; this.ip = ip; this.location = location; this.serial = serial; } public string camera_name { get { return name; } } public IPAddress camera_ip { get { return ip; } } public string camera_location { get { return location; } } public string camera_serial { get { return serial; } } } public MainForm() { InitializeComponent(); bool got_proper_ip = false; foreach (var net_interface in NetworkInterface.GetAllNetworkInterfaces()) { IPInterfaceProperties prop = net_interface.GetIPProperties(); foreach (var ip in prop.UnicastAddresses) { if (ip.Address.ToString().StartsWith("10.")) { if (ip.IPv4Mask.ToString() == "255.0.0.0") { got_proper_ip = true; } } } } if (!got_proper_ip) { DialogResult open_netsettings = MessageBox.Show("No Ethernet interface on the 10.0.0.0/8 subnet found. Cameras might not be detected. Do you want to open network settings ?", "Wrong IP configuration", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (open_netsettings == DialogResult.Yes) { ProcessStartInfo startInfo = new ProcessStartInfo("NCPA.cpl"); startInfo.UseShellExecute = true; Process.Start(startInfo); } } browser.StartBrowse("_rtsp._tcp"); lbMain.DataSource = all_cameras; lbMain.DisplayMember = "camera_name"; lbMain.ValueMember = "camera_ip"; browser.ServiceAdded += (sender, args) => { ServiceAdded(sender, args); }; browser.ServiceChanged += (sender, args) => { ServiceAdded(sender, args); }; browser.ServiceRemoved += (sender, args) => { ServiceRemoved(sender, args); }; } private void ServiceAdded(object sender, ServiceAnnouncementEventArgs args) { foreach (var cam_addr in args.Announcement.Addresses) { if (cam_addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + cam_addr + "/stw-cgi/system.cgi?msubmenu=deviceinfo&action=view"); request.Credentials = new NetworkCredential(MainForm.cam_username, MainForm.cam_password); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string text; using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } string[] resp_lines = text.Split('\n'); string location_line = Array.Find(resp_lines, line => line.Trim().StartsWith("DeviceLocation=")); string serial_line = Array.Find(resp_lines, line => line.Trim().StartsWith("SerialNumber=")); string location = ""; string serial = ""; if (location_line != null) { location = location_line.Substring("DeviceLocation=".Length).Trim(); } if (serial_line != null) { serial = serial_line.Substring("SerialNumber=".Length).Trim(); } Camera camera = new Camera("Camera " + cam_addr.ToString() + " - " + location + " - " + serial, cam_addr, location, serial); all_cameras.Add(camera); } } } private void ServiceRemoved(object sender, ServiceAnnouncementEventArgs args) { foreach (var cam_ip in args.Announcement.Addresses) { if (cam_ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { foreach (var camera in all_cameras) { if (cam_ip == camera.camera_ip) { all_cameras.Remove(camera); return; } } } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { browser.StopBrowse(); } 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 = ""; } } } }