package com.hdl.photovoltaic.other; import android.text.TextUtils; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.hdlhttp.HxHttp; import com.hdl.linkpm.sdk.core.api.HDLCloudHomeApi; import com.hdl.linkpm.sdk.core.callback.IResponseCallBack; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.core.response.HDLResponse; import com.hdl.linkpm.sdk.device.bean.FunctionInfoBean; import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; import com.hdl.photovoltaic.bean.HttpResponsePack; import com.hdl.photovoltaic.bean.LocalResponse; import com.hdl.photovoltaic.config.AppConfigManage; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.internet.HttpClient; import com.hdl.photovoltaic.internet.TcpClient; import com.hdl.photovoltaic.internet.api.HttpApi; import com.hdl.photovoltaic.internet.api.TopicApi; import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.listener.LinkCallBack; import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean; import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.ui.bean.InverterDeviceBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.photovoltaic.uni.HDLUniMP; import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; import com.hdl.sdk.link.common.exception.HDLLinkCode; import com.hdl.sdk.link.common.exception.HDLLinkException; import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.bean.request.BaseLocalRequest; import com.hdl.sdk.link.core.bean.response.BaseLocalResponse; import com.hdl.sdk.link.core.callback.GatewayCallBack; import com.hdl.sdk.link.core.callback.HDLLinkCallBack; import com.hdl.sdk.link.core.config.HDLLinkConfig; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import io.reactivex.rxjava3.disposables.Disposable; /** * 设备逻辑 */ public class HdlDeviceLogic { private static volatile HdlDeviceLogic sHdlDeviceLogic; /** * 获取当前住宅的逆变器列表 */ public List getGatewayList() { List newList = new ArrayList<>(); List list = HDLLinkLocalGateway.getInstance().getGatewayList(); if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) { continue; } if (gatewayBean.getMaster().equals("true") && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) { newList.add(gatewayBean); } } } return newList; } /** * 获取当前对象 * * @return HdlDeviceLogic */ public static synchronized HdlDeviceLogic getInstance() { if (sHdlDeviceLogic == null) { synchronized (HdlDeviceLogic.class) { if (sHdlDeviceLogic == null) { sHdlDeviceLogic = new HdlDeviceLogic(); } } } return sHdlDeviceLogic; } /** * 添加逆变器到云端上 * * @param mac - * @param spk - * @param sid - * @param oid - * @param name - * @param cloudCallBeak - */ public void addInverterDeviceToCloud(String mac, String spk, String sid, String oid, String name, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Add; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); json.addProperty("mac", mac); json.addProperty("spk", spk); json.addProperty("sid", sid); json.addProperty("oid", oid); json.addProperty("name", name); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); //临时的逻辑,上传oid列表到云端 getGatewayOidList(mac, new LinkCallBack>() { @Override public void onSuccess(List obj) { if (obj == null) { return; } fullUpdateOid(obj, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { } @Override public void onFailure(HDLException e) { } }); } @Override public void onError(HDLLinkException e) { } }); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取云端逆变器列表 * * @param homeId 住宅id * @param cloudCallBeak 回调 */ public void getCloudInverterDeviceList(String homeId, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_List; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } } Type type = new TypeToken>() { }.getType(); List list = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 删除逆变器 * * @param deviceId 设备id * @param cloudCallBeak 回调 */ public void delInverterDevice(String deviceId, CloudCallBeak 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(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取逆变器下挂设备列表 * * @param parentOid 上级设备的Oid * @param cloudCallBeak 回调 */ public void getInverterDeviceChildDeviceList(String parentOid, CloudCallBeak> 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(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(jsonStr, typeOfT); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 全量更新OID * * @param oidList oid列表 * @param cloudCallBeak 回调update */ public void fullUpdateOid(List oidList, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Oid; 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.getDevice_mac()); 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(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 设置网关远程参数 * * @param mac 设备mac * @param linkCallBack 回调update */ public void setGatewayRemoteParam(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); json.addProperty("server_addr", AppConfigManage.getUserRegionUrl()); json.addProperty("local_secret", UserConfigManage.getInstance().getLocalSecret()); //解密负载数据(因为写密钥给网关一定明文,因为那时网关还没有密钥) TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); if (gatewayBean != null) { if (!TextUtils.isEmpty(UserConfigManage.getInstance().getLocalSecret())) { gatewayBean.setLocalEncrypt(true); } } if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 网关时间读取 * * @param mac 设备mac * @param linkCallBack 回调update */ public void getGatewayTime(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_TIME; TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 网关时间修改 * * @param mac 设备mac * @param linkCallBack 回调update */ public void editGatewayTime(String mac, JsonObject jsonObject, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_TIME_EDIT; // JsonObject json = new JsonObject(); // json.addProperty("master", "true"); // "objects": { // "date": "2020-08-15", // "time": "17:25:20" // } TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jsonObject, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 编辑网关参数 * * @param mac 设备mac * @param linkCallBack 回调update */ public void editGatewayParam(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_EDIT; JsonObject json = new JsonObject(); json.addProperty("master", "true"); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取网关详情信息 * * @param mac 网关mac * @param linkCallBack 回调 */ public void getGatewayInfo(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_INFO; TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (linkCallBack != null) { linkCallBack.onSuccess(null); } return; } Gson gson = new Gson(); GatewayBean gatewayBean = gson.fromJson(json, GatewayBean.class); if (linkCallBack != null) { linkCallBack.onSuccess(gatewayBean); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取网关oid列表 * * @param mac 网关mac * @param linkCallBack 回调 */ public void getGatewayOidList(String mac, LinkCallBack> linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_OID_LIST; TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (linkCallBack != null) { linkCallBack.onSuccess(null); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>>() { }.getType(); BaseLocalResponse> baseLocalResponse = gson.fromJson(json, typeOfT); if (linkCallBack == null) { return; } if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) { linkCallBack.onSuccess(new ArrayList<>()); } else { linkCallBack.onSuccess(baseLocalResponse.getObjects()); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取本地逆变器列表 * 注意:有外网以云端设备为准,本地存在,云端没有则删除;内网以本地为主,搜索多少个设备就显示多少个; * * @param cloudCallBeak - */ public void getLocalGatewayList(CloudCallBeak cloudCallBeak) { HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() { @Override public void onSuccess(List gatewayBeanList) { HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak>() { @Override public void onSuccess(List list) { if (list == null || list.size() == 0) { // //云端没有绑定逆变器,以本地为主; // HDLLinkLocalGateway.getInstance().getGatewayList().clear(); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } return; } //记录条数 AtomicInteger atomicInteger = new AtomicInteger(0); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak() { @Override public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { atomicInteger.set(atomicInteger.get() + 1); refreshGatewayCacheData(true, cloudInverterDeviceBean, deviceRemoteInfo); if (atomicInteger.get() == list.size()) { //收集删除逆变器sid List removeSidList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) { //本地有,云端没有,删除本地; removeSidList.add(gatewayBean.getSid()); } } } for (int i = 0; i < removeSidList.size(); i++) { //删除本地的逆变器 removeInverter(removeSidList.get(i)); } if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } } @Override public void onFailure(HDLException e) { atomicInteger.set(atomicInteger.get() + 1); //有可能最后一条是失败的 if (atomicInteger.get() == list.size()) { //收集删除逆变器sid List removeSidList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) { //本地有,云端没有,删除本地; removeSidList.add(gatewayBean.getSid()); } } } for (int i = 0; i < removeSidList.size(); i++) { //删除本地的逆变器 removeInverter(removeSidList.get(i)); } if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")"); } }); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } @Override public void onError(HDLLinkException e) { HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak>() { @Override public void onSuccess(List list) { //清楚本地网关列表 HDLLinkLocalGateway.getInstance().getGatewayList().clear(); if (list == null || list.size() == 0) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } return; } //记录条数 AtomicInteger atomicInteger = new AtomicInteger(0); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak() { @Override public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { atomicInteger.set(atomicInteger.get() + 1); refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo); if (atomicInteger.get() == list.size()) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } } @Override public void onFailure(HDLException e) { atomicInteger.set(atomicInteger.get() + 1); if (atomicInteger.get() == list.size()) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")"); } }); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } }); } /** * 获取设备远程通讯信息 * * @param homeId 住宅Id * @param spk spk * @param mac 设备mac * @param cloudCallBeak 回调 * @return - */ public void getDeviceRemoteInfo(String homeId, String spk, String mac, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_remoteInfo; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("spk", spk); json.addProperty("mac", mac); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } } Type type = new TypeToken() { }.getType(); DeviceRemoteInfo deviceRemoteInfo = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(deviceRemoteInfo); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 删除本地逆变器 * * @param sid 设备sid */ private void removeInverter(String sid) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid); if (gatewayBean != null) { HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean); } } /** * 搜索网关列表 * * @param gatewayCallBack - */ public void searchGateway(GatewayCallBack gatewayCallBack) { List spks = this.getGatewaySpk(); //网关搜索 HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack); } /** * 刷新逆变器缓存信息(包括设置mqtt秘钥) * 注: * 1:本地存在更新信息即可; * 2:云端有本地不存在,则虚拟一个逆变器对象添加在本地; * * @param deviceRemoteInfo 设备远程通讯信息 * @param cloudInverterDeviceBean 逆变器 */ private static void refreshGatewayCacheData(boolean isLocal, CloudInverterDeviceBean cloudInverterDeviceBean, DeviceRemoteInfo deviceRemoteInfo) { //本地查找逆变器 GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(cloudInverterDeviceBean.getOsn()); if (gatewayBean == null) { //不存在,虚拟一个逆变器对象; gatewayBean = new GatewayBean(); HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);//添加逆变器设备 } //更新属性 if (deviceRemoteInfo != null) { gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥 gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId } gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId());//住宅id gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态 gatewayBean.setDeviceId(cloudInverterDeviceBean.getDeviceId());//设置link设备id gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());//设置mac gatewayBean.setOid(cloudInverterDeviceBean.getOid());//设备oid gatewayBean.setSid(cloudInverterDeviceBean.getSid());//设备sid gatewayBean.setDevice_name(cloudInverterDeviceBean.getName());//设备名称 gatewayBean.setGatewayType(cloudInverterDeviceBean.getSpk());//设置spk gatewayBean.setLocalEncrypt(true); gatewayBean.setMaster("true"); gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态 gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号 gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//设备类型(产品二级分类名称 gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号 gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率 gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量 } /** * 获取网关spk列表 * * @return - */ private List getGatewaySpk() { List spks = new ArrayList<>(); spks.add(DEVICE_SPK); return spks; } public final String DEVICE_SPK = "energy.hdl_inverter"; }