| | |
| | | import androidx.activity.result.ActivityResultCallback; |
| | | import androidx.activity.result.ActivityResultLauncher; |
| | | import androidx.activity.result.contract.ActivityResultContracts; |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import androidx.annotation.RequiresApi; |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | |
| | | import com.chad.library.adapter.base.BaseQuickAdapter; |
| | | import com.chad.library.adapter.base.listener.OnItemClickListener; |
| | | import com.google.gson.reflect.TypeToken; |
| | | 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.gson.GsonConvert; |
| | | import com.hdl.sdk.connect.HDLLink; |
| | | import com.hdl.sdk.connect.bean.LinkResponse; |
| | | import com.hdl.sdk.connect.bean.request.AuthenticateRequest; |
| | | import com.hdl.sdk.connect.bean.response.BaseLocalResponse; |
| | | 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.config.HDLLinkConfig; |
| | | import com.hdl.sdk.connect.socket.HDLAuthSocket; |
| | | import com.hdl.sdk.connect.bean.request.DeviceControlRequest; |
| | | import com.hdl.sdk.socket.SocketBoot; |
| | | import com.hdl.sdk.link.HDLLinkLocalSdk; |
| | | import com.hdl.sdk.link.bean.LinkFunctionBean; |
| | | import com.hdl.sdk.link.bean.LinkSceneBean; |
| | | import com.hdl.sdk.link.common.config.TopicConstant; |
| | | import com.hdl.sdk.link.common.event.EventListener; |
| | | import com.hdl.sdk.link.common.exception.HDLLinkException; |
| | | import com.hdl.sdk.link.common.utils.IdUtils; |
| | | import com.hdl.sdk.link.common.utils.gson.GsonConvert; |
| | | import com.hdl.sdk.link.core.bean.LinkResponse; |
| | | import com.hdl.sdk.link.core.bean.request.AuthenticateRequest; |
| | | import com.hdl.sdk.link.core.bean.request.DeviceControlRequest; |
| | | import com.hdl.sdk.link.core.bean.response.BaseLocalResponse; |
| | | import com.hdl.sdk.link.core.bean.response.GatewaySearchBean; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkCallBack; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkResponseCallBack; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkTCallBack; |
| | | import com.hdl.sdk.link.core.config.HDLLinkConfig; |
| | | import com.hdl.sdk.link.core.connect.HDLUdpConnect; |
| | | |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.MessageDigest; |
| | |
| | | removeAllTopicsListener(); |
| | | } |
| | | |
| | | void init() { |
| | | HDLLinkConfig.getInstance().setLocalSecret("e186beeb7974998e"); |
| | | HDLLinkConfig.getInstance().setGatewayId("1519863101509361666"); |
| | | /** |
| | | * 初始化HDLSDK |
| | | */ |
| | | void initHDLSDK() { |
| | | HDLLinkLocalSdk.getInstance().init("1530018255883706369","735c1677504a7034"); |
| | | } |
| | | |
| | | /** |
| | | * 三方设备集成到HDL时,需要初始化的信息 |
| | | */ |
| | | void initThird(){ |
| | | initDeviceInfo();//初始化基本信息,非常重要,认证时要用 |
| | | checkIfCertified();//检测是否已经认证了,用于判断 |
| | | } |
| | | |
| | | @RequiresApi(api = Build.VERSION_CODES.O) |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | |
| | | setContentView(R.layout.activity_main); |
| | | responseTv = findViewById(R.id.response_tv); |
| | | tv = findViewById(R.id.state_tv); |
| | | rv = findViewById(R.id.rv); |
| | | rv.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | // init(); |
| | | checkIfCertified(); |
| | | initDeviceInfo();//初始化基本信息,非常重要,认证时要用 |
| | | registerAllTopicsListener(); |
| | | initView(); |
| | | initHDLSDK();//这步比较重要,在使用前要设置好当前网关Id及加解密钥 |
| | | // initThird();//三方设备集成时要初始化 |
| | | 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<>(); |
| | |
| | | rv.setAdapter(demoAdapter); |
| | | |
| | | |
| | | // final SocketOptions options = new SocketOptions(); |
| | | // |
| | | // MessagePipeLine pipeLine = new MessagePipeLine(); |
| | | // options.setHandleMessage(pipeLine); |
| | | // options.setEnabledHeartbeat(false); |
| | | |
| | | demoAdapter.setOnItemClickListener(new OnItemClickListener() { |
| | | @Override |
| | | public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) { |
| | | public void onItemClick(BaseQuickAdapter<?, ?> adapter, View view, int position) { |
| | | switch (position) { |
| | | case 0: |
| | | //入网认证 |
| | |
| | | } |
| | | }); |
| | | } |
| | | //初始化控件 |
| | | void initView(){ |
| | | responseTv = findViewById(R.id.response_tv); |
| | | tv = findViewById(R.id.state_tv); |
| | | rv = findViewById(R.id.rv); |
| | | rv.setLayoutManager(new LinearLayoutManager(this)); |
| | | } |
| | | |
| | | |
| | | public void showToast(String text) { |
| | |
| | | * 检测是否认证过 |
| | | */ |
| | | void checkIfCertified() { |
| | | boolean isCertified = HDLLink.getInstance().checkIfCertified(); |
| | | boolean isCertified = HDLLinkLocalSdk.getInstance().checkIfCertified(); |
| | | String mes = isCertified ? "已经认证过" : "未认证"; |
| | | showToast(mes); |
| | | tv.setText(mes); |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | handleLinkResponse(response); |
| | | } |
| | | }; |
| | | HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); |
| | | HDLLinkLocalSdk.getInstance().registerAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } else { |
| | | //其它主题 |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | * 移除所有主题数据的监听 |
| | | */ |
| | | void removeAllTopicsListener() { |
| | | HDLLink.getInstance().removeAllTopicsListener(allTopicsListener); |
| | | HDLLinkLocalSdk.getInstance().removeAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | void initDeviceInfo() { |
| | |
| | | authbean.setRequest(requestBean); |
| | | //HDLLinkConfig.getInstance().getDeviceInfoBean()这个初始化的时候要先设置好 |
| | | AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean); |
| | | HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | // tv.setText("认证失败"); |
| | |
| | | */ |
| | | void searchGatewayBroadcast() { |
| | | tv.setText("搜索网关中..."); |
| | | HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { |
| | | HDLLinkLocalSdk.getInstance().searchGatewayBroadcast(new HDLUdpConnect.SearchGatewayCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | // tv.setText("网关不在线"); |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "网关不在线" + e.getMsg()); |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | // tv.setText("网关在线"); |
| | | // responseTv.setText("搜索成功 网关id:" + gatewaySearchBean.getGatewayId()); |
| | | Log.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | public void onError(HDLLinkException e) { |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | void getFunctionList() { |
| | | tv.setText("获取功能列表中..."); |
| | | responseTv.setText(""); |
| | | HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().getFunctionList(new HDLLinkTCallBack<List<LinkFunctionBean>>() { |
| | | @Override |
| | | public void onError(HDLLinkException error) { |
| | | // tv.setText(error.getMsg()); |
| | | Log.i("TAG", "onError 获取功能列表失败" + error.getMsg()); |
| | | public void onSuccess(List<LinkFunctionBean> data) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | // tv.setText("获取功能列表成功"); |
| | | // responseTv.setText(data); |
| | | Log.i("TAG", "onSuccess 获取功能列表成功" + data.toString()); |
| | | public void onError(HDLLinkException e) { |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | tv.setText("功能属性读取"); |
| | | List<String> sids = new ArrayList<>(); |
| | | sids.add(testLightSid); |
| | | HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { |
| | | |
| | | HDLLinkLocalSdk.getInstance().getFunctionAttribute(sids, new HDLLinkTCallBack<List<LinkFunctionBean>>() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | // responseTv.setText(msg); |
| | | Log.i("TAG", "onSuccess 功能属性读取成功" + msg.toString()); |
| | | public void onSuccess(List<LinkFunctionBean> data) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "onError 获取功能列表失败" + e.getMsg()); |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | tv.setText("读取状态中..."); |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(testLightSid);//要读取设备的sid |
| | | HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | // tv.setText("读取成功"); |
| | | // responseTv.setText(data); |
| | | Log.i("TAG", "onSuccess 读取设备状态成功" + data.toString()); |
| | | public void onSuccess(String msg) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | // tv.setText("读取失败"); |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "onError 读取设备状态失败" + e.getMsg()); |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | statusBeanList.add(bean); |
| | | request.setStatus(statusBeanList); |
| | | requestList.add(request); |
| | | HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().propertyDown(requestList, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | // responseTv.setText(data); |
| | |
| | | */ |
| | | void getSceneList() { |
| | | tv.setText("读取场景列表"); |
| | | HDLLink.getInstance().getSceneList(new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().getSceneList(new HDLLinkTCallBack<List<LinkSceneBean>>() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | // responseTv.setText(msg); |
| | | Log.i("TAG", "onSuccess 获取场景列表成功" + msg.toString()); |
| | | public void onSuccess(List<LinkSceneBean> data) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "onError 获取场景列表失败" + e.getMsg()); |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | } else { |
| | | sids.add("04010560D2C76E0A0A0100010000"); |
| | | } |
| | | HDLLink.getInstance().controlScene(sids, new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().controlScene(sids, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | // responseTv.setText(msg); |
| | |
| | | tv.setText("TCP发送 只发一次,不监听回复,不重发"); |
| | | String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); |
| | | String bodyStr = getPropertyDownBodyStr(); |
| | | HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); |
| | | HDLLinkLocalSdk.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); |
| | | } |
| | | |
| | | /** |
| | |
| | | tv.setText("UDP发送 只发一次,不监听回复,不重发"); |
| | | String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); |
| | | String bodyStr = getPropertyDownBodyStr(); |
| | | HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr); |
| | | HDLLinkLocalSdk.getInstance().udpSendMsg(propertyDownTopic, bodyStr,null); |
| | | } |
| | | |
| | | private String getPropertyDownBodyStr() { |
| | |
| | | tv.setText("UDP发送,监听主题回复,带重发带回调"); |
| | | String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); |
| | | String bodyStr = getPropertyDownBodyStr(); |
| | | HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() { |
| | | HDLLinkLocalSdk.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() { |
| | | @Override |
| | | public void onSuccess(LinkResponse msg) { |
| | | Log.i("TAG", "onSuccess UDP发送成功" + GsonConvert.getGson().toJson(msg)); |
| | |
| | | tv.setText("TCP发送,监听主题回复,带重发带回调"); |
| | | String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); |
| | | String bodyStr = getPropertyDownBodyStr(); |
| | | HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() { |
| | | HDLLinkLocalSdk.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | Log.i("TAG", "onSuccess TCP发送成功" + msg); |