| | |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
| | | |
| | | import com.hdl.sdk.hdl_core.HDLDeviceManger.Callback.PushCallback; |
| | | import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog; |
| | | |
| | |
| | | |
| | | private static final String clientid = "HDLZigbeeDriver"; |
| | | private static MqttClient client; |
| | | private static boolean isCancel = false; |
| | | private static boolean isCancel = false; |
| | | |
| | | public static void setIsCancel(boolean isCancel) { |
| | | HDLZigbeeMqtt.isCancel = isCancel; |
| | | } |
| | |
| | | */ |
| | | public static void connect1(String host) { |
| | | try { |
| | | client = new MqttClient("tcp://"+host+":1883", clientid, new MemoryPersistence()); |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | options.setCleanSession(false); |
| | | options.setUserName("admin"); |
| | | options.setPassword("password".toCharArray()); |
| | | //设置自动重连 |
| | | options.setAutomaticReconnect(true); |
| | | // 设置超时时间 |
| | | options.setConnectionTimeout(10); |
| | | // 设置会话心跳时间 |
| | | options.setKeepAliveInterval(20); |
| | | PushCallback pushCallback = new PushCallback(); |
| | | pushCallback.setHost(host); |
| | | client.setCallback(pushCallback); |
| | | client.connect(options); |
| | | if (isCancel){ |
| | | client.close(); |
| | | isCancel = false; |
| | | } |
| | | client = new MqttClient("tcp://" + host + ":1883", clientid, new MemoryPersistence()); |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | options.setCleanSession(false); |
| | | options.setUserName("admin"); |
| | | options.setPassword("password".toCharArray()); |
| | | //设置自动重连 |
| | | options.setAutomaticReconnect(true); |
| | | // 设置超时时间 |
| | | options.setConnectionTimeout(10); |
| | | // 设置会话心跳时间 |
| | | options.setKeepAliveInterval(20); |
| | | PushCallback pushCallback = new PushCallback(); |
| | | pushCallback.setHost(host); |
| | | client.setCallback(pushCallback); |
| | | client.connect(options); |
| | | if (isCancel) { |
| | | client.close(); |
| | | isCancel = false; |
| | | } |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | /** |
| | | * 发送mqtt信息 |
| | | */ |
| | | public static void sendMqttData1(String topic, String json) { |
| | | HDLLog.info("send Mqtt to gateway:"+json); |
| | | Log.d("111133", "sendMqttData1: "+json); |
| | | public static void sendMqttData1(String topic, String json) { |
| | | HDLLog.info("send Mqtt to gateway:" + json); |
| | | Log.d("111133", "sendMqttData1: " + json); |
| | | try { |
| | | MqttMessage message = new MqttMessage(); |
| | | message.setQos(2); |
| | |
| | | Log.d("111133", "已经发送"); |
| | | HDLLog.info("message is published completely!"); |
| | | } catch (MqttException e) { |
| | | Log.d("111133", "sendMqttData1: "+e.getMessage()); |
| | | Log.d("111133", "sendMqttData1: " + e.getMessage()); |
| | | HDLLog.info("发送mqtt信息出错"); |
| | | e.printStackTrace(); |
| | | } |