|
| 1 | +using System.Net.NetworkInformation; |
| 2 | +using System.Net; |
| 3 | +using Makaretu.Dns; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | + |
| 7 | + |
| 8 | +namespace EventPi.Advertiser |
| 9 | +{ |
| 10 | + public class RpiAdvertiseService : IHostedService |
| 11 | + { |
| 12 | + private CancellationTokenSource _cancellationTokenSource; |
| 13 | + private readonly ILogger<RpiAdvertiseService> _logger; |
| 14 | + private readonly ServiceProfile _serviceProfile; |
| 15 | + private readonly ServiceDiscovery _serviceDiscovery; |
| 16 | + private string _interfaceWifi; |
| 17 | + private readonly string _interfaceEthernet; |
| 18 | + |
| 19 | + public RpiAdvertiseService(ILogger<RpiAdvertiseService> logger, string serviceName, int port) |
| 20 | + { |
| 21 | + _logger = logger; |
| 22 | + |
| 23 | + _serviceProfile = new ServiceProfile(Dns.GetHostName(), "video.tcp", 9001); |
| 24 | + _serviceDiscovery = new ServiceDiscovery(); |
| 25 | + _interfaceWifi = string.Empty; |
| 26 | + _interfaceEthernet = string.Empty; |
| 27 | + foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) |
| 28 | + { |
| 29 | + switch (ni.Description) |
| 30 | + { |
| 31 | + case "wlan0": |
| 32 | + _interfaceWifi = GetInterfaceAddress(ni); |
| 33 | + break; |
| 34 | + case "eth0": |
| 35 | + _interfaceEthernet = GetInterfaceAddress(ni); |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + _logger.LogInformation($"Interfaces: Wifi:{_interfaceWifi};Ethernet:{_interfaceEthernet}"); |
| 41 | + _serviceProfile.AddProperty("Wifi", $"{_interfaceWifi}"); |
| 42 | + _serviceProfile.AddProperty("Ethernet", $"{_interfaceEthernet}"); |
| 43 | + } |
| 44 | + |
| 45 | + private static string GetInterfaceAddress(NetworkInterface ni) |
| 46 | + { |
| 47 | + Console.WriteLine(ni.GetPhysicalAddress()); |
| 48 | + foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) |
| 49 | + { |
| 50 | + if (ip.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) continue; |
| 51 | + if (ip.Address.ToString() != string.Empty) return ip.Address.ToString(); |
| 52 | + } |
| 53 | + |
| 54 | + return string.Empty; |
| 55 | + } |
| 56 | + |
| 57 | + private async Task AdvertiseThisRpi(CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + _logger.LogInformation($"Starting advertising {Dns.GetHostName()} in local network!"); |
| 60 | + |
| 61 | + |
| 62 | + while (!cancellationToken.IsCancellationRequested) |
| 63 | + { |
| 64 | + _serviceDiscovery.Advertise(_serviceProfile); |
| 65 | + _logger.LogInformation($"Advertised! Wifi:{_interfaceWifi} Ethernet:{_interfaceEthernet}"); |
| 66 | + await Task.Delay(20000); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + } |
| 71 | + public Task StartAsync(CancellationToken cancellationToken) |
| 72 | + { |
| 73 | + if (_cancellationTokenSource != null) |
| 74 | + throw new InvalidOperationException("You cannot invoke start when already running (RpiAdvertiseService)."); |
| 75 | + _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| 76 | + Task.Factory.StartNew(async () => await AdvertiseThisRpi(_cancellationTokenSource.Token), TaskCreationOptions.LongRunning); |
| 77 | + return Task.CompletedTask; |
| 78 | + } |
| 79 | + |
| 80 | + public async Task StopAsync(CancellationToken cancellationToken) |
| 81 | + { |
| 82 | + await _cancellationTokenSource.CancelAsync(); |
| 83 | + _cancellationTokenSource = null; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments