128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Web;
|
|
using Tmds.MDns;
|
|
using Xamarin.Forms;
|
|
|
|
namespace robospot_camera_finder
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
private readonly ServiceBrowser browser = new();
|
|
|
|
private BindingList<Camera> all_cameras = new();
|
|
|
|
class Camera
|
|
{
|
|
private IPAddress ip;
|
|
private string name;
|
|
|
|
public Camera(string name, IPAddress ip)
|
|
{
|
|
this.name = name;
|
|
this.ip = ip;
|
|
}
|
|
|
|
public string camera_name
|
|
{
|
|
get
|
|
{
|
|
return name;
|
|
}
|
|
}
|
|
|
|
public IPAddress camera_ip
|
|
{
|
|
get
|
|
{
|
|
return ip;
|
|
}
|
|
}
|
|
}
|
|
|
|
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) =>
|
|
{
|
|
foreach (var cam_addr in args.Announcement.Addresses)
|
|
{
|
|
if (cam_addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
|
{
|
|
Camera camera = new Camera("Camera " + cam_addr.ToString(), cam_addr);
|
|
all_cameras.Add(camera);
|
|
}
|
|
}
|
|
};
|
|
|
|
browser.ServiceRemoved += (sender, 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)
|
|
{
|
|
Form viewer = new StreamViewer(lbMain.GetItemText(lbMain.SelectedItem), lbMain.SelectedValue.ToString());
|
|
viewer.Show();
|
|
}
|
|
}
|
|
}
|
|
} |