| | |
| | | |
| | | import com.chad.library.adapter.base.BaseQuickAdapter; |
| | | import com.chad.library.adapter.base.listener.OnItemClickListener; |
| | | 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.connect.HDLLink; |
| | | import com.hdl.sdk.connect.bean.AuthenticateRequest; |
| | | import com.hdl.sdk.connect.bean.GatewaySearchBean; |
| | | import com.hdl.sdk.connect.config.HDLLinkConfig; |
| | | import com.hdl.sdk.connect.bean.LinkResponse; |
| | | import com.hdl.sdk.connect.bean.request.AuthenticateRequest; |
| | | import com.hdl.sdk.connect.bean.request.PropertyReadRequest; |
| | | import com.hdl.sdk.connect.bean.response.GatewaySearchBean; |
| | | import com.hdl.sdk.connect.callback.HDLLinkCallBack; |
| | | import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack; |
| | | import com.hdl.sdk.connect.socket.HDLAuthSocket; |
| | | import com.hdl.sdk.connect.socket.HDLSocket; |
| | | import com.hdl.sdk.connect.bean.DeviceControlRequest; |
| | | import com.hdl.sdk.connect.bean.request.DeviceControlRequest; |
| | | import com.hdl.sdk.connect.protocol.LinkMessageDecoder; |
| | | import com.hdl.sdk.connect.protocol.LinkMessageEncoder; |
| | | import com.hdl.sdk.socket.SocketOptions; |
| | |
| | | private TextView tv; |
| | | private TextView responseTv; |
| | | boolean isOn; |
| | | private EventListener allTopicsListener; |
| | | private String testLightSid = "000101B847C71B02020100010101"; |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | removeAllTopicsListener(); |
| | | } |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | |
| | | rv.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | checkIfCertified(); |
| | | registerAllTopicsListener(); |
| | | |
| | | ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { |
| | | @Override |
| | |
| | | launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}); |
| | | |
| | | final List<DemoBean> beans = new ArrayList<>(); |
| | | beans.add(new DemoBean("搜索网关")); |
| | | beans.add(new DemoBean("入网认证")); |
| | | beans.add(new DemoBean("搜索指定网关是否在线")); |
| | | beans.add(new DemoBean("获取功能列表")); |
| | | beans.add(new DemoBean("功能属性读取")); |
| | | beans.add(new DemoBean("设备控制")); |
| | | beans.add(new DemoBean("状态上报")); |
| | | beans.add(new DemoBean("读取状态")); |
| | | beans.add(new DemoBean("入网认证")); |
| | | beans.add(new DemoBean("搜索指定网关是否在线")); |
| | | beans.add(new DemoBean("获取场景列表")); |
| | | beans.add(new DemoBean("场景控制")); |
| | | demoAdapter = new DemoAdapter(beans); |
| | | rv.setAdapter(demoAdapter); |
| | | |
| | |
| | | public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) { |
| | | switch (position) { |
| | | case 0: |
| | | tv.setText("搜索网关中"); |
| | | responseTv.setText(""); |
| | | |
| | | HDLSocket.getInstance().searchGateway(new HDLSocket.CallBack() { |
| | | @Override |
| | | public void onError(String error) { |
| | | tv.setText("网关获取失败"); |
| | | } |
| | | |
| | | @Override |
| | | public void onResponse(String data) { |
| | | tv.setText("获取网关成功"); |
| | | responseTv.setText(data); |
| | | } |
| | | }); |
| | | |
| | | //入网认证 |
| | | sendAuthenticateRequest(); |
| | | break; |
| | | case 1: |
| | | |
| | | tv.setText("获取功能列表中"); |
| | | responseTv.setText(""); |
| | | HDLSocket.getInstance().getFunctionList(new HDLSocket.CallBack() { |
| | | @Override |
| | | public void onError(String error) { |
| | | tv.setText(error); |
| | | } |
| | | |
| | | @Override |
| | | public void onResponse(String data) { |
| | | tv.setText("获取功能列表成功"); |
| | | responseTv.setText(data); |
| | | } |
| | | }); |
| | | //搜索网关 |
| | | searchGatewayBroadcast(); |
| | | break; |
| | | case 2: |
| | | //功能属性读取 |
| | | // HDLSocket.getInstance().getFunctionAttribute(); |
| | | |
| | | //获取功能列表 |
| | | getFunctionList(); |
| | | break; |
| | | case 3: |
| | | //设备控制 |
| | | isOn = !isOn; |
| | | List<DeviceControlRequest> requestList = new ArrayList<>(); |
| | | DeviceControlRequest request = new DeviceControlRequest(); |
| | | request.setSid("000101B847C71B02020100010101"); |
| | | 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); |
| | | HDLAuthSocket.getInstance().propertyDown(requestList,null); |
| | | |
| | | |
| | | //功能属性读取 |
| | | getFunctionAttribute(); |
| | | break; |
| | | case 4: |
| | | //状态上报 |
| | | //HDLSocket.getInstance().propertyUp(); |
| | | //设备控制 |
| | | controlDecide(); |
| | | break; |
| | | case 5: |
| | | //读取状态 |
| | | // HDLSocket.getInstance().propertyRead(); |
| | | propertyRead(); |
| | | break; |
| | | case 6: |
| | | // //入网认证 |
| | | sendAuthenticateRequest(); |
| | | //获取场景列表 |
| | | getSceneList(); |
| | | break; |
| | | case 7: |
| | | // //如果已经认证后,搜索指定网关是否在线 |
| | | HDLLink.getInstance().searchGateway(new HDLAuthSocket.SearchGatewayCallBack() { |
| | | @Override |
| | | public void onEnd(String error) { |
| | | tv.setText("网关不在线"); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | tv.setText("网关在线"); |
| | | responseTv.setText("网关id:"+gatewaySearchBean.getGatewayId()); |
| | | // Log.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | } |
| | | }); |
| | | |
| | | |
| | | //获取场景列表 |
| | | controlScene(); |
| | | break; |
| | | } |
| | | } |
| | |
| | | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检测是否认证过 |
| | | */ |
| | | void checkIfCertified(){ |
| | | boolean isCertified = HDLLink.getInstance().checkIfCertified(); |
| | | showToast(isCertified?"已经认证过":"未认证"); |
| | | String mes = isCertified?"已经认证过":"未认证"; |
| | | showToast(mes); |
| | | tv.setText(mes); |
| | | } |
| | | |
| | | /** |
| | | * 注册所有主题数据的监听 |
| | | */ |
| | | void registerAllTopicsListener(){ |
| | | allTopicsListener = new EventListener() { |
| | | @Override |
| | | public void onMessage(Object msg) { |
| | | LinkResponse response = (LinkResponse)msg; |
| | | Log.i("TAG", "AllTopicsListener onMessage: "+msg.toString()); |
| | | } |
| | | }; |
| | | HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | /** |
| | | * 移除所有主题数据的监听 |
| | | */ |
| | | void removeAllTopicsListener(){ |
| | | HDLLink.getInstance().removeAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | /** |
| | | * 入网认证 |
| | | */ |
| | | void sendAuthenticateRequest(){ |
| | | tv.setText("开始入网认证..."); |
| | | |
| | | String macStr = "AA000000000000BB"; |
| | | String secret = "87ae414b7a853f65"; |
| | |
| | | String ip = IpUtils.getBroadcastAddress(); |
| | | // ip = "192.168.10.102"; |
| | | |
| | | HDLLink.getInstance().sendAuthenticateRequest(ip, request, new HDLAuthSocket.CallBack() { |
| | | HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onError(String error) { |
| | | Log.i("TAG", "onError: 认证失败"); |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("认证失败"); |
| | | responseTv.setText(e.getMsg()); |
| | | // Log.i("TAG", "onError: 认证失败"); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | public void onSuccess(String msg) { |
| | | tv.setText("认证成功"); |
| | | responseTv.setText(data.toString()); |
| | | responseTv.setText(msg.toString()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | String stringToMD5(String text) { |
| | | byte[] hash; |
| | |
| | | |
| | | return hex.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 如果已经认证后,搜索指定网关是否在线 |
| | | * 广播搜索认证过的网关是否在线 |
| | | */ |
| | | void searchGatewayBroadcast(){ |
| | | tv.setText("搜索网关中..."); |
| | | HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("网关不在线"); |
| | | } |
| | | @Override |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | tv.setText("网关在线"); |
| | | responseTv.setText("搜索成功 网关id:"+gatewaySearchBean.getGatewayId()); |
| | | // LogUtils.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 获取功能列表 |
| | | */ |
| | | void getFunctionList(){ |
| | | tv.setText("获取功能列表中..."); |
| | | responseTv.setText(""); |
| | | HDLSocket.getInstance().getFunctionList(new HDLLinkCallBack() { |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException error) { |
| | | tv.setText(error.getMsg()); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("获取功能列表成功"); |
| | | responseTv.setText(data); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 功能属性读取 |
| | | * 支持批量读取 |
| | | */ |
| | | void getFunctionAttribute(){ |
| | | tv.setText("功能属性读取"); |
| | | List<String> sids = new ArrayList<>(); |
| | | sids.add(testLightSid); |
| | | HDLSocket.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | responseTv.setText(msg); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 读取设备状态 |
| | | * 支持批量读取 |
| | | */ |
| | | void propertyRead(){ |
| | | tv.setText("读取状态中..."); |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(testLightSid);//要读取设备的sid |
| | | HDLSocket.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("读取成功"); |
| | | responseTv.setText(data); |
| | | } |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("读取失败"); |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 控制失败 |
| | | * 回复响应code为200 代表执行成功 |
| | | */ |
| | | void controlDecide(){ |
| | | tv.setText("控制设备"); |
| | | isOn = !isOn; |
| | | List<DeviceControlRequest> requestList = new ArrayList<>(); |
| | | DeviceControlRequest request = new DeviceControlRequest(); |
| | | request.setSid(testLightSid); |
| | | 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) { |
| | | responseTv.setText(data); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 获取场景列表 |
| | | */ |
| | | void getSceneList(){ |
| | | tv.setText("读取场景列表"); |
| | | HDLLink.getInstance().getSceneList(new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | responseTv.setText(msg); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | //场景列表 |
| | | // {"id":"8a5eaa143ce943b987b577df5a66759b","time_stamp":"1637040217235","objects":[{"sid":"04010560D2C7170A0A0100000000","name":"回家模式","status":"off","group":"255","delay":"0","modify_time":"1634871490"},{"sid":"04010560D2C76E0A0A0100010000","name":"离家模式","status":"off","group":"255","delay":"0","modify_time":"1634785823"}]} |
| | | /** |
| | | * 控制场景 |
| | | * 执行成功的话 响应code为200 |
| | | */ |
| | | void controlScene(){ |
| | | tv.setText("场景控制"); |
| | | //控制场景sid列表,支持批量控制 |
| | | List<String> sids = new ArrayList<>(); |
| | | isOn = !isOn; |
| | | if(isOn){ |
| | | sids.add("04010560D2C7170A0A0100000000"); |
| | | }else{ |
| | | sids.add("04010560D2C76E0A0A0100010000"); |
| | | } |
| | | HDLLink.getInstance().controlScene(sids,new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | responseTv.setText(msg); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |