From 60f74b306659cba1a8ed7378f9df54a71e48a614 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期三, 28 六月 2023 18:04:09 +0800 Subject: [PATCH] 临时 --- app/src/main/java/com/hdl/photovoltaic/uni/UniToAndroidBean.java | 52 +++++++ HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java | 310 ++++++++++++++++++++++++++++++++++++++++++++ HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/config/TopicConstant.java | 7 + HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/callback/ModbusCallBack.java | 12 + app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java | 26 +-- 5 files changed, 389 insertions(+), 18 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/config/TopicConstant.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/config/TopicConstant.java index e87278f..d66e47d 100644 --- a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/config/TopicConstant.java +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/config/TopicConstant.java @@ -309,4 +309,11 @@ public static final String GATEWAY_LOCATION_GET ="/user/%s/custom/gateway/location/get"; + public static final String NATIVE_MODBUS_DOWN ="/user/%s/custom/native/inverter/down"; + public static final String NATIVE_MODBUS_UP ="/user/%s/custom/native/inverter/up"; + public static final String NATIVE_MODBUS_DOWN_REPLY ="/user/%s/custom/native/inverter/down_reply"; + + public static final String NATIVE_MODBUS_DOWN_SLAVE ="/user/%s/custom/native/inverter/down/slaveoid/%s"; + public static final String NATIVE_MODBUS_DOWN_SLAVE_REPLY ="/user/%s/custom/native/inverter/down_reply/slaveoid/%s"; + } \ No newline at end of file diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/callback/ModbusCallBack.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/callback/ModbusCallBack.java new file mode 100644 index 0000000..7e58147 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/callback/ModbusCallBack.java @@ -0,0 +1,12 @@ +package com.hdl.sdk.link.core.callback; + +import com.hdl.sdk.link.core.bean.gateway.GatewayBean; + +import java.util.List; + +/** + * Created by hxb on 2021/12/15. + */ +public interface ModbusCallBack extends BaseCallBack { + void onSuccess(String msg); +} diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java new file mode 100644 index 0000000..e38c3ad --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java @@ -0,0 +1,310 @@ +package com.hdl.sdk.link.core.connect; + + +import android.text.TextUtils; + +import com.hdl.sdk.link.common.config.TopicConstant; +import com.hdl.sdk.link.common.event.EventDispatcher; +import com.hdl.sdk.link.common.event.EventListener; +import com.hdl.sdk.link.common.exception.HDLLinkCode; +import com.hdl.sdk.link.common.exception.HDLLinkException; +import com.hdl.sdk.link.common.utils.LogUtils; +import com.hdl.sdk.link.core.bean.LinkRequest; +import com.hdl.sdk.link.core.bean.LinkResponse; +import com.hdl.sdk.link.core.bean.ZigbeeResponse; +import com.hdl.sdk.link.core.bean.gateway.GatewayBean; +import com.hdl.sdk.link.core.callback.BaseCallBack; +import com.hdl.sdk.link.core.callback.ModbusCallBack; +import com.hdl.sdk.link.core.callback.ZigbeeCallBack; +import com.hdl.sdk.link.core.config.HDLLinkConfig; +import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; + +/** + * Created by hxb on 2021/12/8. + * 鍘熺敓閫氳鐩稿叧鎺ュ彛 + */ +public class HDLModBusConnect { + + private static final String TAG="HDLModbusConnect"; + private static HDLModBusConnect instance; + /** + * 鍐呴儴鐢紝涓昏鏄鐞嗗鐞嗘帀閫忎紶涓婚鍙妉ink涓婚鍚庯紝杩樺師Zigbee鍘熺敓鏁版嵁鍙婁富棰樼敤 + */ + private final String zigbeeAllTopic = "/Modbus"; + + /** + * 杩斿洖褰撳墠瀹炰緥锛屼笉瀛樺湪灏卞垱寤哄苟鍚屾椂娉ㄥ唽鐩戝惉浜嬩欢 + * + * @return + */ + public static HDLModBusConnect getInstance() { + if (null == instance) { + instance = new HDLModBusConnect(); + instance.initEventListener(); + } + return instance; + } + + /** + * 娉ㄥ唽鐩戝惉Zigbee鎵�鏈夊師鐢熶富棰樺強鏁版嵁 + * + * @param eventListener + */ + public void registerListener(EventListener eventListener) { + if(null==eventListener){ + return; + } + EventDispatcher.getInstance().register(zigbeeAllTopic, eventListener); + } + + /** + * 绉婚櫎鐩戝惉Zigbee鍘熺敓涓婚鍙婃暟鎹� + * + * @param eventListener + */ + public void removeListener(EventListener eventListener) { + if(null==eventListener){ + return; + } + EventDispatcher.getInstance().remove(zigbeeAllTopic, eventListener); + } + + /** + * 鍒濆鍖栫洃鍚簨浠� + */ + private void initEventListener() { + final EventListener eventListener = new EventListener() { + @Override + public void onMessage(Object msg) { +// try { +// if (msg instanceof LinkResponse) { +// LinkResponse linkResponse = (LinkResponse) msg; +// String body = linkResponse.getData(); +// int index = body.indexOf("{"); +// //zigbee鍥炲鐨勬暟鎹墠鏈変富棰橈紝鍚庨潰鎵嶆槸鏁版嵁 +// if (index <= 0) +// return; +// +// String zigbeeTopic = body.substring(0, index).trim(); +// //zigbee鐨勮礋杞芥暟鎹� +// String bodyData = body.substring(index); +// +// ZigbeeResponse zigbeeResponse = new ZigbeeResponse(); +// zigbeeResponse.setTopic(zigbeeTopic); +// zigbeeResponse.setData(bodyData); +// String oid = null; +// //鏄惁鏄�氳繃涓荤綉鍏抽�忎紶涓婚 +// if (linkResponse.getTopic().contains("/slaveoid/")) { +// oid = linkResponse.getTopic().split("/")[8]; +// } else { +// oid = linkResponse.getTopic().split("/")[2]; +// } +// zigbeeResponse.setOid(oid); +// for (GatewayBean gatewayBean : HDLLinkLocalGateway.getInstance().getGatewayList()) { +// if (oid.equals(gatewayBean.getGatewayId()) || oid.equals(gatewayBean.getDevice_mac()) || oid.equals(gatewayBean.getOid())) { +// //涓婇潰鐨刼id鍙兘鏄綉鍏砳d鎴栬�卪ac鎴栬�呮槸oid锛屼笉绠℃槸鍝釜缁熶竴浣跨敤oid琛ㄧず鏂瑰紡 +// zigbeeResponse.setOid(gatewayBean.getOid()); +// break; +// } +// } +// EventDispatcher.getInstance().post(zigbeeTopic, zigbeeResponse); +// //鍙戝竷Zigbee鍘熺敓涓婚鍙婃暟鎹� +// EventDispatcher.getInstance().post(zigbeeAllTopic, zigbeeResponse); +// } +// } catch (Exception e) { +// LogUtils.e(TAG,"LinkResponse杞琙igbeeResponse寮傚父:"+ e.getMessage()); +// } + } + }; + //娉ㄥ唽鐩存帴閫氳鐨勪富棰橈紝鍖呮嫭鐩存帴鍜屼富缃戝叧閫氳鎴栬�呯洿鎺ュ拰浠庣綉鍏抽�氳 + registerListener(String.format(TopicConstant.NATIVE_MODBUS_UP, "+"), eventListener); + registerListener(String.format(TopicConstant.NATIVE_MODBUS_DOWN_REPLY, "+"), eventListener); + + // registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_UP_SLAVE, "+", "+"), eventListener); + } + + /** + * 鍙戦�佸師鐢熸暟鎹� + * + * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d + * @param responeTopic 鍥炲涓婚 + * @param payload 鍙戦�佹暟鎹� + * @param baseCallBack 缁撴灉鍥炶皟 + */ + public void Send(String gatewayOidOrGatewayId,String responeTopic, String payload, final ModbusCallBack baseCallBack) { + //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴 + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId); + if (null == gatewayBean) { + LogUtils.i("鎵句笉鍒扮綉鍏筹紝Oid鏄�" + gatewayOidOrGatewayId); + if (null != baseCallBack) { + baseCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); + } + return; + } + + String tempTopic = String.format(TopicConstant.NATIVE_MODBUS_DOWN, gatewayOidOrGatewayId); + LinkRequest request = new LinkRequest(tempTopic, payload, gatewayBean.getIsLocalEncrypt()); + + if ("true".equals(gatewayBean.getMaster())) { + request.setCloudTopic(String.format(TopicConstant.NATIVE_MODBUS_DOWN, HDLLinkConfig.getInstance().getGatewayId())); + } else { + request.setCloudTopic(String.format(TopicConstant.NATIVE_MODBUS_DOWN_SLAVE, HDLLinkConfig.getInstance().getGatewayId(), gatewayOidOrGatewayId)); + } + request.setReplyTopic(responeTopic); + long timeout = 2 * 1000; + + new HDLConnectHelper(timeout, 1, gatewayBean.getIp_address(), 8586, request, new HDLConnectHelper.HdlSocketListener() { + @Override + public void onSucceed(Object msg) { +// if (msg instanceof String) { +// if (null != baseCallBack) { +// baseCallBack.onSuccess(msg+""); +// } +// } + } + + @Override + public void onFailure(HDLLinkCode hdlLinkCode) { + if (null != baseCallBack) { + baseCallBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); + } + } + }, true).send(); + } + + + + /** + * 鍙戦�佸師鐢熼�忎紶鍛戒护鏁版嵁 + * + * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d + * @param responeTopic 鍥炲涓婚 + * @param payload 鍙戦�佹暟鎹� + * @param zigbeeCallBack 缁撴灉鍥炶皟 + */ + public void SendThrough(String gatewayOidOrGatewayId, String responeTopic, String payload, final ZigbeeCallBack zigbeeCallBack) { +// //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴 +// GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId); +// if (null == gatewayBean) { +// LogUtils.i("鎵句笉鍒扮綉鍏筹紝Oid鏄�" + gatewayOidOrGatewayId); +// if (null != zigbeeCallBack) { +// zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); +// } +// return; +// } +// boolean isLocal = gatewayBean.getIsLocalGateway(); +// //濡傛灉鏄湰鍦伴�氳 +// if (isLocal == true) { +// +// String tempTopic = String.format("/user/%s/custom/native/zigbee/down", gatewayOidOrGatewayId); +// String tempTopicReply = String.format("/user/%s/custom/native/zigbee/up", gatewayOidOrGatewayId); +// +// final boolean[] isCallBack = {false}; +// //閫忎紶鍛戒护涓婚澶勭悊 +// final EventListener eventListener = new EventListener() { +// @Override +// public void onMessage(Object msg) { +// if (msg instanceof LinkResponse) { +// LinkResponse linkResponse = (LinkResponse) msg; +// //TODO 濡傛灉閰嶇疆浠庣綉鍏崇殑淇℃伅锛岄�氳繃涓荤綉鍏宠浆杈撅紝杩欓噷oid瑕佸垽鏂笅 +// String body = getZigbeeData(responeTopic, linkResponse); +// if (null != body) { +// isCallBack[0] = true; +// removeListener(tempTopicReply, this); +// if (null != zigbeeCallBack) { +// zigbeeCallBack.onSuccess(body); +// } +// } +// } +// } +// }; +// //娉ㄥ唽鐩戝惉 +// registerListener(tempTopicReply, eventListener); +// +// //涓�瀹氭椂闂村悗杩樻病鏈夋帴鏀跺埌鏁版嵁锛屽氨鍥炶皟澶辫触 +// ScheduledExecutorService scheduledExecutorService = ThreadToolUtils.getInstance().newScheduledThreadPool(1); +// scheduledExecutorService.schedule(new Runnable() { +// @Override +// public void run() { +// removeListener(tempTopicReply, eventListener); +// scheduledExecutorService.shutdownNow(); +// if (!isCallBack[0]) { +// if (null != zigbeeCallBack) { +// zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_Zigbee_FAILURE_ERROR)); +// } +// } +// } +// }, 5, TimeUnit.SECONDS); +// +// //鏈湴鍙戦�� +// LinkRequest request = new LinkRequest(tempTopic, payload, gatewayBean.getIsLocalEncrypt()); +// new HDLConnectHelper(gatewayBean.getIp_address(), request,true).send(); +// } else { +// //璇锋眰涓婚 +// String tempTopic = null; +// //鍥炲涓婚 +// String tempTopicReply = null; +// +// //杩滅▼鍙戦�� +// if ("true".equals(gatewayBean.getMaster())) { +// tempTopic = String.format("/user/%s/custom/native/zigbee/down", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId()); +// tempTopicReply = String.format("/user/%s/custom/native/zigbee/up", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId()); +// } else { +// tempTopic = String.format("/user/%s/custom/native/zigbee/slaveoid/%s/down", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId(), gatewayOidOrGatewayId); +// tempTopicReply = String.format("/user/%s/custom/native/zigbee/slaveoid/%s/up", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId(), "+"); +// } +// //TODO 鍚庣画瀹屽杽浜戠鐨勫彂閫佹帴鏀舵柟娉� +// } + } + + + /** + * 澶勭悊zigbee鍥炲鐨勬暟鎹� + * + * @param responeTopic 鍥炲涓婚 + * @param linkResponse 鍥炲鐨勯�忎紶鏁版嵁 + */ + private static String getZigbeeData(String responeTopic, LinkResponse linkResponse) { + //涓婚涓虹┖涓嶅鐞� + if(TextUtils.isEmpty(responeTopic)){ + return null; + } + + String body = linkResponse.getData(); + int index = body.indexOf("{"); + //zigbee鍥炲鐨勬暟鎹墠鏈変富棰橈紝鍚庨潰鎵嶆槸鏁版嵁 + if (index <= 0) + return null; + + String zigbeeTopic = body.substring(0, index).trim(); + //zigbee鐨勮礋杞芥暟鎹� + String bodyData = body.substring(index); + + //涓嶆槸褰撳墠璇锋眰鐨勬暟鎹紝涓嶅鐞� + if (!zigbeeTopic.startsWith(responeTopic)) { + return null; + } + + return bodyData; + //Zigbee浠ュ墠鐨勬帴鏀堕�昏緫 +// HdlZbGatewayReceiveLogic.Current.ZigbeeOldReceiveLogic(reportTopic, bodyData, gatewayMac); + } + + /** + * 娉ㄥ唽鐩戝惉 + */ + static void registerListener(String responseTopic, EventListener eventListener) { + if (!TextUtils.isEmpty(responseTopic)) { + EventDispatcher.getInstance().register(responseTopic, eventListener); + } + } + + /** + * 绉婚櫎鐩戝惉 + */ + static void removeListener(String responseTopic, EventListener eventListener) { + if (!TextUtils.isEmpty(responseTopic)) { + EventDispatcher.getInstance().remove(responseTopic, eventListener); + } + } +} diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java index f8bcb24..445dc0c 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java @@ -3,6 +3,7 @@ import android.text.TextUtils; +import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import com.hdl.photovoltaic.HDLApp; import com.hdl.photovoltaic.config.UserConfigManage; @@ -10,19 +11,16 @@ import com.hdl.photovoltaic.ui.bean.HouseIdBean; import com.hdl.photovoltaic.uni.HDLUniMP; import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; +import com.hdl.photovoltaic.uni.UniToAndroidBean; import com.hdl.photovoltaic.utils.WifiUtils; -import com.hdl.sdk.link.HDLLinkLocalSdk; 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.HDLLinkCallBack; -import com.hdl.sdk.link.core.config.HDLLinkConfig; -import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; +import com.hdl.sdk.link.core.connect.HDLModBusConnect; import org.json.JSONObject; -import java.util.ArrayList; import java.util.List; import io.dcloud.feature.unimp.DCUniMPJSCallback; @@ -64,19 +62,11 @@ if (!HDLUniMP.UNI_APP_ID.equals(appId)) { return; } - org.json.JSONObject jsonObject = getJSONObject(data); - String type_value = ""; - String oid=""; - if (jsonObject.has("type")) { - type_value = jsonObject.getString("type"); - } - if (jsonObject.has("oid")) { - oid = jsonObject.getString("oid"); - } + UniToAndroidBean uniToAndroidBean = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(data), UniToAndroidBean.class); if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) { //浣忓畢妯″潡 - switch (type_value) { + switch (uniToAndroidBean.getType()) { case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: { //鍒涘缓 HdlResidenceLogic.getInstance().getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() { @@ -104,7 +94,7 @@ } } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) { //璁惧妯″潡 - switch (type_value) { + switch (uniToAndroidBean.getType()) { case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: { //娣诲姞 } @@ -131,14 +121,14 @@ break; case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND:{ //鍙戦�乵odbus鍗忚 - + HDLModBusConnect.getInstance().Send(uniToAndroidBean.getGatewayOid(),null,); } } } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) { WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance()); //wifi妯″潡 - switch (type_value) { + switch (uniToAndroidBean.getType()) { case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: { //鑾峰彇wifi鍒楄〃 if (callback != null) { diff --git a/app/src/main/java/com/hdl/photovoltaic/uni/UniToAndroidBean.java b/app/src/main/java/com/hdl/photovoltaic/uni/UniToAndroidBean.java new file mode 100644 index 0000000..c6f6b2a --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/uni/UniToAndroidBean.java @@ -0,0 +1,52 @@ +package com.hdl.photovoltaic.uni; + +import java.io.Serializable; + +/** + * Created by hxb on 2023/6/28. + */ +public class UniToAndroidBean implements Serializable { + + private String gatewayOid;//缃戝叧鐨刼id + private String type;//鍔熻兘绫诲瀷 + private Object data;//闄勫姞json鏁版嵁 + + private String responseTopic;//鍥炲涓婚 + + + public UniToAndroidBean() { + + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public String getTopic() { + return topic == null ? "" : topic; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public String getType() { + return type == null ? "" : type; + } + + public void setType(String type) { + this.type = type; + } + + public String getGatewayOid() { + return gatewayOid; + } + + public void setGatewayOid(String gatewayOid) { + this.gatewayOid = gatewayOid; + } +} -- Gitblit v1.8.0