-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
264 lines (219 loc) · 8.89 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content.Res;
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;
using static cec.DaoGpu;
using System.Json;
namespace cec
{
[Activity(Label = "cec", MainLauncher = true)]
public class MainActivity : Activity
{
private String GpuText;
private String AlgoText;
private ArrayAdapter<String> DataAdapter;
private ArrayAdapter<String> DataAdapter2;
private List<string> listA = new List<string>();
private List<string> listG = new List<string>();
private List<string> listS = new List<string>();
private DaoGpu dg = new DaoGpu();
private XmlDocument document = new XmlDocument();
private XmlNode topNode;
private GpuCollect gc = new GpuCollect();
private string hash;
private int i = 0;
JsonValue jsonDoc;
private void StartXml()
{
AssetManager assets = this.Assets;
document.Load(assets.Open("gpu.xml"));
topNode = document.GetElementsByTagName("root")[0];
foreach (XmlNode node in topNode.ChildNodes)
{
dg = new DaoGpu(node.ChildNodes[0].InnerText,
node.ChildNodes[1].InnerText, node.ChildNodes[2].InnerText,
node.ChildNodes[3].InnerText);
gc.Add(dg);
listG.Add(node.ChildNodes[0].InnerText);
}
listA.Add("Ethereum");
listA.Add("Ethereum Classic");
listA.Add("Equihash");
this.DataAdapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleSpinnerItem, listG.Distinct().ToList());
this.DataAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
this.DataAdapter2 = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleSpinnerItem, listA.Distinct().ToList());
this.DataAdapter2.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
}
private void Spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
if (spinner.GetItemAtPosition(e.Position).Equals("minute")) i = 0;
else if (spinner.GetItemAtPosition(e.Position).Equals("hour")) i = 1;
else if (spinner.GetItemAtPosition(e.Position).Equals("day")) i = 2;
else if (spinner.GetItemAtPosition(e.Position).Equals("week")) i = 3;
else i = 4;
if(jsonDoc != null)
{
switch (i)
{
case 0:
ParseAndDisplay(jsonDoc,i);
break;
case 1:
ParseAndDisplay(jsonDoc,i);
break;
case 2:
ParseAndDisplay(jsonDoc,i);
break;
case 3:
ParseAndDisplay(jsonDoc,i);
break;
case 4:
ParseAndDisplay(jsonDoc,i);
break;
default:
Toast.MakeText(ApplicationContext, "Something worng", ToastLength.Long).Show();
break;
}
}
else
{
string message = "No DATA";
Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
StartXml();
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
spinner.Adapter = this.DataAdapter;
Spinner spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);
spinner2.Adapter = this.DataAdapter2;
Button button = FindViewById<Button>(Resource.Id.Check);
button.Click += async (o, e) =>
{
GpuText = spinner.SelectedItem.ToString();
AlgoText = spinner2.SelectedItem.ToString();
string _AlgoText;
if (AlgoText == "Ethereum" || AlgoText == "Ethereum Classic")
{
_AlgoText = "etHash";
await CalculateAsync(GpuText, _AlgoText);
}
else
{
_AlgoText = "equiHash";
await CalculateAsync(GpuText, _AlgoText);
}
Spinner spinner3 = FindViewById<Spinner>(Resource.Id.spinner3);
spinner3.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(Spinner_ItemSelected);
var adapter3 = ArrayAdapter.CreateFromResource(
this, Resource.Array.resultArray, Android.Resource.Layout.SimpleSpinnerItem);
adapter3.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner3.Adapter = adapter3;
};
}
private async Task CalculateAsync(string gpu, string algo)
{
for (int i =0; i<gc.Count; i++)
{
if (gc.Search(i).VGA == gpu && gc.Search(i).Algo == algo)
{
hash = gc.Search(i).Hash;
}
}
Double newHash = Double.Parse(hash);
int newhash1 = (int)(Math.Round(newHash));
if (newhash1 == 0)
{
string message = "no information";
Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
}
else if (algo.Equals("etHash") && hash!=null)
{
string url = "https://api.nanopool.org/v1/eth/approximated_earnings/";
JsonValue json = await FetchData(url+""+ newHash);
ParseAndDisplay(json,i);
}
else if(algo.Equals("equiHash") && hash != null)
{
string url = "https://api.nanopool.org/v1/etc/approximated_earnings/";
JsonValue json = await FetchData(url+""+ newHash);
ParseAndDisplay(json,i);
}
else
{
string message = "no information";
Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
}
}
private async Task<JsonValue> FetchData(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
using (WebResponse response = await request.GetResponseAsync())
{
using (Stream stream = response.GetResponseStream())
{
jsonDoc = await Task.Run(() => JsonObject.Load(stream));
return jsonDoc;
}
}
}
private void ParseAndDisplay(JsonValue json,int i)
{
TextView usd = FindViewById<TextView>(Resource.Id.usdText);
TextView btc = FindViewById<TextView>(Resource.Id.btcText);
JsonValue Result = json["data"];
if (i == 0)
{
string status = "minute";
JsonValue s = Result[status];
usd.Text = s["dollars"].ToString();
btc.Text = s["bitcoins"].ToString();
}
else if (i == 1)
{
string status = "hour";
JsonValue s = Result[status];
usd.Text = s["dollars"].ToString();
btc.Text = s["bitcoins"].ToString();
}
else if (i == 2)
{
string status = "day";
JsonValue s = Result[status];
usd.Text = s["dollars"].ToString();
btc.Text = s["bitcoins"].ToString();
}
else if (i == 3)
{
string status = "week";
JsonValue s = Result[status];
usd.Text = s["dollars"].ToString();
btc.Text = s["bitcoins"].ToString();
}
else if (i == 4)
{
string status = "month";
JsonValue s = Result[status];
usd.Text = s["dollars"].ToString();
btc.Text = s["bitcoins"].ToString();
}
}
}
}