hxb
2022-03-27 cc6023291bc94a229b53d7c2d83bf2b39bbfa9a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.hdl.sdk.connect.config;
 
import android.text.TextUtils;
 
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.SPUtils;
import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
 
 
/**
 * Created by jlchen on 11/11/21.
 *
 * @Description : HDLLinkConfig
 */
public class HDLLinkConfig {
    private static final String AUTHENTICATE_LS_KEY = "auth_ls_key";
    private static final String AUTHENTICATE_GATEWAYID_KEY = "auth_gatewayid_key";
    private static final String AUTHENTICATE_IPADDRESS_KEY = "auth_ipaddress_key";
    private static final String AUTHENTICATE_IS_LS_KEY = "auth_isls_key";
 
    private String localSecret;//本地加密密钥
    private String gatewayId;
    private String ipAddress;
    private GatewaySearchBean currentGateway;//当前网关
 
    private AuthenticateRequest.AuthenticateDeviceInfoBean deviceInfoBean;//当前设备基本信息
 
    /**
     * instance
     */
    private  static final HDLLinkConfig instance=new HDLLinkConfig();
    private HDLLinkConfig()
    {
        loadConfig();
    }
 
    /**
     * getInstance
     *
     * @return AuthenticateConfig
     */
    public static  HDLLinkConfig getInstance() {
//        if (instance == null) {
//            synchronized (HDLLinkConfig.class) {
//                if (instance == null) {
//                    instance = new HDLLinkConfig();
//                    instance.loadConfig();
//                }
//            }
//        }
        return instance;
    }
 
    /**
     * 清空缓存
     */
    public void clearConfig() {
        this.gatewayId = "";
        this.ipAddress = "";
        this.localSecret = "";
        SPUtils.remove(AUTHENTICATE_LS_KEY);
        SPUtils.remove(AUTHENTICATE_GATEWAYID_KEY);
        SPUtils.remove(AUTHENTICATE_IPADDRESS_KEY);
        SPUtils.remove(AUTHENTICATE_IS_LS_KEY);
    }
 
    /**
     * 加载缓存
     */
    void loadConfig() {
        localSecret = SPUtils.getString(AUTHENTICATE_LS_KEY, "");
        gatewayId = SPUtils.getString(AUTHENTICATE_GATEWAYID_KEY, "");
        ipAddress = SPUtils.getString(AUTHENTICATE_IPADDRESS_KEY, "");
    }
 
    /**
     * 保存配置
     *
     * @param localSecret
     * @param gatewayId
     * @param ipAddress
     */
    public void saveConfig(String localSecret, String gatewayId, String ipAddress) {
        this.localSecret = localSecret;
        this.gatewayId = gatewayId;
        this.ipAddress = ipAddress;
        SPUtils.put(AUTHENTICATE_LS_KEY, localSecret);
        SPUtils.put(AUTHENTICATE_GATEWAYID_KEY, gatewayId);
        SPUtils.put(AUTHENTICATE_IPADDRESS_KEY, ipAddress);
    }
 
    /**
     * 重新保存
     */
    public void reSaveConfig() {
        this.saveConfig(this.localSecret, this.gatewayId, this.ipAddress);
    }
 
    /**
     * 检测是否已经认证过
     *
     * @return
     */
    public boolean checkIfCertified() {
        //localSecret不为空并且长度等于16 网关id不能为空
        return !TextUtils.isEmpty(localSecret)
                && localSecret.length() == 16
                && !TextUtils.isEmpty(gatewayId);
    }
 
    public void setLocalSecret(String localSecret) {
        this.localSecret = localSecret;
        SPUtils.put(AUTHENTICATE_LS_KEY, localSecret);
    }
 
    public String getLocalSecret() {
        return localSecret;
    }
 
    public String getGatewayId() {
        return gatewayId;
    }
    public void setGatewayId(String gatewayId) {
        this.gatewayId=gatewayId;
    }
 
    public String getIpAddress() {
        return ipAddress;
    }
    public void setIpAddress(String ipAddress){
        this.ipAddress = ipAddress;
    }
 
    public GatewaySearchBean getCurrentGateway() {
        return currentGateway;
    }
 
    public void setCurrentGateway(GatewaySearchBean currentGateway) {
        this.currentGateway = currentGateway;
        if (this.currentGateway != null && !TextUtils.isEmpty(this.currentGateway.getIp_address())) {
            if (!this.ipAddress.equals(this.currentGateway.getIp_address())) {
                //如果IP地址有变化则更新
                this.ipAddress = this.currentGateway.getIp_address();
                SPUtils.put(AUTHENTICATE_IPADDRESS_KEY, ipAddress);
            }
        }
    }
 
    /**
     * 判断当前主题数据是否需要加密
     *
     * @param topicStr 当前主题
     * @return
     */
    public boolean ifNeedEncrypt(String topicStr) {
        //过滤相关需要加密的主题
        return (!topicStr.contains(TopicConstant.GATEWAY_AUTH_BROADCAST) //网关广播入网指令
                && !topicStr.contains(TopicConstant.DEIVCE_AUTH_REQUEST) //入网认证
                && !topicStr.contains(TopicConstant.GATEWAY_SEARCH) //搜索网关主题
                && !topicStr.contains(TopicConstant.BROADCAST)
                && !TextUtils.isEmpty(localSecret) //有密钥就是加密
        );
    }
 
    /**
     * 拼接网关ID获取完整的主题
     *
     * @param topicStr
     * @return
     */
    public String getFullTopic(String topicStr) {
        return String.format(topicStr, gatewayId);
    }
 
    public AuthenticateRequest.AuthenticateDeviceInfoBean getDeviceInfoBean() {
        return deviceInfoBean;
    }
 
    public void setDeviceInfoBean(AuthenticateRequest.AuthenticateDeviceInfoBean deviceInfoBean) {
        this.deviceInfoBean = deviceInfoBean;
    }
}