public MineWorkViewModel(Guid id) { _id = id; this.Save = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } if (string.IsNullOrEmpty(this.Name)) { VirtualRoot.Out.ShowError("作业名称是必须的", autoHideSeconds: 4); } bool isMineWorkChanged = false; bool isMinerProfileChanged = false; MineWorkData mineWorkData = new MineWorkData().Update(this); if (NTMinerRoot.Instance.MineWorkSet.TryGetMineWork(this.Id, out IMineWork entity)) { string sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1(); // 如果作业设置变更了则一定变更了 if (this.Sha1 != sha1) { isMinerProfileChanged = true; } else { // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了 LocalJsonDb localJsonObj = new LocalJsonDb(NTMinerRoot.Instance, mineWorkData); ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerRoot.Instance, localJsonObj); var serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj); sha1 = HashUtil.Sha1(serverJson); if (sha1 != this.ServerJsonSha1) { isMinerProfileChanged = true; } } if (entity.Name != this.Name || entity.Description != this.Description) { isMineWorkChanged = true; } } else { isMinerProfileChanged = true; VirtualRoot.Execute(new AddMineWorkCommand(this)); this.Edit.Execute(FormType.Edit); } if (isMinerProfileChanged) { Write.DevDebug("检测到MinerProfile状态变更"); NTMinerRoot.ExportWorkJson(mineWorkData, out string localJson, out string serverJson); if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson)) { RpcRoot.Server.MineWorkService.ExportMineWorkAsync(this.Id, localJson, serverJson, callback: (response, e) => { if (!response.IsSuccess()) { VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4); } }); } if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1) { this.ServerJsonSha1 = mineWorkData.ServerJsonSha1; isMineWorkChanged = true; } } if (isMineWorkChanged) { VirtualRoot.Execute(new UpdateMineWorkCommand(mineWorkData)); } }); this.Edit = new DelegateCommand <FormType?>((formType) => { if (this.Id == Guid.Empty) { return; } if (!AppContext.Instance.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel mineWorkVm)) { WpfUtil.ShowInputDialog("作业名称", string.Empty, workName => { if (string.IsNullOrEmpty(workName)) { return("作业名称是必须的"); } return(string.Empty); }, onOk: workName => { new MineWorkViewModel(this) { Name = workName }.Save.Execute(null); }); } else { // 编辑作业前切换上下文 // 根据workId下载json保存到本地并调用LocalJson.Instance.ReInit() string json = RpcRoot.Server.MineWorkService.GetLocalJson(this.Id); if (!string.IsNullOrEmpty(json)) { File.WriteAllText(SpecialPath.LocalJsonFileFullName, json); } else { File.Delete(SpecialPath.LocalJsonFileFullName); } NTMinerRoot.Instance.ReInitMinerProfile(); this.Sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1(); VirtualRoot.Execute(new MineWorkEditCommand(formType ?? FormType.Edit, new MineWorkViewModel(this))); } }, formType => { if (this == PleaseSelect) { return(false); } return(true); }); this.Remove = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } this.ShowSoftDialog(new DialogWindowViewModel(message: $"您确定删除吗?", title: "确认", onYes: () => { VirtualRoot.Execute(new RemoveMineWorkCommand(this.Id)); })); }, () => { if (this == PleaseSelect) { return(false); } return(true); }); }
private void DoSave() { if (string.IsNullOrEmpty(this.Name)) { VirtualRoot.Out.ShowError("作业名称是必须的", autoHideSeconds: 4); } bool isMinerProfileChanged = false; // 表示是否随后打开编辑界面,如果是新建的作业则保存后随即会打开作业编辑界面 bool isShowEdit = false; MineWorkData mineWorkData = new MineWorkData().Update(this); if (MinerStudioRoot.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel vm)) { string sha1 = NTMinerContext.Instance.MinerProfile.GetSha1(); // 如果作业设置变更了则一定变更了 if (this.Sha1 != sha1) { isMinerProfileChanged = true; } else { // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了 LocalJsonDb localJsonObj = new LocalJsonDb(NTMinerContext.Instance, mineWorkData); ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerContext.Instance, localJsonObj); var serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj); sha1 = HashUtil.Sha1(serverJson); if (sha1 != this.ServerJsonSha1) { isMinerProfileChanged = true; } } } else { isMinerProfileChanged = true; isShowEdit = true; NTMinerConsole.UserInfo("保存作业。"); } if (RpcRoot.IsOuterNet) { if (!this.Id.IsSelfMineWorkId()) { RpcRoot.OfficialServer.UserMineWorkService.AddOrUpdateMineWorkAsync(new MineWorkData().Update(this), (r, ex) => { if (r.IsSuccess()) { if (isMinerProfileChanged) { NTMinerContext.Instance.ExportWorkJson(mineWorkData, out string localJson, out string serverJson); if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson)) { RpcRoot.OfficialServer.UserMineWorkService.ExportMineWorkAsync(this.Id, localJson, serverJson, (response, e) => { if (response.IsSuccess()) { if (isShowEdit) { VirtualRoot.RaiseEvent(new MineWorkAddedEvent(Guid.Empty, this)); } else { VirtualRoot.RaiseEvent(new MineWorkUpdatedEvent(Guid.Empty, this)); } if (isShowEdit) { this.Edit.Execute(FormType.Edit); } } else { VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4); } }); } if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1) { this.ServerJsonSha1 = mineWorkData.ServerJsonSha1; } } } else { VirtualRoot.Out.ShowError(r.ReadMessage(ex), autoHideSeconds: 4); } });
public static void ExportWorkJson(MineWorkData mineWorkData, out string localJson, out string serverJson) { localJson = string.Empty; serverJson = string.Empty; try { var root = Current; var minerProfile = root.MinerProfile; CoinProfileData mainCoinProfile = new CoinProfileData(minerProfile.GetCoinProfile(minerProfile.CoinId)); List <CoinProfileData> coinProfiles = new List <CoinProfileData> { mainCoinProfile }; List <PoolProfileData> poolProfiles = new List <PoolProfileData>(); CoinKernelProfileData coinKernelProfile = new CoinKernelProfileData(minerProfile.GetCoinKernelProfile(mainCoinProfile.CoinKernelId)); PoolProfileData mainCoinPoolProfile = new PoolProfileData(minerProfile.GetPoolProfile(mainCoinProfile.PoolId)); poolProfiles.Add(mainCoinPoolProfile); if (coinKernelProfile.IsDualCoinEnabled) { CoinProfileData dualCoinProfile = new CoinProfileData(minerProfile.GetCoinProfile(coinKernelProfile.DualCoinId)); coinProfiles.Add(dualCoinProfile); PoolProfileData dualCoinPoolProfile = new PoolProfileData(minerProfile.GetPoolProfile(dualCoinProfile.DualCoinPoolId)); poolProfiles.Add(dualCoinPoolProfile); } LocalJsonDb localJsonObj = new LocalJsonDb { MinerProfile = new MinerProfileData(minerProfile) { MinerName = "{{MinerName}}" }, MineWork = mineWorkData, CoinProfiles = coinProfiles.ToArray(), CoinKernelProfiles = new CoinKernelProfileData[] { coinKernelProfile }, PoolProfiles = poolProfiles.ToArray(), TimeStamp = Timestamp.GetTimestamp(), Pools = root.PoolSet.Where(a => poolProfiles.Any(b => b.PoolId == a.GetId())).Select(a => new PoolData(a)).ToArray(), Wallets = minerProfile.GetWallets().Select(a => new WalletData(a)).ToArray() }; localJson = VirtualRoot.JsonSerializer.Serialize(localJsonObj); root.CoinKernelSet.TryGetCoinKernel(coinKernelProfile.CoinKernelId, out ICoinKernel coinKernel); root.KernelSet.TryGetKernel(coinKernel.KernelId, out IKernel kernel); var coins = root.CoinSet.Cast <CoinData>().Where(a => localJsonObj.CoinProfiles.Any(b => b.CoinId == a.Id)).ToArray(); var coinGroups = root.CoinGroupSet.Cast <CoinGroupData>().Where(a => coins.Any(b => b.Id == a.CoinId)).ToArray(); var pools = root.PoolSet.Cast <PoolData>().Where(a => localJsonObj.PoolProfiles.Any(b => b.PoolId == a.Id)).ToArray(); ServerJsonDb serverJsonObj = new ServerJsonDb { Coins = coins, CoinGroups = coinGroups, Pools = pools, TimeStamp = Timestamp.GetTimestamp(), Groups = root.GroupSet.Cast <GroupData>().Where(a => coinGroups.Any(b => b.GroupId == a.Id)).ToArray(), KernelInputs = root.KernelInputSet.Cast <KernelInputData>().Where(a => a.Id == kernel.KernelInputId).ToArray(), KernelOutputs = root.KernelOutputSet.Cast <KernelOutputData>().Where(a => a.Id == kernel.KernelOutputId).ToArray(), KernelOutputFilters = root.KernelOutputFilterSet.Cast <KernelOutputFilterData>().Where(a => a.KernelOutputId == kernel.KernelOutputId).ToArray(), KernelOutputTranslaters = root.KernelOutputTranslaterSet.Cast <KernelOutputTranslaterData>().Where(a => a.KernelOutputId == kernel.KernelOutputId).ToArray(), Kernels = new List <KernelData> { (KernelData)kernel }, CoinKernels = root.CoinKernelSet.Cast <CoinKernelData>().Where(a => localJsonObj.CoinKernelProfiles.Any(b => b.CoinKernelId == a.Id)).ToList(), PoolKernels = root.PoolKernelSet.Cast <PoolKernelData>().Where(a => !string.IsNullOrEmpty(a.Args) && pools.Any(b => b.Id == a.PoolId)).ToList(), SysDicItems = root.SysDicItemSet.Cast <SysDicItemData>().ToArray(), SysDics = root.SysDicSet.Cast <SysDicData>().ToArray() }; serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj); } catch (Exception e) { Logger.ErrorDebugLine(e.Message, e); } }