package com.hdl.photovoltaic.ui;
|
|
|
import android.os.Bundle;
|
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.photovoltaic.HDLApp;
|
import com.hdl.photovoltaic.base.CustomBaseActivity;
|
import com.hdl.photovoltaic.bean.MqttInfo;
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.databinding.ActivityCpowerStationBinding;
|
import com.hdl.photovoltaic.listener.CloudCallBeak;
|
import com.hdl.photovoltaic.other.HdlDeviceLogic;
|
import com.hdl.photovoltaic.other.HdlLogLogic;
|
import com.hdl.photovoltaic.other.HdlMqttLogic;
|
import com.hdl.photovoltaic.other.HdlResidenceLogic;
|
import com.hdl.photovoltaic.other.HdlThreadLogic;
|
import com.hdl.photovoltaic.other.HdlUniLogic;
|
import com.hdl.photovoltaic.ui.bean.HouseIdBean;
|
import com.hdl.photovoltaic.ui.bean.OidBean;
|
import com.hdl.photovoltaic.uni.HDLUniMP;
|
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.ModbusResponse;
|
import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient;
|
|
import java.lang.reflect.Type;
|
import java.util.List;
|
|
import cn.jpush.android.api.JPushInterface;
|
|
/**
|
* C端-电站界面
|
*/
|
public class CPowerStationActivity extends CustomBaseActivity {
|
|
private ActivityCpowerStationBinding viewBinding;
|
|
private EventListener allTopicsListener;
|
|
@Override
|
public Object getContentView() {
|
viewBinding = ActivityCpowerStationBinding.inflate(getLayoutInflater());
|
return viewBinding.getRoot();
|
}
|
|
@Override
|
public void onBindView(Bundle savedInstanceState) {
|
|
|
//C端启动App自动打开小程序电站详情界面
|
startAppAutomaticallyOpenUni();
|
//注册监听
|
this.registerAllTopicsListener();
|
//初始化mqtt客户端
|
this.initMqttClient();
|
//上传极光注册ID到云端
|
this.pushTokens();
|
}
|
|
/**
|
* C端启动App自动打开小程序电站详情界面
|
*/
|
private void startAppAutomaticallyOpenUni() {
|
|
List<HouseIdBean> HouseIdList = HdlResidenceLogic.getInstance().getHouseIdList();
|
if (HouseIdList.size() == 0) {
|
// String path = HDLUniMP.UNI_EVENT_OPEN_HOME_Null_C;
|
// HdlUniLogic.getInstance().openUniMP(path, null);
|
|
|
String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS_C
|
+ "?homeId=" + ""
|
+ "&homeName=" + ""
|
+ "&powerStationStatus=" + 1;
|
HdlUniLogic.getInstance().openUniMP(path, null);
|
} else {
|
HouseIdBean houseIdBean = HouseIdList.get(0);
|
String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS_C
|
+ "?homeId=" + houseIdBean.getHomeId()
|
+ "&homeName=" + houseIdBean.getHomeName()
|
+ "&powerStationStatus=" + houseIdBean.getPowerStationStatus();
|
HdlUniLogic.getInstance().openUniMP(path, null);
|
}
|
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
HdlUniLogic.getInstance().checkRemoveOtherUniMPEventCallBack();
|
//移除监听
|
HDLLinkLocalSdk.getInstance().removeAllTopicsListener(allTopicsListener);
|
//断开mqtt连接
|
if (MqttRecvClient.getInstance() != null) {
|
MqttRecvClient.getInstance().stop();
|
}
|
}
|
|
/**
|
* 上传极光注册ID到云端
|
*/
|
private void pushTokens() {
|
String registrationId_tag = "registrationID_" + UserConfigManage.getInstance().getUserId();
|
String is_registrationID = HDLApp.getInstance().getKey(registrationId_tag);
|
if (is_registrationID.equals("true")) {
|
//上过就没有必要在上传啦
|
return;
|
}
|
|
HdlThreadLogic.runSubThread(new Runnable() {
|
@Override
|
public void run() {
|
String registrationID = JPushInterface.getRegistrationID(_mActivity);
|
int addCount = 0;
|
while (TextUtils.isEmpty(registrationID)) {
|
SystemClock.sleep(1000);//1s休眠
|
registrationID = JPushInterface.getRegistrationID(_mActivity);
|
addCount++;
|
if (addCount > 5) {
|
//5s超时
|
break;
|
}
|
}
|
if (!TextUtils.isEmpty(registrationID)) {
|
UserConfigManage.getInstance().setRegistrationID(registrationID);
|
UserConfigManage.getInstance().Save();
|
String finalRegistrationID = registrationID;
|
HdlResidenceLogic.getInstance().pushAdd(new CloudCallBeak<String>() {
|
@Override
|
public void onSuccess(String pushId) {
|
|
if (!TextUtils.isEmpty(pushId)) {
|
UserConfigManage.getInstance().setPushId(pushId);
|
UserConfigManage.getInstance().Save();
|
}
|
HDLApp.getInstance().setInfoMap(registrationId_tag, "true");//记录一下状态,后台杀死app会清空;
|
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);
|
}
|
});
|
|
}
|
|
|
}
|
});
|
}
|
|
/**
|
* 初始化mqtt客户端
|
*/
|
public void initMqttClient() {
|
|
HdlMqttLogic.getInstance().getMqttRemoteInfo(HomeType.A, new CloudCallBeak<MqttInfo>() {
|
@Override
|
public void onSuccess(MqttInfo info) {
|
if (info != null) {
|
MqttRecvClient.init(_mActivity, info.getUrl(), info.getClientId(), info.getUserName(), info.getPassWord());
|
}
|
}
|
|
@Override
|
public void onFailure(HDLException e) {
|
HdlLogLogic.print("C端--->读取mqtt远程信息失败--->" + e.getMessage(), e.getCode(), true);
|
}
|
});
|
}
|
|
/**
|
* 注册所有主题数据的监听
|
*/
|
private void registerAllTopicsListener() {
|
allTopicsListener = new EventListener() {
|
@Override
|
public void onMessage(Object msg) {
|
|
if (msg == null) {
|
return;
|
}
|
if (msg instanceof ModbusResponse) {
|
ModbusResponse response = (ModbusResponse) msg;
|
if (response.getTopic() == null) {
|
return;
|
}
|
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());
|
uniCallBackBaseBean.setData(response.getData());
|
// if (com.hdl.photovoltaic.ui.test.UniAppMqtt.getInstance().getUni()) {
|
// try {
|
// String t = String.format("AndroidToPC/%s/%s", System.currentTimeMillis(), HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL);
|
// com.hdl.photovoltaic.ui.test.UniAppMqtt.getInstance().publish(t, JSONObject.toJSON(uniCallBackBaseBean).toString());
|
// } catch (Exception ignored) {
|
// }
|
// } else {
|
HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, uniCallBackBaseBean);
|
// }
|
// }
|
|
} else if (msg instanceof LinkResponse) {
|
LinkResponse linkResponse = (LinkResponse) msg;
|
if (linkResponse.getTopic() == null) {
|
return;
|
}
|
if (linkResponse.getTopic().endsWith("/ota/device/progress/up")) {
|
HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
|
uniCallBackBaseBean.setType(linkResponse.getTopic());
|
uniCallBackBaseBean.setData(linkResponse.getData());
|
HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL, uniCallBackBaseBean);
|
HdlLogLogic.print(linkResponse.getTopic() + new Gson().toJson(linkResponse.getData()), true);
|
} else if (linkResponse.getTopic().endsWith("/custom/device/list/upload")) {
|
//逆变器有新oid通知主题
|
if (linkResponse.getData() == null) {
|
HdlLogLogic.print("逆变器有新oid通知--->数据为空", false);
|
return;
|
}
|
try {
|
Gson gson = new Gson();
|
Type typeOfT = new TypeToken<List<OidBean>>() {
|
}.getType();
|
String json = gson.toJson(linkResponse.getData());
|
List<OidBean> oidList = gson.fromJson(json, typeOfT);
|
//增量增加add
|
HdlDeviceLogic.getInstance().updateOidAdd(UserConfigManage.getInstance().getHomeId(), oidList, null);
|
} catch (Exception ignored) {
|
}
|
|
|
}
|
}
|
}
|
};
|
HDLLinkLocalSdk.getInstance().registerAllTopicsListener(allTopicsListener);
|
}
|
|
|
}
|