MineWorkViewModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using NTMiner.Core;
  2. using NTMiner.JsonDb;
  3. using NTMiner.MinerServer;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Input;
  8. namespace NTMiner.Vms {
  9. public class MineWorkViewModel : ViewModelBase, IMineWork, IEditableViewModel {
  10. public static readonly MineWorkViewModel PleaseSelect = new MineWorkViewModel(Guid.Empty) {
  11. _name = "不指定"
  12. };
  13. private Guid _id;
  14. private string _name;
  15. private string _description;
  16. private string _serverJsonSha1;
  17. public string Sha1 { get; private set; }
  18. public ICommand Remove { get; private set; }
  19. public ICommand Edit { get; private set; }
  20. public ICommand Save { get; private set; }
  21. public Action CloseWindow { get; set; }
  22. public MineWorkViewModel() {
  23. if (!Design.IsInDesignMode) {
  24. throw new InvalidProgramException();
  25. }
  26. }
  27. public MineWorkViewModel(IMineWork mineWork) : this(mineWork.GetId()) {
  28. _name = mineWork.Name;
  29. _description = mineWork.Description;
  30. _serverJsonSha1 = mineWork.ServerJsonSha1;
  31. }
  32. public MineWorkViewModel(MineWorkViewModel vm) : this((IMineWork)vm) {
  33. Sha1 = vm.Sha1;
  34. }
  35. public MineWorkViewModel(Guid id) {
  36. _id = id;
  37. this.Save = new DelegateCommand(() => {
  38. if (this.Id == Guid.Empty) {
  39. return;
  40. }
  41. if (string.IsNullOrEmpty(this.Name)) {
  42. NotiCenterWindowViewModel.Instance.Manager.ShowErrorMessage("作业名称是必须的");
  43. }
  44. bool isMineWorkChanged = false;
  45. bool isMinerProfileChanged = false;
  46. MineWorkData mineWorkData = new MineWorkData(this);
  47. if (NTMinerRoot.Instance.MineWorkSet.TryGetMineWork(this.Id, out IMineWork entity)) {
  48. string sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1();
  49. // 如果作业设置变更了则一定变更了
  50. if (this.Sha1 != sha1) {
  51. isMinerProfileChanged = true;
  52. }
  53. else {
  54. // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了
  55. LocalJsonDb localJsonObj = new LocalJsonDb(NTMinerRoot.Instance, mineWorkData);
  56. ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerRoot.Instance, localJsonObj);
  57. var serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj);
  58. sha1 = HashUtil.Sha1(serverJson);
  59. if (sha1 != this.ServerJsonSha1) {
  60. isMinerProfileChanged = true;
  61. }
  62. }
  63. if (entity.Name != this.Name || entity.Description != this.Description) {
  64. isMineWorkChanged = true;
  65. }
  66. CloseWindow?.Invoke();
  67. }
  68. else {
  69. isMinerProfileChanged = true;
  70. VirtualRoot.Execute(new AddMineWorkCommand(this));
  71. CloseWindow?.Invoke();
  72. this.Edit.Execute(FormType.Edit);
  73. }
  74. if (isMinerProfileChanged) {
  75. Write.DevDebug("检测到MinerProfile状态变更");
  76. NTMinerRoot.ExportWorkJson(mineWorkData, out string localJson, out string serverJson);
  77. if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson)) {
  78. Server.ControlCenterService.ExportMineWorkAsync(this.Id, localJson, serverJson, callback: null);
  79. }
  80. if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1) {
  81. this.ServerJsonSha1 = mineWorkData.ServerJsonSha1;
  82. isMineWorkChanged = true;
  83. }
  84. }
  85. if (isMineWorkChanged) {
  86. VirtualRoot.Execute(new UpdateMineWorkCommand(mineWorkData));
  87. }
  88. });
  89. this.Edit = new DelegateCommand<FormType?>((formType) => {
  90. if (this.Id == Guid.Empty) {
  91. return;
  92. }
  93. if (!AppContext.Instance.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel mineWorkVm)) {
  94. Wpf.Util.ShowInputDialog("作业名称", string.Empty, workName => {
  95. if (string.IsNullOrEmpty(workName)) {
  96. return "作业名称是必须的";
  97. }
  98. return string.Empty;
  99. }, onOk: workName => {
  100. new MineWorkViewModel(this) { Name = workName }.Save.Execute(null);
  101. });
  102. }
  103. else {
  104. // 编辑作业前切换上下文
  105. // 根据workId下载json保存到本地并调用LocalJson.Instance.ReInit()
  106. string json = Server.ControlCenterService.GetLocalJson(this.Id);
  107. if (!string.IsNullOrEmpty(json)) {
  108. File.WriteAllText(SpecialPath.LocalJsonFileFullName, json);
  109. }
  110. else {
  111. File.Delete(SpecialPath.LocalJsonFileFullName);
  112. }
  113. NTMinerRoot.Instance.ReInitMinerProfile();
  114. this.Sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1();
  115. VirtualRoot.Execute(new MineWorkEditCommand(formType ?? FormType.Edit, new MineWorkViewModel(this)));
  116. }
  117. }, formType => {
  118. if (this == PleaseSelect) {
  119. return false;
  120. }
  121. return true;
  122. });
  123. this.Remove = new DelegateCommand(() => {
  124. if (this.Id == Guid.Empty) {
  125. return;
  126. }
  127. this.ShowDialog(message: $"您确定删除吗?", title: "确认", onYes: () => {
  128. VirtualRoot.Execute(new RemoveMineWorkCommand(this.Id));
  129. }, icon: IconConst.IconConfirm);
  130. }, () => {
  131. if (this == PleaseSelect) {
  132. return false;
  133. }
  134. return true;
  135. });
  136. }
  137. public Guid GetId() {
  138. return this.Id;
  139. }
  140. public Guid Id {
  141. get => _id;
  142. set {
  143. if (_id != value) {
  144. _id = value;
  145. OnPropertyChanged(nameof(Id));
  146. }
  147. }
  148. }
  149. public string Name {
  150. get { return _name; }
  151. set {
  152. if (_name != value) {
  153. _name = value;
  154. OnPropertyChanged(nameof(Name));
  155. if (this == PleaseSelect) {
  156. return;
  157. }
  158. if (string.IsNullOrEmpty(value)) {
  159. throw new ValidationException("名称是必须的");
  160. }
  161. if (AppContext.Instance.MineWorkVms.List.Any(a => a.Name == value && a.Id != this.Id)) {
  162. throw new ValidationException("名称重复");
  163. }
  164. }
  165. }
  166. }
  167. public MinerProfileViewModel MinerProfile {
  168. get {
  169. return AppContext.Instance.MinerProfileVm;
  170. }
  171. }
  172. public string Description {
  173. get => _description;
  174. set {
  175. if (_description != value) {
  176. _description = value;
  177. OnPropertyChanged(nameof(Description));
  178. }
  179. }
  180. }
  181. public string ServerJsonSha1 {
  182. get => _serverJsonSha1;
  183. set {
  184. if (_serverJsonSha1 != value) {
  185. _serverJsonSha1 = value;
  186. OnPropertyChanged(nameof(ServerJsonSha1));
  187. }
  188. }
  189. }
  190. }
  191. }