use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.
the class MPWService method onStartJob.
@Override
public boolean onStartJob(JobParameters job) {
final JobParameters jobC = job;
Log.e(TAG, "SERVICE START");
final Context ctx = MPWService.this;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
try {
final PoolEnum mPool = PoolEnum.valueOf(prefs.getString("poolEnum", ""));
final CurrencyEnum mCur = CurrencyEnum.valueOf(prefs.getString("curEnum", ""));
Log.i(TAG, "Miner Pool Watcher Service call:" + Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(ctx)));
Log.i(TAG, "SERVICE working on:" + mPool.toString() + " - " + mCur.toString());
final PoolDbHelper mDbHelper = new PoolDbHelper(ctx, mPool, mCur);
final NotificationManager mNotifyMgr = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
final GsonBuilder builder = new GsonBuilder();
// gestione UNIX time lungo e non
builder.registerTypeAdapter(Date.class, new MyDateTypeAdapter());
builder.registerTypeAdapter(Calendar.class, new MyTimeStampTypeAdapter());
// load extra
final String minerAddr = job.getExtras().getString("WALLETURL");
final Boolean notifyBlock = job.getExtras().getBoolean("NOTIFY_BLOCK", false);
final Boolean notifyOffline = job.getExtras().getBoolean("NOTIFY_OFFLINE", false);
final Boolean notifyPayment = job.getExtras().getBoolean("NOTIFY_PAYMENT", false);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(ctx)), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Gson gson = builder.create();
// Register an adapter to manage the date types as long values
HomeStats retrieved = gson.fromJson(response.toString(), HomeStats.class);
mDbHelper.logHomeStats(retrieved);
// dati semi grezzi
LinkedMap<Date, HomeStats> ultimi = mDbHelper.getLastHomeStats(LAST_TWO);
Log.d(TAG, "data size: " + ultimi.size() + " notifyOffline: " + ultimi.get(ultimi.get(0)).getMaturedTotal());
// controllo se manca qualcuno
if (notifyBlock && ultimi.size() > 1 && ultimi.get(ultimi.get(0)).getMaturedTotal().compareTo(ultimi.get(ultimi.get(1)).getMaturedTotal()) > 0) {
int diff = ultimi.get(ultimi.get(0)).getMaturedTotal() - ultimi.get(ultimi.get(1)).getMaturedTotal();
sendBlockNotification(getApplication(), diff + " new block. " + mPool.toString() + " has found " + ultimi.get(ultimi.get(0)).getMaturedTotal() + " blocks", mPool);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
if (minerAddr != null) {
Log.i(TAG, "refreshing wallet " + minerAddr + " notify: " + notifyBlock);
JsonObjectRequest jsonObjReqWallet = new JsonObjectRequest(Request.Method.GET, Utils.getWalletStatsUrl(PreferenceManager.getDefaultSharedPreferences(ctx)) + minerAddr, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Gson gson = builder.create();
// Register an adapter to manage the date types as long values
Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
mDbHelper.logWalletStats(retrieved);
// dati semi grezzi
LinkedMap<Date, Wallet> ultimi = mDbHelper.getLastWallets(LAST_TWO);
// controllo se manca qualcuno
if (notifyOffline && ultimi.keySet().size() >= LAST_TWO) {
if (ultimi.get(ultimi.firstKey()).getWorkersOnline() < ultimi.get(ultimi.get(1)).getWorkersOnline()) {
sendOfflineNotification(ctx, "A Worker has gone OFFLINE. Online Workers: " + ultimi.get(ultimi.firstKey()).getWorkersOnline(), mPool);
} else if (ultimi.get(ultimi.firstKey()).getWorkersOnline() > ultimi.get(ultimi.get(1)).getWorkersOnline()) {
// togli notifiche di offline
mNotifyMgr.cancel(NOTIFICATION_MINER_OFFLINE);
}
// else uguali, fa nulla
}
if (notifyPayment && ultimi.keySet().size() >= LAST_TWO && ultimi.get(ultimi.firstKey()).getPayments().size() > ultimi.get(ultimi.get(1)).getPayments().size()) {
sendPaymentNotification(ctx, "You received a payment: " + Utils.formatEthCurrency(ctx, ultimi.get(ultimi.firstKey()).getPayments().get(0).getAmount()), mCur.toString() + " payment from " + mPool.toString());
}
Log.e(TAG, "SERVICE END Ok1");
MPWService.this.jobFinished(jobC, false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "SERVICE END KO1");
MPWService.this.jobFinished(jobC, true);
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hide the progress dialog
}
});
JSONClientSingleton.getInstance(ctx).addToRequestQueue(jsonObjReqWallet);
} else {
// job end tutto bene
MPWService.this.jobFinished(job, false);
}
// Adding request to request queue
JSONClientSingleton.getInstance(ctx).addToRequestQueue(jsonObjReq);
} catch (Exception se) {
Log.e(TAG, "SERVICE ERROR: " + se);
Crashlytics.logException(se);
}
// Answers the question: "Is there still work going on?"
return true;
}
use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.
the class PaymentsActivity method issueRefresh.
private void issueRefresh(final PoolDbHelper mDbHelper, final GsonBuilder builder, String url) {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(Constants.TAG, response.toString());
Gson gson = builder.create();
// Register an adapter to manage the date types as long values
Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
mDbHelper.logWalletStats(retrieved);
// dati semi grezzi
if (retrieved.getPayments() != null) {
drawPaymentsTable(retrieved);
// la seguente inverte ordine lista
ChartUtils.drawPaymentsHistory(paymentsChart, retrieved);
textViewPaymentsTitle.setText(String.format(getString(R.string.paid_out), mPool.toString()) + " " + retrieved.getPayments().size() + " times");
} else {
textViewPaymentsTitle.setText("No payment Yet");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(Constants.TAG, "Error: " + error.getMessage());
Snackbar.make(findViewById(android.R.id.content), "Network Error", Snackbar.LENGTH_SHORT).show();
}
});
JsonObjectRequest jsonObjBlockReq = new JsonObjectRequest(Request.Method.GET, Utils.getBlocksURL(PreferenceManager.getDefaultSharedPreferences(PaymentsActivity.this)), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(final JSONObject response) {
Log.d(Constants.TAG, response.toString());
Gson gson = builder.create();
// Register an adapter to manage the date types as long values
Block matured = gson.fromJson(response.toString(), Block.class);
drawProjectionTable(mDbHelper, matured);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(Constants.TAG, "Error: " + error.getMessage());
Snackbar.make(findViewById(android.R.id.content), "Network Error", Snackbar.LENGTH_SHORT).show();
}
});
// Adding request to request queue
JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjReq);
JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjBlockReq);
}
use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.
the class WalletActivity method issueRefresh.
private void issueRefresh(final PoolDbHelper mDbHelper, final GsonBuilder builder, final String url) {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(Constants.TAG, "URL " + url);
Log.d(Constants.TAG, "RESPONSE " + response.toString());
Gson gson = builder.create();
// Register an adapter to manage the date types as long values
final Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
mDbHelper.logWalletStats(retrieved);
// aggiorna UI
new UpdateUIAsynchTask().execute();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(Constants.TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjReq);
}
Aggregations