SharedConfig.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using ConfigOperator;
  2. using RealtimeData;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. namespace AlarmInfoServerSim.Services;
  7. public class SharedConfig : ISharedConfig
  8. {
  9. private Hardwares _hardwares;
  10. private List<IPAddress> _iPAddress;
  11. private IPAddress? _selectedIPAddress;
  12. private int? _selectedPort;
  13. public SharedConfig()
  14. {
  15. string hardwaresPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings", "Hardwares");
  16. _hardwares = new();
  17. HardwareFileLoader hardwareFileLoader = new(_hardwares);
  18. if (!hardwareFileLoader.Load(hardwaresPath))
  19. {
  20. throw new InvalidOperationException("Failed to Load correct hardware configs!");
  21. }
  22. _iPAddress = new();
  23. foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
  24. {
  25. if (netInterface.OperationalStatus == OperationalStatus.Up)
  26. {
  27. foreach (var ipInfo in netInterface.GetIPProperties().UnicastAddresses)
  28. {
  29. if (ipInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  30. {
  31. _iPAddress.Add(ipInfo.Address);
  32. }
  33. }
  34. }
  35. }
  36. }
  37. public Hardwares Hardwares => _hardwares;
  38. public List<IPAddress> IPAddresses => _iPAddress;
  39. public IPAddress? SelectedIPAddress { get => _selectedIPAddress; set => _selectedIPAddress = value; }
  40. public int? SelectedPort { get => _selectedPort; set => _selectedPort = value; }
  41. }