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 com.hdl.photovoltaic.uni.HDLUniMP; import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; import com.hdl.sdk.link.common.exception.HDLLinkException; import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.callback.GatewayCallBack; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import java.lang.reflect.Type; import java.util.ArrayList; 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 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 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 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> 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>() { }.getType(); List 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 oidList, CloudCallBeak 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); } } }); } public void searchGateway(GatewayCallBack gatewayCallBack) { List spks = new ArrayList<>(); spks.add("energy.hdl_inverter"); //网关搜索 HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack); } }