MinerProfile.partials.PoolProfileSet.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using NTMiner.Profile;
  2. using NTMiner.Repositories;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. namespace NTMiner.Core.Profiles {
  7. internal partial class MinerProfile {
  8. private class PoolProfileSet {
  9. private readonly Dictionary<Guid, PoolProfile> _dicById = new Dictionary<Guid, PoolProfile>();
  10. private readonly INTMinerRoot _root;
  11. private readonly object _locker = new object();
  12. public PoolProfileSet(INTMinerRoot root) {
  13. _root = root;
  14. }
  15. public void Refresh() {
  16. _dicById.Clear();
  17. }
  18. public IPoolProfile GetPoolProfile(Guid poolId) {
  19. if (_dicById.ContainsKey(poolId)) {
  20. return _dicById[poolId];
  21. }
  22. lock (_locker) {
  23. if (_dicById.ContainsKey(poolId)) {
  24. return _dicById[poolId];
  25. }
  26. PoolProfile coinProfile = PoolProfile.Create(_root, poolId);
  27. _dicById.Add(poolId, coinProfile);
  28. return coinProfile;
  29. }
  30. }
  31. public IEnumerable<IPoolProfile> GetPoolProfiles() {
  32. return _dicById.Values;
  33. }
  34. public void SetPoolProfileProperty(Guid poolId, string propertyName, object value) {
  35. PoolProfile coinProfile = (PoolProfile)GetPoolProfile(poolId);
  36. coinProfile.SetValue(propertyName, value);
  37. }
  38. public class PoolProfile : IPoolProfile {
  39. private static readonly PoolProfile Empty = new PoolProfile(new PoolProfileData());
  40. public static PoolProfile Create(INTMinerRoot root, Guid poolIdId) {
  41. if (root.ServerContext.PoolSet.TryGetPool(poolIdId, out IPool pool)) {
  42. var data = GetPoolProfileData(root, pool.GetId());
  43. if (data == null) {
  44. data = PoolProfileData.CreateDefaultData(pool);
  45. }
  46. PoolProfile coinProfile = new PoolProfile(data);
  47. return coinProfile;
  48. }
  49. else {
  50. return Empty;
  51. }
  52. }
  53. private readonly PoolProfileData _data;
  54. private static PoolProfileData GetPoolProfileData(INTMinerRoot root, Guid poolId) {
  55. var repository = NTMinerRoot.CreateLocalRepository<PoolProfileData>();
  56. var result = repository.GetByKey(poolId);
  57. if (result == null) {
  58. if (root.ServerContext.PoolSet.TryGetPool(poolId, out IPool pool)) {
  59. // 如果本地未设置用户名密码则使用默认的测试用户名密码
  60. result = PoolProfileData.CreateDefaultData(pool);
  61. }
  62. }
  63. return result;
  64. }
  65. private PoolProfile(PoolProfileData data) {
  66. _data = data ?? throw new ArgumentNullException(nameof(data));
  67. }
  68. [IgnoreReflectionSet]
  69. public Guid PoolId {
  70. get => _data.PoolId;
  71. private set {
  72. _data.PoolId = value;
  73. }
  74. }
  75. public string UserName {
  76. get => _data.UserName;
  77. private set {
  78. _data.UserName = value;
  79. }
  80. }
  81. public string Password {
  82. get => _data.Password;
  83. private set {
  84. _data.Password = value;
  85. }
  86. }
  87. private static Dictionary<string, PropertyInfo> _sProperties;
  88. [IgnoreReflectionSet]
  89. private static Dictionary<string, PropertyInfo> Properties {
  90. get {
  91. if (_sProperties == null) {
  92. _sProperties = GetPropertiesCanSet<PoolProfile>();
  93. }
  94. return _sProperties;
  95. }
  96. }
  97. public void SetValue(string propertyName, object value) {
  98. if (Properties.TryGetValue(propertyName, out PropertyInfo propertyInfo)) {
  99. if (propertyInfo.CanWrite) {
  100. if (propertyInfo.PropertyType == typeof(Guid)) {
  101. value = VirtualRoot.ConvertToGuid(value);
  102. }
  103. var oldValue = propertyInfo.GetValue(this, null);
  104. if (oldValue != value) {
  105. propertyInfo.SetValue(this, value, null);
  106. var repository = NTMinerRoot.CreateLocalRepository<PoolProfileData>();
  107. repository.Update(_data);
  108. VirtualRoot.RaiseEvent(new PoolProfilePropertyChangedEvent(this.PoolId, propertyName));
  109. }
  110. }
  111. }
  112. }
  113. public override string ToString() {
  114. if (_data == null) {
  115. return string.Empty;
  116. }
  117. return _data.ToString();
  118. }
  119. }
  120. }
  121. }
  122. }