|
@@ -1,11 +1,14 @@
|
|
|
package com.welampiot.utils;
|
|
|
|
|
|
+import com.alibaba.druid.support.json.JSONUtils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.welampiot.common.BaseResult;
|
|
|
import com.welampiot.common.DevInfoEnum;
|
|
|
import com.welampiot.common.InterfaceResultEnum;
|
|
|
import com.welampiot.common.LampControlTypeEnum;
|
|
|
import com.welampiot.configuration.MqttConfig;
|
|
|
+import com.welampiot.configuration.PlcMqttConfig;
|
|
|
import com.welampiot.dto.*;
|
|
|
import com.welampiot.handle.MqttHandler;
|
|
|
import com.welampiot.service.GlobalLocationService;
|
|
@@ -15,6 +18,7 @@ import com.welampiot.service.UserService;
|
|
|
import com.welampiot.vo.DevEnumVO;
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
@@ -29,8 +33,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.text.DecimalFormat;
|
|
@@ -53,7 +59,8 @@ public class ToolUtils {
|
|
|
private MqttHandler mqttHandler;
|
|
|
@Autowired
|
|
|
private LampPoleService lampPoleService;
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private PlcMqttConfig plcMqttConfig;
|
|
|
private static final String PATTERN = "^(?=.*[0-9])(?=.*[a-zA-Z]).{8,20}$";
|
|
|
private static final String PATTERN_PHONE = "^1[3-9]\\d{9}$";
|
|
|
private static final String PATTERN_EMAIL = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
|
@@ -326,6 +333,167 @@ public class ToolUtils {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 发送mqtt指令
|
|
|
+ * @param sendTopic 发送的topic
|
|
|
+ * @param cmdInfo 发送的指令内容
|
|
|
+ * @param resTopic 接收指令返回的topic
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String plcSendMqttCmd(String sendTopic,Object cmdInfo,String resTopic){
|
|
|
+ MqttClient client;
|
|
|
+ String res = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ client=new MqttClient(plcMqttConfig.getUrl(),plcMqttConfig.getClientId()+"_"+(new Date()).getTime(),new MemoryPersistence());
|
|
|
+ MqttConnectOptions options=new MqttConnectOptions();
|
|
|
+ options.setCleanSession(true);
|
|
|
+ options.setUserName(plcMqttConfig.getUsername());
|
|
|
+ options.setPassword(plcMqttConfig.getPassword().toCharArray());
|
|
|
+ options.setConnectionTimeout(plcMqttConfig.getTimeout());
|
|
|
+ options.setKeepAliveInterval(plcMqttConfig.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 plcSendMqttCmd(String sendTopic,String cmdInfo, String resTopic, Integer timeout){
|
|
|
+ MqttClient client;
|
|
|
+ String res = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ client=new MqttClient(plcMqttConfig.getUrl(),plcMqttConfig.getClientId()+"_"+(new Date()).getTime(),new MemoryPersistence());
|
|
|
+ MqttConnectOptions options=new MqttConnectOptions();
|
|
|
+ options.setCleanSession(true);
|
|
|
+ options.setUserName(plcMqttConfig.getUsername());
|
|
|
+ options.setPassword(plcMqttConfig.getPassword().toCharArray());
|
|
|
+ options.setConnectionTimeout(plcMqttConfig.getTimeout());
|
|
|
+ options.setKeepAliveInterval(plcMqttConfig.getKeepalive());
|
|
|
+ mqttHandler.setTopic(resTopic);
|
|
|
+
|
|
|
+ // 设置回调
|
|
|
+ client.setCallback(mqttHandler);
|
|
|
+
|
|
|
+ // 建立连接
|
|
|
+ client.connect(options);
|
|
|
+ // MqttCustomerClient.setClient(client);
|
|
|
+ client.subscribe(resTopic);
|
|
|
+
|
|
|
+ // 消息发布所需参数
|
|
|
+ MqttMessage message;
|
|
|
+ message = new MqttMessage(cmdInfo.getBytes());
|
|
|
+ message.setQos(0);
|
|
|
+ client.publish(sendTopic, message);
|
|
|
+ System.out.println("==send:"+sendTopic+"====res:"+resTopic);
|
|
|
+ int in = 0;
|
|
|
+ if (timeout > 0){
|
|
|
+ while (true){
|
|
|
+ System.out.println("==d====:"+in+"====");
|
|
|
+ Thread.sleep(100);
|
|
|
+ if (mqttHandler.getRes() != null && mqttHandler.getRes().length() != 0){
|
|
|
+ res = mqttHandler.getRes();
|
|
|
+System.out.println(res);
|
|
|
+ //原来的
|
|
|
+ JSONObject oldObject = JSON.parseObject(cmdInfo);
|
|
|
+ String oldDataType = oldObject.getObject("dataType", String.class);
|
|
|
+// String[] oldThirdIdArr = oldObject.getObject("thirdId",String.class).split("-");
|
|
|
+// String[] oldSnArr = oldThirdIdArr[0].split(":");
|
|
|
+// String oldSn = oldSnArr[1]; //设备地址
|
|
|
+// String[] oldNumberArr = oldThirdIdArr[1].split(":");
|
|
|
+// int oldNum = Integer.parseInt(oldNumberArr[1]); //设备灯序号
|
|
|
+ //解析json
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res);
|
|
|
+ String dataType = jsonObject.getObject("dataType", String.class);
|
|
|
+// String[] thirdIdArr = jsonObject.getObject("thirdId",String.class).split("-");
|
|
|
+// String[] snArr = thirdIdArr[0].split(":");
|
|
|
+// String sn = snArr[1]; //设备地址
|
|
|
+// String[] numberArr = thirdIdArr[1].split(":");
|
|
|
+// int num = Integer.parseInt(numberArr[1]); //设备灯序号
|
|
|
+
|
|
|
+ //它是字符串
|
|
|
+ if (dataType.equals("lightStatus") ){ //1.读取灯信息,2.调光的
|
|
|
+ //原来的
|
|
|
+ String[] oldThirdIdArr = oldObject.getObject("thirdId",String.class).split("-");
|
|
|
+ String[] oldSnArr = oldThirdIdArr[0].split(":");
|
|
|
+ String oldSn = oldSnArr[1]; //设备地址
|
|
|
+ String[] oldNumberArr = oldThirdIdArr[1].split(":");
|
|
|
+ int oldNum = Integer.parseInt(oldNumberArr[1]); //设备灯序号
|
|
|
+ //解析json
|
|
|
+ String[] thirdIdArr = jsonObject.getObject("thirdId",String.class).split("-");
|
|
|
+ String[] snArr = thirdIdArr[0].split(":");
|
|
|
+ String sn = snArr[1]; //设备地址
|
|
|
+ String[] numberArr = thirdIdArr[1].split(":");
|
|
|
+ int num = Integer.parseInt(numberArr[1]); //设备灯序号
|
|
|
+ //类型相等
|
|
|
+ System.out.println("====ddd:"+oldSn+"====sn:"+sn+"====ol:"+oldNum+"==nu:"+num);
|
|
|
+ if ((oldDataType.equals("lightStatus") || oldDataType.equals("adjustLightController")) && oldSn.equals(sn) && (oldNum == num)){
|
|
|
+ //查看灯的信息
|
|
|
+ System.out.println("====ddd:"+res);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ in ++;
|
|
|
+ if (in > timeout) return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ client.disconnect();
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("==dd=="+e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
/**
|
|
|
* 字节数组转16进制字符串
|
|
|
* @param b 字节数组
|
|
@@ -895,4 +1063,94 @@ public class ToolUtils {
|
|
|
}
|
|
|
return protocolTypeStr;
|
|
|
}
|
|
|
+
|
|
|
+ /* 向第三方请求post
|
|
|
+ *@param request:请求方式
|
|
|
+ * @param url:路径
|
|
|
+ * @param param:参数
|
|
|
+ * @return 返回类型字符串
|
|
|
+ */
|
|
|
+ public String sendHttp(String request,String url, String param,Integer timeOut) throws IOException {
|
|
|
+ String result = "";
|
|
|
+ URL postUrl = new URL(url);
|
|
|
+ // 打开连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
|
|
|
+ // 设置是否向connection输出,因为这个是post请求,参数要放在
|
|
|
+ // http正文内,因此需要设为true
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // Set the post method. Default is GET
|
|
|
+ connection.setRequestMethod(request.toUpperCase());
|
|
|
+ // Post 请求不能使用缓存
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ // 进行编码
|
|
|
+ connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
|
|
|
+ // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
|
|
|
+ // 要注意的是connection.getOutputStream会隐含的进行connect。
|
|
|
+ connection.connect();
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
|
|
|
+ // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
|
|
|
+ out.writeBytes(param);
|
|
|
+ out.flush();
|
|
|
+ out.close(); // flush and close
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
|
|
|
+ connection.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ reader.close();
|
|
|
+ connection.disconnect();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 向第三方请求post
|
|
|
+ *@param request:请求方式
|
|
|
+ * @param url:路径
|
|
|
+ * @param param:参数
|
|
|
+ * @return 返回类型字符串
|
|
|
+ */
|
|
|
+ public String sendHttp(String request,String url, String param,String token,String sessionId,Integer timeOut) throws IOException {
|
|
|
+ String result = "";
|
|
|
+ URL postUrl = new URL(url);
|
|
|
+ // 打开连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
|
|
|
+ // 设置是否向connection输出,因为这个是post请求,参数要放在
|
|
|
+ // http正文内,因此需要设为true
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // Set the post method. Default is GET
|
|
|
+ connection.setRequestMethod(request.toUpperCase());
|
|
|
+ // Post 请求不能使用缓存
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ // 进行编码
|
|
|
+ connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
|
|
|
+ connection.setRequestProperty("Authorization","Bearer "+token);
|
|
|
+ if (sessionId != null){
|
|
|
+ connection.setRequestProperty("SessionId"," "+sessionId);
|
|
|
+ }
|
|
|
+ // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
|
|
|
+ // 要注意的是connection.getOutputStream会隐含的进行connect。
|
|
|
+ connection.connect();
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
|
|
|
+ // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
|
|
|
+ out.writeBytes(param);
|
|
|
+ out.flush();
|
|
|
+ out.close(); // flush and close
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
|
|
|
+ connection.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ reader.close();
|
|
|
+ connection.disconnect();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|