ServerCredentials.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using MediaBrowser.Model.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MediaBrowser.Model.ApiClient
  6. {
  7. public class ServerCredentials
  8. {
  9. public List<ServerInfo> Servers { get; set; }
  10. public string ConnectUserId { get; set; }
  11. public string ConnectAccessToken { get; set; }
  12. public ServerCredentials()
  13. {
  14. Servers = new List<ServerInfo>();
  15. }
  16. public void AddOrUpdateServer(ServerInfo server)
  17. {
  18. if (server == null)
  19. {
  20. throw new ArgumentNullException("server");
  21. }
  22. var list = Servers.ToList();
  23. var index = FindIndex(list, server.Id);
  24. if (index != -1)
  25. {
  26. var existing = list[index];
  27. // Merge the data
  28. existing.DateLastAccessed = new[] { existing.DateLastAccessed, server.DateLastAccessed }.Max();
  29. if (!string.IsNullOrEmpty(server.AccessToken))
  30. {
  31. existing.AccessToken = server.AccessToken;
  32. existing.UserId = server.UserId;
  33. }
  34. if (!string.IsNullOrEmpty(server.ExchangeToken))
  35. {
  36. existing.ExchangeToken = server.ExchangeToken;
  37. }
  38. if (!string.IsNullOrEmpty(server.RemoteAddress))
  39. {
  40. existing.RemoteAddress = server.RemoteAddress;
  41. }
  42. if (!string.IsNullOrEmpty(server.LocalAddress))
  43. {
  44. existing.LocalAddress = server.LocalAddress;
  45. }
  46. if (!string.IsNullOrEmpty(server.Name))
  47. {
  48. existing.Name = server.Name;
  49. }
  50. if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
  51. {
  52. existing.WakeOnLanInfos = server.WakeOnLanInfos.ToList();
  53. }
  54. }
  55. else
  56. {
  57. list.Add(server);
  58. }
  59. Servers = list;
  60. }
  61. private int FindIndex(List<ServerInfo> servers, string id)
  62. {
  63. var index = 0;
  64. foreach (var server in servers)
  65. {
  66. if (StringHelper.Equals(id, server.Id))
  67. {
  68. return index;
  69. }
  70. index++;
  71. }
  72. return -1;
  73. }
  74. }
  75. }