|
@@ -1,13 +1,19 @@
|
|
|
package com.welampiot.utils;
|
|
|
|
|
|
import com.welampiot.common.BaseResult;
|
|
|
+import com.welampiot.configuration.MqttConfig;
|
|
|
import com.welampiot.configuration.ResponseConfig;
|
|
|
import com.welampiot.dto.GlobalLocationDTO;
|
|
|
import com.welampiot.dto.SectionDTO;
|
|
|
import com.welampiot.dto.UserDTO;
|
|
|
+import com.welampiot.handle.MqttHandler;
|
|
|
import com.welampiot.service.GlobalLocationService;
|
|
|
import com.welampiot.service.SectionService;
|
|
|
import com.welampiot.service.UserService;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
@@ -25,7 +31,10 @@ public class ToolUtils {
|
|
|
private GlobalLocationService globalLocationService;
|
|
|
@Autowired
|
|
|
private ResponseConfig responseConfig;
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private MqttConfig mqttConfig;
|
|
|
+ @Autowired
|
|
|
+ private MqttHandler mqttHandler;
|
|
|
|
|
|
/**
|
|
|
* 获取用户所有路段 id
|
|
@@ -155,4 +164,218 @@ public class ToolUtils {
|
|
|
}
|
|
|
return new BaseResult<>(code,msg,obj);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送mqtt指令
|
|
|
+ * @param sendTopic 发送的topic
|
|
|
+ * @param cmdInfo 发送的指令内容
|
|
|
+ * @param resTopic 接收指令返回的topic
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String sendMqttCmd(String sendTopic,Object cmdInfo,String resTopic){
|
|
|
+ MqttClient client;
|
|
|
+ String res = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ client=new MqttClient(mqttConfig.getUrl(),mqttConfig.getClientId()+"_"+(new Date()).getTime(),new MemoryPersistence());
|
|
|
+ MqttConnectOptions options=new MqttConnectOptions();
|
|
|
+ options.setCleanSession(true);
|
|
|
+ options.setUserName(mqttConfig.getUsername());
|
|
|
+ options.setPassword(mqttConfig.getPassword().toCharArray());
|
|
|
+ options.setConnectionTimeout(mqttConfig.getTimeout());
|
|
|
+ options.setKeepAliveInterval(mqttConfig.getKeepalive());
|
|
|
+ mqttHandler.setTopic(resTopic);
|
|
|
+
|
|
|
+ // 设置回调
|
|
|
+ client.setCallback(mqttHandler);
|
|
|
+
|
|
|
+ // 建立连接
|
|
|
+ client.connect(options);
|
|
|
+ // MqttCustomerClient.setClient(client);
|
|
|
+ client.subscribe(resTopic);
|
|
|
+
|
|
|
+ // 消息发布所需参数
|
|
|
+ MqttMessage message;
|
|
|
+ if (cmdInfo instanceof String){
|
|
|
+ message = new MqttMessage(((String) cmdInfo).getBytes());
|
|
|
+ }else {
|
|
|
+ message = new MqttMessage((byte[]) cmdInfo);
|
|
|
+ }
|
|
|
+ message.setQos(0);
|
|
|
+ client.publish(sendTopic, message);
|
|
|
+ int in = 0;
|
|
|
+
|
|
|
+ while (true){
|
|
|
+ Thread.sleep(1000);
|
|
|
+ if (mqttHandler.getRes() != null && mqttHandler.getRes().length() != 0){
|
|
|
+ res = mqttHandler.getRes();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ in ++;
|
|
|
+ if (in > 30) break;
|
|
|
+ }
|
|
|
+ client.disconnect();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送mqtt指令
|
|
|
+ * @param sendTopic 发送的topic
|
|
|
+ * @param cmdInfo 发送的指令内容
|
|
|
+ * @param resTopic 接收指令返回的topic
|
|
|
+ * @param timeout 指令超时时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String sendMqttCmd(String sendTopic,Object cmdInfo,String resTopic,Integer timeout){
|
|
|
+ MqttClient client;
|
|
|
+ String res = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ client=new MqttClient(mqttConfig.getUrl(),mqttConfig.getClientId()+"_"+(new Date()).getTime(),new MemoryPersistence());
|
|
|
+ MqttConnectOptions options=new MqttConnectOptions();
|
|
|
+ options.setCleanSession(true);
|
|
|
+ options.setUserName(mqttConfig.getUsername());
|
|
|
+ options.setPassword(mqttConfig.getPassword().toCharArray());
|
|
|
+ options.setConnectionTimeout(mqttConfig.getTimeout());
|
|
|
+ options.setKeepAliveInterval(mqttConfig.getKeepalive());
|
|
|
+ mqttHandler.setTopic(resTopic);
|
|
|
+
|
|
|
+ // 设置回调
|
|
|
+ client.setCallback(mqttHandler);
|
|
|
+
|
|
|
+ // 建立连接
|
|
|
+ client.connect(options);
|
|
|
+ // MqttCustomerClient.setClient(client);
|
|
|
+ client.subscribe(resTopic);
|
|
|
+
|
|
|
+ // 消息发布所需参数
|
|
|
+ MqttMessage message;
|
|
|
+ if (cmdInfo instanceof String){
|
|
|
+ message = new MqttMessage(((String) cmdInfo).getBytes());
|
|
|
+ }else {
|
|
|
+ message = new MqttMessage((byte[]) cmdInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ message.setQos(0);
|
|
|
+ client.publish(sendTopic, message);
|
|
|
+ int in = 0;
|
|
|
+ if (timeout > 0){
|
|
|
+ while (true){
|
|
|
+ Thread.sleep(1000);
|
|
|
+ if (mqttHandler.getRes() != null && mqttHandler.getRes().length() != 0){
|
|
|
+ res = mqttHandler.getRes();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ in ++;
|
|
|
+ if (in > timeout) break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ client.disconnect();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组转16进制字符串
|
|
|
+ * @param b 字节数组
|
|
|
+ * @return 16进制字符串
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static String bytes2HexString(byte[] b) {
|
|
|
+ StringBuffer result = new StringBuffer();
|
|
|
+ String hex;
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ hex = Integer.toHexString(b[i] & 0xFF);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hex = '0' + hex;
|
|
|
+ }
|
|
|
+ result.append(hex.toUpperCase());
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 16进制字符串转字节数组
|
|
|
+ * @param src 16进制字符串
|
|
|
+ * @return 字节数组
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public byte[] hexString2Bytes(String src) {
|
|
|
+ int l = src.length() / 2;
|
|
|
+ byte[] ret = new byte[l];
|
|
|
+ for (int i = 0; i < l; i++) {
|
|
|
+ ret[i] = (byte) Integer
|
|
|
+ .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算modbus CRC16校验码
|
|
|
+ * @param bytes 需要计算的数据
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getCRC(byte[] bytes) {
|
|
|
+ // CRC寄存器全为1
|
|
|
+ int CRC = 0x0000ffff;
|
|
|
+ // 多项式校验值
|
|
|
+ int POLYNOMIAL = 0x0000a001;
|
|
|
+ int i, j;
|
|
|
+ for (i = 0; i < bytes.length; i++) {
|
|
|
+ CRC ^= ((int) bytes[i] & 0x000000ff);
|
|
|
+ for (j = 0; j < 8; j++) {
|
|
|
+ if ((CRC & 0x00000001) != 0) {
|
|
|
+ CRC >>= 1;
|
|
|
+ CRC ^= POLYNOMIAL;
|
|
|
+ } else {
|
|
|
+ CRC >>= 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 结果转换为16进制
|
|
|
+ String result = Integer.toHexString(CRC).toUpperCase();
|
|
|
+ if (result.length() != 4) {
|
|
|
+ StringBuffer sb = new StringBuffer("0000");
|
|
|
+ result = sb.replace(4 - result.length(), 4, result).toString();
|
|
|
+ }
|
|
|
+ //高位在前地位在后
|
|
|
+ //return result.substring(2, 4) + " " + result.substring(0, 2);
|
|
|
+ // 交换高低位,低位在前高位在后
|
|
|
+ return result.substring(2, 4)+result.substring(0, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getRequestContent(HttpServletRequest request,String key){
|
|
|
+ return request.getParameter(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求参数
|
|
|
+ * @param request 请求对象
|
|
|
+ * @param key 参数值
|
|
|
+ * @param type 参数类型 1 int 2 string
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object getRequestContent(HttpServletRequest request,String key,Integer type){
|
|
|
+ if (type == 1){ // 获取 int 数据
|
|
|
+ Integer keyId = request.getParameter(key) == null || request.getParameter(key).length() == 0? 0 : Integer.parseInt(request.getParameter(key));
|
|
|
+ return keyId;
|
|
|
+ }else if (type == 2){
|
|
|
+ String keyStr = request.getParameter(key) == null ? "" : request.getParameter(key).toString();
|
|
|
+ return keyStr;
|
|
|
+ }
|
|
|
+ return request.getParameter(key);
|
|
|
+ }
|
|
|
}
|