HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java
@@ -24,7 +24,7 @@ private static final LockList<EventListener> ALL_TOPICS_EVENT = new LockList<EventListener>();//所有主题消息 // private static final ArrayMap<String, List<EventListener>> EVENT = new ArrayMap<>(); // private static final ArrayMap<String, List<EventListener>> EVENT = new ArrayMap<>(); private static final LockArrayMap<String, List<EventListener>> EVENT = new LockArrayMap<String, List<EventListener>>(); private static final ExecutorService ioThread = ThreadToolUtils.getInstance().newFixedThreadPool(2); @@ -99,7 +99,7 @@ if (ev != null && !ev.isEmpty()) { // TYPE.remove(listener); ev.remove(listener); LogUtils.i(String.format("移除订阅主题:%s,当前回调数量:%s",tag,ev.size())); LogUtils.i(String.format("移除订阅主题:%s,当前回调数量:%s", tag, ev.size())); } } } catch (Exception e) { @@ -112,11 +112,12 @@ /** * 两个主题是否匹配 * @param desString 字典中的主题 * * @param desString 字典中的主题 * @param sourceString 接收到的主题 * @return */ boolean isMatch(String desString,String sourceString) { boolean isMatch(String desString, String sourceString) { String[] des = desString.split("/"); String[] source = sourceString.split("/"); if (des.length != source.length) { @@ -135,8 +136,9 @@ /** * 事件分发器,分发所有在接口列表中的事件 * * @param topicTag - * @param o - * @param o - */ public synchronized void post(String topicTag, final Object o) { try { @@ -171,13 +173,13 @@ // ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { // @Override // public void run() { if (listener != null) { listener.onMessage(o); } if (listener != null) { listener.onMessage(o); } // } // }); } }catch (Exception e){ } catch (Exception e) { LogUtils.e(e.getMessage()); } @@ -189,8 +191,10 @@ public synchronized void filePost() { //TODO } /** * 注册所有主题消息的监听 * * @param listener */ public synchronized void registerAllTopicsListener(EventListener listener) { @@ -206,6 +210,7 @@ /** * 取消所有主题消息的监听 * * @param listener */ public synchronized void removeAllTopicsListener(EventListener listener) { @@ -224,13 +229,13 @@ }); } public synchronized void clear() { private synchronized void clear() { ALL_TOPICS_EVENT.clear(); EVENT.clear(); // TYPE.clear(); } public synchronized void release() { private synchronized void release() { clear(); ioThread.shutdownNow(); } HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/bean/LinkRequest.java
@@ -4,6 +4,8 @@ import com.hdl.sdk.link.common.utils.ByteUtils; import java.nio.charset.StandardCharsets; /** * Created by Tong on 2021/9/29. @@ -123,7 +125,7 @@ "Length:" + getLength() + "\r\n\r\n"; return ByteUtils.concatBytes(header.getBytes("utf-8"), getData()); return ByteUtils.concatBytes(header.getBytes(StandardCharsets.UTF_8), getData()); } catch (Exception e) { return new byte[]{}; } HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/bean/gateway/GatewayBean.java
@@ -82,6 +82,9 @@ private String sid; private String spk = "energy.hdl_inverter"; private String systemStatusDesc;//云端-状态 private String hwVersion;//云端-软件版本号 @@ -423,4 +426,12 @@ public void setTotalElectricityPvToday(String totalElectricityPvToday) { this.totalElectricityPvToday = totalElectricityPvToday; } public String getSpk() { return spk == null ? "" : spk; } public void setSpk(String spk) { this.spk = spk; } } HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLConnectHelper.java
@@ -21,6 +21,8 @@ import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -112,7 +114,7 @@ /** * 可能返回code属性可能没有 没有的话直接成功 有的话只有200才会成功 */ if (code == null || code.intValue() == 200 || code.intValue() == 0) { if (code == null || code == 200 || code == 0) { notifySucceed(msg); } else { notifyFailure(ErrorUtils.getByCode(code)); @@ -332,12 +334,20 @@ if (MqttRecvClient.getInstance() != null) { MqttRecvClient.getInstance().send(requestTopic, encryBytes); } LogUtils.i("远程发送数据:" + linkRequest.getCloudTopic() + "\r\n" + new String(linkRequest.getCloudSendBytes())); if (HDLConnectHelper.isInverterTopic(linkRequest.getCloudTopic())) { LogUtils.i("远程发送数据:" + linkRequest.getCloudTopic() + "\r\n" + Arrays.toString(byteArrayConvertIntArray(linkRequest.getCloudSendBytes()))); } else { LogUtils.i("远程发送数据:" + linkRequest.getCloudTopic() + "\r\n" + new String(linkRequest.getCloudSendBytes())); } } //本地TCP else { if (!linkRequest.getTopic().endsWith("heartbeat")) {//心跳主题数据过多,过滤下 LogUtils.i("本地发送数据:\r\n" + new String(linkRequest.getSendBytes())); if (HDLConnectHelper.isInverterTopic(linkRequest.getTopic())) { LogUtils.i("本地发送数据:\r\n" + Arrays.toString(byteArrayConvertIntArray(linkRequest.getSendBytes()))); } else { LogUtils.i("本地发送数据:\r\n" + new String(linkRequest.getSendBytes())); } } HDLTcpConnect.getTcpSocketBoot(ipAddress).sendMsg(EncryptUtil.getEncryBytes(linkRequest)); } @@ -408,6 +418,7 @@ } } /** * 支持毫米类型 * @@ -433,4 +444,31 @@ listener = null; } } public static boolean isInverterTopic(String topic) { if (TextUtils.isEmpty(topic)) { return false; } return topic.endsWith("custom/native/inverter/down_reply") || topic.endsWith("custom/native/inverter/down") || topic.endsWith("custom/native/inverter/up"); } /** * byte数组转换成int数组(为了打印出无符号的btye数组数据) * * @param bytes 数组 * @return */ public static Integer[] byteArrayConvertIntArray(byte[] bytes) { if (bytes == null || bytes.length == 0) { return new Integer[]{}; } Integer[] arr = new Integer[bytes.length]; for (int i = 0; i < bytes.length; i++) { arr[i] = bytes[i] & 0xff; } return arr; } } HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/QueueUtils.java
@@ -10,6 +10,7 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.ExecutorService; @@ -113,17 +114,21 @@ response.setByteData(bodyBytes); } else { LogUtils.e("解密失败\r\n" + linkPacket.getTopic() + "\r\n" + ByteUtils.encodeHexString(linkPacket.getBody())); response.setData(new String(linkPacket.getBody(), "utf-8")); response.setData(new String(linkPacket.getBody(), StandardCharsets.UTF_8)); response.setByteData(linkPacket.getBody()); } } else { response.setData(new String(linkPacket.getBody(), "utf-8")); response.setData(new String(linkPacket.getBody(), StandardCharsets.UTF_8)); response.setByteData(linkPacket.getBody()); } // if (HDLConnectHelper.isLocal()) { LogUtils.i("本地接收到数据:\r\n" + response.getTopic() + "\r\n" + response.getData()); // } if (HDLConnectHelper.isLocal()) { if (HDLConnectHelper.isInverterTopic(response.getTopic())) { LogUtils.i("本地接收到数据:\r\n" + response.getTopic() + "\r\n" + Arrays.toString(HDLConnectHelper.byteArrayConvertIntArray(response.getByteData()))); } else { LogUtils.i("本地接收到数据:\r\n" + response.getTopic() + "\r\n" + response.getData()); } } //解析完成,topic发送一次 EventDispatcher.getInstance().post(response.getTopic(), response); } HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/mqtt/MqttRecvClient.java
@@ -30,6 +30,7 @@ import org.greenrobot.eventbus.EventBus; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** @@ -239,7 +240,11 @@ return; } String bodyStr = new String(bytes); LogUtils.d(TAG, "\r\n" + "mqtt->远程回复数据" + bodyStr); if (HDLConnectHelper.isInverterTopic(topic)) { LogUtils.d(TAG, "\r\n" + "mqtt->远程回复数据" + Arrays.toString(HDLConnectHelper.byteArrayConvertIntArray(bytes))); } else { LogUtils.d(TAG, "\r\n" + "mqtt->远程回复数据" + bodyStr); } /** * 红外宝设备通过/thing/topo/found主题 上报红外宝设备已经入网了 然后直接return 不需要再下行了 */ app/src/main/java/com/hdl/photovoltaic/HDLApp.java
@@ -156,6 +156,8 @@ if (MqttRecvClient.getInstance() != null) { MqttRecvClient.getInstance().stop(); } //要清空之前tcp连接缓存 // TODO: 10/10/23 } }); app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java
@@ -804,6 +804,7 @@ gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率 gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量 gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号 gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk } app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java
@@ -69,6 +69,10 @@ if (isDebug) { return; } // StringBuilder b = new StringBuilder(); // b.append(title); // b.append(tag); // b. System.out.println(title + tag + "======" + mgs + "(" + code + ")"); } app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -20,6 +20,7 @@ import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.callback.GatewayCallBack; import com.hdl.sdk.link.core.config.HDLLinkConfig; import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import java.nio.charset.StandardCharsets; @@ -518,6 +519,12 @@ HdlLogLogic.print("点击同一个住宅"); return; } //订阅要全部取消 if (MqttRecvClient.getInstance() != null) { MqttRecvClient.getInstance().removeAllTopic(); } //要清空之前tcp连接缓存 // TODO: 10/10/23 //删除旧的住宅文件夹 HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath()); //重新设置住宅id app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -468,7 +468,7 @@ // modBusBean.setOid("0101050217BBC400"); // } if (modBusBean.getMac() == null || modBusBean.getData() == null) { HdlLogLogic.print("内容为空,oid=" + modBusBean.getMac() + " data=" + modBusBean.getData()); HdlLogLogic.print("内容为空,oid=" + modBusBean.getMac() + " data=" + Arrays.toString(modBusBean.getData())); return; } app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java
@@ -6,25 +6,44 @@ import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import com.google.gson.Gson; import com.hdl.linkpm.sdk.core.callback.IResponseCallBack; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.device.HDLLinkPMDevice; import com.hdl.linkpm.sdk.device.bean.GatewayCloudBean; import com.hdl.linkpm.sdk.home.type.HomeType; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.base.CustomBaseActivity; import com.hdl.photovoltaic.bean.MqttInfo; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.databinding.ActivityMyPowerStationBinding; 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.HdlUniLogic; import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.uni.HDLUniMP; import com.hdl.photovoltaic.utils.PermissionUtils; import com.hdl.sdk.link.HDLLinkLocalSdk; import com.hdl.sdk.link.common.event.EventListener; import com.hdl.sdk.link.core.bean.ModbusResponse; import com.hdl.sdk.link.core.bean.eventbus.EventNotifyRefreshGatewayAesKeyInfo; 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.hdl.sdk.link.gateway.HDLLinkLocalGateway; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.ArrayList; import java.util.List; /** @@ -226,5 +245,43 @@ }); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMessage(EventNotifyRefreshGatewayAesKeyInfo event) { //网关更新mqtt秘钥更新通知 if (event == null) { return; } int index = -1; List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(); for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean.getGatewayId().equals(event.getGatewayId())) { index = i; break; } } if (index > -1) { GatewayBean gatewayBean = list.get(index); HdlDeviceLogic.getInstance().getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), gatewayBean.getSpk(), gatewayBean.getDevice_mac(), new CloudCallBeak<DeviceRemoteInfo>() { @Override public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { if (deviceRemoteInfo != null) { //更新mqtt通讯的新秘钥 gatewayBean.setAesKey(deviceRemoteInfo.getSecret()); //用之前的库,底层mqtt订阅,加解密会用到该参数; HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库 } } @Override public void onFailure(HDLException e) { } }); } } } app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java
@@ -160,7 +160,7 @@ MqttRecvClient.getInstance().removeAllTopic(); } } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST.equals(eventBus.getType())) { //进去住宅详情读取逆变器列表成功后通知 //进去住宅详情uni读取逆变器列表成功后通知 for (int i = 0; i < HdlDeviceLogic.getInstance().getCurrentHomeGatewayList().size(); i++) { //String gatewayId = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList().get(i).getGatewayId(); //字符串是自己按规则拼接的,里面注册主题时会解析字符串,只拿getGatewayId()值;