diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java index a7f7c3b..b570839 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java @@ -39,9 +39,10 @@ public class GetMarketHistory extends BaseGrapheneHandler { private Date start; private Date end; private WitnessResponseListener mListener; - + private WebSocket mWebsocket; private int currentId = 1; private int apiId = -1; + private int counter = 0; public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){ super(listener); @@ -53,8 +54,68 @@ public class GetMarketHistory extends BaseGrapheneHandler { this.mListener = listener; } + public Asset getBase() { + return base; + } + + public void setBase(Asset base) { + this.base = base; + } + + public Asset getQuote() { + return quote; + } + + public void setQuote(Asset quote) { + this.quote = quote; + } + + public long getBucket() { + return bucket; + } + + public void setBucket(long bucket) { + this.bucket = bucket; + } + + public Date getStart() { + return start; + } + + public void setStart(Date start) { + this.start = start; + } + + public Date getEnd() { + return end; + } + + public void setEnd(Date end) { + this.end = end; + } + + public int getCount(){ + return this.counter; + } + + public void disconnect(){ + if(mWebsocket != null && mWebsocket.isOpen()){ + mWebsocket.disconnect(); + } + } + + /** + * Retries the 'get_market_history' API call. + * Hopefully with different 'start' and 'stop' parameters. + */ + public void retry(){ + sendHistoricalMarketDataRequest(); + } + + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { + mWebsocket = websocket; ArrayList loginParams = new ArrayList<>(); loginParams.add(null); loginParams.add(null); @@ -93,17 +154,37 @@ public class GetMarketHistory extends BaseGrapheneHandler { ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_MARKET_HISTORY, params, RPC.VERSION, currentId); websocket.sendText(getRelativeAccountHistoryCall.toJsonString()); - }else if(baseResponse.id == GET_HISTORY_DATA){ + }else if(baseResponse.id >= GET_HISTORY_DATA){ GsonBuilder builder = new GsonBuilder(); Type MarketHistoryResponse = new TypeToken>>(){}.getType(); builder.registerTypeAdapter(BucketObject.class, new BucketObject.BucketDeserializer()); WitnessResponse> marketHistoryResponse = builder.create().fromJson(response, MarketHistoryResponse); mListener.onSuccess(marketHistoryResponse); - websocket.disconnect(); } } } + /** + * Actually sends the 'get_market_history' API call request. This method might be called multiple + * times during the life-cycle of this instance because we might not have gotten anything + * in the first requested interval. + */ + private void sendHistoricalMarketDataRequest(){ + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); + + ArrayList params = new ArrayList<>(); + params.add(this.base.getObjectId()); + params.add(this.quote.getObjectId()); + params.add(this.bucket); + params.add(dateFormat.format(this.start)); + params.add(dateFormat.format(this.end)); + + ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_MARKET_HISTORY, params, RPC.VERSION, currentId); + mWebsocket.sendText(getRelativeAccountHistoryCall.toJsonString()); + + counter++; + } + @Override public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { if(frame.isTextFrame()) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java index 285a78d..fda7b19 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -43,6 +43,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { private int limit; private int start; private WitnessResponseListener mListener; + private WebSocket mWebsocket; private int currentId = 1; private int apiId = -1; @@ -81,6 +82,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { + mWebsocket = websocket; ArrayList loginParams = new ArrayList<>(); loginParams.add(null); loginParams.add(null); @@ -108,26 +110,54 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { WitnessResponse witnessResponse = gson.fromJson(response, ApiIdResponse); apiId = witnessResponse.result.intValue(); - ArrayList params = new ArrayList<>(); - params.add(mUserAccount.toJsonString()); - params.add(this.stop); - params.add(this.limit); - params.add(this.start); - - ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY, params, RPC.VERSION, currentId); - websocket.sendText(getRelativeAccountHistoryCall.toJsonString()); - }else if(baseResponse.id == GET_HISTORY_DATA){ + sendRelativeAccountHistoryRequest(); + }else if(baseResponse.id >= GET_HISTORY_DATA){ Type RelativeAccountHistoryResponse = new TypeToken>>(){}.getType(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); WitnessResponse> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse); mListener.onSuccess(transfersResponse); - websocket.disconnect(); } } } + /** + * Sends the actual get_relative_account_history request. + */ + private void sendRelativeAccountHistoryRequest(){ + ArrayList params = new ArrayList<>(); + params.add(mUserAccount.toJsonString()); + params.add(this.stop); + params.add(this.limit); + params.add(this.start); + + ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY, params, RPC.VERSION, currentId); + mWebsocket.sendText(getRelativeAccountHistoryCall.toJsonString()); + } + + /** + * Updates the arguments and makes a new call to the get_relative_account_history API + * @param stop Sequence number of earliest operation + * @param limit Maximum number of operations to retrieve (must not exceed 100) + * @param start Sequence number of the most recent operation to retrieve + */ + public void retry(int stop, int limit, int start){ + this.stop = stop; + this.limit = limit; + this.start = start; + sendRelativeAccountHistoryRequest(); + } + + /** + * Disconnects the websocket + */ + public void disconnect(){ + if(mWebsocket != null && mWebsocket.isOpen()){ + mWebsocket.disconnect(); + } + } + @Override public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { if(frame.isTextFrame())