second commit by ailanyin
This commit is contained in:
@ -79,4 +79,6 @@ public class CaptchaUtil {
|
||||
return new Object[]{sb.toString(), image};
|
||||
}
|
||||
|
||||
//public static String getRandom
|
||||
|
||||
}
|
||||
|
@ -798,4 +798,82 @@ public class ConvertUtil {
|
||||
return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制转2进制
|
||||
*
|
||||
* @param hexString
|
||||
* @return
|
||||
*/
|
||||
public static String hex2Binary(String hexString) {
|
||||
if (hexString == null || hexString.length() % 2 != 0) {
|
||||
return null;
|
||||
}
|
||||
String bString = "", tmp;
|
||||
|
||||
for (int i = 0; i < hexString.length(); i++) {
|
||||
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
|
||||
bString += tmp.substring(tmp.length() - 4);
|
||||
}
|
||||
return bString;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 二进制相加
|
||||
*
|
||||
* @param a
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
public static String binaryAdd(String a, String b) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int pre = 0;
|
||||
int sum = 0;
|
||||
|
||||
while (a.length() != b.length()) {
|
||||
if (a.length() > b.length()) {
|
||||
b = "0" + b;
|
||||
} else {
|
||||
a = "0" + a;
|
||||
}
|
||||
}
|
||||
for (int i = a.length() - 1; i >= 0; i--) {
|
||||
x = a.charAt(i) - '0';
|
||||
y = b.charAt(i) - '0';
|
||||
sum = x + y + pre;
|
||||
if (sum >= 2) {
|
||||
pre = 1;
|
||||
sb.append(sum - 2);
|
||||
} else {
|
||||
pre = 0;
|
||||
sb.append(sum);
|
||||
}
|
||||
}
|
||||
if (pre == 1) {
|
||||
sb.append("1");
|
||||
}
|
||||
return sb.reverse().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 二进制取反 0->1 1->
|
||||
*
|
||||
* @param binary
|
||||
* @return
|
||||
*/
|
||||
public static String binaryReverse(String binary) {
|
||||
String[] split = binary.split("");
|
||||
String result = "";
|
||||
for (String s : split) {
|
||||
if ("0".equals(s)) {
|
||||
s = "1";
|
||||
} else {
|
||||
s = "0";
|
||||
}
|
||||
result = result + (s);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,60 @@
|
||||
package com.ailanyin.common.utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* 操作数据库
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2022-1-7 11:12:01
|
||||
*/
|
||||
public class DbUtil {
|
||||
|
||||
private static Statement stmt = null;
|
||||
private static Connection connection = null;
|
||||
|
||||
public static void runSql(String sql) {
|
||||
try {
|
||||
//1.加载jdbc驱动类
|
||||
Class.forName("dm.jdbc.driver.DmDriver");
|
||||
//2.获取远程mysql连接
|
||||
String url = "jdbc:dm://192.168.0.153:5236/AITEST?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||
String user = "SYSDBA";
|
||||
connection = DriverManager.getConnection(url, user, "root12345");
|
||||
//3.开启sql称述,(Statement,PreparedStatement(?占位符形式)),执行sql语句
|
||||
stmt = connection.createStatement();
|
||||
String[] split = sql.split(";");
|
||||
for (String s : split) {
|
||||
if (EmptyUtil.isNotEmpty(s)) {
|
||||
stmt.addBatch(s);
|
||||
}
|
||||
}
|
||||
stmt.executeBatch();
|
||||
//4.获取结果集(只有查询有)
|
||||
//5.对结果集进行操作(只有查询有)
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -255,4 +255,19 @@ public class FileUtil {
|
||||
String contextPath = request.getServletContext().getContextPath();
|
||||
return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
|
||||
}
|
||||
|
||||
public static String getJarPath() {
|
||||
try {
|
||||
String filePath = FileUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();
|
||||
String jarPath = filePath.split(".jar!")[0];
|
||||
jarPath = jarPath.split("file:")[1];
|
||||
return jarPath.substring(0,jarPath.lastIndexOf("/"));
|
||||
} catch (Exception e) {
|
||||
File desc = new File("/opt/jar");
|
||||
if (!desc.exists()) {
|
||||
desc.mkdir();
|
||||
}
|
||||
return "/opt/jar";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 获取IP工具类
|
||||
@ -25,7 +27,12 @@ public class IpUtil {
|
||||
private final static String X_REAL_IP = "X-Real-IP";
|
||||
private final static String LOCALHOST = "127.0.0.1";
|
||||
private final static String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
|
||||
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
|
||||
private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
|
||||
private static final String CHECK_IP = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
|
||||
+ "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
|
||||
+ "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
|
||||
+ "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
|
||||
private static Pattern pattern = Pattern.compile(CHECK_IP);
|
||||
|
||||
/**
|
||||
* 获取请求的ip地址
|
||||
@ -97,4 +104,9 @@ public class IpUtil {
|
||||
}
|
||||
return CHINESE_UNKNOWN;
|
||||
}
|
||||
|
||||
public static boolean checkIpIsTrue(String ipStr) {
|
||||
Matcher matcher = pattern.matcher(ipStr);
|
||||
return matcher.matches();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,125 @@
|
||||
package com.ailanyin.common.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2022/1/6 0006 上午 10:03
|
||||
*/
|
||||
public class LinuxUtil {
|
||||
|
||||
public static void createJarService(String jarName,String filePath) {
|
||||
|
||||
if (checkFileIsExist(jarName)) {
|
||||
System.out.println("/etc/systemd/system/"+jarName+".Service 文件已存在,无需重新创建");
|
||||
return;
|
||||
}
|
||||
jarName = filePath + "/" + jarName;
|
||||
createFiles(jarName);
|
||||
createStartShell(jarName);
|
||||
createStopShell(jarName);
|
||||
addPersmission(jarName);
|
||||
createServiceShell(jarName);
|
||||
handleService(jarName);
|
||||
}
|
||||
|
||||
private static void createStartShell(String jarName) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("#!/bin/bash");
|
||||
list.add("export JAVA_HOME=/opt/jdk1.8");
|
||||
list.add("export JRE_HOME=/opt/jdk1.8/jre");
|
||||
list.add("export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH");
|
||||
list.add("export PATH=$JAVA_HOME/bin:$PATH");
|
||||
list.add("nohup java -jar "+ jarName + ".jar >" + jarName + ".out 2>&1 &");
|
||||
list.add("echo $! > /var/run/" + jarName.substring(jarName.lastIndexOf("/") + 1) + ".pid");
|
||||
cn.hutool.core.io.FileUtil.writeLines(list, jarName + "-start.sh", "UTF-8", false);
|
||||
}
|
||||
|
||||
private static void createStopShell(String jarName) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("#!/bin/bash");
|
||||
list.add("PID=$(cat /var/run/" + jarName.substring(jarName.lastIndexOf("/") + 1) + ".pid)");
|
||||
list.add("kill -9 $PID");
|
||||
cn.hutool.core.io.FileUtil.writeLines(list, jarName + "-stop.sh", "UTF-8", false);
|
||||
}
|
||||
|
||||
private static void createServiceShell(String jarName) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("[Unit]");
|
||||
list.add("Description=service for description");
|
||||
list.add("After=syslog.target network.target remote-fs.target nss-lookup.target");
|
||||
list.add("");
|
||||
list.add("[Service]");
|
||||
list.add("User=root");
|
||||
list.add("Type=forking");
|
||||
list.add("ExecStart=/bin/sh " + jarName + "-start.sh");
|
||||
list.add("ExecStop=/bin/sh " + jarName + "-stop.sh");
|
||||
list.add("");
|
||||
list.add("[Install]");
|
||||
list.add("WantedBy=multi-user.target");
|
||||
FileUtil.writeLines(list, jarName + ".service", "UTF-8", false);
|
||||
}
|
||||
|
||||
private static void addPersmission(String jarName) {
|
||||
shell("chmod +x " + jarName + "-start.sh");
|
||||
shell("chmod +x " + jarName + "-stop.sh");
|
||||
shell("chmod +x " + jarName + ".service");
|
||||
}
|
||||
|
||||
private static void handleService(String jarName) {
|
||||
String projectName = jarName.substring(jarName.lastIndexOf("/") + 1);
|
||||
shell("mv " + jarName + ".service /etc/systemd/system/" + projectName + ".service");
|
||||
shell("chmod +x " + "/etc/systemd/system/" + projectName + ".service");
|
||||
shell("systemctl daemon-reload");
|
||||
shell("systemctl enable " + projectName);
|
||||
shell("systemctl daemon-reload");
|
||||
}
|
||||
|
||||
private static void createFiles(String jarName) {
|
||||
createFile( jarName + "-start.sh");
|
||||
createFile( jarName + "-stop.sh");
|
||||
createFile( jarName + ".service");
|
||||
}
|
||||
|
||||
private static void createFile(String path) {
|
||||
// 新建文件
|
||||
File newFile = new File(path);
|
||||
if (!newFile.exists()) {
|
||||
try {
|
||||
if (!newFile.createNewFile()) {
|
||||
System.out.println(path + "创建失败!");
|
||||
} else {
|
||||
System.out.println("成功创建文件: " + path);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* linux执行shell
|
||||
*
|
||||
* ("sh", "-c" 可以执行复杂命令)
|
||||
* @param shell 命令
|
||||
*/
|
||||
public static void shell(String shell) {
|
||||
String[] cmd = new String[]{"sh", "-c", shell};
|
||||
try {
|
||||
Runtime.getRuntime().exec(cmd);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean checkFileIsExist(String jarName) {
|
||||
String path = "/etc/systemd/system/"+jarName+".Service";
|
||||
return cn.hutool.core.io.FileUtil.isFile(path);
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package com.ailanyin.common.utils;
|
||||
|
||||
/**
|
||||
* 阅读器工具类
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2022/1/14 0014 上午 9:49
|
||||
*/
|
||||
public class ReaderUtil {
|
||||
|
||||
/**
|
||||
* 阅读器主动上报帧头 5A
|
||||
*/
|
||||
private static final String ACTIVE_INITIATIVE_HEAD = "5A";
|
||||
/**
|
||||
* 阅读器主动上报长度 08
|
||||
*/
|
||||
private static final String ACTIVE_INITIATIVE_LENGTH = "08";
|
||||
/**
|
||||
* 阅读器操作执行的结果 00 表示正常
|
||||
*/
|
||||
private static final String ACTIVE_INITIATIVE_STATUS_OK = "00";
|
||||
|
||||
/**
|
||||
* 检查阅读器主动上报数据的真实性
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkActiveInitiative(String data) {
|
||||
// 5A FF 08 00 00 01 60 7B 00 00 C3
|
||||
// 5A FF 08 00 00 01 62 87 00 2C 89
|
||||
if (!ACTIVE_INITIATIVE_HEAD.equals(data.substring(0, 2))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ACTIVE_INITIATIVE_LENGTH.equals(data.substring(6, 8))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ACTIVE_INITIATIVE_STATUS_OK.equals(data.substring(9, 11))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!checkActiveInitiativeData(data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean checkActiveInitiativeData(String data) {
|
||||
// 5A FF 08 00 00 01 60 7B 00 00 C3
|
||||
// (5A+FF+08+00+00+01+60+7B+00+00) 相加取反加1,取最后两个字节位 和 最后C3比较
|
||||
String result = "";
|
||||
String[] s1 = data.split(" ");
|
||||
for (int i = 0; i < s1.length - 1; i++) {
|
||||
if (EmptyUtil.isNotEmpty(s1[i])) {
|
||||
result = ConvertUtil.binaryAdd(result, ConvertUtil.hex2Binary(s1[i]));
|
||||
}
|
||||
}
|
||||
String s2 = ConvertUtil.binaryAdd(ConvertUtil.binaryReverse(result), "1");
|
||||
return s2.substring(s2.length() - 8).equals(ConvertUtil.hex2Binary(s1[s1.length - 1]));
|
||||
}
|
||||
|
||||
public static Integer calculateDistance(String rssi) {
|
||||
//十六进制 -> 十进制
|
||||
int distance = Integer.parseInt(rssi, 16);
|
||||
|
||||
// 2A-2C
|
||||
if (distance >= 42 && distance <= 44) {
|
||||
return 0;
|
||||
}
|
||||
// 2D-2E
|
||||
if (distance >= 45 && distance <= 46) {
|
||||
return 5;
|
||||
}
|
||||
// 2F-32
|
||||
if (distance >= 47 && distance <= 50) {
|
||||
return 10;
|
||||
}
|
||||
// 33-36
|
||||
if (distance >= 51 && distance <= 54) {
|
||||
return 15;
|
||||
}
|
||||
// 38-3E
|
||||
if (distance >= 56 && distance <= 62) {
|
||||
return 25;
|
||||
}
|
||||
// 3F-42
|
||||
if (distance >= 63 && distance <= 66) {
|
||||
return 50;
|
||||
}
|
||||
// 43-47
|
||||
if (distance >= 67 && distance <= 71) {
|
||||
return 80;
|
||||
}
|
||||
// 48-50
|
||||
if (distance >= 72 && distance <= 80) {
|
||||
return 110;
|
||||
}
|
||||
// 51-59
|
||||
if (distance >= 81 && distance <= 89) {
|
||||
return 150;
|
||||
}
|
||||
|
||||
// 异常情况
|
||||
return 200;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println(checkActiveInitiativeData("5A FF 08 00 00 01 60 7B 00 00 C3"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user