package com.hdl.photovoltaic.ui.test; import androidx.recyclerview.widget.LinearLayoutManager; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import com.google.gson.Gson; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.photovoltaic.base.CustomBaseActivity; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.databinding.ActivityTestMainBinding; import com.hdl.photovoltaic.enums.ShowErrorMode; import com.hdl.photovoltaic.internet.TcpClient; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.listener.LinkCallBack; import com.hdl.photovoltaic.other.HdlDeviceLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.photovoltaic.ui.test.dialogs.DialogDeviceList; import com.hdl.photovoltaic.widget.ConfirmationCancelDialog; 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.core.callback.ModbusCallBack; import java.util.ArrayList; import java.util.List; /** * 测试网关数据主入口界面 */ public class TestMainActivity extends CustomBaseActivity implements View.OnClickListener { private ActivityTestMainBinding viewBinding; private TestTitleAdapter testTitleAdapter; private List mOidList; private GatewayBean mGatewayBean; private String s = ""; @Override public Object getContentView() { viewBinding = ActivityTestMainBinding.inflate(getLayoutInflater()); return viewBinding.getRoot(); } @Override public void onBindView(Bundle savedInstanceState) { LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); testTitleAdapter = new TestTitleAdapter(getTitleList()); viewBinding.listItemRl.setLayoutManager(linearLayoutManager); viewBinding.listItemRl.setAdapter(testTitleAdapter); initView(); initEvent(); } /** * 初始化 */ private void initView() { String homeId = "电站Id=" + UserConfigManage.getInstance().getHomeId(); String homeName = "电站名称=" + UserConfigManage.getInstance().getHomeName(); viewBinding.homeIdTv.setText(homeId); viewBinding.homeNameTv.setText(homeName); } private void initEvent() { viewBinding.topBackLl.setOnClickListener(this); viewBinding.topLogBtn.setOnClickListener(this); viewBinding.clickCommandTv.setOnClickListener(this); viewBinding.clickSelectDeviceTv.setOnClickListener(this); testTitleAdapter.setItemOnClickListener(new TestTitleAdapter.OnClickListener() { @Override public void OnClick(int position, String title) { if (TextUtils.isEmpty(UserConfigManage.getInstance().getHomeId())) { // TODO: 10/30/23 请先选择住宅进行测试,否则无法进行逆变器指令测试; TipDialog("请先选择一个【电站】进行测试,否则无法进行逆变器指令测试.", null); return; } if (mGatewayBean == null && !title.equals("搜索【所有】逆变器列表(0)")) { TipDialog("找不到住宅的【逆变器】,请先确认是否绑定过逆变器,再进行逆变器指令测试.", null); return; } startRequest(position, mGatewayBean); } }); } @Override public void onClick(View v) { int id = v.getId(); if (id == viewBinding.topBackLl.getId()) { finish(); } else if (id == viewBinding.topLogBtn.getId()) { startActivity(TestLogActivity.class); } else if (id == viewBinding.clickCommandTv.getId()) { } else if (id == viewBinding.clickSelectDeviceTv.getId()) { selectDevice(); } } /** * 自定义的标题 * * @return - */ private List getTitleList() { List s = new ArrayList<>(); s.add("搜索【所有】逆变器列表(0)"); s.add("搜索【住宅】逆变器列表(1)"); s.add("获取逆变器oid列表(2)"); s.add("读取逆变器详情(3)"); s.add("解绑逆变器(4)"); s.add("上传oid列表给云端(5)"); s.add("获取逆变器下挂设备列表(6)"); s.add("获取逆变器的信息(7)"); s.add("恢复出厂设置(8)"); return s; } /** * 请求指令 * * @param position 索引值 */ private void startRequest(int position, GatewayBean gatewayBean) { showLoading(); String title = getTitleList().get(position); switch (title) { case "搜索【所有】逆变器列表(0)": { HdlDeviceLogic.getInstance().searchAllNetworkGateway(new GatewayCallBack() { @Override public void onSuccess(List gatewayBeanList) { List newGatewayBeanList = new ArrayList<>(); for (int i = 0; i < gatewayBeanList.size(); i++) { if (HdlDeviceLogic.getInstance().isGatewaySpk(gatewayBeanList.get(i).getGatewayType())) { newGatewayBeanList.add(gatewayBeanList.get(i)); } } gsonConvertJsonStr(newGatewayBeanList); } @Override public void onError(HDLLinkException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "搜索【住宅】逆变器列表(1)": { List gatewayBeanList = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId()); gsonConvertJsonStr(gatewayBeanList); } break; case "获取逆变器oid列表(2)": { HdlDeviceLogic.getInstance().getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack>() { @Override public void onSuccess(List obj) { mOidList = obj; gsonConvertJsonStr(obj); } @Override public void onError(HDLLinkException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "读取逆变器详情(3)": { HdlDeviceLogic.getInstance().getGatewayInfo(gatewayBean.getDevice_mac(), new LinkCallBack() { @Override public void onSuccess(GatewayBean obj) { gsonConvertJsonStr(obj); } @Override public void onError(HDLLinkException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "解绑逆变器(4)": { TipDialog("一旦清除掉逆变器住宅id,逆变器需要重新绑定住宅,否则无法测试逆变器指令;", new Listener() { @Override public void Confirmation() { HdlDeviceLogic.getInstance().clearInverterHomeId(gatewayBean.getDevice_mac(), new LinkCallBack() { @Override public void onSuccess(Boolean obj) { s += "解绑【本地】成功--->"; HdlDeviceLogic.getInstance().delInverterDevice(UserConfigManage.getInstance().getHomeId(), gatewayBean.getDeviceId(), new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { s += "解绑【云端】成功"; gsonConvertJsonStr(s); } @Override public void onFailure(HDLException e) { setError(e.getMsg(), e.getCode()); } }); } @Override public void onError(HDLLinkException e) { setError(e.getMsg(), e.getCode()); } }); } }); } break; case "上传oid列表给云端(5)": { if (this.mOidList == null || this.mOidList.size() == 0) { hideLoading(); HdlThreadLogic.toast(this, "没有oid列表数据,请先获取逆变器oid列表数据,再重新上传."); return; } HdlDeviceLogic.getInstance().fullUpdateOid(UserConfigManage.getInstance().getHomeId(), this.mOidList, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { gsonConvertJsonStr(obj); } @Override public void onFailure(HDLException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "获取逆变器下挂设备列表(6)": { HdlDeviceLogic.getInstance().getInverterDeviceChildDeviceList(UserConfigManage.getInstance().getHomeId(), gatewayBean.getOid(), new CloudCallBeak>() { @Override public void onSuccess(List obj) { gsonConvertJsonStr(obj); } @Override public void onFailure(HDLException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "获取逆变器的信息(7)": { if (TextUtils.isEmpty(mGatewayBean.getAddresses())) { TipDialog("逆变器addresses地址为null.", null); return; } TcpClient.getInstance().sendModBusDataToLinkGateway(gatewayBean.getDevice_mac(), mGatewayBean.getAddresses(), TcpClient.FunctionCode.ReadMultipleRegisters, 550, "8", new ModbusCallBack() { @Override public void onSuccess(int[] data) { gsonConvertJsonStr(data); } @Override public void onError(HDLLinkException e) { setError(e.getMsg(), e.getCode()); } }); } break; case "恢复出厂设置(8)": { ConfirmationCancelDialog confirmationCancelDialog = new ConfirmationCancelDialog(_mActivity); confirmationCancelDialog.show(); confirmationCancelDialog.setTitle("提示!"); confirmationCancelDialog.setContent("是否要解绑\"" + mGatewayBean.getDevice_name() + "\"?"); confirmationCancelDialog.setYesOnclickListener(new ConfirmationCancelDialog.onYesOnclickListener() { @Override public void Confirm() { confirmationCancelDialog.dismiss(); showLoading("解绑中..."); HdlDeviceLogic.getInstance().initializeInverter(gatewayBean.getDevice_mac(), new LinkCallBack() { @Override public void onSuccess(Boolean obj) { hideLoading(); gsonConvertJsonStr(obj); HdlDeviceLogic.getInstance().removeLocalCacheMemoryGatewayToMac(UserConfigManage.getInstance().getHomeId(), mGatewayBean.getDevice_mac()); HdlDeviceLogic.getInstance().delInverterDevice(UserConfigManage.getInstance().getHomeId(), mGatewayBean.getDeviceId(), null); } @Override public void onError(HDLLinkException e) { hideLoading(); setError(e.getMsg(), e.getCode()); } }); } }); confirmationCancelDialog.setNoOnclickListener(new ConfirmationCancelDialog.onNoOnclickListener() { @Override public void Cancel() { hideLoading(); confirmationCancelDialog.dismiss(); } }); } break; default: { HdlThreadLogic.runMainThread(new Runnable() { @Override public void run() { hideLoading(); } }, this, ShowErrorMode.YES); break; } } } private void setError(String msg, int code) { String error = msg + "(" + code + ")"; showText(error); } private void gsonConvertJsonStr(Object src) { Gson gson = new Gson(); String json = gson.toJson(src); showText(json); } /** * 回复的数据 * * @param s 数据 */ private void showText(String s) { HdlThreadLogic.runMainThread(new Runnable() { @Override public void run() { hideLoading(); String s1 = "数据回复:\r\n" + s; viewBinding.replyContentTv.setText(s1); } }, this, ShowErrorMode.YES); } /** * @param s 错误信息 */ private void TipDialog(String s, Listener listener) { ConfirmationCancelDialog confirmationCancelDialog = new ConfirmationCancelDialog(this); confirmationCancelDialog.setTitle("提示"); confirmationCancelDialog.setContent(s); confirmationCancelDialog.setConfirmation("确认"); confirmationCancelDialog.setCancel("取消"); confirmationCancelDialog.show(); confirmationCancelDialog.setNoOnclickListener(new ConfirmationCancelDialog.onNoOnclickListener() { @Override public void Cancel() { confirmationCancelDialog.dismiss(); hideLoading(); } }); confirmationCancelDialog.setYesOnclickListener(new ConfirmationCancelDialog.onYesOnclickListener() { @Override public void Confirm() { hideLoading(); confirmationCancelDialog.dismiss(); if (listener != null) { listener.Confirmation(); } } }); } /** * 选择逆变器调试 */ public void selectDevice() { List list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId()); DialogDeviceList dialogDeviceList = new DialogDeviceList(_mActivity, list); dialogDeviceList.show(); dialogDeviceList.DeviceOnclickListener(new DialogDeviceList.OnclickListener() { @Override public void onClick(int position, GatewayBean gatewayBean) { dialogDeviceList.dismiss(); if (gatewayBean == null) { return; } mGatewayBean = gatewayBean; String str = "逆变器名称=" + gatewayBean.getDevice_name() + "_" + gatewayBean.getDevice_mac(); viewBinding.deviceNameTv.setText(str); } }); } public interface Listener { void Confirmation(); } }