wxr
2022-10-28 bab36f8002d92e7125dfc40023f566266e3fdb38
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.common.openapi;
 
import android.os.Message;
 
import com.common.openapi.entity.DeviceDetailListData;
import com.common.openapi.entity.DeviceListData;
import com.google.gson.Gson;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessRunnable;
import com.mm.android.deviceaddmodule.mobilecommon.base.LCBusinessHandler;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.BusinessErrorTip;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.HandleMessageCode;
 
import java.util.ArrayList;
import java.util.List;
 
public class DeviceListService {
 
    public static final int pageSize=8;
 
 
    /**
     * 获取设备列表 1:开放平台添加的 2:乐橙App添加的
     *
     * @param deviceListData
     * @param getDeviceListCallBack
     */
    public void deviceBaseList(final DeviceListData deviceListData, final IGetDeviceInfoCallBack.IDeviceListCallBack getDeviceListCallBack) {
        final LCBusinessHandler handler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (getDeviceListCallBack == null) {
                    return;
                }
                if (msg.what == HandleMessageCode.HMC_SUCCESS) {
                    //成功
                    getDeviceListCallBack.deviceList((DeviceDetailListData.Response) msg.obj);
                } else {
                    //失败
                    getDeviceListCallBack.onError(BusinessErrorTip.throwError(msg));
                }
            }
        };
        new BusinessRunnable(handler) {
            @Override
            public void doBusiness() throws BusinessException {
                try {
                    dealDeviceDetailList(handler, deviceListData);
                } catch (BusinessException e) {
                    throw e;
                }
            }
        };
    }
 
    private void dealDeviceDetailList(LCBusinessHandler handler, DeviceListData deviceListData) throws BusinessException {
        if (deviceListData == null) {
            deviceListData = new DeviceListData();
        }
        //成功返回值
        DeviceDetailListData.Response result = null;
        //获取乐橙添加的设备
        DeviceDetailListData.Response deviceBaseDetailList = getDeviceDetailListFromCloud(false, deviceListData);
        //乐橙返回
        if (deviceBaseDetailList.data != null && deviceBaseDetailList.data.deviceList != null && deviceBaseDetailList.data.deviceList.size() > 0) {
            result = new DeviceDetailListData.Response();
            result.data = new DeviceDetailListData.ResponseData();
            result.data.count = deviceBaseDetailList.data.deviceList.size();
            result.data.deviceList = new ArrayList<>();
            for (DeviceDetailListData.ResponseData.DeviceListBean a : deviceBaseDetailList.data.deviceList) {
                DeviceDetailListData.ResponseData.DeviceListBean b = new DeviceDetailListData.ResponseData.DeviceListBean();
                Gson gson = new Gson();
                b = gson.fromJson(gson.toJson(a), DeviceDetailListData.ResponseData.DeviceListBean.class);
                b.deviceSource=2;
                result.data.deviceList.add(b);
            }
            result.baseBindId = deviceBaseDetailList.baseBindId;
            if (result.data.deviceList.size() >= pageSize) {
                //单次已经达到8条不再拉取
                handler.obtainMessage(HandleMessageCode.HMC_SUCCESS, result).sendToTarget();
                return;
            } else {
                //单次没有达到8条
                deviceListData.data.limit = (8 - result.data.deviceList.size()) + "";
            }
        }
        //获取开放平台添加的设备
        DeviceDetailListData.Response deviceOpenDetailList = getDeviceDetailListFromCloud(true, deviceListData);
        //开放平台返回
        if (deviceOpenDetailList.data != null && deviceOpenDetailList.data.deviceList != null && deviceOpenDetailList.data.deviceList.size() > 0) {
            if (result == null) {
                result = new DeviceDetailListData.Response();
                result.data = new DeviceDetailListData.ResponseData();
                result.data.count = deviceOpenDetailList.data.deviceList.size();
                result.data.deviceList = new ArrayList<>();
                for (DeviceDetailListData.ResponseData.DeviceListBean a : deviceOpenDetailList.data.deviceList) {
                    DeviceDetailListData.ResponseData.DeviceListBean b = new DeviceDetailListData.ResponseData.DeviceListBean();
                    Gson gson = new Gson();
                    b = gson.fromJson(gson.toJson(a), DeviceDetailListData.ResponseData.DeviceListBean.class);
                    b.deviceSource=1;
                    result.data.deviceList.add(b);
                }
            } else {
                result.data.count = deviceOpenDetailList.data.count + result.data.count;
                for (DeviceDetailListData.ResponseData.DeviceListBean a : deviceOpenDetailList.data.deviceList) {
                    DeviceDetailListData.ResponseData.DeviceListBean b = new DeviceDetailListData.ResponseData.DeviceListBean();
                    Gson gson = new Gson();
                    b = gson.fromJson(gson.toJson(a), DeviceDetailListData.ResponseData.DeviceListBean.class);
                    result.data.deviceList.add(b);
                }
            }
            result.openBindId = deviceOpenDetailList.openBindId;
        }
        if (result == null) {
            result = new DeviceDetailListData.Response();
        }
        handler.obtainMessage(HandleMessageCode.HMC_SUCCESS, result).sendToTarget();
    }
 
    private DeviceDetailListData.Response getDeviceDetailListFromCloud(boolean isOpenPlatform, DeviceListData deviceListDat) throws BusinessException {
        DeviceDetailListData.Response result = new DeviceDetailListData.Response();
        DeviceListData.Response deviceList = null;
        //获取到设备基本信息列表
        if (isOpenPlatform) {
            deviceList = DeviceInfoOpenApiManager.deviceOpenList(deviceListDat);
        } else {
            deviceList = DeviceInfoOpenApiManager.deviceBaseList(deviceListDat);
        }
        if (deviceList.data == null || deviceList.data.deviceList == null || deviceList.data.deviceList.size() == 0) {
            return result;
        }
        //获取设备详情列表request参数封装
        DeviceDetailListData deviceDetailListData = new DeviceDetailListData();
        List<DeviceDetailListData.RequestData.DeviceListBean> deviceListBeans = new ArrayList<>();
        for (DeviceListData.ResponseData.DeviceListElement deviceListElement : deviceList.data.deviceList) {
            DeviceDetailListData.RequestData.DeviceListBean deviceListBean = new DeviceDetailListData.RequestData.DeviceListBean();
            deviceListBean.deviceId = deviceListElement.deviceId;
            StringBuilder stringBuilder=new StringBuilder();
            if (deviceListElement.channels.size()>0){
                for (DeviceListData.ResponseData.DeviceListElement.ChannelsElement channelsElement:deviceListElement.channels){
                    stringBuilder.append(channelsElement.channelId).append(",");
                }
                deviceListBean.channelList=stringBuilder.substring(0,stringBuilder.length()-1);
            }
            deviceListBeans.add(deviceListBean);
        }
        deviceDetailListData.data.deviceList = deviceListBeans;
        //获取设备详情列表
        if (isOpenPlatform) {
            result = DeviceInfoOpenApiManager.deviceOpenDetailList(deviceDetailListData);
        } else {
            result = DeviceInfoOpenApiManager.deviceBaseDetailList(deviceDetailListData);
        }
        if (result == null) {
            result = new DeviceDetailListData.Response();
        }
        if (isOpenPlatform) {
            result.openBindId = Long.parseLong(deviceList.data.deviceList.get(deviceList.data.deviceList.size() - 1).bindId);
        } else {
            result.baseBindId = Long.parseLong(deviceList.data.deviceList.get(deviceList.data.deviceList.size() - 1).bindId);
        }
        return result;
    }
}