| | |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import android.Manifest; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.View; |
| | | import android.widget.TextView; |
| | |
| | | |
| | | import com.chad.library.adapter.base.BaseQuickAdapter; |
| | | import com.chad.library.adapter.base.listener.OnItemClickListener; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.hdlsdk.device.DevicesListActivity; |
| | | 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.LinkResponse; |
| | | import com.hdl.sdk.connect.bean.request.AuthenticateRequest; |
| | | import com.hdl.sdk.connect.bean.request.PropertyReadRequest; |
| | | 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 java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | |
| | | public class MainActivity extends AppCompatActivity { |
| | | |
| | | private static final String TAG = "MainActivity"; |
| | | private DemoAdapter demoAdapter; |
| | | private RecyclerView rv; |
| | | private TextView tv; |
| | | private TextView responseTv; |
| | | boolean isOn; |
| | | private EventListener allTopicsListener; |
| | | private String testLightSid = "000101B847C71B02020100010101"; |
| | | private String testLightSid = "0001010D48C71B02020100010101"; |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | |
| | | rv.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | checkIfCertified(); |
| | | |
| | | registerAllTopicsListener(); |
| | | |
| | | ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { |
| | |
| | | beans.add(new DemoBean("读取状态")); |
| | | beans.add(new DemoBean("获取场景列表")); |
| | | beans.add(new DemoBean("场景控制")); |
| | | beans.add(new DemoBean("设备功能列表")); |
| | | demoAdapter = new DemoAdapter(beans); |
| | | rv.setAdapter(demoAdapter); |
| | | |
| | | |
| | | final SocketOptions options = new SocketOptions(); |
| | | |
| | | // final SocketOptions options = new SocketOptions(); |
| | | // |
| | | MessagePipeLine pipeLine = new MessagePipeLine(); |
| | | pipeLine.add(new LinkMessageDecoder()); |
| | | pipeLine.add(new LinkMessageEncoder()); |
| | | options.setHandleMessage(pipeLine); |
| | | options.setEnabledHeartbeat(false); |
| | | // options.setHandleMessage(pipeLine); |
| | | // options.setEnabledHeartbeat(false); |
| | | |
| | | demoAdapter.setOnItemClickListener(new OnItemClickListener() { |
| | | @Override |
| | |
| | | //获取场景列表 |
| | | controlScene(); |
| | | break; |
| | | case 8: |
| | | //功能列表 |
| | | startDevicesListActivity(); |
| | | break; |
| | | } |
| | | } |
| | | }); |
| | |
| | | @Override |
| | | public void onMessage(Object msg) { |
| | | LinkResponse response = (LinkResponse)msg; |
| | | Log.i("TAG", "AllTopicsListener onMessage: "+msg.toString()); |
| | | if(response != null){ |
| | | Log.i(TAG, "AllTopicsListener" |
| | | + "\nTopic: " + response.getTopic() |
| | | +"\n Data: " + response.getData()); |
| | | handleLinkResponse(response); |
| | | } |
| | | |
| | | } |
| | | }; |
| | | HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | /** |
| | | * 处理收到的主题 |
| | | * @param response |
| | | */ |
| | | private void handleLinkResponse(LinkResponse response){ |
| | | //网关搜索回复 |
| | | if(response.getTopic().contains("/user/all/custom/gateway/search_reply")){ |
| | | String data = response.getData(); |
| | | if (!TextUtils.isEmpty(data)) { |
| | | Log.i("handleLinkResponse", "data:"+data); |
| | | final BaseLocalResponse<GatewaySearchBean> bean = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() { |
| | | }.getType()); |
| | | GatewaySearchBean searchBean = bean.getObjects(); |
| | | Log.i("handleLinkResponse", "GatewaySearchBean: "+searchBean.getGatewayId()); |
| | | } |
| | | }else{ |
| | | //其它主题 |
| | | |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); |
| | | infoBean.setDeviceMAC(macStr); |
| | | infoBean.setIPMAC(macStr); |
| | | infoBean.setDeviceName("HDL设备");//设备名字 |
| | | infoBean.setDeviceName("红外宝");//设备名字 |
| | | infoBean.setDeviceModel("HDL");// |
| | | infoBean.setAccessMode("WIFI"); |
| | | infoBean.setIPGateway("192.168.10.1"); |
| | | infoBean.setIPGateway("192.168.12.1"); |
| | | infoBean.setIPAddress(IpUtils.getIP(this)); |
| | | |
| | | infoBean.setOID("010105000000FE08"); |
| | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("网关不在线"); |
| | | responseTv.setText(e.getMsg()); |
| | | } |
| | | @Override |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | |
| | | void getFunctionList(){ |
| | | tv.setText("获取功能列表中..."); |
| | | responseTv.setText(""); |
| | | HDLSocket.getInstance().getFunctionList(new HDLLinkCallBack() { |
| | | HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException error) { |
| | | tv.setText(error.getMsg()); |
| | |
| | | tv.setText("功能属性读取"); |
| | | List<String> sids = new ArrayList<>(); |
| | | sids.add(testLightSid); |
| | | HDLSocket.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { |
| | | HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | responseTv.setText(msg); |
| | |
| | | tv.setText("读取状态中..."); |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(testLightSid);//要读取设备的sid |
| | | HDLSocket.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("读取成功"); |
| | |
| | | } |
| | | |
| | | |
| | | void startDevicesListActivity(){ |
| | | Intent intent = new Intent(this, DevicesListActivity.class); |
| | | startActivity(intent); |
| | | } |
| | | } |