From a3f9e5ce8dce3d86a60a79b85ce44424a612c6fa Mon Sep 17 00:00:00 2001 From: 562935844@qq.com Date: 星期五, 13 五月 2022 14:06:45 +0800 Subject: [PATCH] 提交jar版本 --- HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java | 341 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 198 insertions(+), 143 deletions(-) diff --git a/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java b/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java index d6c6e37..862e9fe 100644 --- a/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java +++ b/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java @@ -4,12 +4,13 @@ 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 androidx.recyclerview.widget.RecyclerView; import android.Manifest; -import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; @@ -19,31 +20,22 @@ 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; 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.socket.HDLSocket; 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; -import com.hdl.sdk.socket.codec.MessagePipeLine; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; @@ -51,8 +43,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; - -import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY; public class MainActivity extends AppCompatActivity { @@ -63,7 +53,7 @@ private TextView responseTv; boolean isOn; private EventListener allTopicsListener; - private String testLightSid = "0001010D48C71B02020100010101"; + private String testLightSid = "1493092497588469761"; @Override protected void onDestroy() { @@ -71,17 +61,26 @@ removeAllTopicsListener(); } + void init() { + HDLLinkConfig.getInstance().setLocalSecret("e186beeb7974998e"); + HDLLinkConfig.getInstance().setGatewayId("1519863101509361666"); +// HDLLinkConfig.getInstance().setLocalEncrypt(true); + } + + @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(); ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { @@ -103,10 +102,10 @@ beans.add(new DemoBean("鑾峰彇鍦烘櫙鍒楄〃")); beans.add(new DemoBean("鍦烘櫙鎺у埗")); beans.add(new DemoBean("璁惧鍔熻兘鍒楄〃")); + beans.add(new DemoBean("UDP鍙戦��")); + beans.add(new DemoBean("TCP鍙戦��")); beans.add(new DemoBean("UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); beans.add(new DemoBean("TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); - beans.add(new DemoBean("UDP鍙戦�侊紝涓嶅洖璋冧笉閲嶅彂")); - beans.add(new DemoBean("TCP鍙戦�侊紝涓嶅洖璋冧笉閲嶅彂")); demoAdapter = new DemoAdapter(beans); rv.setAdapter(demoAdapter); @@ -130,12 +129,12 @@ searchGatewayBroadcast(); break; case 2: - //鑾峰彇鍔熻兘鍒楄〃 + //鑾峰彇鍔熻兘鍒楄〃 getFunctionList(); break; case 3: //鍔熻兘灞炴�ц鍙� - getFunctionAttribute(); + getFunctionAttribute(); break; case 4: //璁惧鎺у埗 @@ -155,29 +154,28 @@ break; case 8: //鍔熻兘鍒楄〃 - startDevicesListActivity(); +// startDevicesListActivity(); break; case 9: //UDP鍙戦�� - udpSendWithCallback(); + udpSend(); break; case 10: //TCP鍙戦�� - tcpSendWithCallback(); + tcpSend(); + break; case 11: - //UDP鍙戦�� - udpSend(); + //UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + udpSendWithCallback(); break; case 12: - //TCP鍙戦�� - tcpSend(); + //TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + tcpSendWithCallback(); break; } } }); } - - public void showToast(String text) { @@ -187,9 +185,9 @@ /** * 妫�娴嬫槸鍚﹁璇佽繃 */ - void checkIfCertified(){ + void checkIfCertified() { boolean isCertified = HDLLink.getInstance().checkIfCertified(); - String mes = isCertified?"宸茬粡璁よ瘉杩�":"鏈璇�"; + String mes = isCertified ? "宸茬粡璁よ瘉杩�" : "鏈璇�"; showToast(mes); tv.setText(mes); } @@ -197,11 +195,12 @@ /** * 娉ㄥ唽鎵�鏈変富棰樻暟鎹殑鐩戝惉 */ - void registerAllTopicsListener(){ + void registerAllTopicsListener() { allTopicsListener = new EventListener() { @Override public void onMessage(Object msg) { - LinkResponse response = (LinkResponse)msg; + LinkResponse response = (LinkResponse) msg; + handleLinkResponse(response); } }; HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); @@ -209,20 +208,21 @@ /** * 澶勭悊鏀跺埌鐨勪富棰� + * * @param response */ - private void handleLinkResponse(LinkResponse response){ + private void handleLinkResponse(LinkResponse response) { //缃戝叧鎼滅储鍥炲 - if(response.getTopic().contains("/user/all/custom/gateway/search_reply")){ + if (response.getTopic().contains("/user/all/custom/gateway/search_reply")) { String data = response.getData(); if (!TextUtils.isEmpty(data)) { - Log.i("handleLinkResponse", "data:"+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()); + Log.i("handleLinkResponse", "GatewaySearchBean: " + searchBean.getGatewayId()); } - }else{ + } else { //鍏跺畠涓婚 } @@ -238,7 +238,6 @@ }.getType()); searchBean = response.getObjects(); } - } return searchBean; } @@ -246,63 +245,83 @@ /** * 绉婚櫎鎵�鏈変富棰樻暟鎹殑鐩戝惉 */ - void removeAllTopicsListener(){ + void removeAllTopicsListener() { HDLLink.getInstance().removeAllTopicsListener(allTopicsListener); + } + + void initDeviceInfo() { + AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); +// infoBean.setDeviceMAC("AA00000000000100"); +// infoBean.setIPMAC("AA00000000000100"); + + infoBean.setDeviceMAC("BB000000000000BF"); + infoBean.setIPMAC("BB000000000000BF"); + infoBean.setDeviceName("闊充箰鎾斁鍣�");//璁惧鍚嶅瓧 + infoBean.setDeviceModel("MCLog.431");//璁惧鍨嬪彿 + infoBean.setAccessMode("WIFI"); +// infoBean.setIPGateway("192.168.1.1"); +// infoBean.setIPAddress("192.168.1.116"); + + infoBean.setIPGateway("192.168.3.1"); + infoBean.setIPAddress("192.168.3.126"); + infoBean.setGateway_type("music.standard"); + infoBean.setHw_version("HW2.0"); + infoBean.setFw_version("Fw1.0"); +// infoBean.setOID("010105000000FE11");//姣忎釜璁惧oid閮借涓嶄竴鏍� +// infoBean.setSid("110105000000FE08110100000011");//姣忎釜璁惧鐨剆id閮借涓嶄竴鏍� + + infoBean.setOID("010105010101FE07");//姣忎釜璁惧oid閮借涓嶄竴鏍� + infoBean.setSid("010105010101FE02020100040101");//姣忎釜璁惧鐨剆id閮借涓嶄竴鏍� + HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean); } /** * 鍏ョ綉璁よ瘉 */ - void sendAuthenticateRequest(){ + void sendAuthenticateRequest() { tv.setText("寮�濮嬪叆缃戣璇�..."); //璁よ瘉鎻愪氦鍙傛暟鍑嗗 - String spkStr = "screen.mirror";//浜у搧spk - String macStr = "AA000000000000AC";//璁惧鍞竴MAC鍦板潃 - String secret = "ee62124c151b737c";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret +// 娴嬭瘯鏈嶅姟 + String spkStr = "ir.module";//浜у搧spk + String macStr = "BB000000000000BF";//璁惧鍞竴MAC鍦板潃 + String secret = "d70f8872ddd6fe6a";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret + +// 姝e紡鏈嶅姟鍣� +// String spkStr = "screen.mirror";//浜у搧spk +// String macStr = "AA00000000000100";//璁惧鍞竴MAC鍦板潃 +// String secret = "e186beeb7974998e";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret + String mac_key = stringToMD5(stringToMD5(macStr + secret)); String versionString = "HDL_V1.0.1";// String time = String.valueOf(System.currentTimeMillis()); + HDLLinkConfig.getInstance().getDeviceInfoBean().setDeviceMAC(macStr); //1.璁剧疆璁よ瘉淇℃伅 AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean(); requestBean.setMAC(macStr); - requestBean.setSupplier("raysgem"); + requestBean.setSupplier("WISE"); requestBean.setFirmwareVersion(versionString); requestBean.setHardwareModel("1956F"); AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean(); authbean.setSpk(spkStr); authbean.setMACKey(mac_key); authbean.setRequest(requestBean); - - //2.璁剧疆璁惧淇℃伅 - AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); - infoBean.setDeviceMAC(macStr); - infoBean.setIPMAC(macStr); - infoBean.setDeviceName("绾㈠瀹�");//璁惧鍚嶅瓧 - infoBean.setDeviceModel("HDL");// - infoBean.setAccessMode("WIFI"); - infoBean.setIPGateway("192.168.88.1"); - infoBean.setIPAddress(IpUtils.getIP(this)); - - infoBean.setOID("010105000000FE08"); - infoBean.setSid("010105000000FE08110100000000"); -// 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); + //HDLLinkConfig.getInstance().getDeviceInfoBean()杩欎釜鍒濆鍖栫殑鏃跺�欒鍏堣缃ソ + AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean); HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { @Override public void onError(HDLLinkException e) { - tv.setText("璁よ瘉澶辫触"); - responseTv.setText(e.getMsg()); - Log.i("TAG", "onError: 璁よ瘉澶辫触"); +// tv.setText("璁よ瘉澶辫触"); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError: 璁よ瘉澶辫触" + e.getMsg()); } @Override public void onSuccess(String msg) { - tv.setText("璁よ瘉鎴愬姛"); - responseTv.setText(msg.toString()); +// tv.setText("璁よ瘉鎴愬姛"); +// responseTv.setText(msg.toString()); + Log.i("TAG", "onSuccess: 璁よ瘉鎴愬姛" + msg.toString()); } }); } @@ -333,19 +352,21 @@ * 濡傛灉宸茬粡璁よ瘉鍚庯紝鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎 * 骞挎挱鎼滅储璁よ瘉杩囩殑缃戝叧鏄惁鍦ㄧ嚎 */ - void searchGatewayBroadcast(){ + void searchGatewayBroadcast() { tv.setText("鎼滅储缃戝叧涓�..."); HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { @Override public void onError(HDLLinkException e) { - tv.setText("缃戝叧涓嶅湪绾�"); - responseTv.setText(e.getMsg()); +// tv.setText("缃戝叧涓嶅湪绾�"); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "缃戝叧涓嶅湪绾�" + e.getMsg()); } + @Override public void onSuccess(GatewaySearchBean gatewaySearchBean) { - tv.setText("缃戝叧鍦ㄧ嚎"); - responseTv.setText("鎼滅储鎴愬姛 缃戝叧id锛�"+gatewaySearchBean.getGatewayId()); -// LogUtils.i("TAG", "onSuccess: 鎼滅储鎴愬姛锛�"+gatewaySearchBean.getGatewayId()); +// tv.setText("缃戝叧鍦ㄧ嚎"); +// responseTv.setText("鎼滅储鎴愬姛 缃戝叧id锛�" + gatewaySearchBean.getGatewayId()); + Log.i("TAG", "onSuccess: 鎼滅储鎴愬姛锛�"+gatewaySearchBean.getGatewayId()); } }); } @@ -353,19 +374,21 @@ /** * 鑾峰彇鍔熻兘鍒楄〃 */ - void getFunctionList(){ + void getFunctionList() { tv.setText("鑾峰彇鍔熻兘鍒楄〃涓�..."); responseTv.setText(""); HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() { @Override public void onError(HDLLinkException error) { - tv.setText(error.getMsg()); +// tv.setText(error.getMsg()); + Log.i("TAG", "onError 鑾峰彇鍔熻兘鍒楄〃澶辫触" + error.getMsg()); } @Override public void onSuccess(String data) { - tv.setText("鑾峰彇鍔熻兘鍒楄〃鎴愬姛"); - responseTv.setText(data); +// tv.setText("鑾峰彇鍔熻兘鍒楄〃鎴愬姛"); +// responseTv.setText(data); + Log.i("TAG", "onSuccess 鑾峰彇鍔熻兘鍒楄〃鎴愬姛" + data.toString()); } }); } @@ -374,19 +397,21 @@ * 鍔熻兘灞炴�ц鍙� * 鏀寔鎵归噺璇诲彇 */ - void getFunctionAttribute(){ + void getFunctionAttribute() { tv.setText("鍔熻兘灞炴�ц鍙�"); List<String> sids = new ArrayList<>(); sids.add(testLightSid); HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { @Override public void onSuccess(String msg) { - responseTv.setText(msg); +// responseTv.setText(msg); + Log.i("TAG", "onSuccess 鍔熻兘灞炴�ц鍙栨垚鍔�" + msg.toString()); } @Override public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError 鑾峰彇鍔熻兘鍒楄〃澶辫触" + e.getMsg()); } }); } @@ -395,36 +420,39 @@ * 璇诲彇璁惧鐘舵�� * 鏀寔鎵归噺璇诲彇 */ - void propertyRead(){ + void propertyRead() { tv.setText("璇诲彇鐘舵�佷腑..."); List<String> list = new ArrayList<>(); list.add(testLightSid);//瑕佽鍙栬澶囩殑sid HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { @Override public void onSuccess(String data) { - tv.setText("璇诲彇鎴愬姛"); - responseTv.setText(data); +// tv.setText("璇诲彇鎴愬姛"); +// responseTv.setText(data); + Log.i("TAG", "onSuccess 璇诲彇璁惧鐘舵�佹垚鍔�" + data.toString()); } + @Override public void onError(HDLLinkException e) { - tv.setText("璇诲彇澶辫触"); - responseTv.setText(e.getMsg()); +// tv.setText("璇诲彇澶辫触"); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError 璇诲彇璁惧鐘舵�佸け璐�" + e.getMsg()); } }); } /** - * 鎺у埗澶辫触 + * 鎺у埗璁惧 * 鍥炲鍝嶅簲code涓�200 浠h〃鎵ц鎴愬姛 */ - void controlDecide(){ + 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(); + List<DeviceControlRequest.StatusBean> statusBeanList = new ArrayList<>(); + DeviceControlRequest.StatusBean bean = new DeviceControlRequest.StatusBean(); bean.setKey("on_off"); bean.setValue(isOn ? "on" : "off"); statusBeanList.add(bean); @@ -433,12 +461,14 @@ HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() { @Override public void onSuccess(String data) { - responseTv.setText(data); +// responseTv.setText(data); + Log.i("TAG", "onSuccess 鎺у埗璁惧鎴愬姛" + data.toString()); } @Override public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError 鎺у埗璁惧澶辫触" + e.getMsg()); } }); } @@ -446,119 +476,144 @@ /** * 鑾峰彇鍦烘櫙鍒楄〃 */ - void getSceneList(){ + void getSceneList() { tv.setText("璇诲彇鍦烘櫙鍒楄〃"); HDLLink.getInstance().getSceneList(new HDLLinkCallBack() { @Override public void onSuccess(String msg) { - responseTv.setText(msg); +// responseTv.setText(msg); + Log.i("TAG", "onSuccess 鑾峰彇鍦烘櫙鍒楄〃鎴愬姛" + msg.toString()); } @Override public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError 鑾峰彇鍦烘櫙鍒楄〃澶辫触" + 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(){ + void controlScene() { tv.setText("鍦烘櫙鎺у埗"); //鎺у埗鍦烘櫙sid鍒楄〃锛屾敮鎸佹壒閲忔帶鍒� List<String> sids = new ArrayList<>(); isOn = !isOn; - if(isOn){ + if (isOn) { sids.add("04010560D2C7170A0A0100000000"); - }else{ + } else { sids.add("04010560D2C76E0A0A0100010000"); } - HDLLink.getInstance().controlScene(sids,new HDLLinkCallBack() { + HDLLink.getInstance().controlScene(sids, new HDLLinkCallBack() { @Override public void onSuccess(String msg) { - responseTv.setText(msg); +// responseTv.setText(msg); + Log.i("TAG", "onSuccess 鍦烘櫙鎺у埗鎴愬姛" + msg.toString()); } @Override public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError 鍦烘櫙鎺у埗澶辫触" + e.getMsg()); } }); } - void startDevicesListActivity(){ +/* void startDevicesListActivity() { Intent intent = new Intent(this, DevicesListActivity.class); startActivity(intent); + }*/ + + /** + * TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� + */ + private void tcpSend() { + tv.setText("TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�"); + String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); + String bodyStr = getPropertyDownBodyStr(); + HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); } /** - * TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + * UDP鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� */ - private void tcpSendWithCallback() { - 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(), new HDLLinkCallBack() { - @Override - public void onSuccess(String data) { - responseTv.setText(data); - } + private void udpSend() { + tv.setText("UDP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�"); + String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); + String bodyStr = getPropertyDownBodyStr(); + HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr); + } - @Override - public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); - } - }); + private String getPropertyDownBodyStr() { + 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); + String time = String.valueOf(System.currentTimeMillis()); + + final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>(); + data.setId(IdUtils.getUUId()); + data.setTime_stamp(time); + data.setObjects(requestList); + + return GsonConvert.getGson().toJson(data); } /** * UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� */ private void udpSendWithCallback() { - 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(), new HDLLinkResponseCallBack() { + tv.setText("UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�"); + String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); + String bodyStr = getPropertyDownBodyStr(); + HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() { @Override - public void onSuccess(LinkResponse linkResponse) { - if(linkResponse != null){ - responseTv.setText(linkResponse.getData()); - } + public void onSuccess(LinkResponse msg) { + Log.i("TAG", "onSuccess UDP鍙戦�佹垚鍔�" + GsonConvert.getGson().toJson(msg)); +// responseTv.setText(GsonConvert.getGson().toJson(msg)); } @Override public void onError(HDLLinkException e) { - responseTv.setText(e.getMsg()); +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError UDP鍙戦�佸け璐�" + e.getMsg()); } }); } /** - * TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� + * 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 tcpSendWithCallback() { + tv.setText("TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�"); + String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); + String bodyStr = getPropertyDownBodyStr(); + HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() { + @Override + public void onSuccess(String msg) { + Log.i("TAG", "onSuccess TCP鍙戦�佹垚鍔�" + msg); +// responseTv.setText(msg); + } - /** - * 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� - */ - 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()); + @Override + public void onError(HDLLinkException e) { +// responseTv.setText(e.getMsg()); + Log.i("TAG", "onError TCP鍙戦�佸け璐�"); + } + }); } } \ No newline at end of file -- Gitblit v1.8.0