package com.hdl.sdk.connect;
|
|
import android.text.TextUtils;
|
import android.util.Log;
|
|
import com.google.gson.Gson;
|
import com.google.gson.JsonObject;
|
import com.google.gson.reflect.TypeToken;
|
import com.hdl.sdk.common.config.AuthenticateConfig;
|
import com.hdl.sdk.common.config.TopicConstant;
|
import com.hdl.sdk.common.utils.IdUtils;
|
import com.hdl.sdk.common.utils.IpUtils;
|
import com.hdl.sdk.common.utils.gson.GsonConvert;
|
import com.hdl.sdk.connect.bean.AuthenticateRequest;
|
import com.hdl.sdk.connect.bean.AuthenticateRequest.AuthenticateDeviceInfoBean;
|
import com.hdl.sdk.connect.bean.BaseLocalResponse;
|
import com.hdl.sdk.connect.bean.DeviceControlRequest;
|
import com.hdl.sdk.connect.bean.GatewaySearchBean;
|
import com.hdl.sdk.connect.bean.LinkRequest;
|
import com.hdl.sdk.connect.bean.LinkResponse;
|
import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
|
import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
|
import com.hdl.sdk.socket.SocketBoot;
|
import com.hdl.sdk.socket.SocketOptions;
|
import com.hdl.sdk.socket.client.UdpClient;
|
import com.hdl.sdk.socket.codec.MessagePipeLine;
|
|
import java.io.UnsupportedEncodingException;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.util.List;
|
|
import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
|
|
/**
|
* Created by jlchen on 11/11/21.
|
*
|
* @Description : HDLAuthSocket
|
*/
|
public class HDLAuthSocket {
|
/**
|
* udp默认端口
|
*/
|
private static final int UDP_PORT = 8585;
|
private static SocketBoot updBoot;
|
/**
|
* instance
|
*/
|
private volatile static HDLAuthSocket instance;
|
|
public interface CallBack {
|
|
void onError(String error);
|
|
void onSuccess(String data);
|
|
}
|
|
/**
|
* getInstance
|
*
|
* @return HDLAuthSocket
|
*/
|
public static synchronized HDLAuthSocket getInstance() {
|
if (instance == null) {
|
synchronized (AuthenticateConfig.class) {
|
if (instance == null) {
|
instance = new HDLAuthSocket();
|
}
|
}
|
}
|
return instance;
|
}
|
|
private SocketOptions getUdpOptions() {
|
final SocketOptions options = new SocketOptions();
|
final MessagePipeLine pipeLine = new MessagePipeLine();
|
pipeLine.add(new LinkMessageDecoder());
|
pipeLine.add(new LinkMessageEncoder());
|
options.setHandleMessage(pipeLine);
|
options.setEnabledHeartbeat(false);
|
return options;
|
}
|
|
private SocketBoot getUdpBoot(String ip) {
|
if (updBoot == null) {
|
updBoot = UdpClient.init(ip, UDP_PORT, getUdpOptions());
|
updBoot.connect();
|
}
|
return updBoot;
|
}
|
|
private String getOid() {
|
return "000101EA9B8C1C22";
|
}
|
|
private String getSid() {
|
return "000101EA9B8C1C2202010001021A";
|
}
|
|
/**
|
* 发送入网及认证请求
|
*
|
* @param ip 网关IP
|
* @param request 认证请求信息
|
* @param callBack 结果回调
|
*/
|
public void sendAuthenticateRequest(String ip, AuthenticateRequest request, CallBack callBack) {
|
if (request == null) {
|
callBack.onError("request null");
|
}
|
String topic = DEIVCE_AUTH_REQUEST;
|
Gson gs = new Gson();
|
String requestStr = gs.toJson(request);
|
LinkRequest message = new LinkRequest(topic, requestStr);
|
|
HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() {
|
@Override
|
public void onSucceed(Object msg) {
|
callBack.onSuccess(msg.toString());
|
}
|
|
@Override
|
public void onFailure() {
|
Log.i("TAG", "onFailure: ");
|
callBack.onError("超时");
|
}
|
});
|
}
|
|
/**
|
* 发送入网及认证请求
|
*
|
* @param callBack 结果回调
|
*/
|
public void sendAuthenticateRequest(CallBack callBack) {
|
String macStr = "AA000000000000BB";
|
String secret = "87ae414b7a853f65";
|
String mac_key = stringToMD5(stringToMD5(macStr + secret));
|
|
String versionString = "HDL_V1.0.1";
|
String time = String.valueOf(System.currentTimeMillis());
|
|
//1.设置认证信息
|
AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean();
|
requestBean.setMAC(macStr);
|
requestBean.setSupplier("HDL");
|
requestBean.setFirmwareVersion(versionString);
|
requestBean.setHardwareModel("1956F");
|
AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean();
|
authbean.setSpk("ir.module");
|
authbean.setMACKey(mac_key);
|
authbean.setRequest(requestBean);
|
|
|
//2.设置设备信息
|
AuthenticateDeviceInfoBean infoBean = new AuthenticateDeviceInfoBean();
|
infoBean.setDeviceMAC(macStr);
|
infoBean.setIPMAC(macStr);
|
infoBean.setDeviceName("HDL面板");
|
infoBean.setAccessMode("HDL");
|
infoBean.setOID(getOid());
|
infoBean.setSid(getSid());
|
// infoBean.set
|
AuthenticateRequest.VersionBean[] versionBeans = new AuthenticateRequest.VersionBean[]{new AuthenticateRequest.VersionBean("FW", versionString), new AuthenticateRequest.VersionBean("HW", "1956F")};
|
infoBean.setVersions(versionBeans);
|
AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, infoBean, authbean);
|
|
String ip = IpUtils.getBroadcastAddress();
|
ip = "192.168.10.102";
|
sendAuthenticateRequest(ip, request, callBack);
|
}
|
|
public interface SearchGatewayCallBack {
|
/**
|
* 搜索结束
|
*
|
* @param error
|
*/
|
void onEnd(String error);
|
|
/**
|
* 搜索网关成功
|
*
|
* @param gatewaySearchBean
|
*/
|
void onSuccess(GatewaySearchBean gatewaySearchBean);
|
}
|
|
/**
|
* 开始搜索所有网关,有网关回复就回调,上层自己做去重判断
|
*
|
* @param callBack 回调
|
*/
|
public void startSearchAllGateway(SearchGatewayCallBack callBack) {
|
|
}
|
|
/**
|
* 暂停搜索网关
|
*/
|
public void endSearchAllGateway() {
|
|
}
|
|
/**
|
* 搜索指定网关是否在线,搜索到则返回指定的网关对象
|
*
|
* @param gatewayId 网关id
|
* @param callBack 回调
|
*/
|
public void searchGateway(String gatewayId, SearchGatewayCallBack callBack) {
|
String time = String.valueOf(System.currentTimeMillis());
|
JsonObject jsonObject = new JsonObject();
|
jsonObject.addProperty("id", IdUtils.getUUId());
|
jsonObject.addProperty("time_stamp", time);
|
String ip = IpUtils.getBroadcastAddress();
|
LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
|
jsonObject.toString());
|
|
HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() {
|
@Override
|
public void onSucceed(Object msg) {
|
GatewaySearchBean searchBean = getGatewaySearchBean(msg);
|
if (searchBean != null && searchBean.getGatewayId() == gatewayId) {
|
Log.i("TAG", "onSuccess: ");
|
callBack.onSuccess(searchBean);
|
}
|
}
|
|
@Override
|
public void onFailure() {
|
Log.i("TAG", "onFailure: ");
|
callBack.onEnd("超时");
|
}
|
});
|
}
|
|
private GatewaySearchBean getGatewaySearchBean(Object msg) {
|
GatewaySearchBean searchBean = null;
|
if (msg instanceof LinkResponse) {
|
LinkResponse linkResponse = (LinkResponse) msg;
|
String data = linkResponse.getData();
|
if (!TextUtils.isEmpty(data)) {
|
final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
|
}.getType());
|
searchBean = response.getObjects();
|
}
|
|
}
|
return searchBean;
|
}
|
|
/**
|
* 设备控制
|
*/
|
public void propertyDown(List<DeviceControlRequest> request, HDLSocket.CallBack callBack) {
|
|
// if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
|
String time = String.valueOf(System.currentTimeMillis());
|
|
final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>();
|
data.setId(IdUtils.getUUId());
|
data.setTime_stamp(time);
|
data.setObjects(request);
|
|
|
String topic = String.format(TopicConstant.PROPERTY_DOWN, "1406844230123372545");
|
LinkRequest message = new LinkRequest(topic,
|
GsonConvert.getGson().toJson(request));
|
|
Log.i("TAG", "propertyDown: " + message.getData());
|
|
String ip = IpUtils.getBroadcastAddress();
|
HdlSocketHelper.send(getUdpBoot(ip), message, null);
|
|
// }
|
}
|
|
String stringToMD5(String text) {
|
byte[] hash;
|
try {
|
hash = MessageDigest.getInstance("MD5").digest(text.getBytes("UTF-8"));
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
return null;
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
return null;
|
}
|
|
StringBuilder hex = new StringBuilder(hash.length * 2);
|
for (byte b : hash) {
|
if ((b & 0xFF) < 0x10)
|
hex.append("0");
|
hex.append(Integer.toHexString(b & 0xFF));
|
}
|
|
return hex.toString();
|
}
|
|
}
|