weimingfei
7 天以前 4caffc77d00a9048ffd51eea9973f62888e13ba6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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("心跳 报错");
            }
        });
    }
}