JLChen
2021-12-10 dfb62af125bcb4eed4eadbcfc5a49a1232501c0c
2021-12-10 1.增加tcp和UDP 单次发送,不监听回复
5个文件已修改
213 ■■■■ 已修改文件
HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDLSDK/app/src/main/java/com/hdl/hdlsdk/device/DevicesListAdapter.java 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/HDLLink.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -19,14 +19,17 @@
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlsdk.device.DevicesListActivity;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.event.EventListener;
import com.hdl.sdk.common.exception.HDLLinkException;
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.HDLLink;
import com.hdl.sdk.connect.bean.LinkRequest;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
import com.hdl.sdk.connect.bean.request.PropertyReadRequest;
@@ -100,6 +103,8 @@
        beans.add(new DemoBean("获取场景列表"));
        beans.add(new DemoBean("场景控制"));
        beans.add(new DemoBean("设备功能列表"));
        beans.add(new DemoBean("UDP发送"));
        beans.add(new DemoBean("TCP发送"));
        demoAdapter = new DemoAdapter(beans);
        rv.setAdapter(demoAdapter);
@@ -150,10 +155,21 @@
                        //功能列表
                        startDevicesListActivity();
                        break;
                    case 9:
                        //UDP发送
                        udpSend();
                        break;
                    case 10:
                        //TCP发送
                        tcpSend();
                        break;
                }
            }
        });
    }
    public void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
@@ -231,9 +247,9 @@
        tv.setText("开始入网认证...");
        //认证提交参数准备
        String spkStr = "ir.module";//产品spk
        String macStr = "AA000000000000BB";//设备唯一MAC地址
        String secret = "87ae414b7a853f65";//通过spk和mac提交云端认证后分配的secret
        String spkStr = "screen.mirror";//产品spk
        String macStr = "AA000000000000AC";//设备唯一MAC地址
        String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret
        String mac_key = stringToMD5(stringToMD5(macStr + secret));
        String versionString = "HDL_V1.0.1";//
        String time = String.valueOf(System.currentTimeMillis());
@@ -241,7 +257,7 @@
        //1.设置认证信息
        AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean();
        requestBean.setMAC(macStr);
        requestBean.setSupplier("HDL");
        requestBean.setSupplier("raysgem");
        requestBean.setFirmwareVersion(versionString);
        requestBean.setHardwareModel("1956F");
        AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean();
@@ -256,7 +272,7 @@
        infoBean.setDeviceName("红外宝");//设备名字
        infoBean.setDeviceModel("HDL");//
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.12.1");
        infoBean.setIPGateway("192.168.88.1");
        infoBean.setIPAddress(IpUtils.getIP(this));
        infoBean.setOID("010105000000FE08");
@@ -469,4 +485,26 @@
        Intent intent = new Intent(this, DevicesListActivity.class);
        startActivity(intent);
    }
    /**
     * TCP发送 只发一次,不监听回复,不重发
     */
    private void tcpSend() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().tcpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
    }
    /**
     * 只发一次,不监听回复,不重发
     */
    private void udpSend() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().udpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
    }
}
HDLSDK/app/src/main/java/com/hdl/hdlsdk/device/DevicesListAdapter.java
@@ -4,6 +4,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
@@ -14,7 +16,13 @@
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hdl.hdlsdk.DemoBean;
import com.hdl.hdlsdk.R;
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import java.util.ArrayList;
import java.util.List;
public class DevicesListAdapter extends RecyclerView.Adapter<DevicesListAdapter.ViewHolder> {
@@ -50,6 +58,12 @@
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.item_tv.setText(mList.get(position).getName());
        holder.device_on_off_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                propertyDown(mList.get(position), isChecked);
            }
        });
    }
    @Override
@@ -60,10 +74,38 @@
    //自定义的ViewHolder,持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView item_tv;
        public Switch device_on_off_switch;
        public ViewHolder(View view) {
            super(view);
            item_tv = (TextView) view.findViewById(R.id.device_title_tv);
            device_on_off_switch = view.findViewById(R.id.device_on_off_switch);
        }
    }
    private void propertyDown(FunctionBean functionBean, Boolean isOn){
        List<DeviceControlRequest> requestList = new ArrayList<>();
        DeviceControlRequest request = new DeviceControlRequest();
        request.setSid(functionBean.getSid());
        List<DeviceControlRequest.StatusBean>  statusBeanList= new ArrayList<>();
        DeviceControlRequest.StatusBean bean =  new DeviceControlRequest.StatusBean();
        bean.setKey("on_off");
        bean.setValue(isOn ? "on" : "off");
        statusBeanList.add(bean);
        request.setStatus(statusBeanList);
        requestList.add(request);
        HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
                LogUtils.i(data);
//                responseTv.setText(data);
            }
            @Override
            public void onError(HDLLinkException e) {
//                responseTv.setText(e.getMsg());
                LogUtils.i("code:"+e.getCode() + " " + e.getMsg());
            }
        });
    }
}
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/HDLLink.java
@@ -198,4 +198,24 @@
    public void tcpSendMsg(String topic, String bodyStr, HDLLinkCallBack callBack) {
        HDLSocket.getInstance().tcpSendMsg(topic, bodyStr, callBack);
    }
    /**
     * 通用发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     */
    public void udpSendMsg(String topic, String bodyStr) {
        HDLAuthSocket.getInstance().udpSendMsg(topic, bodyStr);
    }
    /**
     * 通用TCP发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     */
    public void tcpSendMsg(String topic, String bodyStr) {
        HDLSocket.getInstance().tcpSendMsg(topic, bodyStr);
    }
}
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -170,20 +170,31 @@
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                AuthenticateResponse bean = getAuthenticateResponseBean(msg);
                if (bean != null) {
                    String localSecret = bean.getAuth().getLocalSecret();
                    String gatewayId = bean.getObjects().getGatewayID();
                    String ipAddress = bean.getObjects().getIPAddress();
                    //判断网关是否已经注册到云端
                    if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
                        //认证失败,网关未注册到云端
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
                try {
                    AuthenticateResponse bean = getAuthenticateResponseBean(msg);
                    if (bean != null) {
                        String localSecret = "";
                        String gatewayId = "";
                        String ipAddress = "";
                        if(bean.getAuth() != null) {
                            localSecret = bean.getAuth().getLocalSecret();
                        }
                        if(bean.getObjects() != null){
                             gatewayId = bean.getObjects().getGatewayID();
                             ipAddress = bean.getObjects().getIPAddress();
                        }
                        //判断网关是否已经注册到云端
                        if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
                            //认证失败,网关未注册到云端
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
                        } else {
                            HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                            callBack.onSuccess("认证成功");
                        }
                    } else {
                        HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                        callBack.onSuccess("认证成功");
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                    }
                } else {
                } catch (Exception e) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                }
            }
@@ -285,8 +296,6 @@
    }
    /**
     * 设备控制
     */
@@ -350,6 +359,21 @@
        );
    }
    /**
     * 通用发送指令 只发一次,不监听回复,不重发
     *
     * @param topic   发送数据
     * @param bodyStr 回复的主题
     */
    public void udpSendMsg(String topic, String bodyStr) {
        if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
            LogUtils.e("udpSendMsg", "参数不能为空");
            return;
        }
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = IpUtils.getBroadcastAddress();
        getUdpBoot(ip).sendMsg(message.getSendBytes());
    }
    private GatewaySearchBean getGatewaySearchBean(Object msg) {
        GatewaySearchBean searchBean = null;
@@ -399,13 +423,15 @@
    /**
     * 网关搜索相关
     */
    private static final int MAX_SEARCH_COUNT  = 10;//总共搜索测试
    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);;
    private static final int MAX_SEARCH_COUNT = 10;//总共搜索测试
    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);
    ;
    private final AtomicBoolean isSearchGatewaySuccess = new AtomicBoolean(true);
    private String searchGatewayId = "";
    private SearchGatewayCallBack mSearchGatewayCallBack;
    private void initSearchGatewayEvent(){
        LogUtils.i("搜索网关--","initSearchGatewayEvent");
    private void initSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "initSearchGatewayEvent");
        searchGatewayEvent = new EventListener() {
            @Override
            public void onMessage(Object msg) {
@@ -425,7 +451,7 @@
                                    searchGatewayCount.set(11);//次数标记
                                    HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//设置当前网关
                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//设置是否加密
                                    if(mSearchGatewayCallBack != null){
                                    if (mSearchGatewayCallBack != null) {
                                        mSearchGatewayCallBack.onSuccess(searchBean);
                                    }
                                }
@@ -463,11 +489,11 @@
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get()) ) {
                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
                    try {
                        //搜索网关
                        searchGatewayCount.set(searchGatewayCount.get() + 1);
                        LogUtils.i("搜索网关--","搜索网关第"+searchGatewayCount.get()+"次");
                        LogUtils.i("搜索网关--", "搜索网关第" + searchGatewayCount.get() + "次");
                        getUdpBoot(ip).sendMsg(message.getSendBytes());
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
@@ -475,10 +501,10 @@
                    }
                }
                if(!isSearchGatewaySuccess.get()){
                if (!isSearchGatewaySuccess.get()) {
                    //搜索10次,指定网关都没回复,回调超时
                    callBackSearchGatewayTimeout();
                    LogUtils.e("搜索网关--","搜索10次,指定网关都没回复,回调超时");
                    LogUtils.e("搜索网关--", "搜索10次,指定网关都没回复,回调超时");
                }
            }
        }).start();
@@ -488,27 +514,28 @@
    /**
     * 注册搜索网关监听
     */
    private void registerSearchGatewayEvent(){
        LogUtils.i("搜索网关--","注册搜索网关监听");
    private void registerSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "注册搜索网关监听");
        EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
    }
    /**
     * 移除搜索网关监听
     */
    private void removeSearchGatewayEvent(){
        LogUtils.i("搜索网关--","移除搜索网关监听");
    private void removeSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "移除搜索网关监听");
        EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
    }
    /**
     * 回调搜索网关超时
     */
    private void callBackSearchGatewayTimeout(){
    private void callBackSearchGatewayTimeout() {
        removeSearchGatewayEvent();
        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(mSearchGatewayCallBack != null){
                if (mSearchGatewayCallBack != null) {
                    mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
                }
            }
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java
@@ -682,6 +682,24 @@
        }
    }
    /**
     * 通用TCP发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     */
    public void tcpSendMsg(String topic, String bodyStr) {
        try {
            if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
                LogUtils.e("udpSendMsg", "参数不能为空");
                return;
            }
            LinkRequest message = new LinkRequest(topic, bodyStr);
            getTcp().sendMsg(message.getSendBytes());
        } catch (Exception e) {
            LogUtils.e("tcpSendMsg", "发送失败 :"+e.getMessage());
        }
    }
    /**
     * 发送指令