| | |
| | | package com.hdl.photovoltaic.ui; |
| | | |
| | | |
| | | import android.app.ActivityManager; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.Build; |
| | | import android.os.Bundle; |
| | | import android.os.Process; |
| | | import android.os.SystemClock; |
| | | import android.text.TextUtils; |
| | | |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.linkpm.sdk.home.type.HomeType; |
| | | import com.hdl.linkpm.sdk.user.HDLLinkPMUser; |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.base.CustomBaseActivity; |
| | | import com.hdl.photovoltaic.bean.MqttInfo; |
| | | import com.hdl.photovoltaic.config.ConstantManage; |
| | |
| | | import com.hdl.photovoltaic.enums.NetworkType; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlDeviceLogic; |
| | | import com.hdl.photovoltaic.other.HdlESLocalJsonLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.other.HdlMqttLogic; |
| | | import com.hdl.photovoltaic.other.HdlOtaLogic; |
| | | import com.hdl.photovoltaic.other.HdlResidenceLogic; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.other.HdlUniLogic; |
| | | import com.hdl.photovoltaic.services.ForeService; |
| | | import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; |
| | | import com.hdl.photovoltaic.ui.bean.HouseIdBean; |
| | | import com.hdl.photovoltaic.ui.bean.OidBean; |
| | | import com.hdl.photovoltaic.uni.HDLUniMP; |
| | | import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | import com.hdl.sdk.link.HDLLinkLocalSdk; |
| | | import com.hdl.sdk.link.common.event.EventListener; |
| | | import com.hdl.sdk.link.core.bean.LinkResponse; |
| | |
| | | import com.hdl.sdk.link.core.bean.gateway.GatewayBean; |
| | | import com.hdl.sdk.link.core.config.HDLLinkConfig; |
| | | import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient; |
| | | import com.umeng.analytics.MobclickAgent; |
| | | |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | |
| | | private ActivityCpowerStationBinding viewBinding; |
| | | |
| | | private EventListener allTopicsListener; |
| | | private int backPressTimes; |
| | | |
| | | @Override |
| | | public Object getContentView() { |
| | |
| | | |
| | | @Override |
| | | public void onBindView(Bundle savedInstanceState) { |
| | | setStatusBarTranslucent(); |
| | | getWindow().setNavigationBarColor(getColor(R.color.text_FF000000)); |
| | | //C端启动App自动打开小程序电站详情界面 |
| | | startAppAutomaticallyOpenUni(); |
| | | //注册监听 |
| | |
| | | this.initMqttClient(); |
| | | //上传极光注册ID到云端 |
| | | this.pushTokens(); |
| | | //获取云端脚本 |
| | | HdlESLocalJsonLogic.getInstance().getAllHdlESLocalJson(); |
| | | // mForegroundService= new Intent(this, ForeService.class); |
| | | // startService(); |
| | | } |
| | | |
| | | Intent mForegroundService; |
| | | |
| | | private void startService() { |
| | | // Android 8.0使用startForegroundService在前台启动新服务 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| | | startForegroundService(mForegroundService); |
| | | } else { |
| | | startService(mForegroundService); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void onResume() { |
| | | super.onResume(); |
| | | //启动页与mainActivity样式一样,这样启动页和mainActivity看上去是一个界面,目的是为了mainActivity遮住下一页; |
| | | // AppManagerUtils.getAppManager().finishActivity(StartActivity.class); |
| | | // this.portConflictDialog(); |
| | | } |
| | | |
| | | public void startAppAutomaticallyOpenUni() { |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | SystemClock.sleep(10); |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | startAppAutomaticallyOpenUni(false); |
| | | // //低于安卓14版本 |
| | | // if (android.os.Build.VERSION.SDK_INT < 34) { |
| | | // startAppAutomaticallyOpenUni(false); |
| | | // } else { |
| | | // startAppAutomaticallyOpenUni(false); |
| | | // } |
| | | } |
| | | }); |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | @Subscribe(threadMode = ThreadMode.MAIN) |
| | |
| | | if (TextUtils.isEmpty(event.getTopic())) { |
| | | return; |
| | | } |
| | | //网关更新mqtt秘钥更新通知 |
| | | //网关mqtt秘钥更新通知 |
| | | if (event.getTopic().contains("/custom/mqtt/secret/change")) { |
| | | String[] topics = event.getTopic().split("/"); |
| | | //主题不符合规则不处理 |
| | |
| | | return; |
| | | } |
| | | int index = -1; |
| | | List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId()); |
| | | List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId(), null); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | GatewayBean gatewayBean = list.get(i); |
| | | if (gatewayBean.getGatewayId().equals(topics[2])) { |
| | |
| | | HdlLogLogic.print(event.getTopic() + new Gson().toJson(event.getData()), true); |
| | | } else if (event.getTopic().contains(ConstantManage.network_change_post)) { |
| | | // String wifi_ssid = WifiUtils.getInstance().getCurrentConnectWifiSsid();//获取WiFi的ssid需要位置权限的 |
| | | HdlLogLogic.print("监听网络状态--->" + event.getData().toString() + "(" + event.getType() + ")", true); |
| | | HdlLogLogic.print("监听网络状态---" + event.getData().toString() + "(" + event.getType() + ")", true); |
| | | //第一次启动App,以及切换网络的时候更新 |
| | | if (TextUtils.isEmpty(UserConfigManage.getInstance().getHomeId())) { |
| | | return; |
| | |
| | | HdlThreadLogic.runSubThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | //防止app启动的时候没有网络(app也登录不了),导致获取不了mqtt远程连接信息; |
| | | initMqttClient(); |
| | | |
| | | HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<GatewayBean>>() { |
| | | @Override |
| | | public void onSuccess(List<GatewayBean> obj) { |
| | |
| | | } |
| | | }); |
| | | } |
| | | } else if (event.getTopic().contains(HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL) && event.getType().contains(HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK)) { |
| | | //物理按键返回事件(包括左滑移除事件) |
| | | killProcessApp(); |
| | | } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST.equals(event.getType())) { |
| | | if (MqttRecvClient.getInstance() != null) { |
| | | MqttRecvClient.getInstance().removeAllTopic(); |
| | | } |
| | | String homeId = event.getData().toString(); |
| | | //进去住宅详情uni读取逆变器列表成功后通知 |
| | | for (int i = 0; i < HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId, null).size(); i++) { |
| | | String gatewayId = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId, null).get(i).getGatewayId(); |
| | | //字符串是自己按规则拼接的,里面注册主题时会解析字符串,只拿getGatewayId()值; |
| | | String topic = "/user/" + gatewayId + "/#"; |
| | | //进去住宅详情开始订阅主题 |
| | | MqttRecvClient.getInstance().checkAndsubscribeAllTopics(topic);//订阅【逆变器】消息 |
| | | |
| | | } |
| | | String topicHome = "/user/" + homeId + "/#"; |
| | | MqttRecvClient.getInstance().checkAndsubscribeAllTopics(topicHome);//订阅【电站】消息 |
| | | |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * C端启动App自动打开小程序电站详情界面 |
| | | */ |
| | | private void startAppAutomaticallyOpenUni() { |
| | | private void startAppAutomaticallyOpenUni(boolean delay) { |
| | | |
| | | // if (delay) { |
| | | // SystemClock.sleep(2000); |
| | | // } |
| | | if (TextUtils.isEmpty(HDLLinkPMUser.getInstance().getAccessToken())) { |
| | | //其实刷新token失败已通知退出登录,数据已经清空,因为等2s uni加载慢导致请求链接的时候出现token为空 |
| | | HdlLogLogic.print("返回去---",true); |
| | | return; |
| | | } |
| | | UserConfigManage.getInstance().setUniBottomSafeDistanceBackgroundColor(0); |
| | | List<HouseIdBean> HouseIdList = HdlResidenceLogic.getInstance().getHouseIdList(); |
| | | if (HouseIdList.size() == 0) { |
| | | String path = HDLUniMP.UNI_EVENT_OPEN_HOME_Null_C; |
| | | HdlUniLogic.getInstance().openUniMP(path, null); |
| | | if (HouseIdList.isEmpty()) { |
| | | if (TextUtils.isEmpty(UserConfigManage.getInstance().getHomeId())) { |
| | | String path = HDLUniMP.UNI_EVENT_OPEN_HOME_Null_C; |
| | | HdlUniLogic.getInstance().openUniMP(path, null); |
| | | } else { |
| | | //解决问题手机没有网自动登录进来,默认传上一次的住宅id |
| | | String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS_C + "?homeId=" + UserConfigManage.getInstance().getHomeId() + "&homeName=" + UserConfigManage.getInstance().getHomeName(); |
| | | HdlUniLogic.getInstance().openUniMP(path, null); |
| | | } |
| | | } else { |
| | | int select_home = 0; |
| | | for (int i = 0; i < HouseIdList.size(); i++) { |
| | |
| | | } |
| | | } |
| | | HouseIdBean houseIdBean = HouseIdList.get(select_home); |
| | | HdlResidenceLogic.getInstance().switchHouse(houseIdBean); |
| | | String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS_C |
| | | + "?homeId=" + houseIdBean.getHomeId() |
| | | + "&homeName=" + houseIdBean.getHomeName() |
| | | + "&powerStationStatus=" + houseIdBean.getPowerStationStatus(); |
| | | HdlResidenceLogic.getInstance().switchHouse(houseIdBean, false); |
| | | String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS_C + "?homeId=" + houseIdBean.getHomeId() + "&homeName=" + houseIdBean.getHomeName() + "&powerStationStatus=" + houseIdBean.getPowerStationStatus(); |
| | | HdlUniLogic.getInstance().openUniMP(path, null); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | // /** |
| | | // * 物理按键返回事件(包括左滑移除事件) |
| | | // */ |
| | | // @Override |
| | | // public void onBackPressed() { |
| | | // killProcessApp(); |
| | | // super.onBackPressed(); |
| | | // } |
| | | |
| | | private void killProcessApp() { |
| | | //物理按键返回事件(包括左滑移除事件) |
| | | if (AppManagerUtils.getAppManager().getActivitySize() <= 1) { |
| | | if (backPressTimes == 0) { |
| | | HdlThreadLogic.toast(_mActivity, R.string.kill_app); |
| | | backPressTimes = 1; |
| | | new Thread() { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | Thread.sleep(2000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | backPressTimes = 0; |
| | | } |
| | | } |
| | | }.start(); |
| | | return; |
| | | } else { |
| | | //关闭小程序 |
| | | if (null != HDLUniMPSDKManager.getInstance().getUniMP()) { |
| | | HDLUniMPSDKManager.getInstance().getUniMP().closeUniMP(); |
| | | } |
| | | AppManagerUtils.getAppManager().finishAllActivity(); |
| | | //如果开发者调用kill或者exit之类的方法杀死进程,或者双击back键会杀死进程,请务必在此之前调用MobclickAgent.onKillProcess方法,用来保存统计数据。 |
| | | MobclickAgent.onKillProcess(_mActivity); |
| | | Process.killProcess(Process.myPid()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | |
| | | HdlUniLogic.getInstance().checkRemoveOtherUniMPEventCallBack(); |
| | | //移除监听 |
| | | HDLLinkLocalSdk.getInstance().removeAllTopicsListener(allTopicsListener); |
| | | // stopService(mForegroundService); |
| | | } |
| | | |
| | | /** |
| | |
| | | UserConfigManage.getInstance().Save(); |
| | | } |
| | | HDLApp.getInstance().setInfoMap(registrationId_tag, "true");//记录一下状态,后台杀死app会清空; |
| | | HdlLogLogic.print("C端--->添加极光ID到云端--->registrationID:" + UserConfigManage.getInstance().getUserName() + "--->" + finalRegistrationID, true); |
| | | HdlLogLogic.print("C端---添加极光ID到云端---registrationID:" + UserConfigManage.getInstance().getUserName() + "--->" + finalRegistrationID, true); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLApp.getInstance().setInfoMap(registrationId_tag, "false");//记录一下状态,后台杀死app会清空; |
| | | HdlLogLogic.print("C端--->添加极光ID到云端失败--->registrationID:" + UserConfigManage.getInstance().getUserName() + "--->" + finalRegistrationID, true); |
| | | HdlLogLogic.print("C端---添加极光ID到云端失败---registrationID:" + UserConfigManage.getInstance().getUserName() + "--->" + finalRegistrationID, true); |
| | | } |
| | | }); |
| | | |
| | |
| | | * 初始化mqtt客户端 |
| | | */ |
| | | public void initMqttClient() { |
| | | //检查断开状态 |
| | | if (!MqttRecvClient.getInstance().isConnected()) { |
| | | MqttRecvClient.getInstance().reConnect(); |
| | | } |
| | | if (!MqttRecvClient.getInstance().isInit()) { |
| | | HdlMqttLogic.getInstance().getMqttRemoteInfo(HomeType.A, new CloudCallBeak<MqttInfo>() { |
| | | @Override |
| | |
| | | if (info != null) { |
| | | MqttRecvClient.getInstance().setConnectParam(info.getUrl(), info.getClientId(), info.getUserName(), info.getPassWord()); |
| | | MqttRecvClient.getInstance().connect(); |
| | | HdlLogLogic.print("mqtt链接成功---ClientId---" + info.getClientId(), true); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HdlLogLogic.print("读取mqtt远程信息失败--->" + e.getMessage(), e.getCode(), true); |
| | | HdlLogLogic.print("读取mqtt远程信息失败---" + e.getMessage(), e.getCode(), true); |
| | | } |
| | | }); |
| | | } |
| | |
| | | if (response.getTopic() == null) { |
| | | return; |
| | | } |
| | | HdlLogLogic.print("C端--->监听到Modbus数据--->" + new Gson().toJson(response), false); |
| | | HdlLogLogic.print("C端---监听到Modbus数据---" + new Gson().toJson(response), false); |
| | | // if (response.getTopic().endsWith("custom/native/inverter/up")) { |
| | | HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); |
| | | uniCallBackBaseBean.setType(response.getTopic()); |
| | |
| | | } |
| | | |
| | | |
| | | } else if (linkResponse.getTopic().endsWith("/app/thing/property/send")) { |
| | | //设备状态变更topic:/user/${homeId}/app/thing/property/send |
| | | if (linkResponse.getData() == null) { |
| | | HdlLogLogic.print("设备状态变更--->数据为空", false); |
| | | return; |
| | | } |
| | | HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); |
| | | uniCallBackBaseBean.setType(linkResponse.getTopic()); |
| | | uniCallBackBaseBean.setData(linkResponse.getData()); |
| | | HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, uniCallBackBaseBean); |
| | | } |
| | | |
| | | } |
| | | } |
| | | }; |
| | |
| | | } |
| | | |
| | | |
| | | } |
| | | } |