| | |
| | | package com.hdl.sdk.common.utils; |
| | | |
| | | import android.text.TextUtils; |
| | | import android.util.Base64; |
| | | import android.util.Log; |
| | | |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.sdk.common.config.TopicConstant; |
| | | import com.hdl.sdk.common.exception.HDLLinkException; |
| | | import com.hdl.sdk.common.utils.gson.GsonConvert; |
| | | import com.hdl.sdk.connect.HDLLink; |
| | | import com.hdl.sdk.connect.bean.LinkResponse; |
| | | import com.hdl.sdk.connect.bean.request.GatewayRemoteEditRequest; |
| | | import com.hdl.sdk.connect.bean.response.DeviceDeleteResponse; |
| | | import com.hdl.sdk.connect.bean.response.DeviceInfoResponse; |
| | | import com.hdl.sdk.connect.callback.HDLLinkCallBack; |
| | | import com.hdl.sdk.connect.config.HDLLinkConfig; |
| | | import com.hdl.sdk.connect.socket.HDLAuthSocket; |
| | | import com.hdl.sdk.connect.socket.HDLSocket; |
| | | |
| | | import java.time.chrono.IsoChronology; |
| | | import com.hdl.sdk.connect.utils.AesUtil; |
| | | |
| | | /** |
| | | * Created by hxb on 2023/9/14. |
| | | */ |
| | | public class AllTopicManagerUtils { |
| | | public class AllTopicManagerUtils { |
| | | |
| | | /** |
| | | * 全局主题处理方法,可以处理所有接收到的数据 |
| | |
| | | */ |
| | | public static void manager(LinkResponse linkResponse) { |
| | | try { |
| | | if (linkResponse == null) { |
| | | return; |
| | | } |
| | | String topic = linkResponse.getTopic(); |
| | | if (TextUtils.isEmpty(topic)) { |
| | | return; |
| | |
| | | |
| | | String mac_Oid_GatewayId = topics[2]; |
| | | |
| | | if(!isLocalDevice(mac_Oid_GatewayId)){ |
| | | LogUtils.i("不是当前设备的网关Id,Id是"+mac_Oid_GatewayId); |
| | | if (!isLocalDevice(mac_Oid_GatewayId)) { |
| | | LogUtils.i("不是当前设备的网关Id,Id是" + mac_Oid_GatewayId); |
| | | //非当前设备的数据 |
| | | return; |
| | | } |
| | | |
| | | if(isSameTopic(TopicConstant.GATEWAY_EDIT_REMOTE,topic)){ |
| | | if (isSameTopic(TopicConstant.GATEWAY_EDIT_REMOTE, topic)) { |
| | | gatewayRemoteEditRequest(mac_Oid_GatewayId, linkResponse.getData()); |
| | | } else if (isSameTopic(TopicConstant.GATEWAY_SEARCH, topic)) { |
| | | HDLAuthSocket.getInstance().UploadGatewayInfo(null); |
| | | } else if (isSameTopic(TopicConstant.LINK_BROADCAST, topic)) { |
| | | managerLinkBroadcast(linkResponse.getData()); |
| | | } else if (isSameTopic(TopicConstant.DELETE_NOTIFY, topic)) { |
| | | managerDeleteNofity(linkResponse.getData()); |
| | | } |
| | | |
| | | //TODO 通过增加if else 增加其它主题类似这样使用 |
| | | |
| | | } catch (Exception e) { |
| | | LogUtils.e("全局处理模块异常:" + e.getMessage()); |
| | |
| | | |
| | | /** |
| | | * 是否当前的设备 |
| | | * |
| | | * @param mac_Oid_GatewayId |
| | | * @return |
| | | */ |
| | |
| | | return false; |
| | | } |
| | | String mac = HDLLinkConfig.getInstance().getDeviceInfoBean().getDeviceMAC(); |
| | | String oid = "123";//HDLLinkConfig.getInstance().getDeviceInfoBean().getOID(); |
| | | String oid = HDLLinkConfig.getInstance().getDeviceInfoBean().getOID(); |
| | | String gatewayId = HDLLinkConfig.getInstance().getGatewayId(); |
| | | String all = "all"; |
| | | |
| | | return mac_Oid_GatewayId.equals(mac) || mac_Oid_GatewayId.equals(oid) || mac_Oid_GatewayId.equals(gatewayId); |
| | | return mac_Oid_GatewayId.equals(mac) || mac_Oid_GatewayId.equals(oid) || mac_Oid_GatewayId.equals(gatewayId) || mac_Oid_GatewayId.toLowerCase().equals(all); |
| | | } |
| | | |
| | | /** |
| | | * 是否相同主题 |
| | | * |
| | | * @param targetTopic 目标的主题 |
| | | * @param sourceTopic 接收的主题 |
| | | * @return |
| | | */ |
| | | private static boolean isSameTopic(String targetTopic,String sourceTopic) { |
| | | private static boolean isSameTopic(String targetTopic, String sourceTopic) { |
| | | if (TextUtils.isEmpty(targetTopic) || TextUtils.isEmpty(sourceTopic)) { |
| | | return false; |
| | | } |
| | |
| | | return false; |
| | | } |
| | | for (int i = 0; i < targetTopics.length; i++) { |
| | | if(i==2){ |
| | | if (i == 2) { |
| | | //这个位置不匹配,是网关id |
| | | continue; |
| | | } |
| | | if (!targetTopics[i] .equals( sourceTopics[i])) { |
| | | if (!targetTopics[i].equals(sourceTopics[i])) { |
| | | return false; |
| | | } |
| | | } |
| | |
| | | |
| | | /** |
| | | * 网关信息配置 |
| | | * |
| | | * @param body |
| | | */ |
| | | private static void gatewayRemoteEditRequest(String mac_Oid_GatewayId,String body) { |
| | | private static void gatewayRemoteEditRequest(String mac_Oid_GatewayId, String body) { |
| | | if (TextUtils.isEmpty(body)) { |
| | | return; |
| | | } |
| | |
| | | |
| | | HDLSocket.getInstance().gatewayRemoteEditReply(mac_Oid_GatewayId, gatewayRemoteEditRequest.getId(), null); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新密钥 |
| | | * |
| | | * @param body |
| | | */ |
| | | private static void managerLinkBroadcast(String body) { |
| | | try { |
| | | DeviceInfoResponse deviceInfoResponse = GsonConvert.getGson().fromJson(body, new TypeToken<DeviceInfoResponse>() { |
| | | }.getType()); |
| | | |
| | | if (deviceInfoResponse == null || deviceInfoResponse.getObjects() == null) { |
| | | return; |
| | | } |
| | | if (!TextUtils.isEmpty(deviceInfoResponse.getObjects().getLocalSecret())) { |
| | | byte[] baseBytes = Base64.decode(deviceInfoResponse.getObjects().getLocalSecret(), Base64.NO_WRAP); |
| | | String mackey = ""; |
| | | if (!TextUtils.isEmpty(SPUtils.getString("auth_mackey_key", ""))) { |
| | | mackey = SPUtils.getString("auth_mackey_key", ""); |
| | | byte[] bodyBytes = AesUtil.aesDecrypt(baseBytes, mackey.substring(mackey.length() - 16)); |
| | | String localSecret = new String(bodyBytes, "utf-8"); |
| | | Log.d("panlili", "更新密钥----->localSecret= " + localSecret); |
| | | HDLLinkConfig.getInstance().setLocalSecret(localSecret); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | LogUtils.i("LinkMessageDecoder.java:getLocalSecret----->e= " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 退网 |
| | | * |
| | | * @param body |
| | | */ |
| | | private static void managerDeleteNofity(String body) { |
| | | try { |
| | | DeviceDeleteResponse deviceDeleteResponse = GsonConvert.getGson().fromJson(body, new TypeToken<DeviceDeleteResponse>() { |
| | | }.getType()); |
| | | |
| | | if (deviceDeleteResponse == null || deviceDeleteResponse.getObjects() == null) { |
| | | return; |
| | | } |
| | | HDLSocket.getInstance().deleteNetwork(deviceDeleteResponse.getObjects().get(0).getOID(), new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | |
| | | } |
| | | }); |
| | | } catch (Exception e) { |
| | | LogUtils.i("LinkMessageDecoder.java:deleteNetwork----->e= " + e.getMessage()); |
| | | } |
| | | } |
| | | } |