using NTMiner.Controllers; using NTMiner.Core; using NTMiner.Core.Daemon; using NTMiner.Core.Impl; using NTMiner.Core.MinerClient; using NTMiner.Core.MinerServer; using NTMiner.Core.MinerStudio; using NTMiner.JsonDb; using NTMiner.VirtualMemory; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; namespace NTMiner.MinerStudio.Impl { public class LocalMinerStudioService : ILocalMinerStudioService { private readonly string _daemonControllerName = RpcRoot.GetControllerName(); private readonly IClientDataSet _clientDataSet; private readonly ICoinSnapshotSet _coinSnapshotSet; public LocalMinerStudioService() { _clientDataSet = new ClientDataSet(); _coinSnapshotSet = new CoinSnapshotSet(_clientDataSet); } #region AddClientsAsync public void AddClientsAsync(List clientIps, Action callback) { try { foreach (var clientIp in clientIps) { ClientData clientData = _clientDataSet.AsEnumerable().FirstOrDefault(a => a.MinerIp == clientIp); if (clientData != null) { continue; } _clientDataSet.AddClient(clientIp); } callback?.Invoke(ResponseBase.Ok(), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region RemoveClientsAsync public void RemoveClientsAsync(List objectIds, Action callback) { try { foreach (var objectId in objectIds) { _clientDataSet.RemoveByObjectId(objectId); } callback?.Invoke(ResponseBase.Ok(), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region QueryClientsAsync public void QueryClientsAsync(QueryClientsRequest query, Action callback) { try { var data = _clientDataSet.QueryClients( user: null, query, out int total, out List latestSnapshots, out int totalOnlineCount, out int totalMiningCount); callback?.Invoke(QueryClientsResponse.Ok(data, total, latestSnapshots, totalMiningCount, totalOnlineCount), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region UpdateClientAsync public void UpdateClientAsync(string objectId, string propertyName, object value, Action callback) { try { _clientDataSet.UpdateClient(objectId, propertyName, value); callback?.Invoke(ResponseBase.Ok(), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region UpdateClientsAsync public void UpdateClientsAsync(string propertyName, Dictionary values, Action callback) { try { _clientDataSet.UpdateClients(propertyName, values); callback?.Invoke(ResponseBase.Ok(), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region GetLatestSnapshotsAsync public void GetLatestSnapshotsAsync(int limit, Action callback) { try { List data = _coinSnapshotSet.GetLatestSnapshots( limit, out int totalMiningCount, out int totalOnlineCount) ?? new List(); callback?.Invoke(GetCoinSnapshotsResponse.Ok(data, totalMiningCount, totalOnlineCount), null); } catch (Exception e) { callback?.Invoke(ResponseBase.ServerError(e.Message), e); } } #endregion #region EnableRemoteDesktopAsync public void EnableRemoteDesktopAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.EnableRemoteDesktop), null, null, timeountMilliseconds: 3000); } #endregion #region GetConsoleOutLinesAsync public void GetConsoleOutLinesAsync(IMinerData client, long afterTime) { RpcRoot.Client.MinerClientService.GetConsoleOutLinesAsync(client.GetLocalIp(), afterTime, (data, e) => { if (data != null && data.Count > 0) { VirtualRoot.RaiseEvent(new ClientConsoleOutLinesEvent(client.ClientId, data)); } }); } #endregion #region BlockWAUAsync public void BlockWAUAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.BlockWAU), null, null, timeountMilliseconds: 3000); } #endregion #region AtikmdagPatcherAsync public void AtikmdagPatcherAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.AtikmdagPatcher), null, null, timeountMilliseconds: 3000); } #endregion #region SwitchRadeonGpuAsync public void SwitchRadeonGpuAsync(IMinerData client, bool on) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SwitchRadeonGpu), new Dictionary { {"on", on.ToString() } }, null, null, timeountMilliseconds: 3000); } #endregion #region RestartWindowsAsync public void RestartWindowsAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.RestartWindows), new object(), null, timeountMilliseconds: 3000); } #endregion #region ShutdownWindowsAsync public void ShutdownWindowsAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.ShutdownWindows), new object(), null, timeountMilliseconds: 3000); } #endregion #region UpgradeNTMinerAsync // ReSharper disable once InconsistentNaming public void UpgradeNTMinerAsync(IMinerData client, string ntminerFileName) { UpgradeNTMinerRequest request = new UpgradeNTMinerRequest { NTMinerFileName = ntminerFileName }; RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.UpgradeNTMiner), request, null, timeountMilliseconds: 3000); } #endregion #region SetAutoBootStartAsync public void SetAutoBootStartAsync(IMinerData client, SetAutoBootStartRequest request) { RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetAutoBootStart), new Dictionary { {"autoBoot", request.AutoBoot.ToString() }, {"autoStart", request.AutoStart.ToString() } }, null, callback: null, timeountMilliseconds: 3000); } #endregion #region StartMineAsync public void StartMineAsync(IMinerData client, Guid workId) { string localJson = string.Empty, serverJson = string.Empty; if (workId != Guid.Empty) { localJson = MinerStudioPath.ReadMineWorkLocalJsonFile(workId).Replace(NTKeyword.MinerNameParameterName, client.WorkerName); serverJson = MinerStudioPath.ReadMineWorkServerJsonFile(workId); } WorkRequest request = new WorkRequest { WorkId = workId, WorkerName = client.WorkerName, LocalJson = localJson, ServerJson = serverJson }; RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.StartMine), request, null, timeountMilliseconds: 3000); } #endregion #region StopMineAsync public void StopMineAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.StopMine), new object(), null, timeountMilliseconds: 3000); } #endregion #region GetDrivesAsync public void GetDrivesAsync(IMinerData client) { RpcRoot.PostAsync>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetDrives), null, (data, e) => { VirtualRoot.RaiseEvent(new GetDrivesResponsedEvent(client.ClientId, data)); }, timeountMilliseconds: 3000); } #endregion #region SetVirtualMemoryAsync public void SetVirtualMemoryAsync(IMinerData client, Dictionary data) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetVirtualMemory), new DataRequest> { Data = data }, null, timeountMilliseconds: 3000); } #endregion #region GetLocalIpsAsync public void GetLocalIpsAsync(IMinerData client) { RpcRoot.PostAsync>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetLocalIps), null, (data, e) => { VirtualRoot.RaiseEvent(new GetLocalIpsResponsedEvent(client.ClientId, data)); }, timeountMilliseconds: 3000); } #endregion #region SetLocalIpsAsync public void SetLocalIpsAsync(IMinerData client, List data) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetLocalIps), new DataRequest> { Data = data }, null, timeountMilliseconds: 3000); } #endregion #region GetOperationResultsAsync public void GetOperationResultsAsync(IMinerData client, long afterTime) { RpcRoot.GetAsync>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetOperationResults), new Dictionary { {"afterTime",afterTime.ToString() } }, (data, e) => { if (data != null && data.Count > 0) { VirtualRoot.RaiseEvent(new ClientOperationResultsEvent(client.ClientId, data)); } }, timeountMilliseconds: 3000); } #endregion #region GetSelfWorkLocalJsonAsync public void GetSelfWorkLocalJsonAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetSelfWorkLocalJson), null, (json, e) => { VirtualRoot.RaiseEvent(new GetSelfWorkLocalJsonResponsedEvent(client.ClientId, json)); }, timeountMilliseconds: 3000); } #endregion #region SaveSelfWorkLocalJsonAsync public void SaveSelfWorkLocalJsonAsync(IMinerData client, string localJson, string serverJson) { if (string.IsNullOrEmpty(localJson) || string.IsNullOrEmpty(serverJson)) { return; } WorkRequest request = new WorkRequest { WorkId = MineWorkData.SelfMineWorkId, WorkerName = client.WorkerName, LocalJson = localJson.Replace(NTKeyword.MinerNameParameterName, client.WorkerName), ServerJson = serverJson }; RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SaveSelfWorkLocalJson), null, request, timeountMilliseconds: 3000); } #endregion #region GetGpuProfilesJsonAsync public void GetGpuProfilesJsonAsync(IMinerData client) { RpcRoot.PostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetGpuProfilesJson), null, (json, e) => { GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize(json) ?? new GpuProfilesJsonDb(); VirtualRoot.RaiseEvent(new GetGpuProfilesResponsedEvent(client.ClientId, data)); }, timeountMilliseconds: 3000); } #endregion #region SaveGpuProfilesJsonAsync public void SaveGpuProfilesJsonAsync(IMinerData client, string json) { HttpContent content = new StringContent(json); RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SaveGpuProfilesJson), null, content, null, timeountMilliseconds: 3000); } #endregion } }