Skip to content

Commit

Permalink
添加额外反馈数据绑定监听
Browse files Browse the repository at this point in the history
指的是:串口直接发送,非客户端请求的数据
  • Loading branch information
sunxudong committed Apr 12, 2020
1 parent 9e037fe commit f55edea
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion seriallibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.github.Sheedon:SerialDispatcher:1.0.0'
implementation 'com.github.Sheedon:SerialDispatcher:1.0.2'
}
10 changes: 10 additions & 0 deletions seriallibrary/src/main/java/org/sheedon/serial/retrofit/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public interface Call<T> extends Cloneable {
*/
void enqueue(Callback<T> responseCallback);

/**
* 添加绑定额外
*/
void addBindCallback(Callback<T> callback);

/**
* 移除绑定
*/
void unBindCallback();

/**
* Cancels the request, if possible. Requests that are already complete cannot be canceled.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,45 @@ public void publishNotCallback() {
enqueue(null);
}

@Override
public void addBindCallback(final Callback<T> callback) {
if(callback == null){
return;
}

delegate.addBindCallback(new Callback<T>() {
@Override
public void onResponse(Call<T> call, final Response<T> response) {
callbackExecutor.execute(new Runnable() {
@Override
public void run() {
if (delegate.isCanceled()) {
// Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
} else {
callback.onResponse(ExecutorCallbackCall.this, response);
}
}
});
}

@Override
public void onFailure(Call<T> call, final Throwable t) {
callbackExecutor.execute(new Runnable() {
@Override
public void run() {
callback.onFailure(ExecutorCallbackCall.this, t);
}
});
}
});
}

@Override
public void unBindCallback() {
delegate.unBindCallback();
}

@Override
public void enqueue(final Callback callback) {
// checkNotNull(callback, "callback == null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* 串口Call,用于触发串口调度
*
* @Author: sheedon
* @Email: [email protected]
* @Date: 2020/2/23 22:27
Expand Down Expand Up @@ -74,6 +75,90 @@ public void publishNotCallback() {
enqueue(null);
}

@Override
public void addBindCallback(final Callback<T> callback) {
org.sheedon.serial.Call call;
Throwable failure;

synchronized (this) {

call = rawCall;
failure = creationFailure;
if (call == null && failure == null) {
try {
call = rawCall = createRawCall();
} catch (Throwable t) {
failure = creationFailure = t;
}
}
}

if (failure != null) {
dealWithCallback(callback, SerialCall.this, null, failure, false);
return;
}

if (canceled) {
call.cancel();
}

if (callback == null)
return;

call.addBindCallback(new org.sheedon.serial.Callback<org.sheedon.serial.Response>() {
@Override
public void onFailure(Throwable e) {
try {
dealWithCallback(callback, SerialCall.this, null, e, false);
} catch (Throwable t) {
t.printStackTrace();
}
}

@Override
public void onResponse(org.sheedon.serial.Response rawResponse) {
Response<T> response;
try {
response = parseResponse(rawResponse);
} catch (Throwable e) {
callFailure(e);
return;
}
callSuccess(response);
}

private void callFailure(Throwable e) {
try {
dealWithCallback(callback, SerialCall.this, null, e, false);
} catch (Throwable t) {
t.printStackTrace();
}
}

private void callSuccess(Response<T> response) {
try {
dealWithCallback(callback, SerialCall.this, response, null, true);
} catch (Throwable t) {
t.printStackTrace();
}
}
});
}

@Override
public void unBindCallback() {
org.sheedon.serial.Call call;

synchronized (this) {
call = rawCall;
}

if (call == null)
return;

call.removeBindCallback();
}

@Override
public void enqueue(final Callback callback) {

Expand Down Expand Up @@ -178,7 +263,7 @@ private org.sheedon.serial.Call createRawCall() throws IOException {
return call;
}

Response<T> parseResponse(org.sheedon.serial.Response rawResponse) throws IOException {
Response<T> parseResponse(org.sheedon.serial.Response rawResponse) {
ResponseBody rawBody = rawResponse.body();

try {
Expand Down

0 comments on commit f55edea

Please sign in to comment.