colors, new currencies, new price provider

This commit is contained in:
Radek Davidek 2021-10-01 16:32:08 +02:00
parent 157ed1f162
commit 2934baee9d
3 changed files with 104 additions and 46 deletions

View File

@ -6,9 +6,12 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -40,8 +43,8 @@ public class MiningBean {
List<KeyValue> pool = new ArrayList<>(); List<KeyValue> pool = new ArrayList<>();
List<KeyValue> network = new ArrayList<>(); List<KeyValue> network = new ArrayList<>();
String[] cryptoCur = new String[] { "btc", "ltc", "xrp", "eth" }; String[] cryptoCur = new String[] { "BTC", "LTC", "XRP", "ETH", "DOGE", "ADA" };
double[] lastPrices = new double[] { 0, 0, 0, 0 }; double[] lastPrices = new double[cryptoCur.length];
ltcapikey = request.getParameter("ltcapikey"); ltcapikey = request.getParameter("ltcapikey");
dogapikey = request.getParameter("dogapikey"); dogapikey = request.getParameter("dogapikey");
@ -60,22 +63,41 @@ public class MiningBean {
else if (currency.equals("eur")) else if (currency.equals("eur"))
curSym = "\u20ac"; curSym = "\u20ac";
for (int i = 0; i < cryptoCur.length; i++) { Map<String, String> headers = new HashMap<>();
String ccur = cryptoCur[i]; headers.put("X-CMC_PRO_API_KEY", "d9fae81b-79a4-476e-8f5d-2b4f4033e9a1");
restRequest = getStringResponse("https://www.bitstamp.net/api/v2/ticker/".concat(ccur).concat(currency)); restRequest = getStringResponse("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest", headers);
if (restRequest != null && restRequest.length() > 0) {
JSONObject tmp = new JSONObject(restRequest); if (restRequest != null && restRequest.length() > 0) {
double res = tmp.getDouble("last"); JSONObject tmp = new JSONObject(restRequest);
lastPrices[i] = res; JSONArray data = (JSONArray) tmp.get("data");
if (res > 0d)
resultStr = curSym + res; for (int i=0;i<data.length();i++) {
else JSONObject o = data.getJSONObject(i);
resultStr = "Unknown"; String symbol = o.getString("symbol");
KeyValue wi = new KeyValue(); int pos = searchArray(cryptoCur, symbol);
wi.setKey(ccur.toUpperCase().concat("/").concat(currency.toUpperCase()).concat(" (Bitstamp)")); if (pos>=0) {
wi.setValue(resultStr); JSONObject quote = (JSONObject) o.get("quote");
prices.add(wi); JSONObject usd = (JSONObject) quote.get(currency.toUpperCase());
double res = usd.getDouble("price");
double change = usd.getDouble("percent_change_24h");
lastPrices[pos] = res;
if (res > 0d)
resultStr = curSym + res + " ("+String.format("%.2f",(change))+"%)";
else
resultStr = "Unknown";
KeyValue wi = new KeyValue();
wi.setKey(symbol.concat("/").concat(currency.toUpperCase()).concat(" (CoinMarketCap)"));
wi.setValue(resultStr);
if (change>0)
wi.setColor("GREEN");
else
wi.setColor("RED");
prices.add(wi);
}
} }
} }
new Thread(new Runnable() { new Thread(new Runnable() {
@ -480,11 +502,11 @@ public class MiningBean {
} }
while (finished != 0b1111111) { while (finished != 0b1111111) {
//System.out.println(Integer.toBinaryString(finished)); // System.out.println(Integer.toBinaryString(finished));
Thread.sleep(2000); Thread.sleep(2000);
} }
//System.out.println("Done: " + Integer.toBinaryString(finished)); // System.out.println("Done: " + Integer.toBinaryString(finished));
finished = 0b0; finished = 0b0;
KeyValue wi = new KeyValue(); KeyValue wi = new KeyValue();
@ -496,8 +518,10 @@ public class MiningBean {
String tmp = ""; String tmp = "";
for (KeyValue kv : prices) { for (KeyValue kv : prices) {
if (kv.getColor()==null)
kv.setColor("BLUE");
tmp = tmp.concat("<pair><key>").concat( tmp = tmp.concat("<pair><key>").concat(
kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>")); kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value>").concat("<color>").concat(kv.getColor()).concat("</color></pair>"));
} }
res = res.replace("#PRICES#", tmp); res = res.replace("#PRICES#", tmp);
@ -525,6 +549,16 @@ public class MiningBean {
return res; return res;
} }
private int searchArray(String[] array, String key) {
int i=0;
for (String str:array) {
if (str.equals(key))
return i;
i++;
}
return -1;
}
public String getVersion() { public String getVersion() {
return Constants.APP_VERSION; return Constants.APP_VERSION;
} }
@ -542,11 +576,16 @@ public class MiningBean {
} }
public String getStringResponse(String urlStr) { public String getStringResponse(String urlStr) {
return getStringResponse(urlStr, Constants.DEFAULT_HTTP_CONNECTION_TIMEOUT, return getStringResponse(urlStr, Constants.DEFAULT_HTTP_CONNECTION_TIMEOUT, Constants.DEFAULT_HTTP_READ_TIMEOUT,
Constants.DEFAULT_HTTP_READ_TIMEOUT); null);
} }
public String getStringResponse(String urlStr, int timeout, int readTimeout) { public String getStringResponse(String urlStr, Map<String, String> headers) {
return getStringResponse(urlStr, Constants.DEFAULT_HTTP_CONNECTION_TIMEOUT, Constants.DEFAULT_HTTP_READ_TIMEOUT,
headers);
}
public String getStringResponse(String urlStr, int timeout, int readTimeout, Map<String, String> headers) {
String output = ""; String output = "";
try { try {
@ -558,6 +597,11 @@ public class MiningBean {
conn.setReadTimeout(readTimeout); conn.setReadTimeout(readTimeout);
conn.setRequestProperty("User-Agent", conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"); "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
if (headers != null && !headers.isEmpty()) {
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key));
}
}
conn.connect(); conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine; String inputLine;

View File

@ -3,26 +3,37 @@ package cz.kamma.mining.vo;
import java.io.Serializable; import java.io.Serializable;
public class KeyValue implements Serializable { public class KeyValue implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
String key; String key;
String value; String value;
String color;
public KeyValue() { public KeyValue() {
} }
public String getKey() { public String getKey() {
return key; return key;
} }
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
} }

View File

@ -95,10 +95,13 @@ function AX_asynchUpdateResponse() {
var resStr = "<li class='group' id='prices'>Prices</li>"; var resStr = "<li class='group' id='prices'>Prices</li>";
for (var i = 0; i <= (prices.length - 1); i++) { for (var i = 0; i <= (prices.length - 1); i++) {
var color = prices[i].getElementsByTagName("color")[0].firstChild.data;
if (color==null)
color = "GREEN";
resStr = resStr resStr = resStr
+ "<li>" + "<li>"
+ prices[i].getElementsByTagName("key")[0].firstChild.data + prices[i].getElementsByTagName("key")[0].firstChild.data
+ " <span class='value' style='transition: text-shadow 0.7s ease-in-out;'>" + " <span class='value' style='transition: text-shadow 0.7s ease-in-out; color:"+color+";'>"
+ prices[i].getElementsByTagName("value")[0].firstChild.data + prices[i].getElementsByTagName("value")[0].firstChild.data
+ "</span></li>"; + "</span></li>";
} }