package com.doumee.keyCabinet.ui.service;
|
|
|
import android.app.Service;
|
import android.content.Intent;
|
import android.os.CountDownTimer;
|
import android.os.IBinder;
|
|
import androidx.annotation.Nullable;
|
|
import com.doumee.keyCabinet.MApplication;
|
import com.doumee.keyCabinet.bean.DevConfigBean;
|
import com.doumee.keyCabinet.event.DevConfigEvent;
|
import com.doumee.keyCabinet.http.Apis;
|
import com.doumee.keyCabinet.http.param.BaseResponse;
|
import com.doumee.keyCabinet.http.param.DevLoginParam;
|
import com.doumee.keyCabinet.http.param.RequestBaseObject;
|
import com.doumee.keyCabinet.utils.LMobileInfo;
|
import com.doumee.keyCabinet.utils.OkHttpClientUtil;
|
import com.doumee.lib_coremodel.http.utils.GsonTools;
|
import com.doumee.lib_coremodel.util.SpUtil;
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yuyh.library.imgsel.utils.LogUtils;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.json.JSONException;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import timber.log.Timber;
|
|
public class HeartbeatService extends Service {
|
private CountDownTimer countTimer;
|
|
@Override
|
public void onCreate() {
|
//Log.i("Kathy","onCreate - Thread ID = " + Thread.currentThread().getId());
|
super.onCreate();
|
}
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
if(countTimer==null){
|
countTimer = new CountDownTimer(300000, 300000) {
|
@Override
|
public void onTick(long millisUntilFinished) {
|
//Timber.tag("CountTimer==>").d(millisUntilFinished+"");
|
}
|
|
@Override
|
public void onFinish() {
|
Timber.tag("CountTimer==>").d("心跳 onFinish");
|
try {
|
sendPost();
|
} catch (JSONException e) {
|
|
}
|
}
|
};
|
}
|
try {
|
sendPost();
|
} catch (JSONException e) {
|
|
}
|
return super.onStartCommand(intent, flags, startId);
|
}
|
|
@Nullable
|
@Override
|
public IBinder onBind(Intent intent) {
|
//Log.i("Kathy", "onBind - Thread ID = " + Thread.currentThread().getId());
|
return null;
|
}
|
|
@Override
|
public void onDestroy() {
|
//Log.i("Kathy", "onDestroy - Thread ID = " + Thread.currentThread().getId());
|
super.onDestroy();
|
if(countTimer!=null){
|
countTimer.cancel();
|
countTimer = null;
|
}
|
}
|
|
private void sendPost() throws JSONException {
|
String url = SpUtil.getString("base_url",Apis.HTTP)+Apis.HEAT_HTTP;
|
RequestBaseObject<DevLoginParam> request = new RequestBaseObject<>();
|
DevLoginParam param = new DevLoginParam();
|
param.setDevCode(LMobileInfo.getDeviceUniqueId());
|
request.setParam(param);
|
Gson gson = new Gson();
|
String post = gson.toJson(request);
|
if(MApplication.getConfigBean().getShopId()==null||
|
MApplication.getConfigBean().getGymId()==null){
|
if(countTimer!=null){
|
countTimer.start();
|
}
|
return;
|
}
|
Map<String, String> headMap = new HashMap<>();
|
headMap.put("shopId", MApplication.getConfigBean().getShopId());
|
headMap.put("gymId",MApplication.getConfigBean().getGymId());
|
headMap.put("Cookie",MApplication.getCookie());
|
if(countTimer!=null){
|
countTimer.start();
|
}
|
OkHttpClientUtil.asynPost(url, post, headMap, new OkHttpClientUtil.IOkHttpClientCallBack() {
|
@Override
|
public void onSuccessful(String retBody) {
|
LogUtils.d("http==>",retBody);
|
Timber.tag("CountTimer==>").d("心跳 onSuccessful");
|
BaseResponse<DevConfigBean> resp = GsonTools.changeGsonToBean(retBody,
|
new TypeToken<BaseResponse<DevConfigBean>>() {}.getType());
|
if(resp!=null&&resp.getRecord()!=null){
|
EventBus.getDefault().post(new DevConfigEvent(resp.getRecord()));
|
}
|
}
|
|
@Override
|
public void onError() {
|
Timber.tag("CountTimer==>").d("心跳 报错");
|
}
|
});
|
}
|
}
|