private static bool EndpointIsMiner(IPEndPoint ipEndpoint) { bool endpointIsMiner = false; ApiContext context = new ApiContext(ipEndpoint.Port, ipEndpoint.Address.ToString()); string response = null; try { //give the call more time than default (500 ms) //we want to minimize removing valid endpoints due to //device resource limitations const int TimeoutMs = 3000; response = context.GetResponse(ApiVerb.Version, TimeoutMs); } catch (Exception) { response = null; } if (!String.IsNullOrEmpty(response) && response.Contains("VERSION")) endpointIsMiner = true; return endpointIsMiner; }
private static bool EndpointIsMiner(IPEndPoint ipEndpoint) { bool endpointIsMiner = false; ApiContext context = new ApiContext(ipEndpoint.Port, ipEndpoint.Address.ToString()); string response = null; try { response = context.GetResponse(ApiVerb.Version); } catch (Exception ex) { response = null; } if (!String.IsNullOrEmpty(response) && response.Contains("VERSION")) endpointIsMiner = true; return endpointIsMiner; }
private double GetNetworkDifficultyFromMiner(ApiContext apiContext) { //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; NetworkCoinInformation coinInformation; try { try { coinInformation = apiContext.GetCoinInformation(); } catch (IOException) { //don't fail and crash out due to any issues communicating via the API coinInformation = null; } } catch (SocketException) { //won't be able to connect for the first 5s or so coinInformation = null; } return coinInformation == null ? 0.0 : coinInformation.NetworkDifficulty; }
private void CheckAndSetNetworkDifficulty(string ipAddress, int port, string poolUri) { double difficulty = GetCachedNetworkDifficulty(poolUri); if (difficulty == 0.0) { ApiContext apiContext = new ApiContext(port, ipAddress); difficulty = GetNetworkDifficultyFromMiner(apiContext); SetCachedNetworkDifficulty(poolUri, difficulty); } }
private void ProcessNetworkDeviceRemoteCommand(string commandText, DeviceViewModel networkDevice) { Uri uri = new Uri("http://" + networkDevice.Path); ApiContext apiContext = new ApiContext(uri.Port, uri.Host); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; string action = "accessing " + networkDevice.FriendlyName; try { if (commandText.StartsWith(RemoteCommandText.Switch, StringComparison.OrdinalIgnoreCase)) { action = "switching pools on " + networkDevice.FriendlyName; string[] parts = commandText.Split('|'); string poolName = parts[1]; //we may not have the pool info cached yet / anymore if (NetworkDevicePools.ContainsKey(networkDevice.Path)) { List<PoolInformation> pools = NetworkDevicePools[networkDevice.Path]; int poolIndex = pools.FindIndex(pi => pi.Url.DomainFromHost().Equals(poolName, StringComparison.OrdinalIgnoreCase)); apiContext.SwitchPool(poolIndex); } } else if (commandText.Equals(RemoteCommandText.Restart, StringComparison.OrdinalIgnoreCase)) { RestartNetworkDevice(networkDevice); } else if (commandText.Equals(RemoteCommandText.Stop, StringComparison.OrdinalIgnoreCase)) { StopNetworkDevice(networkDevice); } else if (commandText.Equals(RemoteCommandText.Start, StringComparison.OrdinalIgnoreCase)) { StartNetworkDevice(networkDevice); } } catch (SocketException ex) { PostNotification(String.Format("Error {0}: {1}", action, ex.Message), NotificationKind.Danger); } }
private VersionInformation GetVersionInfoFromAddress(string ipAddress, int port) { ApiContext apiContext = new ApiContext(port, ipAddress); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; VersionInformation versionInformation; try { try { versionInformation = apiContext.GetVersionInformation(); } catch (IOException) { //don't fail and crash out due to any issues communicating via the API versionInformation = null; } } catch (SocketException) { //won't be able to connect for the first 5s or so versionInformation = null; } return versionInformation; }
private List<DeviceInformation> GetDeviceInfoFromAddress(string ipAddress, int port) { ApiContext apiContext = new ApiContext(port, ipAddress); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; List<DeviceInformation> deviceInformationList; try { try { //some Network Devices don't have the horsepower to return API results immediately const int commandTimeoutMs = 3000; deviceInformationList = apiContext.GetDeviceInformation(commandTimeoutMs).Where(d => d.Enabled).ToList(); } catch (IOException) { //don't fail and crash out due to any issues communicating via the API deviceInformationList = null; } } catch (SocketException) { //won't be able to connect for the first 5s or so deviceInformationList = null; } return deviceInformationList; }
private string GetCoinNameForApiContext(ApiContext apiContext) { string coinName = string.Empty; foreach (MinerProcess minerProcess in MiningEngine.MinerProcesses) { ApiContext loopContext = minerProcess.ApiContext; if (loopContext == apiContext) { coinName = minerProcess.MinerConfiguration.CoinName; break; } } return coinName; }
private bool ToggleNetworkDevicePools(DeviceViewModel networkDevice, bool enabled) { // networkDevicePools is keyed by IP:port, use .Path List<PoolInformation> poolInformation = GetCachedPoolInfoFromAddress(networkDevice.Path); if (poolInformation == null) //RPC API call timed out return false; Uri uri = new Uri("http://" + networkDevice.Path); ApiContext apiContext = new ApiContext(uri.Port, uri.Host); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; string verb = enabled ? "enablepool" : "disablepool"; for (int i = 0; i < poolInformation.Count; i++) { string response = apiContext.GetResponse(String.Format("{0}|{1}", verb, i)); if (!response.ToLower().Contains("STATUS=S".ToLower())) return false; } //remove cached data for pools NetworkDevicePools.Remove(networkDevice.Path); return true; }
private List<MinerStatistics> GetMinerStatisticsFromAddress(string ipAddress, int port) { ApiContext apiContext = new ApiContext(port, ipAddress); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; List<MinerStatistics> minerStatistics; try { try { minerStatistics = apiContext.GetMinerStatistics(); } catch (IOException) { //don't fail and crash out due to any issues communicating via the API minerStatistics = null; } } catch (SocketException) { //won't be able to connect for the first 5s or so minerStatistics = null; } return minerStatistics; }
public bool RestartNetworkDevice(DeviceViewModel networkDevice) { Uri uri = new Uri("http://" + networkDevice.Path); ApiContext apiContext = new ApiContext(uri.Port, uri.Host); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; string response = apiContext.RestartMining(); bool result = !response.ToLower().Contains("STATUS=E".ToLower()); if (result) networkDevice.LastRestart = DateTime.Now; return result; }
public bool SetNetworkDevicePoolIndex(DeviceViewModel networkDevice, int poolIndex) { if (poolIndex < 0) return false; //invalid index // networkDevicePools is keyed by IP:port, use .Path List<PoolInformation> poolInformation = NetworkDevicePools[networkDevice.Path]; if ((poolInformation != null) && (poolIndex >= poolInformation.Count)) return false; //invalid index Uri uri = new Uri("http://" + networkDevice.Path); ApiContext apiContext = new ApiContext(uri.Port, uri.Host); //setup logging apiContext.LogEvent -= LogApiEvent; apiContext.LogEvent += LogApiEvent; apiContext.SwitchPool(poolIndex); return true; }