Search in sources :

Example 1 with Wallet

use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.

the class KratosInstrumentedTest method testJsonWalletRequest.

@Test
public void testJsonWalletRequest() throws Exception {
    final GsonBuilder builder = new GsonBuilder();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getWalletStatsUrl(sharedPreferences) + minerAddr, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.i(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);
            assertNotNull(retrieved);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(Constants.TAG, "Error: " + error.getMessage());
            fail(error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(appContext).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 2 with Wallet

use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.

the class PoolDbHelper method getAveragePending.

/**
 * This HEAVY method can serve stats/charts only, as it is not precise
 * The last block pending before payout is not being counted.
 *
 * @return average 'pending' increase per block
 */
public Long getAveragePending() {
    int cnt = 0;
    int rec = 0;
    Long pendings = 0L;
    Long prevPending = 0L;
    SQLiteDatabase db = this.getReadableDatabase();
    // Cursor cursor = db.rawQuery(selectQuery, null);
    Cursor cursor = db.query(DataBaseContract.Wallet_.TABLE_NAME, new String[] { DataBaseContract.Wallet_._ID, DataBaseContract.Wallet_.COLUMN_NAME_DTM, DataBaseContract.Wallet_.COLUMN_NAME_JSON }, null, // String[] selectionArgs
    null, null, // HAVING
    null, // ORDER BY
    DataBaseContract.Wallet_.COLUMN_NAME_DTM + " ASC");
    if (cursor.moveToFirst()) {
        Gson gson = builder.create();
        try {
            Wallet camp = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.Wallet_.COLUMN_NAME_JSON)), Wallet.class);
            prevPending = camp.getStats().getBalance().longValue();
            do {
                Wallet retrieved = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.Wallet_.COLUMN_NAME_JSON)), Wallet.class);
                rec++;
                Long curPending = retrieved.getStats().getBalance().longValue();
                if (curPending > prevPending) {
                    Log.d(TAG, "Block detected in history. Prev balance: " + prevPending + " current: " + curPending);
                    cnt++;
                    // pending increased
                    pendings += (curPending - prevPending);
                }
                prevPending = curPending;
            } while (cursor.moveToNext());
        } catch (Exception caz) {
            Log.e(TAG, "Cant read getAveragePending entry: " + caz.getMessage());
        }
    }
    Log.i(TAG, "SELECT DONE. PENDINGS HISTORY SIZE: " + cnt + " FROM RECORDS: " + rec);
    cursor.close();
    if (cnt == 0)
        return 0L;
    return pendings / cnt;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) Cursor(android.database.Cursor)

Example 3 with Wallet

use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.

the class PoolDbHelper method getLastWallets.

public LinkedMap<Date, Wallet> getLastWallets(int limit) {
    LinkedMap<Date, Wallet> ret = new LinkedMap<>();
    int cnt = 0;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(DataBaseContract.Wallet_.TABLE_NAME, new String[] { DataBaseContract.Wallet_._ID, DataBaseContract.Wallet_.COLUMN_NAME_DTM, DataBaseContract.Wallet_.COLUMN_NAME_JSON }, null, // String[] selectionArgs
    null, null, // HAVING
    null, DataBaseContract.Wallet_.COLUMN_NAME_DTM + " DESC", // 2 results to do compare
    "" + limit);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        Gson gson = builder.create();
        do {
            try {
                Wallet retrieved = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(DataBaseContract.Wallet_.COLUMN_NAME_JSON)), Wallet.class);
                cnt++;
                // Adding contact to list
                Date curDate = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(DataBaseContract.Wallet_.COLUMN_NAME_DTM)));
                ret.put(curDate, retrieved);
            } catch (Exception ce) {
                Log.e(TAG, "Cant read wallet entry: " + ce.getMessage());
            }
        } while (cursor.moveToNext());
    }
    Log.i(TAG, "SELECT DONE. WALLET HISTORY SIZE: " + cnt);
    cursor.close();
    return ret;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) Cursor(android.database.Cursor) LinkedMap(org.apache.commons.collections4.map.LinkedMap) Date(java.util.Date)

Example 4 with Wallet

use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.

the class MinersActivity method fetchMinerStats.

private void fetchMinerStats(final MinerDBRecord rec) {
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Utils.getWalletStatsUrl(PreferenceManager.getDefaultSharedPreferences(this)) + rec.getAddress(), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            // update known miners TABLE
            Log.d(Constants.TAG, 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);
            if (retrieved.getWorkersTotal() > (rec.getTopMiners() == null ? -1 : rec.getTopMiners()))
                rec.setTopMiners(retrieved.getWorkersTotal());
            if (retrieved.getCurrentHashrate() > (rec.getTopHr() == null ? -1 : rec.getTopHr()))
                rec.setTopHr(retrieved.getCurrentHashrate());
            rec.setPaid(retrieved.getStats().getPaid());
            try {
                // compute first paymt
                Payment pp = retrieved.getPayments().get(retrieved.getPayments().size() - 1);
                rec.setFirstSeen(pp.getTimestamp());
            } catch (Exception io) {
            // dont look back in anger
            }
            rec.setAvgHr(rec.getAvgHr() == null ? retrieved.getHashrate() : ((rec.getAvgHr() + retrieved.getHashrate()) / 2));
            // aggiorna UI
            rec.setLastSeen(retrieved.getStats().getLastShare());
            rec.setBlocksFound(retrieved.getStats().getBlocksFound());
            mDbHelper.updateMiner(rec);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e(Constants.TAG, "Error: " + error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) Payment(it.angelic.mpw.model.jsonpojos.wallet.Payment) JSONObject(org.json.JSONObject) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 5 with Wallet

use of it.angelic.mpw.model.jsonpojos.wallet.Wallet in project MPW by shineangelic.

the class Utils method fillEthereumStats.

public static void fillEthereumStats(Context ctx, PoolDbHelper mDbHelper, NavigationView navigationView, PoolEnum activePool, CurrencyEnum cur) {
    TextView ethPlaceholder = navigationView.findViewById(R.id.textView10);
    TextView ethC = navigationView.findViewById(R.id.textViewEthCourtesy);
    TextView textViewWhoPaid = navigationView.findViewById(R.id.textViewWhoPaid);
    SharedPreferences settings = ctx.getSharedPreferences("COINMARKETCAP", MODE_PRIVATE);
    try {
        String val = settings.getString("CURUSD", "---");
        String chg = settings.getString("CURCHG", "---");
        ethC.setText("Courtesy of coinmarketcap. Last update: " + MainActivity.yearFormatExtended.format(new Date(settings.getLong("CURTIMESTAMP", 0))));
        ethPlaceholder.setText(String.format(ctx.getString(R.string.currency_placeholder), cur.name(), val, Double.valueOf(chg)));
    } catch (Exception e) {
        Log.e(TAG, "Error eth currency panel:" + e.getMessage());
        ethC.setVisibility(View.INVISIBLE);
        ethPlaceholder.setVisibility(View.INVISIBLE);
    }
    try {
        Wallet last = mDbHelper.getLastWallet();
        textViewWhoPaid.setText(String.format(ctx.getString(R.string.paid_out_full), activePool.toString(), "" + Utils.formatCurrency(ctx, last.getStats().getPaid(), cur)));
    } catch (Exception e) {
        Log.e(TAG, "Errore aggiornamento eth paid panel: " + e.getMessage());
        textViewWhoPaid.setVisibility(View.INVISIBLE);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) TextView(android.widget.TextView) Date(java.util.Date) IllegalFormatException(java.util.IllegalFormatException)

Aggregations

Wallet (it.angelic.mpw.model.jsonpojos.wallet.Wallet)13 Gson (com.google.gson.Gson)10 Response (com.android.volley.Response)7 VolleyError (com.android.volley.VolleyError)7 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)7 JSONObject (org.json.JSONObject)7 Date (java.util.Date)5 GsonBuilder (com.google.gson.GsonBuilder)4 LinkedMap (org.apache.commons.collections4.map.LinkedMap)4 Cursor (android.database.Cursor)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Test (org.junit.Test)3 SharedPreferences (android.content.SharedPreferences)2 Calendar (java.util.Calendar)2 NotificationManager (android.app.NotificationManager)1 Context (android.content.Context)1 TextView (android.widget.TextView)1 JobParameters (com.firebase.jobdispatcher.JobParameters)1 MyDateTypeAdapter (it.angelic.mpw.model.MyDateTypeAdapter)1 MyTimeStampTypeAdapter (it.angelic.mpw.model.MyTimeStampTypeAdapter)1