package com.hdl.sdk.common.utils;
|
|
import android.text.TextUtils;
|
|
import com.google.gson.reflect.TypeToken;
|
import com.hdl.sdk.common.config.TopicConstant;
|
import com.hdl.sdk.common.utils.gson.GsonConvert;
|
import com.hdl.sdk.connect.bean.LinkResponse;
|
import com.hdl.sdk.connect.bean.request.GatewayRemoteEditRequest;
|
import com.hdl.sdk.connect.bean.response.DeviceInfoResponse;
|
import com.hdl.sdk.connect.config.HDLLinkConfig;
|
import com.hdl.sdk.connect.socket.HDLSocket;
|
|
import java.time.chrono.IsoChronology;
|
|
/**
|
* Created by hxb on 2023/9/14.
|
*/
|
public class AllTopicManagerUtils {
|
|
/**
|
* 全局主题处理方法,可以处理所有接收到的数据
|
*
|
* @param linkResponse
|
*/
|
public static void manager(LinkResponse linkResponse) {
|
try {
|
String topic = linkResponse.getTopic();
|
if (TextUtils.isEmpty(topic)) {
|
return;
|
}
|
String[] topics = topic.split("/");
|
//非当前住宅网关的数据返回
|
if (topics.length < 3) {
|
return;
|
}
|
|
String mac_Oid_GatewayId = topics[2];
|
|
if(!isLocalDevice(mac_Oid_GatewayId)){
|
LogUtils.i("不是当前设备的网关Id,Id是"+mac_Oid_GatewayId);
|
//非当前设备的数据
|
return;
|
}
|
|
if(isSameTopic(TopicConstant.GATEWAY_EDIT_REMOTE,topic)){
|
gatewayRemoteEditRequest(mac_Oid_GatewayId, linkResponse.getData());
|
}
|
|
//TODO 通过增加if else 增加其它主题类似这样使用
|
|
} catch (Exception e) {
|
LogUtils.e("全局处理模块异常:" + e.getMessage());
|
}
|
}
|
|
/**
|
* 是否当前的设备
|
* @param mac_Oid_GatewayId
|
* @return
|
*/
|
private static boolean isLocalDevice(String mac_Oid_GatewayId) {
|
if (TextUtils.isEmpty(mac_Oid_GatewayId)) {
|
return false;
|
}
|
String mac = HDLLinkConfig.getInstance().getDeviceInfoBean().getDeviceMAC();
|
String oid = "123";//HDLLinkConfig.getInstance().getDeviceInfoBean().getOID();
|
String gatewayId = HDLLinkConfig.getInstance().getGatewayId();
|
|
return mac_Oid_GatewayId.equals(mac) || mac_Oid_GatewayId.equals(oid) || mac_Oid_GatewayId.equals(gatewayId);
|
}
|
|
/**
|
* 是否相同主题
|
* @param targetTopic 目标的主题
|
* @param sourceTopic 接收的主题
|
* @return
|
*/
|
private static boolean isSameTopic(String targetTopic,String sourceTopic) {
|
if (TextUtils.isEmpty(targetTopic) || TextUtils.isEmpty(sourceTopic)) {
|
return false;
|
}
|
String[] targetTopics = targetTopic.split("/");
|
String[] sourceTopics = sourceTopic.split("/");
|
|
if (targetTopics.length != sourceTopics.length) {
|
return false;
|
}
|
for (int i = 0; i < targetTopics.length; i++) {
|
if(i==2){
|
//这个位置不匹配,是网关id
|
continue;
|
}
|
if (!targetTopics[i] .equals( sourceTopics[i])) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
/**
|
* 网关信息配置
|
* @param body
|
*/
|
private static void gatewayRemoteEditRequest(String mac_Oid_GatewayId,String body) {
|
if (TextUtils.isEmpty(body)) {
|
return;
|
}
|
GatewayRemoteEditRequest gatewayRemoteEditRequest = GsonConvert.getGson().fromJson(body, new TypeToken<GatewayRemoteEditRequest>() {
|
}.getType());
|
|
if (gatewayRemoteEditRequest == null || gatewayRemoteEditRequest.getObjects() == null) {
|
return;
|
}
|
|
String homeId = gatewayRemoteEditRequest.getObjects().getHomeId();
|
HDLLinkConfig.getInstance().setHomeId(homeId);
|
|
HDLSocket.getInstance().gatewayRemoteEditReply(mac_Oid_GatewayId, gatewayRemoteEditRequest.getId(), null);
|
}
|
}
|