package com.hdl.photovoltaic.other; import android.Manifest; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; import android.os.IBinder; import android.text.TextUtils; import android.view.View; import android.widget.TextView; import androidx.core.app.ActivityCompat; import com.alibaba.fastjson.JSON; 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.GatewayDriverBean; import com.hdl.linkpm.sdk.user.HDLLinkPMUser; import com.hdl.linkpm.sdk.utils.HDLMD5Utils; import com.hdl.photovoltaic.HDLApp; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.bean.ModBusBean; import com.hdl.photovoltaic.config.AppConfigManage; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.listener.LinkCallBack; import com.hdl.photovoltaic.ui.bean.CUserInfo; import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean; import com.hdl.photovoltaic.ui.bean.DeviceTimeBean; import com.hdl.photovoltaic.ui.bean.Geolocation; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.photovoltaic.ui.me.AsRegardsActivity; import com.hdl.photovoltaic.ui.me.PersonalDataActivity; import com.hdl.photovoltaic.ui.me.SetActivity; import com.hdl.photovoltaic.ui.newC.MessageCenterList; import com.hdl.photovoltaic.ui.newC.PowerStationsListEdit; import com.hdl.photovoltaic.uni.HDLUniMP; import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; import com.hdl.photovoltaic.utils.NetworkUtils; import com.hdl.photovoltaic.utils.WifiUtils; import com.hdl.photovoltaic.widget.ConfirmationCancelDialog; import com.hdl.sdk.link.common.exception.HDLLinkCode; import com.hdl.sdk.link.common.exception.HDLLinkException; import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus; import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.callback.GatewayCallBack; import com.hdl.sdk.link.core.callback.ModbusCallBack; import com.hdl.sdk.link.core.connect.HDLModBusConnect; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import org.greenrobot.eventbus.EventBus; import org.json.JSONObject; import java.io.File; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import io.dcloud.feature.unimp.DCUniMPJSCallback; import okhttp3.ResponseBody; /** * uni小程序的逻辑 */ public class HdlUniLogic implements HDLUniMPSDKManager.IOnOtherUniMPEventCallBack { private static volatile HdlUniLogic sHdlUniLogic; /** * 获取当前对象 * * @return HdlAccountLogic */ public static synchronized HdlUniLogic getInstance() { if (sHdlUniLogic == null) { synchronized (HdlUniLogic.class) { if (sHdlUniLogic == null) { sHdlUniLogic = new HdlUniLogic(); } } } return sHdlUniLogic; } //region ******【原生】和【小程序】通讯的3个方法名 onOtherUniMPEventReceive();openUniMP();sendUni();****** /** * 原生接收到uni发来的数据 * * @param appId -uni小程序指定的appId * @param topic -uni小程序发来主题(大类) * @param data JSONObject com.alibaba.fastjson解析处理 * @param callback callback.invoke(JSONObject) 用com.alibaba.fastjson */ @Override public void onOtherUniMPEventReceive(String appId, String topic, Object data, DCUniMPJSCallback callback) { try { if (!HDLUniMP.UNI_APP_ID.equals(appId)) { return; } String type = getKeyValue("type", data);//小类 String logTag = getKeyValue("logTag", data);//标记uni指令 String mode_type = "大类->" + topic + "--->" + "小类->" + type;//用来打印的 HdlLogLogic.print(logTag + ":" + "uni--->发送--->" + mode_type + "\r\n" + data, true); if (HDLUniMP.UNI_EVENT_REPLY_USER_MODEL.equals(topic)) { //用户模块 switch (type) { //用户信息 case HDLUniMP.UNI_EVENT_REPLY_USER_INFO: { this.uniGetUserInfo(mode_type, data, callback); } break; //用户头像(旧平台逻辑是头像单独获取下载地址) case HDLUniMP.UNI_EVENT_REPLY_USER_IMAGE: { uniGetImageKey(mode_type, data, callback); } break; //用户本地缓存信息 case HDLUniMP.UNI_EVENT_REPLY_USER_LOCAL_INFO: { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("user_name", UserConfigManage.getInstance().getUserName()); jsonObject.addProperty("user_image_url", UserConfigManage.getInstance().getUserImageUrl()); jsonObject.addProperty("user_account", UserConfigManage.getInstance().getAccount()); uniCallbackData(mode_type, jsonObject, callback); } break; } } else if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(topic)) { //住宅模块 switch (type) { //创建电站 case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: { //EventBus事件分发 BaseEventBus baseEventBus = new BaseEventBus(); baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL); baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION); baseEventBus.setData(getKeyValue("data", data)); EventBus.getDefault().post(baseEventBus); } break; //读取详情 case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: { } break; //关闭详情页 case HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE: { //EventBus事件分发 BaseEventBus baseEventBus = new BaseEventBus(); baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE); baseEventBus.setData(getKeyValue("data", data)); EventBus.getDefault().post(baseEventBus); } break; //住宅【电站】编辑 case HDLUniMP.UNI_EVENT_REPLY_HOME_EDIT: { //EventBus事件分发 BaseEventBus baseEventBus = new BaseEventBus(); baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL); baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_EDIT); baseEventBus.setData(getKeyValue("data", data)); EventBus.getDefault().post(baseEventBus); } break; } if (callback != null) { uniCallbackData(type, null, callback); } } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(topic)) { //设备模块 switch (type) { //添加逆变器到云端 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: { this.uniAddInverterDeviceToCloud(mode_type, data, callback); } //添加多个逆变器到云端 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD_All: { this.uniAddAllInverterDeviceToCloud(mode_type, data, callback); } break; //删除云端逆变器 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_DEL: { this.uniDelInverterDevice(mode_type, data, callback); } break; //局域网搜索逆变器列表 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_NET_LIST: { this.uniSearchGateway(mode_type, callback); } break; //获取逆变器列表 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: { this.uniGetCurrentHomeLocalAndCloudGatewayList(mode_type, data, callback); } break; //modBus协议专用 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: { sendModBus(mode_type, data, callback); } break; //获取oid列表 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_OID: { this.uniGetInverterOidList(mode_type, data, callback); } break; //逆变器时间读取 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME: { this.uniGetInverterTime(mode_type, data, callback); } break; //逆变器时间编辑 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME_EDIT: { this.uniEditInverterTime(mode_type, data, callback); } break; //逆变器上传数据到云端 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_UPLOADING_DATA: { this.uniUploadDataToCloud(mode_type, data, callback); } break; //逆变器清空住宅id case HDLUniMP.UNI_EVENT_REPLY_DEVICE_CLEAR_DATA: { this.uniClearInverterHomeId(mode_type, data, callback); } break; //获取未处理/未读消息数量 case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MESSAGE_NUMBER: { this.uniGetMessageNumber(mode_type, data, callback); } break; } } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(topic)) { WifiUtils wifiUtils = WifiUtils.getInstance(); //wifi模块 switch (type) { //获取wifi列表 case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: { if (callback != null) { uniCallbackData(mode_type, wifiUtils.getScanResult(), callback); } } break; //当前wifi详情 case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: { if (callback != null) { uniCallbackData(mode_type, wifiUtils.getCurrentConnectWifiInfo(), callback); } } break; //wifi连接 case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: { } break; } } else if (HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL.equals(topic)) { //OTA升级模块 switch (type) { //向云端获取oid列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOUD_OID_LIST: { this.uniGetCloudOidList(mode_type, data, callback); } break; //当前设备固件列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LIST: { this.uniGetCurrentDeviceFirmwares(mode_type, data, callback); } break; //设备新固件列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_NEW_LIST: { this.uniGetNewDeviceFirmwares(mode_type, data, callback); } break; //设备本地固件列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LOCAL: { this.uniGetLocalFirmwares(mode_type, data, callback); } break; //设备固件下载 case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_DOWNLOAD: { this.uniDeviceFirmwareDownload(mode_type, data, callback); } break; //设备固件升级 case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_UPGRADE: { this.uniUpgradeDeviceFirmware(mode_type, data, callback); } break; //当前设备驱动列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LIST: { this.uniGetCurrentGatewayDrivers(mode_type, data, callback); } break; //设备新驱动列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_NEW: { this.uniGetNewGatewayDrivers(mode_type, data, callback); } break; //设备本地驱动列表 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LOCAL: { this.uniGetLocalDrivers(mode_type, data, callback); } break; //设备驱动下载 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_DOWNLOAD: { this.uniGatewayDriverDownload(mode_type, data, callback); } break; //设备驱动升级 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_UPGRADE: { this.uniUpgradeGatewayDriver(mode_type, data, callback); } break; //设备取消驱动,固件下载升级文件 case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_CANCEL_DOWNLOAD: { this.uniCancelDownloadingUpgradeFile(mode_type, data, callback); } break; //设备驱动,固件取消升级 case HDLUniMP.UNI_EVENT_REPLY_OTA_CANCEL_UPGRADE: { this.uniCancelUpgrade(mode_type, data, callback); } break; //打开服务 case HDLUniMP.UNI_EVENT_REPLY_OTA_OPEN_SERVICE: { this.uniOpenService(mode_type, data, callback); } break; //关闭服务 case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOSE_SERVICE: { this.uniCloseService(mode_type, data, callback); } break; } } else if (HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL.equals(topic)) { //其它模块 switch (type) { //获取温度单位 case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNIT: { uniCallbackData(mode_type, UserConfigManage.getInstance().getTemperature_unit(), callback); } break; //获取当前app语言 case HDLUniMP.UNI_EVENT_REPLY_OTHER_APP_LANGUAGE: { uniCallbackData(mode_type, UserConfigManage.getInstance().getCurrentAppLanguage(), callback); } break; //获取位置信息 case HDLUniMP.UNI_EVENT_REPLY_OTHER_LOCATION_INFO: { this.uniGetLocationInfo(mode_type, data, callback); } //专门收集uni日志 case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNI_LOG: { String customizeContentFormat = getKeyValue("customizeContentFormat", getKeyValue("data", data)); HdlLogLogic.print("uni--->log--->" + customizeContentFormat, true); } } } else if (HDLUniMP.UNI_EVENT_REPLY_MINE_MODEL.equals(topic)) { //我的模块 switch (type) { //资料编辑 case HDLUniMP.UNI_EVENT_REPLY_MINE_EDIT_USER_INFO: { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(HDLApp.getInstance(), PersonalDataActivity.class); HDLApp.getInstance().startActivity(intent); } break; //电站选择 case HDLUniMP.UNI_EVENT_REPLY_MINE_SELECTED_POWERSTATION: { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(HDLApp.getInstance(), PowerStationsListEdit.class); HDLApp.getInstance().startActivity(intent); } break; //消息中心 case HDLUniMP.UNI_EVENT_REPLY_MINE_MESSAGE_CENTER: { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(HDLApp.getInstance(), MessageCenterList.class); HDLApp.getInstance().startActivity(intent); } break; //关于 case HDLUniMP.UNI_EVENT_REPLY_MINE_ABOUT: { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(HDLApp.getInstance(), AsRegardsActivity.class); HDLApp.getInstance().startActivity(intent); } break; //设置 case HDLUniMP.UNI_EVENT_REPLY_MINE_SETTING: { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(HDLApp.getInstance(), SetActivity.class); HDLApp.getInstance().startActivity(intent); } break; } } } catch (Exception e) { HdlLogLogic.print("uni--->发送--->" + e.getMessage(), true); } } /** * 原生【打开】uni指定页面 * * @param path 打开路径 * @param jsonObject 附件数据(没有数据填null) */ public void openUniMP(String path, JSONObject jsonObject) { JSONObject json = this.createdJSONObject(jsonObject, true); HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this); HdlLogLogic.print("uni--->组装uni发送数据格式--->" + json, false); } /** * 原生【主动】向小程序发送通知事件 * 注意:需要提前小程序在运行才可成功 * * @param topic 主题大类(如:UNI_EVENT_REPLY_HOME_MODEL) * @param callBackBaseBean 自定义实体类 */ public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) { try { HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean)); } catch (Exception e) { HdlLogLogic.print("uni--->原生主动向小程序发送通知事件--->" + e.getMessage(), false); } } /** * 检测是否当前页面注册的callback,是的话则移除 */ public void checkRemoveOtherUniMPEventCallBack() { HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this); } //endregion //region ******uni接口方法****** /** * 获取位置信息,包括经纬度 */ private void uniGetLocationInfo(String type, Object data, DCUniMPJSCallback callback) { try { // 初始化LocationManager对象 LocationManager locationManager = (LocationManager) HDLApp.getInstance().getSystemService(Context.LOCATION_SERVICE); Geolocation geolocation = new Geolocation(); //INTERNET 允许使用网络 //ACCESS_FINE_LOCATION 允许使用GPS定位 //ACCESS_COARSE_LOCATION 允许使用WIFI热点或基站来获取粗略的定位 if (ActivityCompat.checkSelfPermission(HDLApp.getInstance(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(HDLApp.getInstance(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { geolocation.setResult(-2); geolocation.setDes(HDLApp.getInstance().getString(R.string.exception_no_location_information_permission)); uniCallbackData(type, geolocation, callback); return; } // 判断设备是否支持定位功能 if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { geolocation.setResult(-1); geolocation.setDes(HDLApp.getInstance().getString(R.string.exception_the_location_function_is_not_enabled)); //todo 提示用户打开定位功能或者引导用户去系统设置页面打开定位功能 uniCallbackData(type, geolocation, callback); return; } Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { geolocation.setResult(-3); geolocation.setDes(HDLApp.getInstance().getString(R.string.exception_unable_location_move10_meters_later)); uniCallbackData(type, geolocation, callback); return; } geolocation.setResult(0); geolocation.setDes(HDLApp.getInstance().getString(R.string.succeed)); BigDecimal latitude = BigDecimal.valueOf(location.getLatitude());//经度 BigDecimal longitude = BigDecimal.valueOf(location.getLongitude());//纬度 double lat = latitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue(); double lon = longitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue(); geolocation.setLatitude(lat);//经度 geolocation.setLongitude(lon);//纬度 uniCallbackData(type, geolocation, callback); } catch (Exception e) { Geolocation geolocation = new Geolocation(); geolocation.setResult(-4); geolocation.setDes(e.getMessage()); uniCallbackData(type, geolocation, callback); } } /** * 向云端获取逆变器oid列表 * 前提条件:要上传逆变器oid列表给云端 * * @param callback - */ private void uniGetCloudOidList(String type, Object data, DCUniMPJSCallback callback) { HdlOtaLogic.getInstance().getCloudOidList(new CloudCallBeak>() { @Override public void onSuccess(List obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 向云端获取【当前设备固件】列表 * 前提条件:设备自动上报oid信息给云端 */ private void uniGetCurrentDeviceFirmwares(String type, Object data, DCUniMPJSCallback callback) { String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id HdlOtaLogic.getInstance().getCurrentDeviceFirmwares(deviceOidId, new CloudCallBeak>() { @Override public void onSuccess(List obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 向云端获取【设备新固件】列表 * 前提条件:要通过平台软件上传新固件 */ private void uniGetNewDeviceFirmwares(String type, Object data, DCUniMPJSCallback callback) { String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//硬件型号 String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//系统镜像id HdlOtaLogic.getInstance().getNewDeviceFirmwares(hardwareModel, osImageId, new CloudCallBeak>() { @Override public void onSuccess(List obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取【设备本地固件】列表 */ private void uniGetLocalFirmwares(String type, Object data, DCUniMPJSCallback callback) { try { // String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//硬件型号 String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//系统镜像id // String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//固件版本id // String oid = getKeyValue("oid", getKeyValue("data", data));//固件oid List list = new ArrayList<>(); String driverFileList = HdlFileLogic.getInstance().getFirmwareRootPath(); File file = new File(driverFileList); if (file.list() != null) { for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) { String path = Objects.requireNonNull(file.list())[i]; if (!path.contains(osImageId)) { continue; } String[] ary = path.split("/"); String[] driverInfoAry = ary[ary.length - 1].split("_"); DeviceFirmwareBean deviceFirmwareBean = new DeviceFirmwareBean(); deviceFirmwareBean.setOid(driverInfoAry[0]); deviceFirmwareBean.setImageId(driverInfoAry[1]); deviceFirmwareBean.setVersion(driverInfoAry[2].replace(".zip", "")); deviceFirmwareBean.setLocalUrl(HdlFileLogic.getInstance().getFirmwarePathFileName(deviceFirmwareBean.getOid(), osImageId, deviceFirmwareBean.getVersion())); list.add(deviceFirmwareBean); } } if (callback != null) { this.uniCallbackData(type, list, callback); } } catch (Exception ignored) { } } /** * 向云端发起【设备固件下载】指令 */ private void uniDeviceFirmwareDownload(String type, Object data, DCUniMPJSCallback callback) { // String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id String oid = getKeyValue("oid", getKeyValue("data", data));//该固件的oid String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id String version = getKeyValue("version", getKeyValue("data", data));//固件版本 String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地固件文件路径 String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址 String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5 HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(url, new CloudCallBeak() { @Override public void onSuccess(ResponseBody responseBody) { HdlThreadLogic.runSubThread(new Runnable() { @Override public void run() { String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version); //不在子线程读流会卡死主线程 boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(firmwarePathFileName, responseBody, md5, HdlOtaLogic.firmware_type); if (isBoolean) { HdlFileLogic.getInstance().deleteFile(localUrl);//下载成功,删除旧固件文件; } else { HdlFileLogic.getInstance().deleteFile(firmwarePathFileName);//下载失败,删除不完整固件文件; } if (isBoolean) { uniCallbackData(type, null, 0, "写入新驱动文件到内存成功", callback); } else { uniCallbackData(type, null, -2, "下载固件升级文件失败", callback); } } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 向云端发起【设备固件】升级OTA指令 */ private void uniUpgradeDeviceFirmware(String type, Object data, DCUniMPJSCallback callback) { String homeId = UserConfigManage.getInstance().getHomeId(); String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket) String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id String oid = getKeyValue("oid", getKeyValue("data", data));//升级固件的oid String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//固件版本id String version = getKeyValue("version", getKeyValue("data", data));//固件版本 // String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//云端上文件MD5 String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id String module = "FW#" + imageId;//模块信息(HW:硬件) //升级本地优先->云端升级 HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak() { @Override public void onSuccess(Boolean b) { if (b) { HdlLogLogic.print("本地升级--->", true); //本地 //1,建立本地服务; //2,告诉网关手机ip和端口; //实例化 获取ip 地址 HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //本地升级固件文件路径 String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version); byte[] data = HdlFileLogic.getInstance().readFileByte(firmwarePathFileName); if (data == null || data.length == 0) { uniCallbackData(type, null, -2, "本地找不到升级固件文件,请下载好固件文件,再重新升级.", callback); return; } String md5 = HDLMD5Utils.encodeMD5(data);//网关固件需要 //升级固件文件地址 String upgradeFileLocalPathUrl = "http://" + NetworkUtils.getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + firmwarePathFileName; //通知给网关升级固件文件地址等信息 HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, oid, module, version, data.length + "", upgradeFileLocalPathUrl, md5, new LinkCallBack() { @Override public void onSuccess(String obj) { HdlLogLogic.print("通知给网关升级固件文件地址成功.", true); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, -2, "通知给网关升级固件文件地址失败,无法升级,", callback); } }); } @Override public void onServiceDisconnected(ComponentName name) { uniCallbackData(type, null, -2, "本地服务有异常失败,无法升级,", callback); } }); } else { HdlLogLogic.print("在线升级--->", true); //远程升级需要检测在逆变器有没有连接上云 HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak() { @Override public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) { //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义) if (cloudInverterDeviceBean == null) { uniCallbackData(type, null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback); return; } if (cloudInverterDeviceBean.getDeviceStatus() != 3) { uniCallbackData(type, null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback); return; } //4:运行 HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } } @Override public void onFailure(HDLException e) { HdlLogLogic.print("在线升级--->", true); //远程升级需要检测在逆变器有没有连接上云 HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak() { @Override public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) { //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义) if (cloudInverterDeviceBean == null) { uniCallbackData(type, null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback); return; } if (cloudInverterDeviceBean.getDeviceStatus() != 3) { uniCallbackData(type, null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback); return; } //4:运行 HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } }); } /** * 向云端获取【当前设备驱动】列表 * 前提条件:设备自动上报oid信息给云端 */ private void uniGetCurrentGatewayDrivers(String type, Object data, DCUniMPJSCallback callback) { String deviceOid = getKeyValue("oid", getKeyValue("data", data)); HdlOtaLogic.getInstance().getCurrentGatewayDrivers(deviceOid, new CloudCallBeak>() { @Override public void onSuccess(List obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 向云端获取【设备新驱动】列表 * 前提条件:要通过平台软件上传新驱动 */ private void uniGetNewGatewayDrivers(String type, Object data, DCUniMPJSCallback callback) { String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//驱动编号或驱动名称 String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//驱动类型id HdlOtaLogic.getInstance().getNewGatewayDrivers(driveCode, osImageId, new CloudCallBeak() { @Override public void onSuccess(CloudGatewayDriversBean obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取【设备本地驱动】列表 */ private void uniGetLocalDrivers(String type, Object data, DCUniMPJSCallback callback) { try { String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//驱动编号或驱动名称 // String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//驱动类型id // String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动类型id List list = new ArrayList<>(); String driverFileList = HdlFileLogic.getInstance().getDriveRootPath(); File file = new File(driverFileList); if (file.list() != null) { for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) { String fileName = Objects.requireNonNull(file.list())[i]; if (!fileName.contains(driveCode)) { continue; } String[] driverInfoAry = fileName.split("_"); GatewayDriverBean gatewayDriverBean = new GatewayDriverBean(); gatewayDriverBean.setDriverCode(driverInfoAry[0]); gatewayDriverBean.setVersion(driverInfoAry[1].replace(".zip", "")); gatewayDriverBean.setLocalUrl(HdlFileLogic.getInstance().getDrivePathFileName(gatewayDriverBean.getDriverCode(), gatewayDriverBean.getVersion())); list.add(gatewayDriverBean); } } if (callback != null) { this.uniCallbackData(type, list, callback); } } catch (Exception exception) { this.uniCallbackData(type, new ArrayList<>(), callback); } } /** * 向云端发起【设备驱动下载】指令 */ private void uniGatewayDriverDownload(String type, Object data, DCUniMPJSCallback callback) { // String deviceOid = getKeyValue("oid", getKeyValue("data", data));//逆变器设备oid String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//驱动编码 // String imageId = getKeyValue("imageId", getKeyValue("data", data));//驱动镜像id String version = getKeyValue("version", getKeyValue("data", data));//驱动版本 // String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地驱动文件路径 String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址 String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5 HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(url, new CloudCallBeak() { @Override public void onSuccess(ResponseBody responseBody) { HdlThreadLogic.runSubThread(new Runnable() { @Override public void run() { String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); //不在子线程读流会卡死主线程 boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(drivePathFileName, responseBody, md5, HdlOtaLogic.driver_type); if (isBoolean) { HdlFileLogic.getInstance().deleteFile(localUrl);//下载成功,删除旧驱动文件; } else { HdlFileLogic.getInstance().deleteFile(drivePathFileName);//下载失败,删除不完整驱动文件; } if (isBoolean) { uniCallbackData(type, null, 0, "写入新驱动文件到内存成功", callback); } else { uniCallbackData(type, null, -2, "下载驱动升级文件失败", callback); } } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 向云端发起【设备驱动】升级OTA指令 */ private void uniUpgradeGatewayDriver(String type, Object data, DCUniMPJSCallback callback) { String homeId = UserConfigManage.getInstance().getHomeId(); String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket) String deviceOid = getKeyValue("oid", getKeyValue("data", data));//逆变器设备oid String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id String version = getKeyValue("version", getKeyValue("data", data));//驱动版本号 String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//驱动编码 String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id String module = driverCode + "#" + imageId; //升级本地优先->云端升级 HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak() { @Override public void onSuccess(Boolean b) { if (b) { HdlLogLogic.print("本地升级--->", true); //本地 //1,建立本地服务; // 2,告诉网关手机ip和端口; //实例化 获取ip 地址 HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //本地升级驱动文件路径 String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); byte[] data = HdlFileLogic.getInstance().readFileByte(drivePathFileName); if (data == null || data.length == 0) { uniCallbackData(type, null, -2, "本地找不到升级驱动文件,请下载好驱动文件,再重新升级.", callback); return; } String md5 = HDLMD5Utils.encodeMD5(data);//网关驱动需要 //升级驱动文件地址 String upgradeFileLocalPathUrl = "http://" + NetworkUtils.getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + drivePathFileName; //通知给网关升级驱动文件地址等信息 HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, deviceOid, module, version, data.length + "", upgradeFileLocalPathUrl, md5, new LinkCallBack() { @Override public void onSuccess(String obj) { HdlLogLogic.print("通知给网关升级驱动文件地址成功.", true); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, -2, "通知给网关升级驱动文件地址失败,无法升级,", callback); } }); } @Override public void onServiceDisconnected(ComponentName name) { uniCallbackData(type, null, -2, "本地服务有异常失败,无法升级,", callback); } }); } else { HdlLogLogic.print("在线升级--->", true); //远程升级需要【检测】在逆变器有没有连接上云 HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak() { @Override public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) { //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义) if (cloudInverterDeviceBean == null) { uniCallbackData(type, null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback); return; } if (cloudInverterDeviceBean.getDeviceStatus() != 3) { uniCallbackData(type, null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback); return; } //4:运行 HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } } @Override public void onFailure(HDLException e) { HdlLogLogic.print("在线升级--->", true); //远程升级需要【检测】在逆变器有没有连接上云 HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak() { @Override public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) { //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义) if (cloudInverterDeviceBean == null) { uniCallbackData(type, null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback); return; } if (cloudInverterDeviceBean.getDeviceStatus() != 3) { uniCallbackData(type, null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback); return; } //4:运行 HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, obj, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } }); } /** * 取消下载【驱动,固件】升级文件 */ private void uniCancelDownloadingUpgradeFile(String type, Object data, DCUniMPJSCallback callback) { HdlOtaLogic.getInstance().stopDownloadUpgradeFile(); uniCallbackData(type, null, 0, "取消下载成功.", callback); } /** * 取消【驱动,固件】升级 */ private void uniCancelUpgrade(String type, Object data, DCUniMPJSCallback callback) { HdlOtaLogic.getInstance().cancelUpgrade("", new LinkCallBack() { @Override public void onSuccess(Boolean obj) { } @Override public void onError(HDLLinkException e) { } }); } /** * 开启服务 */ private void uniOpenService(String type, Object data, DCUniMPJSCallback callback) { HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { HdlOtaLogic.getInstance().mServiceStart = false; } }); } /** * 关闭服务 */ private void uniCloseService(String type, Object data, DCUniMPJSCallback callback) { HdlOtaLogic.getInstance().unLocalService(); } /** * @param deviceStatus 逆变器连接云端状态值( 1:连接中,2:故障,3:运行,4:离线) * @return 返回文本 */ public String getDeviceStatusString(int deviceStatus) { switch (deviceStatus) { case 1: { return HDLApp.getInstance().getString(R.string.my_power_station_connecting); } case 2: { return HDLApp.getInstance().getString(R.string.my_power_station_malfunction); } case 4: { return HDLApp.getInstance().getString(R.string.my_power_station_off_line); } default: return ""; } } /** * 获取用户信息 * * @param data uni数据 * @param callback uni回调 */ private void uniGetUserInfo(String type, Object data, DCUniMPJSCallback callback) { HdlAccountLogic.getInstance().getMemberInfo_C(new CloudCallBeak() { @Override public void onSuccess(CUserInfo cUserInfo) { if (cUserInfo == null) { return; } UserConfigManage.getInstance().setBingPhone(cUserInfo.getMemberPhone()); UserConfigManage.getInstance().setBingEmail(cUserInfo.getMemberEmail()); UserConfigManage.getInstance().Save(); //获取用户头像路径 if (cUserInfo.getMemberHeadIcon().contains("http")) { UserConfigManage.getInstance().setUserImageUrl(cUserInfo.getMemberHeadIcon()); //更新用户头像 uniCallbackData(type, cUserInfo, callback); } else { HdlAccountLogic.getInstance().getImageKey_C(cUserInfo.getMemberHeadIcon(), new CloudCallBeak() { @Override public void onSuccess(String url) { cUserInfo.setMemberHeadIcon(url); UserConfigManage.getInstance().setUserImageUrl(cUserInfo.getMemberHeadIcon()); uniCallbackData(type, cUserInfo, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, cUserInfo, callback); } }); } } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取用户头像 * * @param data uni数据 * @param callback uni回调 */ private void uniGetImageKey(String type, Object data, DCUniMPJSCallback callback) { String imageKey = getKeyValue("imageKey", getKeyValue("data", data)); HdlAccountLogic.getInstance().getImageKey_C(imageKey, new CloudCallBeak() { @Override public void onSuccess(String url) { uniCallbackData(type, url, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 逆变器清空住宅id * * @param data uni数据 * @param callback uni回调 */ private void uniClearInverterHomeId(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); HdlDeviceLogic.getInstance().clearInverterHomeId(mac, new LinkCallBack() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, null, callback); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取未处理/未读消息数量 * * @param data uni数据 * @param callback uni回调 */ private void uniGetMessageNumber(String type, Object data, DCUniMPJSCallback callback) { String homeId = getKeyValue("homeId", getKeyValue("data", data)); HdlMessageLogic.getInstance().getMessageUntreatedCount(homeId, new CloudCallBeak() { @Override public void onSuccess(String data) { uniCallbackData(type, data, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 逆变器【上传数据】到云端 * * @param data uni数据 * @param callback uni回调 */ private void uniUploadDataToCloud(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); List list = new ArrayList<>(); GatewayBean gatewayBean = new GatewayBean(); gatewayBean.setDevice_mac(mac); list.add(gatewayBean); HdlDeviceLogic.getInstance().uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null); } /** * 编辑逆变器时间 * * @param data uni数据 * @param callback uni回调 */ private void uniEditInverterTime(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); String date = getKeyValue("date", getKeyValue("data", data)); String time = getKeyValue("time", getKeyValue("data", data)); String timezone = getKeyValue("timezone", getKeyValue("data", data)); HdlDeviceLogic.getInstance().editGatewayTime(mac, date, time, timezone, new LinkCallBack() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, true, callback); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取逆变器时间 * * @param data uni数据 * @param callback uni回调 */ private void uniGetInverterTime(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); HdlDeviceLogic.getInstance().getGatewayTime(mac, new LinkCallBack() { @Override public void onSuccess(DeviceTimeBean deviceTimeBean) { if (callback != null) { uniCallbackData(type, deviceTimeBean, callback); } } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取oid列表 * * @param data uni数据 * @param callback uni回调 */ private void uniGetInverterOidList(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); HdlDeviceLogic.getInstance().getInverterOidList(mac, new LinkCallBack>() { @Override public void onSuccess(List list) { if (callback != null) { uniCallbackData(type, list, callback); } } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 逆变器添加到云端上 * * @param data uni数据 * @param callback uni回调 */ private void uniAddInverterDeviceToCloud(String type, Object data, DCUniMPJSCallback callback) { String mac = getKeyValue("mac", getKeyValue("data", data)); String homeId = UserConfigManage.getInstance().getHomeId(); HdlDeviceLogic.getInstance().setGatewayRemoteParam(homeId, mac, new LinkCallBack() { @Override public void onSuccess(Boolean obj) { HdlDeviceLogic.getInstance().editGatewayParam(mac, new LinkCallBack() { @Override public void onSuccess(Boolean obj) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); if (gatewayBean == null) { uniCallbackData(type, null, -100, HDLApp.getInstance().getString(R.string.The_gateway_cannot_be_found_locally), callback); return; } HdlDeviceLogic.getInstance().addInverterDeviceToCloud(homeId, mac, gatewayBean.getGatewayType(), gatewayBean.getSid(), gatewayBean.getOid(), gatewayBean.getDevice_name(), new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, null, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 多个逆变器添加到云端上 * * @param data uni数据 * @param callback uni回调 */ private void uniAddAllInverterDeviceToCloud(String type, Object data, DCUniMPJSCallback callback) { // com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray(data); try { String devices = getKeyValue("devices", getKeyValue("data", data)); String homeId = UserConfigManage.getInstance().getHomeId(); //getKeyValue("homeId", getKeyValue("data", data)); Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(devices, typeOfT); if (list == null || list.size() == 0) { uniCallbackData(type, null, -100, HDLApp.getInstance().getString(R.string.The_gateway_cannot_be_found_locally), callback); return; } //记录条数 AtomicInteger atomicInteger = new AtomicInteger(0); final boolean[] is_boolean = {false}; for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); HdlDeviceLogic.getInstance().setGatewayRemoteParam(homeId, gatewayBean.getDevice_mac(), new LinkCallBack() { @Override public void onSuccess(Boolean obj) { HdlDeviceLogic.getInstance().editGatewayParam(gatewayBean.getDevice_mac(), new LinkCallBack() { @Override public void onSuccess(Boolean obj) { atomicInteger.set(atomicInteger.get() + 1); //最后一条才做添加逆变器逻辑处理 if (atomicInteger.get() == list.size()) { HdlDeviceLogic.getInstance().addAllInverterDeviceToCloud(homeId, list, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, null, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); is_boolean[0] = true; } }); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); is_boolean[0] = true; } }); if (is_boolean[0]) { //只有中间有一条指令设置失败,默认全部失败; break; } } } catch (Exception ignored) { } } /** * 删除云端上逆变器 * * @param data uni数据 * @param callback uni回调 */ private void uniDelInverterDevice(String type, Object data, DCUniMPJSCallback callback) { String deviceId = getKeyValue("deviceId", getKeyValue("data", data)); String homeId = UserConfigManage.getInstance().getHomeId(); HdlDeviceLogic.getInstance().delInverterDevice(homeId, deviceId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { uniCallbackData(type, null, callback); } @Override public void onFailure(HDLException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 局域网搜索逆变器列表 * * @param callback uni回调 */ private void uniSearchGateway(String type, DCUniMPJSCallback callback) { HdlDeviceLogic.getInstance().searchAllNetworkGateway(new GatewayCallBack() { @Override public void onSuccess(List gatewayBeanList) { uniCallbackData(type, gatewayBeanList, callback); } @Override public void onError(HDLLinkException e) { //发送失败 uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } }); } /** * 获取逆变器列表 * * @param callback uni回调 */ private void uniGetCurrentHomeLocalAndCloudGatewayList(String type, Object data, DCUniMPJSCallback callback) { String homeId = getKeyValue("homeId", getKeyValue("data", data)); HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List list) { if (list == null || list.size() == 0) { if (callback != null) { uniCallbackData(type, list, callback); } return; } if (callback != null) { uniCallbackData(type, list, callback); } //EventBus事件分发,进入住宅开始订阅主题 BaseEventBus baseEventBus = new BaseEventBus(); baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST); baseEventBus.setData(list); EventBus.getDefault().post(baseEventBus); //进来住宅详情都要上传一次oid列表到云端; HdlDeviceLogic.getInstance().uploadDataToCloud(homeId, list, null); } @Override public void onFailure(HDLException e) { if (callback != null) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); } } }); } /** * 发送modbus协议数据 * 透传协议 * 下发主题:/user/${gw_id}/custom/native/${driver}/down; * 逆变器回复主题:/user/${gw_id}/custom/native/${driver}/down_reply; * Modbus ECU协议:事件ID(2个byte)->协议[固定:0,0](2个byte)->长度(2byte)->标识符[oid的addresses值](4个byte)->功能码(1个byte)->负载数据(N个byte); * 长度(2个byte)=标识符(4个byte)+功能码(1个byte)+负载数据(N个byte); * 负载数据=寄存器地址(2个byte)+寄存器长度(2个byte)+内容长度(1个byte)+内容数据(N个byte)【注意:单个写入寄存器-->去掉<寄存器长度>和<内容长度>】; * 寄存器长度=(内容数据/2); * 例子:new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01}; * * @param data modbus数据 * @param callback 回调 */ private void sendModBus(String type, Object data, DCUniMPJSCallback callback) { String tempData = getKeyValue("data", data); if (TextUtils.isEmpty(tempData)) { uniCallbackData(type, null, -2, "data内容为空", callback); //HdlLogLogic.print("data内容为空--->", false); return; } ModBusBean modBusBean = new Gson().fromJson(tempData, ModBusBean.class); // ModBusBean modBusBean = new ModBusBean(); // modBusBean.setOid("0101050219D44A00"); // modBusBean.setData(new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01}); // if (TextUtils.isEmpty(modBusBean.getOid())) { // modBusBean.setOid("0101050217BBC400"); // } if (modBusBean.getMac() == null || modBusBean.getData() == null) { uniCallbackData(type, null, -2, "发送失败,(mac或者oid)为空", callback); //HdlLogLogic.print("uni--->发送--->ModBus--->发送失败,(mac或者oid)为空." + Arrays.toString(modBusBean.getData()), true); return; } //HdlLogLogic.print("uni--->发送--->ModBus--->" + Arrays.toString(modBusBean.getData()), true); //发送modbus协议 HDLModBusConnect.getInstance().Send(modBusBean.getMac(), modBusBean.getData(), new ModbusCallBack() { @Override public void onSuccess(int[] data) { uniCallbackData(type, data, callback); // HdlLogLogic.print("uni--->接收--->ModBus--->" + Arrays.toString(data), true); } @Override public void onError(HDLLinkException e) { uniCallbackData(type, null, e.getCode(), e.getMsg(), callback); // HdlLogLogic.print("uni--->接收--->ModBus--->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } //endregion //region ******一般方法****** /** * 组装uni发送数据格式 * * @param data 附加数据(没有数据填null) * @param isTokenAndRefreshToken (true=底层默认添加token和refreshToken;false=不加) * @return JSONObject uni方法名里面参数需要的JSONObject对象 */ private JSONObject createdJSONObject(JSONObject data, boolean isTokenAndRefreshToken) { HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); try { if (data == null) { data = new JSONObject(); } if (isTokenAndRefreshToken) { //小程序那里有自己的请求方法,需要这些数据 data.put("token", UserConfigManage.getInstance().getToken()); data.put("refreshToken", UserConfigManage.getInstance().getRefreshToken()); if (TextUtils.isEmpty(AppConfigManage.getUserRegionUrl())) { AppConfigManage.setUserRegionUrl(HDLLinkPMUser.getInstance().getUserRegionUrl()); } data.put("serverAddress", AppConfigManage.getUserRegionUrl()); data.put("appKey", AppConfigManage.getAppKey()); data.put("appSecret", AppConfigManage.getAppSecret()); } uniCallBackBaseBean.setData(data); return getJSONObject(uniCallBackBaseBean); } catch (Exception e) { HdlLogLogic.print("uni--->组装发送给uni数据格式有异常--->" + e.getMessage(), false); } return new JSONObject(); } /** * 组装uni发送数据格式 * 回调数据给uni小程序,统一在这里处理; * * @param type 请求的小类 * @param obj 附加数据 * @param code 状态码-成功(0)或者失败(-2) * @param msg 结果描述的信息 * @param callback 回调 */ private void uniCallbackData(String type, Object obj, int code, String msg, DCUniMPJSCallback callback) { HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); try { uniCallBackBaseBean.setCode(code); uniCallBackBaseBean.setMes(msg); uniCallBackBaseBean.setData(obj); if (callback != null) { callback.invoke(getJSONObject(uniCallBackBaseBean)); // callback.invoke(uniCallBackBaseBean); HdlLogLogic.print("uni--->接收--->" + type + "--->" + new Gson().toJson(uniCallBackBaseBean), true); } } catch (Exception e) { HdlLogLogic.print("uni--->接收--->" + type + "--->" + e.getMessage(), true); } } private void uniCallbackData(String type, Object obj, DCUniMPJSCallback callback) { uniCallbackData(type, obj, HDLLinkCode.HDL_SUCCESS.getCode(), HDLLinkCode.HDL_SUCCESS.getMsg(), callback); } /** * 当前对象转 JSONObject * * @return JSONObject */ public JSONObject getJSONObject(Object obj) { try { if (obj == null) { return new JSONObject(); } if (TextUtils.isEmpty(obj.toString())) { return new JSONObject(); } if (obj.toString().startsWith("{") && obj.toString().endsWith("}")) { return new JSONObject(obj.toString()); } if (obj instanceof JSONObject) { return (JSONObject) obj; } String json = new Gson().toJson(obj); return new JSONObject(json); } catch (Exception e) { return new JSONObject(); } } /** * 获取 KeyValue * * @param key - * @param obj - * @return value */ public String getKeyValue(String key, Object obj) { try { JSONObject jsonObject = this.getJSONObject(obj); if (jsonObject.has(key)) { return jsonObject.getString(key); } return ""; } catch (Exception e) { return ""; } } //endregion }