package com.hdl.photovoltaic.other; import android.text.TextUtils; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.device.bean.DeviceOidInfoBean; import com.hdl.linkpm.sdk.ota.bean.CloudDeviceFirmwaresBean; import com.hdl.linkpm.sdk.ota.bean.CloudGatewayDriversBean; import com.hdl.linkpm.sdk.ota.bean.DeviceFirmwareBean; import com.hdl.linkpm.sdk.ota.bean.DownloadUrlBean; import com.hdl.linkpm.sdk.ota.bean.GatewayDriverBean; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.internet.HttpClient; import com.hdl.photovoltaic.internet.api.HttpApi; import com.hdl.photovoltaic.listener.CloudCallBeak; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * ota升级逻辑 */ public class HdlOtaLogic { private static volatile HdlOtaLogic sHdlOtaLogic; /** * 获取当前对象 * * @return HdlDeviceLogic */ public static synchronized HdlOtaLogic getInstance() { if (sHdlOtaLogic == null) { synchronized (HdlOtaLogic.class) { if (sHdlOtaLogic == null) { sHdlOtaLogic = new HdlOtaLogic(); } } } return sHdlOtaLogic; } //region -----固件升级--------- /** * 向云端获取逆变器oid列表 * 前提条件:要上传逆变器oid列表给云端 * * @param callBack - */ public void getCloudOidList(CloudCallBeak> callBack) { String requestUrl = HttpApi.POST_DEVICE_OID_LIST_GET; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new ArrayList<>()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端获取【当前设备固件】列表 * 前提条件:设备自动上报oid信息给云端 * * @param deviceOidId oid云端id * @param callBack - */ public void getCurrentDeviceFirmwares(String deviceOidId, CloudCallBeak> callBack) { String requestUrl = HttpApi.POST_OTA_GET_DeviceFirmwares; JsonObject json = new JsonObject(); json.addProperty("deviceOidId", deviceOidId); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new ArrayList<>()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端获取【设备新固件】列表 * 前提条件:要通过平台软件上传新固件 * * @param hardwareModel 硬件型号 * @param osImageId 系统镜像id * @param callBack - */ public void getNewDeviceFirmwares(String hardwareModel, String osImageId, CloudCallBeak> callBack) { String requestUrl = HttpApi.POST_OTA_GET_CloudDeviceFirmwares; JsonObject json = new JsonObject(); json.addProperty("hardwareModel", hardwareModel); json.addProperty("osImageId", osImageId); json.addProperty("protocolType", "ZIGBEE");//协议类型,可用值:BUSPRO,KNX,ZIGBEE,OTHER HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new ArrayList<>()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端发起【设备固件】升级OTA指令 * * @param deviceOidId 设备id * @param firmwareVersionId 固件版本id * @param callBack- */ public void upgradeDeviceFirmware(String deviceOidId, String firmwareVersionId, CloudCallBeak callBack) { String requestUrl = HttpApi.POST_OTA_DeviceFirmwareUpgrade; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); json.addProperty("deviceOidId", deviceOidId); json.addProperty("firmwareVersionId", firmwareVersionId); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (callBack != null) { callBack.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端获取【设备固件】升级包下载地址 * * @param firmwareVersionId 固件版本Id */ public void getDeviceFirmwareDownloadUrl(String firmwareVersionId, CloudCallBeak callBack) { String requestUrl = HttpApi.POST_OTA_GET_LinkDeviceFirmwareDownloadUrl; JsonObject json = new JsonObject(); json.addProperty("firmwareVersionId", firmwareVersionId); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new DownloadUrlBean()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken() { }.getType(); DownloadUrlBean downloadUrlBean = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(downloadUrlBean); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } //endregion //region -----驱动升级--------- /** * 向云端获取【当前设备驱动】列表 * 前提条件:设备自动上报oid信息给云端 * * @param oid 网关设备oid * @param callBack - */ public void getCurrentGatewayDrivers(String oid, CloudCallBeak> callBack) { String requestUrl = HttpApi.POST_OTA_GET_GatewayDrivers; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); json.addProperty("oid", oid); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new ArrayList<>()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端获取【设备新驱动】列表 * 前提条件:要通过平台软件上传新驱动 * * @param driveCode 驱动编号或驱动名称 * @param osImageId 驱动类型id * @param callBack - */ public void getNewGatewayDrivers(String driveCode, String osImageId, CloudCallBeak callBack) { String requestUrl = HttpApi.POST_OTA_GET_CloudGatewayDrivers; JsonObject json = new JsonObject(); json.addProperty("driveCode", driveCode); json.addProperty("osImageId", osImageId); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new CloudGatewayDriversBean()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken() { }.getType(); CloudGatewayDriversBean cloudGatewayDriversBean = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(cloudGatewayDriversBean); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 云端发起【设备驱动】升级OTA指令 * * @param oid 网关设备oid * @param driverVersionId 驱动版本id * @param callBack - */ public void upgradeGatewayDriver(String oid, String driverVersionId, CloudCallBeak callBack) { String requestUrl = HttpApi.POST_OTA_GatewayDriverUpgrade; JsonObject json = new JsonObject(); json.addProperty("homeId", UserConfigManage.getInstance().getHomeId()); json.addProperty("oid", oid); json.addProperty("driverVersionId", driverVersionId); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (callBack != null) { callBack.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } /** * 向云端获取【设备驱动】升级包下载地址 * * @param driverVersionId 驱动版本Id */ public void getDeviceDriverDownloadUrl(String driverVersionId, CloudCallBeak callBack) { String requestUrl = HttpApi.POST_OTA_GET_LinkDeviceDriverDownloadUrl; JsonObject json = new JsonObject(); json.addProperty("driverVersionId", driverVersionId); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (callBack != null) { callBack.onSuccess(new DownloadUrlBean()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken() { }.getType(); DownloadUrlBean downloadUrlBean = gson.fromJson(json, typeOfT); if (callBack != null) { callBack.onSuccess(downloadUrlBean); } } @Override public void onFailure(HDLException e) { if (callBack != null) { callBack.onFailure(e); } } }); } //endregion }