package com.ld.igds.io.fzzy;
|
|
|
import com.alibaba.fastjson.JSON;
|
import com.ld.igds.constant.RedisConst;
|
import com.ld.igds.io.fzzy.dto.IoMessage;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.RandomUtils;
|
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Base64;
|
|
/**
|
* 工具类
|
*
|
* @author vince
|
*/
|
@Slf4j
|
public class ServerUtils {
|
|
|
public static final String VERSION_1 = "v1.0";
|
|
|
public static final String SIGN_DEFAULT = "0000";
|
|
public static final String CHARSET = "UTF-8";
|
public static final String MSG_BEGIN = "<BHZN>";
|
public static final String MSG_END = "<END>";
|
|
//功能码
|
public static final String FUNCTION_1001 = "1001";// 基础参数配置
|
public static final String FUNCTION_2001 = "2001";// 开始检测
|
public static final String FUNCTION_2002 = "2002";// 停止检测
|
public static final String FUNCTION_2003 = "2003";// 进度查询
|
public static final String FUNCTION_2004 = "2004";// 结果上报
|
public static final String FUNCTION_2005 = "2005";// 坐标提取
|
public static final String FUNCTION_2006 = "2006";// 远程抓拍
|
public static final String FUNCTION_2007 = "2007";// 心跳维持
|
public static final String FUNCTION_1002 = "1002";// 时间同步
|
public static final String FUNCTION_1003 = "1003";// 设备登陆
|
|
|
//响应码
|
public static String RESP_0 = "0";// 成功
|
public static String RESP_1 = "1";// 命令格式错误
|
public static String RESP_2 = "2";// 命令数据检验异常
|
public static String RESP_3 = "3";// 发送超时错误
|
public static String RESP_4 = "4";// 终端设备不在线
|
public static String RESP_5 = "5";// 终端设备执行中
|
public static String RESP_6 = "6";// 终端设备抓拍失败
|
public static String RESP_9 = "9";// 其它错误
|
|
/**
|
* 生成TCP连接的KEY
|
*
|
* @param ip
|
* @param port
|
* @return
|
*/
|
public static String getServerKey(String ip, Integer port) {
|
return ip + ":" + port;
|
}
|
|
/**
|
* @param message
|
* @return
|
*/
|
public static IoMessage buildMessage(String message) throws Exception {
|
IoMessage ioMessage = JSON.parseObject(message, IoMessage.class);
|
return ioMessage;
|
}
|
|
/**
|
* base64转为图片
|
*
|
* @param path 文件路径:到文件夹即可,代码里会在文件夹里生成对应的jpg文件
|
* @param base64
|
* @return
|
*/
|
public static String base64ToJpg(String path, String fileName, String base64) {
|
// 判断文件路径是否存在
|
File filePath = new File(path);
|
if (!filePath.exists()) {
|
filePath.mkdirs();
|
}
|
// 创建文件
|
String jpgFile = path + "\\" + fileName + ".jpg";
|
File file = new File(jpgFile);
|
boolean jpgFileExist = false;
|
try {
|
jpgFileExist = file.createNewFile();
|
log.info("jpg文件创建成功");
|
} catch (IOException e) {
|
log.info("jpg文件创建失败");
|
e.printStackTrace();
|
}
|
if (jpgFileExist) {
|
// 解密
|
Base64.Decoder decoder = Base64.getDecoder();
|
// 去掉base64前缀 data:image/jpeg;base64,
|
base64 = base64.substring(base64.indexOf(",", 1) + 1, base64.length());
|
byte[] b = decoder.decode(base64);
|
// 处理数据
|
for (int i = 0; i < b.length; ++i) {
|
if (b[i] < 0) {
|
b[i] += 256;
|
}
|
}
|
// 保存图片
|
try {
|
FileOutputStream out = new FileOutputStream(jpgFile);
|
out.write(b);
|
out.flush();
|
out.close();
|
// 写入成功返回文件路径
|
return jpgFile;
|
} catch (FileNotFoundException e) {
|
log.info("文件未找到");
|
e.printStackTrace();
|
} catch (IOException e) {
|
log.info("写入失败");
|
e.printStackTrace();
|
}
|
}
|
return "文件不存在";
|
}
|
|
public static String buildRequestKey(String companyId, String sn) {
|
return RedisConst.buildKey(companyId, sn);
|
}
|
|
public static String createOrderId() {
|
return RandomUtils.nextInt(1000, 9999) + "";
|
}
|
}
|