wjc
2023-06-28 44822e2a916485e12d73e03926dc583abd561642
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.hdl.photovoltaic.other;
 
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.InverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
 
import java.lang.reflect.Type;
import java.util.List;
 
/**
 * 设备逻辑的界面
 */
public class HdlDeviceLogic {
    private static volatile HdlDeviceLogic sHdlDeviceLogic;
 
    /**
     * 获取当前对象
     *
     * @return HdlDeviceLogic
     */
    public static synchronized HdlDeviceLogic getInstance() {
        if (sHdlDeviceLogic == null) {
            synchronized (HdlDeviceLogic.class) {
                if (sHdlDeviceLogic == null) {
                    sHdlDeviceLogic = new HdlDeviceLogic();
                }
            }
 
        }
        return sHdlDeviceLogic;
    }
 
    /**
     * 添加逆变器
     *
     * @param inverterDeviceBean 逆变器对象
     * @param cloudCallBeak      回调
     */
    public void addInverterDevice(InverterDeviceBean inverterDeviceBean, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Add;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("mac", inverterDeviceBean.getDevice_mac());
        json.addProperty("spk", inverterDeviceBean.getGateway_type());
        json.addProperty("sid", inverterDeviceBean.getSid());
        json.addProperty("oid", inverterDeviceBean.getOid());
        json.addProperty("name", inverterDeviceBean.getDevice_name());
        // json.addProperty("zoneType", "password");//区域
 
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
    /**
     * 获取逆变器列表
     *
     * @param inverterDeviceBean 逆变器对象
     * @param cloudCallBeak      回调
     */
    public void getInverterDeviceList(InverterDeviceBean inverterDeviceBean, CloudCallBeak<CloudInverterDeviceBean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        // json.addProperty("zoneType", "password");//区域
 
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (httpResponsePack != null && httpResponsePack.getData() != null) {
                    Gson gson = new Gson();
                    String json = gson.toJson(httpResponsePack.getData());
                    CloudInverterDeviceBean loginUserRegionBean = new Gson().fromJson(json, CloudInverterDeviceBean.class);
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(loginUserRegionBean);
                    }
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
    /**
     * 删除逆变器
     *
     * @param deviceId      设备id
     * @param cloudCallBeak 回调
     */
    public void delInverterDevice(String deviceId, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Remove;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("deviceId", deviceId);
        // json.addProperty("zoneType", "password");//区域
 
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
    /**
     * 获取逆变器下挂设备列表
     *
     * @param parentOid     上级设备的Oid
     * @param cloudCallBeak 回调
     */
    public void getInverterDeviceChildDeviceList(String parentOid, CloudCallBeak<List<CloudInverterChildDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("parentOid", parentOid);
        // json.addProperty("zoneType", "password");//区域
 
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
                if (httpResponsePack != null && httpResponsePack.getData() != null) {
                    Gson gson = new Gson();
                    String json = gson.toJson(httpResponsePack.getData());
                    Type typeOfT = new TypeToken<List<CloudInverterChildDeviceBean>>() {
                    }.getType();
                    List<CloudInverterChildDeviceBean> list = gson.fromJson(json, typeOfT);
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                }
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
    /**
     * 全量更新OID
     *
     * @param oidList       oid列表
     * @param cloudCallBeak 回调
     */
    public void fullRenewalOid(List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("operationSource", "PROGRAM_ENERGY");//
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < oidList.size(); i++) {
            OidBean oidBean = oidList.get(i);
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("protocolType", oidBean.getProtocolType());
            jsonObject.addProperty("deviceType", oidBean.getDeviceType());
            jsonObject.addProperty("mac", oidBean.getMac());
            jsonObject.addProperty("oid", oidBean.getOid());
            jsonObject.addProperty("device_name", oidBean.getDevice_name());
            jsonObject.addProperty("device_model", oidBean.getDevice_model());
            jsonObject.addProperty("addresses", oidBean.getAddresses());
            jsonObject.addProperty("parentOid", oidBean.getParentOid());
            jsonArray.add(jsonObject);
 
        }
        json.add("devices", jsonArray);
        // json.addProperty("zoneType", "password");//区域
 
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
            @Override
            public void onSuccess(HttpResponsePack httpResponsePack) {
 
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
 
            }
 
            @Override
            public void onFailure(Exception exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
    }
 
}