29 changed files with 891 additions and 767 deletions
@ -1,91 +1,91 @@
|
||||
package com.mh.user.aspect; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import com.mh.common.utils.StringUtils; |
||||
import com.mh.user.utils.SecurityUtils; |
||||
import org.apache.commons.beanutils.BeanUtils; |
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Pointcut; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
/** |
||||
* DAO切面,插入创建人,创建时间,修改人,修改时间 |
||||
* @author Louis |
||||
* @date Oct 29, 2018 |
||||
*/ |
||||
@Aspect |
||||
@Component |
||||
@Configuration |
||||
public class DaoAspect { |
||||
private static final String createBy = "createBy"; |
||||
private static final String createTime = "createTime"; |
||||
private static final String lastUpdateBy = "lastUpdateBy"; |
||||
private static final String lastUpdateTime = "lastUpdateTime"; |
||||
|
||||
@Pointcut("execution(* com.mh.*.mapper.*.update*(..))") |
||||
public void daoUpdate() { |
||||
} |
||||
|
||||
@Pointcut("execution(* com.mh.*.mapper.*.insert*(..))") |
||||
public void daoCreate() { |
||||
} |
||||
|
||||
@Around("daoUpdate()") |
||||
public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable { |
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
||||
if (attributes == null) { |
||||
return pjp.proceed(); |
||||
} |
||||
HttpServletRequest request = attributes.getRequest(); |
||||
String token = request.getHeader("token"); |
||||
String username = getUserName(); |
||||
if (token != null && username != null) { |
||||
Object[] objects = pjp.getArgs(); |
||||
if (objects != null && objects.length > 0) { |
||||
for (Object arg : objects) { |
||||
BeanUtils.setProperty(arg, lastUpdateBy, username); |
||||
BeanUtils.setProperty(arg, lastUpdateTime, new Date()); |
||||
} |
||||
} |
||||
} |
||||
Object object = pjp.proceed(); |
||||
return object; |
||||
|
||||
} |
||||
|
||||
@Around("daoCreate()") |
||||
public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable { |
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
||||
if (attributes == null) { |
||||
return pjp.proceed(); |
||||
} |
||||
Object[] objects = pjp.getArgs(); |
||||
if (objects != null && objects.length > 0) { |
||||
for (Object arg : objects) { |
||||
String username = getUserName(); |
||||
if (username != null) { |
||||
if (StringUtils.isBlank(BeanUtils.getProperty(arg, createBy))) { |
||||
BeanUtils.setProperty(arg, createBy, username); |
||||
} |
||||
if (StringUtils.isBlank(BeanUtils.getProperty(arg, createTime))) { |
||||
BeanUtils.setProperty(arg, createTime, new Date()); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Object object = pjp.proceed(); |
||||
return object; |
||||
} |
||||
|
||||
private String getUserName() { |
||||
return SecurityUtils.getUsername(); |
||||
} |
||||
} |
||||
//package com.mh.user.aspect;
|
||||
//
|
||||
//import java.util.Date;
|
||||
//
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//
|
||||
//import com.mh.common.utils.StringUtils;
|
||||
//import com.mh.user.utils.SecurityUtils;
|
||||
//import org.apache.commons.beanutils.BeanUtils;
|
||||
//import org.aspectj.lang.ProceedingJoinPoint;
|
||||
//import org.aspectj.lang.annotation.Around;
|
||||
//import org.aspectj.lang.annotation.Aspect;
|
||||
//import org.aspectj.lang.annotation.Pointcut;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//import org.springframework.web.context.request.RequestContextHolder;
|
||||
//import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
//
|
||||
///**
|
||||
// * DAO切面,插入创建人,创建时间,修改人,修改时间
|
||||
// * @author Louis
|
||||
// * @date Oct 29, 2018
|
||||
// */
|
||||
//@Aspect
|
||||
//@Component
|
||||
//@Configuration
|
||||
//public class DaoAspect {
|
||||
// private static final String createBy = "createBy";
|
||||
// private static final String createTime = "createTime";
|
||||
// private static final String lastUpdateBy = "lastUpdateBy";
|
||||
// private static final String lastUpdateTime = "lastUpdateTime";
|
||||
//
|
||||
// @Pointcut("execution(* com.mh.*.mapper.*.update*(..))")
|
||||
// public void daoUpdate() {
|
||||
// }
|
||||
//
|
||||
// @Pointcut("execution(* com.mh.*.mapper.*.insert*(..))")
|
||||
// public void daoCreate() {
|
||||
// }
|
||||
//
|
||||
// @Around("daoUpdate()")
|
||||
// public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable {
|
||||
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
// if (attributes == null) {
|
||||
// return pjp.proceed();
|
||||
// }
|
||||
// HttpServletRequest request = attributes.getRequest();
|
||||
// String token = request.getHeader("token");
|
||||
// String username = getUserName();
|
||||
// if (token != null && username != null) {
|
||||
// Object[] objects = pjp.getArgs();
|
||||
// if (objects != null && objects.length > 0) {
|
||||
// for (Object arg : objects) {
|
||||
// BeanUtils.setProperty(arg, lastUpdateBy, username);
|
||||
// BeanUtils.setProperty(arg, lastUpdateTime, new Date());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Object object = pjp.proceed();
|
||||
// return object;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Around("daoCreate()")
|
||||
// public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable {
|
||||
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
// if (attributes == null) {
|
||||
// return pjp.proceed();
|
||||
// }
|
||||
// Object[] objects = pjp.getArgs();
|
||||
// if (objects != null && objects.length > 0) {
|
||||
// for (Object arg : objects) {
|
||||
// String username = getUserName();
|
||||
// if (username != null) {
|
||||
// if (StringUtils.isBlank(BeanUtils.getProperty(arg, createBy))) {
|
||||
// BeanUtils.setProperty(arg, createBy, username);
|
||||
// }
|
||||
// if (StringUtils.isBlank(BeanUtils.getProperty(arg, createTime))) {
|
||||
// BeanUtils.setProperty(arg, createTime, new Date());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Object object = pjp.proceed();
|
||||
// return object;
|
||||
// }
|
||||
//
|
||||
// private String getUserName() {
|
||||
// return SecurityUtils.getUsername();
|
||||
// }
|
||||
//}
|
||||
|
@ -1,281 +1,281 @@
|
||||
package com.mh.user.serialport; |
||||
|
||||
import com.mh.user.constants.Constant; |
||||
import com.mh.user.entity.DeviceCodeParamEntity; |
||||
import com.mh.user.service.BuildingService; |
||||
import com.mh.user.service.DeviceCodeParamService; |
||||
import com.mh.user.service.DeviceInstallService; |
||||
import com.mh.user.service.NowDataService; |
||||
import com.mh.user.utils.*; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.context.ApplicationContext; |
||||
import gnu.io.SerialPort; |
||||
import java.nio.ByteBuffer; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author nxr |
||||
* @title : |
||||
* @description : 串口发送和接收处理,采集类 |
||||
* @updateTime 2022-08-10 |
||||
* @throws : |
||||
*/ |
||||
@Slf4j |
||||
public class SerialPortSendReceive { |
||||
|
||||
private String receiveStr = null; |
||||
public SerialPort serialPort = null; |
||||
private int size = 0; |
||||
private int baudrate=9600; |
||||
private String parity=null; |
||||
List<DeviceCodeParamEntity> deviceManageEntityList; |
||||
// 调用service
|
||||
ApplicationContext context = SpringBeanUtil.getApplicationContext(); |
||||
DeviceCodeParamService deviceCodeParamService = context.getBean(DeviceCodeParamService.class); |
||||
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); |
||||
NowDataService nowDataService = context.getBean(NowDataService.class); |
||||
BuildingService buildingService = context.getBean(BuildingService.class); |
||||
AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485(); |
||||
|
||||
public void serialPortSend(String sort,String thread) { |
||||
//查看所有串口
|
||||
SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil(); |
||||
ArrayList<String> port = serialPortUtil.findPort(); |
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||
Date date = new Date(); |
||||
String dateStr = df.format(date); |
||||
|
||||
// SerialTool serialPortUtil = SerialTool.getSerialPortUtil();
|
||||
//package com.mh.user.serialport;
|
||||
//
|
||||
//import com.mh.user.constants.Constant;
|
||||
//import com.mh.user.entity.DeviceCodeParamEntity;
|
||||
//import com.mh.user.service.BuildingService;
|
||||
//import com.mh.user.service.DeviceCodeParamService;
|
||||
//import com.mh.user.service.DeviceInstallService;
|
||||
//import com.mh.user.service.NowDataService;
|
||||
//import com.mh.user.utils.*;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.context.ApplicationContext;
|
||||
//import gnu.io.SerialPort;
|
||||
//import java.nio.ByteBuffer;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//import java.text.SimpleDateFormat;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Date;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @author nxr
|
||||
// * @title :
|
||||
// * @description : 串口发送和接收处理,采集类
|
||||
// * @updateTime 2022-08-10
|
||||
// * @throws :
|
||||
// */
|
||||
//@Slf4j
|
||||
//public class SerialPortSendReceive {
|
||||
//
|
||||
// private String receiveStr = null;
|
||||
// public SerialPort serialPort = null;
|
||||
// private int size = 0;
|
||||
// private int baudrate=9600;
|
||||
// private String parity=null;
|
||||
// List<DeviceCodeParamEntity> deviceManageEntityList;
|
||||
// // 调用service
|
||||
// ApplicationContext context = SpringBeanUtil.getApplicationContext();
|
||||
// DeviceCodeParamService deviceCodeParamService = context.getBean(DeviceCodeParamService.class);
|
||||
// DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class);
|
||||
// NowDataService nowDataService = context.getBean(NowDataService.class);
|
||||
// BuildingService buildingService = context.getBean(BuildingService.class);
|
||||
// AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
|
||||
//
|
||||
// public void serialPortSend(String sort,String thread) {
|
||||
// //查看所有串口
|
||||
// SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil();
|
||||
// ArrayList<String> port = serialPortUtil.findPort();
|
||||
// comName=comName.toUpperCase(); //转为大写
|
||||
// if (port.contains(comName)){
|
||||
try{ |
||||
//生成对应的采集指令
|
||||
if (sort.equals("1")){ //水温、水位
|
||||
deviceManageEntityList = deviceCodeParamService.queryCodeParam3(thread); |
||||
}else if (sort.equals("2")){ //采集水、电、运行状态!
|
||||
deviceManageEntityList = deviceCodeParamService.queryCodeParam4(thread); |
||||
}else if (sort.equals("3")){ //采集设定温度、设定水位、故障状态!
|
||||
deviceManageEntityList = deviceCodeParamService.queryCodeParam5(thread); |
||||
}else{ |
||||
deviceManageEntityList = deviceCodeParamService.queryCodeParam3(thread); |
||||
} |
||||
size = deviceManageEntityList.size(); |
||||
for (int i=0;i<size;i++){ |
||||
//判断网页端是否有操作设备的
|
||||
if (Constant.WEB_FLAG) { |
||||
Constant.FLAG=false; |
||||
serialPortUtil.closePort(serialPort); |
||||
// Thread.sleep(2000);
|
||||
log.info("有指令下发退出定时采集"); |
||||
break; |
||||
} |
||||
//获取设备实际波特率
|
||||
baudrate=deviceManageEntityList.get(i).getBaudrate(); |
||||
//获取设备实际校验位
|
||||
parity=deviceManageEntityList.get(i).getParity(); |
||||
String comName=deviceManageEntityList.get(i).getDataCom(); |
||||
comName=comName.toUpperCase(); //转为大写
|
||||
if (port.contains(comName)){ |
||||
String deviceAddr=deviceManageEntityList.get(i).getDeviceAddr(); |
||||
String deviceType=deviceManageEntityList.get(i).getDeviceType(); |
||||
String registerAddr=deviceManageEntityList.get(i).getRegisterAddr(); |
||||
String brand=deviceManageEntityList.get(i).getBrand();//品牌
|
||||
String buildingId=deviceManageEntityList.get(i).getBuildingId(); |
||||
String buildingName=buildingService.queryBuildingName(buildingId); //查询楼栋名称
|
||||
try{ |
||||
//传入对应的串口参数并打开串口
|
||||
if (parity==null || parity.equals("") || parity.equalsIgnoreCase("none")){ |
||||
serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_NONE, SerialPort.PARITY_ODD); |
||||
}else{ |
||||
serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.PARITY_ODD); |
||||
} |
||||
//向串口发送指令
|
||||
if (serialPort != null) { |
||||
// log.info("---------波特率:"+baudrate+",校验位:"+parity+" -----------");
|
||||
SendOrderUtils.sendSerialPort(deviceManageEntityList.get(i), serialPort); |
||||
if (deviceType.equals("热泵")){ |
||||
Thread.sleep(4000); |
||||
}else{ |
||||
Thread.sleep(2000); |
||||
} |
||||
}else{ |
||||
continue; //continue时,跳出本次循环,继续执行下次循环。Break时,跳出循环(结束循环),执行下面的语句。
|
||||
} |
||||
}catch (Exception e){ |
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
} |
||||
log.error("发送指令异常==>", e); |
||||
} |
||||
receiveStr=""; |
||||
//从串口读取数据
|
||||
byte[] bytes= serialPortUtil.readFromPort(serialPort); |
||||
try { |
||||
String byteStr = new String(bytes, 0, bytes.length).trim(); |
||||
} catch (NullPointerException e) { |
||||
serialPortUtil.closePort(serialPort); |
||||
Thread.sleep(2000); |
||||
log.info("串口"+serialPort+"没有数据返回!"+i); |
||||
log.info("----------------"+deviceType+"离线,设备号:"+deviceAddr+",所属楼栋:"+buildingName+"----------------"); |
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
} |
||||
String time1=deviceInstallService.selectLastDate(deviceType,deviceAddr,buildingId); |
||||
Date date1=new Date(); |
||||
String time2=df.format(date1); |
||||
if (time1==null){ |
||||
time1=df.format(date1); |
||||
} |
||||
int d= ExchangeStringUtil.compareCopyTime(time1,time2); |
||||
if (d==1){ |
||||
deviceInstallService.updateNotOnline(deviceAddr,deviceType,buildingId,"离线"); //所有设备离线
|
||||
if (deviceType.equals("热泵")){ |
||||
nowDataService.updateRunState(buildingId,deviceAddr,"离线", buildingName); //监控界面状态表热泵在线状态
|
||||
} |
||||
} |
||||
continue; |
||||
} |
||||
receiveStr = receiveStr + printHexString(bytes); |
||||
//去掉空格和null
|
||||
receiveStr = receiveStr.replace("null", ""); |
||||
receiveStr = receiveStr.replace(" ", ""); |
||||
log.info("串口"+serialPort+"接受第"+i+"数据:" + receiveStr + ",大小: " + receiveStr.length()); |
||||
try{ |
||||
serialPortUtil.closePort(serialPort); |
||||
log.info("关闭"+serialPort); |
||||
}catch (Exception e){ |
||||
// e.printStackTrace();
|
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
} |
||||
log.error("关闭"+serialPort+"失败!"); |
||||
} |
||||
//返回值全部变成大写
|
||||
String receiveData = receiveStr.toUpperCase(); |
||||
//截取去掉FE
|
||||
String dataStr; |
||||
if (receiveData.length()>8){ |
||||
String str1=receiveData.substring(0,8); |
||||
String str2=receiveData.substring(8); |
||||
dataStr=str1.replace("FE", "")+str2; |
||||
}else{ |
||||
dataStr = receiveData.replace("FE", ""); |
||||
} |
||||
deviceInstallService.updateOnline(deviceAddr,deviceType,buildingId,"在线"); //设备在线
|
||||
log.info("----------------"+deviceType+"在线,设备号:"+deviceAddr+",所属楼栋:"+buildingName+"----------------"); |
||||
if (deviceType.equals("热泵")){ |
||||
String strState=nowDataService.selectState(buildingId,deviceAddr); |
||||
if (strState!=null && strState.equals("离线")){ //采集到数据
|
||||
nowDataService.updateRunState(buildingId,deviceAddr,"不运行", buildingName); //监控界面状态表热泵在线状态
|
||||
} |
||||
} |
||||
try{ |
||||
SerialPortSendReceive2.analysisReceiveData(dateStr, deviceType, registerAddr, brand, buildingId, dataStr, analysisReceiveOrder485, nowDataService, buildingName); |
||||
}catch (Exception e){ |
||||
// e.printStackTrace();
|
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
} |
||||
log.error(deviceManageEntityList.get(i).getDeviceType()+"保存数据库失败!"+i); |
||||
} |
||||
Thread.sleep(1000); |
||||
}else{ |
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
} |
||||
log.info("-------------串口:"+comName+"不存在!-------------"); |
||||
} |
||||
if(i==size-1){ |
||||
Constant.FLAG=false; |
||||
log.info("-------------一轮采集完,采集标志Constant.Flag="+Constant.FLAG+"-------------"); |
||||
} |
||||
} |
||||
}catch (Exception e){ |
||||
e.printStackTrace(); |
||||
Constant.WEB_FLAG=false;//可以采集的状态
|
||||
Constant.FLAG=false; |
||||
log.error("-------------串口采集异常!-------------"); |
||||
} |
||||
// }else {
|
||||
// log.info("串口:"+comName+"不存在!");
|
||||
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// Date date = new Date();
|
||||
// String dateStr = df.format(date);
|
||||
//
|
||||
//// SerialTool serialPortUtil = SerialTool.getSerialPortUtil();
|
||||
//// ArrayList<String> port = serialPortUtil.findPort();
|
||||
//// comName=comName.toUpperCase(); //转为大写
|
||||
//// if (port.contains(comName)){
|
||||
// try{
|
||||
// //生成对应的采集指令
|
||||
// if (sort.equals("1")){ //水温、水位
|
||||
// deviceManageEntityList = deviceCodeParamService.queryCodeParam3(thread);
|
||||
// }else if (sort.equals("2")){ //采集水、电、运行状态!
|
||||
// deviceManageEntityList = deviceCodeParamService.queryCodeParam4(thread);
|
||||
// }else if (sort.equals("3")){ //采集设定温度、设定水位、故障状态!
|
||||
// deviceManageEntityList = deviceCodeParamService.queryCodeParam5(thread);
|
||||
// }else{
|
||||
// deviceManageEntityList = deviceCodeParamService.queryCodeParam3(thread);
|
||||
// }
|
||||
// size = deviceManageEntityList.size();
|
||||
// for (int i=0;i<size;i++){
|
||||
// //判断网页端是否有操作设备的
|
||||
// if (Constant.WEB_FLAG) {
|
||||
// Constant.FLAG=false;
|
||||
// serialPortUtil.closePort(serialPort);
|
||||
//// Thread.sleep(2000);
|
||||
// log.info("有指令下发退出定时采集");
|
||||
// break;
|
||||
// }
|
||||
// //获取设备实际波特率
|
||||
// baudrate=deviceManageEntityList.get(i).getBaudrate();
|
||||
// //获取设备实际校验位
|
||||
// parity=deviceManageEntityList.get(i).getParity();
|
||||
// String comName=deviceManageEntityList.get(i).getDataCom();
|
||||
// comName=comName.toUpperCase(); //转为大写
|
||||
// if (port.contains(comName)){
|
||||
// String deviceAddr=deviceManageEntityList.get(i).getDeviceAddr();
|
||||
// String deviceType=deviceManageEntityList.get(i).getDeviceType();
|
||||
// String registerAddr=deviceManageEntityList.get(i).getRegisterAddr();
|
||||
// String brand=deviceManageEntityList.get(i).getBrand();//品牌
|
||||
// String buildingId=deviceManageEntityList.get(i).getBuildingId();
|
||||
// String buildingName=buildingService.queryBuildingName(buildingId); //查询楼栋名称
|
||||
// try{
|
||||
// //传入对应的串口参数并打开串口
|
||||
// if (parity==null || parity.equals("") || parity.equalsIgnoreCase("none")){
|
||||
// serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_NONE, SerialPort.PARITY_ODD);
|
||||
// }else{
|
||||
// serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.PARITY_ODD);
|
||||
// }
|
||||
// //向串口发送指令
|
||||
// if (serialPort != null) {
|
||||
//// log.info("---------波特率:"+baudrate+",校验位:"+parity+" -----------");
|
||||
// SendOrderUtils.sendSerialPort(deviceManageEntityList.get(i), serialPort);
|
||||
// if (deviceType.equals("热泵")){
|
||||
// Thread.sleep(4000);
|
||||
// }else{
|
||||
// Thread.sleep(2000);
|
||||
// }
|
||||
// }else{
|
||||
// continue; //continue时,跳出本次循环,继续执行下次循环。Break时,跳出循环(结束循环),执行下面的语句。
|
||||
// }
|
||||
// }catch (Exception e){
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// }
|
||||
// log.error("发送指令异常==>", e);
|
||||
// }
|
||||
// receiveStr="";
|
||||
// //从串口读取数据
|
||||
// byte[] bytes= serialPortUtil.readFromPort(serialPort);
|
||||
// try {
|
||||
// String byteStr = new String(bytes, 0, bytes.length).trim();
|
||||
// } catch (NullPointerException e) {
|
||||
// serialPortUtil.closePort(serialPort);
|
||||
// Thread.sleep(2000);
|
||||
// log.info("串口"+serialPort+"没有数据返回!"+i);
|
||||
// log.info("----------------"+deviceType+"离线,设备号:"+deviceAddr+",所属楼栋:"+buildingName+"----------------");
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// }
|
||||
// String time1=deviceInstallService.selectLastDate(deviceType,deviceAddr,buildingId);
|
||||
// Date date1=new Date();
|
||||
// String time2=df.format(date1);
|
||||
// if (time1==null){
|
||||
// time1=df.format(date1);
|
||||
// }
|
||||
// int d= ExchangeStringUtil.compareCopyTime(time1,time2);
|
||||
// if (d==1){
|
||||
// deviceInstallService.updateNotOnline(deviceAddr,deviceType,buildingId,"离线"); //所有设备离线
|
||||
// if (deviceType.equals("热泵")){
|
||||
// nowDataService.updateRunState(buildingId,deviceAddr,"离线", buildingName); //监控界面状态表热泵在线状态
|
||||
// }
|
||||
// }
|
||||
// continue;
|
||||
// }
|
||||
// receiveStr = receiveStr + printHexString(bytes);
|
||||
// //去掉空格和null
|
||||
// receiveStr = receiveStr.replace("null", "");
|
||||
// receiveStr = receiveStr.replace(" ", "");
|
||||
// log.info("串口"+serialPort+"接受第"+i+"数据:" + receiveStr + ",大小: " + receiveStr.length());
|
||||
// try{
|
||||
// serialPortUtil.closePort(serialPort);
|
||||
// log.info("关闭"+serialPort);
|
||||
// }catch (Exception e){
|
||||
//// e.printStackTrace();
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// }
|
||||
// log.error("关闭"+serialPort+"失败!");
|
||||
// }
|
||||
// //返回值全部变成大写
|
||||
// String receiveData = receiveStr.toUpperCase();
|
||||
// //截取去掉FE
|
||||
// String dataStr;
|
||||
// if (receiveData.length()>8){
|
||||
// String str1=receiveData.substring(0,8);
|
||||
// String str2=receiveData.substring(8);
|
||||
// dataStr=str1.replace("FE", "")+str2;
|
||||
// }else{
|
||||
// dataStr = receiveData.replace("FE", "");
|
||||
// }
|
||||
// deviceInstallService.updateOnline(deviceAddr,deviceType,buildingId,"在线"); //设备在线
|
||||
// log.info("----------------"+deviceType+"在线,设备号:"+deviceAddr+",所属楼栋:"+buildingName+"----------------");
|
||||
// if (deviceType.equals("热泵")){
|
||||
// String strState=nowDataService.selectState(buildingId,deviceAddr);
|
||||
// if (strState!=null && strState.equals("离线")){ //采集到数据
|
||||
// nowDataService.updateRunState(buildingId,deviceAddr,"不运行", buildingName); //监控界面状态表热泵在线状态
|
||||
// }
|
||||
// }
|
||||
// try{
|
||||
// SerialPortSendReceive2.analysisReceiveData(dateStr, deviceType, registerAddr, brand, buildingId, dataStr, analysisReceiveOrder485, nowDataService, buildingName);
|
||||
// }catch (Exception e){
|
||||
//// e.printStackTrace();
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// }
|
||||
// log.error(deviceManageEntityList.get(i).getDeviceType()+"保存数据库失败!"+i);
|
||||
// }
|
||||
// Thread.sleep(1000);
|
||||
// }else{
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// }
|
||||
// log.info("-------------串口:"+comName+"不存在!-------------");
|
||||
// }
|
||||
// if(i==size-1){
|
||||
// Constant.FLAG=false;
|
||||
// log.info("-------------一轮采集完,采集标志Constant.Flag="+Constant.FLAG+"-------------");
|
||||
// }
|
||||
// }
|
||||
// }catch (Exception e){
|
||||
// e.printStackTrace();
|
||||
// Constant.WEB_FLAG=false;//可以采集的状态
|
||||
// Constant.FLAG=false;
|
||||
// log.error("-------------串口采集异常!-------------");
|
||||
// }
|
||||
//// }else {
|
||||
//// log.info("串口:"+comName+"不存在!");
|
||||
//// }
|
||||
// }
|
||||
// /**
|
||||
// * 字节数组转16进制字符串
|
||||
// * @param b 字节数组
|
||||
// * @return 16进制字符串
|
||||
// */
|
||||
// public static String printHexString(byte[] b) {
|
||||
// StringBuilder sbf = new StringBuilder();
|
||||
// for (byte value : b) {
|
||||
// String hex = Integer.toHexString(value & 0xFF);
|
||||
// if (hex.length() == 1) {
|
||||
// hex = '0' + hex;
|
||||
// }
|
||||
// sbf.append(hex.toUpperCase()).append(" ");
|
||||
// }
|
||||
} |
||||
/** |
||||
* 字节数组转16进制字符串 |
||||
* @param b 字节数组 |
||||
* @return 16进制字符串 |
||||
*/ |
||||
public static String printHexString(byte[] b) { |
||||
StringBuilder sbf = new StringBuilder(); |
||||
for (byte value : b) { |
||||
String hex = Integer.toHexString(value & 0xFF); |
||||
if (hex.length() == 1) { |
||||
hex = '0' + hex; |
||||
} |
||||
sbf.append(hex.toUpperCase()).append(" "); |
||||
} |
||||
return sbf.toString().trim(); |
||||
} |
||||
|
||||
/** |
||||
* 十六进制字符串转byte[] |
||||
* @param hex 十六进制字符串 |
||||
* @return byte[] |
||||
*/ |
||||
public static byte[] hexStr2Byte(String hex) { |
||||
if (hex == null) { |
||||
return new byte[] {}; |
||||
} |
||||
|
||||
// 奇数位补0
|
||||
if (hex.length() % 2 != 0) { |
||||
hex = "0" + hex; |
||||
} |
||||
|
||||
int length = hex.length(); |
||||
ByteBuffer buffer = ByteBuffer.allocate(length / 2); |
||||
for (int i = 0; i < length; i++) { |
||||
String hexStr = hex.charAt(i) + ""; |
||||
i++; |
||||
hexStr += hex.charAt(i); |
||||
byte b = (byte) Integer.parseInt(hexStr, 16); |
||||
buffer.put(b); |
||||
} |
||||
return buffer.array(); |
||||
} |
||||
|
||||
/** |
||||
* 16进制转换成为string类型字符串 |
||||
* @param s 待转换字符串 |
||||
*/ |
||||
public static String hexStringToString(String s) { |
||||
if (s == null || "".equals(s)) { |
||||
return null; |
||||
} |
||||
s = s.replace(" ", ""); |
||||
byte[] baKeyword = new byte[s.length() / 2]; |
||||
for (int i = 0; i < baKeyword.length; i++) { |
||||
try { |
||||
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
try { |
||||
s = new String(baKeyword, StandardCharsets.UTF_8); |
||||
} catch (Exception e1) { |
||||
e1.printStackTrace(); |
||||
} |
||||
return s; |
||||
} |
||||
} |
||||
// return sbf.toString().trim();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 十六进制字符串转byte[]
|
||||
// * @param hex 十六进制字符串
|
||||
// * @return byte[]
|
||||
// */
|
||||
// public static byte[] hexStr2Byte(String hex) {
|
||||
// if (hex == null) {
|
||||
// return new byte[] {};
|
||||
// }
|
||||
//
|
||||
// // 奇数位补0
|
||||
// if (hex.length() % 2 != 0) {
|
||||
// hex = "0" + hex;
|
||||
// }
|
||||
//
|
||||
// int length = hex.length();
|
||||
// ByteBuffer buffer = ByteBuffer.allocate(length / 2);
|
||||
// for (int i = 0; i < length; i++) {
|
||||
// String hexStr = hex.charAt(i) + "";
|
||||
// i++;
|
||||
// hexStr += hex.charAt(i);
|
||||
// byte b = (byte) Integer.parseInt(hexStr, 16);
|
||||
// buffer.put(b);
|
||||
// }
|
||||
// return buffer.array();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 16进制转换成为string类型字符串
|
||||
// * @param s 待转换字符串
|
||||
// */
|
||||
// public static String hexStringToString(String s) {
|
||||
// if (s == null || "".equals(s)) {
|
||||
// return null;
|
||||
// }
|
||||
// s = s.replace(" ", "");
|
||||
// byte[] baKeyword = new byte[s.length() / 2];
|
||||
// for (int i = 0; i < baKeyword.length; i++) {
|
||||
// try {
|
||||
// baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// try {
|
||||
// s = new String(baKeyword, StandardCharsets.UTF_8);
|
||||
// } catch (Exception e1) {
|
||||
// e1.printStackTrace();
|
||||
// }
|
||||
// return s;
|
||||
// }
|
||||
//}
|
||||
|
@ -1,219 +1,219 @@
|
||||
package com.mh.user.serialport; |
||||
|
||||
import com.mh.user.constants.Constant; |
||||
import com.mh.user.entity.DeviceCodeParamEntity; |
||||
import com.mh.user.service.BuildingService; |
||||
import com.mh.user.service.DeviceCodeParamService; |
||||
import com.mh.user.service.DeviceInstallService; |
||||
import com.mh.user.service.NowDataService; |
||||
import com.mh.user.utils.AnalysisReceiveOrder485; |
||||
import com.mh.user.utils.SendOrderUtils; |
||||
import com.mh.user.utils.SpringBeanUtil; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.stereotype.Component; |
||||
//import purejavacomm.SerialPort;
|
||||
import gnu.io.SerialPort; |
||||
|
||||
import java.nio.ByteBuffer; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author nxr |
||||
* @title : |
||||
* @description : 串口发送和接收处理,操作类 |
||||
* @updateTime 2022-08-10 |
||||
* @throws : |
||||
*/ |
||||
@Slf4j |
||||
public class SerialPortSingle { |
||||
|
||||
public SerialPort serialPort = null; |
||||
private String receiveStr = null; |
||||
private int baudrate = 9600; |
||||
private String parity = null; |
||||
|
||||
// 调用service
|
||||
ApplicationContext context = SpringBeanUtil.getApplicationContext(); |
||||
//DeviceCodeParamService deviceCodeParamService = context.getBean(DeviceCodeParamService.class);
|
||||
AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485(); |
||||
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); |
||||
NowDataService nowDataService = context.getBean(NowDataService.class); |
||||
BuildingService buildingService = context.getBean(BuildingService.class); |
||||
|
||||
public String serialPortSend(DeviceCodeParamEntity deviceCodeParamEntity) { |
||||
|
||||
//查看所有串口
|
||||
SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil(); |
||||
ArrayList<String> port = serialPortUtil.findPort(); |
||||
// SerialTool serialPortUtil = SerialTool.getSerialPortUtil();
|
||||
//package com.mh.user.serialport;
|
||||
//
|
||||
//import com.mh.user.constants.Constant;
|
||||
//import com.mh.user.entity.DeviceCodeParamEntity;
|
||||
//import com.mh.user.service.BuildingService;
|
||||
//import com.mh.user.service.DeviceCodeParamService;
|
||||
//import com.mh.user.service.DeviceInstallService;
|
||||
//import com.mh.user.service.NowDataService;
|
||||
//import com.mh.user.utils.AnalysisReceiveOrder485;
|
||||
//import com.mh.user.utils.SendOrderUtils;
|
||||
//import com.mh.user.utils.SpringBeanUtil;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.context.ApplicationContext;
|
||||
//import org.springframework.stereotype.Component;
|
||||
////import purejavacomm.SerialPort;
|
||||
//import gnu.io.SerialPort;
|
||||
//
|
||||
//import java.nio.ByteBuffer;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @author nxr
|
||||
// * @title :
|
||||
// * @description : 串口发送和接收处理,操作类
|
||||
// * @updateTime 2022-08-10
|
||||
// * @throws :
|
||||
// */
|
||||
//@Slf4j
|
||||
//public class SerialPortSingle {
|
||||
//
|
||||
// public SerialPort serialPort = null;
|
||||
// private String receiveStr = null;
|
||||
// private int baudrate = 9600;
|
||||
// private String parity = null;
|
||||
//
|
||||
// // 调用service
|
||||
// ApplicationContext context = SpringBeanUtil.getApplicationContext();
|
||||
// //DeviceCodeParamService deviceCodeParamService = context.getBean(DeviceCodeParamService.class);
|
||||
// AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
|
||||
// DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class);
|
||||
// NowDataService nowDataService = context.getBean(NowDataService.class);
|
||||
// BuildingService buildingService = context.getBean(BuildingService.class);
|
||||
//
|
||||
// public String serialPortSend(DeviceCodeParamEntity deviceCodeParamEntity) {
|
||||
//
|
||||
// //查看所有串口
|
||||
// SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil();
|
||||
// ArrayList<String> port = serialPortUtil.findPort();
|
||||
String rtData = ""; |
||||
// System.out.println("发现全部串口:" + port);
|
||||
String comName = deviceCodeParamEntity.getDataCom().toUpperCase(); |
||||
if (!port.contains(comName)) { |
||||
log.info("串口:" + comName + "不存在!"); |
||||
return "fail"; |
||||
} |
||||
try { |
||||
try { |
||||
baudrate = deviceCodeParamEntity.getBaudrate(); |
||||
parity = deviceCodeParamEntity.getParity(); |
||||
if (parity == null || parity.equals("") || parity.equalsIgnoreCase("none")) { |
||||
serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_NONE, SerialPort.PARITY_ODD); |
||||
} else { |
||||
serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.PARITY_ODD); |
||||
} |
||||
//向串口发送指令
|
||||
log.info("-----------------------------单抄向串口" + serialPort + "发送指令!-----------------------------"); |
||||
SendOrderUtils.sendSerialPort(deviceCodeParamEntity, serialPort); |
||||
Thread.sleep(1500); |
||||
} catch (Exception e) { |
||||
log.error("前端设置出现异常==>", e); |
||||
return "fail"; |
||||
} |
||||
//对返回数据进行相关解析处理
|
||||
receiveStr = null; |
||||
byte[] bytes = serialPortUtil.readFromPort(serialPort); //读取串口数据
|
||||
if (null == bytes) { |
||||
serialPortUtil.closePort(serialPort); |
||||
log.info("单抄串口" + serialPort + "异常,没有数据返回!关闭串口"); |
||||
return "fail"; |
||||
} |
||||
receiveStr = receiveStr + printHexString(bytes); |
||||
//去掉空格和null
|
||||
receiveStr = receiveStr.replace("null", ""); |
||||
receiveStr = receiveStr.replace(" ", ""); |
||||
log.info("串口" + serialPort + "接收数据:" + receiveStr + ",大小: " + receiveStr.length()); |
||||
//返回值全部变成大写
|
||||
String receiveData = receiveStr.toUpperCase(); |
||||
//截取去掉FE
|
||||
String dataStr = receiveData.replace("FE", ""); |
||||
String deviceType = deviceCodeParamEntity.getDeviceType(); |
||||
String deviceAddr = deviceCodeParamEntity.getDeviceAddr(); |
||||
String registerAddr = deviceCodeParamEntity.getRegisterAddr(); |
||||
String brand = deviceCodeParamEntity.getBrand(); |
||||
String buildingId = deviceCodeParamEntity.getBuildingId(); |
||||
String buildingName = buildingService.queryBuildingName(buildingId); //查询楼栋名称
|
||||
|
||||
deviceInstallService.updateOnline(deviceAddr, deviceType, buildingId, "在线"); //设备在线
|
||||
log.info(deviceType + "在线,设备号:" + deviceAddr + ",所属楼栋:" + buildingName); |
||||
if (deviceType.equals("热泵")) { |
||||
String strState = nowDataService.selectState(buildingId, deviceAddr); |
||||
if (strState != null && strState.equals("离线")) { //采集到数据
|
||||
nowDataService.updateRunState(buildingId, deviceAddr, "不运行", buildingName); //监控界面状态表热泵在线状态
|
||||
} |
||||
} |
||||
try { |
||||
if ((dataStr.length() == 18 || dataStr.length() == 70 || dataStr.length() == 44) && deviceType.equals("水表")) { |
||||
rtData = analysisReceiveOrder485.analysisWtMeterOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if ((dataStr.length() == 36 || dataStr.length() == 44 || dataStr.length() == 40 || dataStr.length() == 50) && deviceType.equals("电表")) { |
||||
rtData = analysisReceiveOrder485.analysisMeterOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("压变")) { |
||||
rtData = analysisReceiveOrder485.analysisPressureOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if ((dataStr.length() == 30) && deviceType.equals("状态检测")) {//五路状态读取,兼容旧版系统
|
||||
analysisReceiveOrder485.analysisStateOrder485(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("水位开关") && (registerAddr.equals("0018") || registerAddr.equals("0017"))) { |
||||
rtData = analysisReceiveOrder485.analysisRelayOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("热泵")) { |
||||
rtData = analysisReceiveOrder485.analysisPumpOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("时控")) { |
||||
rtData = analysisReceiveOrder485.analysisTimeSetOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("水位开关") && registerAddr.equals("0010")) { //热泵状态
|
||||
rtData = analysisReceiveOrder485.analysisPumpStateOrder2(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("温度变送器")) { |
||||
rtData = analysisReceiveOrder485.analysisMulTempOrder4852(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} else if (deviceType.equals("热泵状态")) { |
||||
rtData = analysisReceiveOrder485.analysisPumpStateOrder2(dataStr, registerAddr, brand, buildingId, buildingName); |
||||
} |
||||
} catch (Exception e) { |
||||
log.error(deviceCodeParamEntity.getDeviceType() + "单抄保存数据库失败!"); |
||||
return "fail"; |
||||
} |
||||
Thread.sleep(500); |
||||
log.info("-----------------------------" + serialPort + "单抄结束!-----------------------------"); |
||||
return rtData; |
||||
} catch (Exception e) { |
||||
log.error("前端设置出现异常==>", e); |
||||
return "fail"; |
||||
} finally { |
||||
if (null != serialPort) { |
||||
serialPortUtil.closePort(serialPort); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 字节数组转16进制字符串 |
||||
* |
||||
* @param b 字节数组 |
||||
* @return 16进制字符串 |
||||
*/ |
||||
public static String printHexString(byte[] b) { |
||||
StringBuilder sbf = new StringBuilder(); |
||||
for (byte value : b) { |
||||
String hex = Integer.toHexString(value & 0xFF); |
||||
if (hex.length() == 1) { |
||||
hex = '0' + hex; |
||||
} |
||||
sbf.append(hex.toUpperCase()).append(" "); |
||||
} |
||||
return sbf.toString().trim(); |
||||
} |
||||
|
||||
/** |
||||
* 十六进制字符串转byte[] |
||||
* |
||||
* @param hex 十六进制字符串 |
||||
* @return byte[] |
||||
*/ |
||||
public static byte[] hexStr2Byte(String hex) { |
||||
if (hex == null) { |
||||
return new byte[]{}; |
||||
} |
||||
|
||||
// 奇数位补0
|
||||
if (hex.length() % 2 != 0) { |
||||
hex = "0" + hex; |
||||
} |
||||
|
||||
int length = hex.length(); |
||||
ByteBuffer buffer = ByteBuffer.allocate(length / 2); |
||||
for (int i = 0; i < length; i++) { |
||||
String hexStr = hex.charAt(i) + ""; |
||||
i++; |
||||
hexStr += hex.charAt(i); |
||||
byte b = (byte) Integer.parseInt(hexStr, 16); |
||||
buffer.put(b); |
||||
} |
||||
return buffer.array(); |
||||
} |
||||
|
||||
/** |
||||
* 16进制转换成为string类型字符串 |
||||
* |
||||
* @param s 待转换字符串 |
||||
*/ |
||||
public static String hexStringToString(String s) { |
||||
if (s == null || "".equals(s)) { |
||||
return null; |
||||
} |
||||
s = s.replace(" ", ""); |
||||
byte[] baKeyword = new byte[s.length() / 2]; |
||||
for (int i = 0; i < baKeyword.length; i++) { |
||||
try { |
||||
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
try { |
||||
s = new String(baKeyword, StandardCharsets.UTF_8); |
||||
} catch (Exception e1) { |
||||
e1.printStackTrace(); |
||||
} |
||||
return s; |
||||
} |
||||
} |
||||
//// SerialTool serialPortUtil = SerialTool.getSerialPortUtil();
|
||||
//// ArrayList<String> port = serialPortUtil.findPort();
|
||||
// String rtData = "";
|
||||
//// System.out.println("发现全部串口:" + port);
|
||||
// String comName = deviceCodeParamEntity.getDataCom().toUpperCase();
|
||||
// if (!port.contains(comName)) {
|
||||
// log.info("串口:" + comName + "不存在!");
|
||||
// return "fail";
|
||||
// }
|
||||
// try {
|
||||
// try {
|
||||
// baudrate = deviceCodeParamEntity.getBaudrate();
|
||||
// parity = deviceCodeParamEntity.getParity();
|
||||
// if (parity == null || parity.equals("") || parity.equalsIgnoreCase("none")) {
|
||||
// serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_NONE, SerialPort.PARITY_ODD);
|
||||
// } else {
|
||||
// serialPort = serialPortUtil.openPort(comName, baudrate, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.PARITY_ODD);
|
||||
// }
|
||||
// //向串口发送指令
|
||||
// log.info("-----------------------------单抄向串口" + serialPort + "发送指令!-----------------------------");
|
||||
// SendOrderUtils.sendSerialPort(deviceCodeParamEntity, serialPort);
|
||||
// Thread.sleep(1500);
|
||||
// } catch (Exception e) {
|
||||
// log.error("前端设置出现异常==>", e);
|
||||
// return "fail";
|
||||
// }
|
||||
// //对返回数据进行相关解析处理
|
||||
// receiveStr = null;
|
||||
// byte[] bytes = serialPortUtil.readFromPort(serialPort); //读取串口数据
|
||||
// if (null == bytes) {
|
||||
// serialPortUtil.closePort(serialPort);
|
||||
// log.info("单抄串口" + serialPort + "异常,没有数据返回!关闭串口");
|
||||
// return "fail";
|
||||
// }
|
||||
// receiveStr = receiveStr + printHexString(bytes);
|
||||
// //去掉空格和null
|
||||
// receiveStr = receiveStr.replace("null", "");
|
||||
// receiveStr = receiveStr.replace(" ", "");
|
||||
// log.info("串口" + serialPort + "接收数据:" + receiveStr + ",大小: " + receiveStr.length());
|
||||
// //返回值全部变成大写
|
||||
// String receiveData = receiveStr.toUpperCase();
|
||||
// //截取去掉FE
|
||||
// String dataStr = receiveData.replace("FE", "");
|
||||
// String deviceType = deviceCodeParamEntity.getDeviceType();
|
||||
// String deviceAddr = deviceCodeParamEntity.getDeviceAddr();
|
||||
// String registerAddr = deviceCodeParamEntity.getRegisterAddr();
|
||||
// String brand = deviceCodeParamEntity.getBrand();
|
||||
// String buildingId = deviceCodeParamEntity.getBuildingId();
|
||||
// String buildingName = buildingService.queryBuildingName(buildingId); //查询楼栋名称
|
||||
//
|
||||
// deviceInstallService.updateOnline(deviceAddr, deviceType, buildingId, "在线"); //设备在线
|
||||
// log.info(deviceType + "在线,设备号:" + deviceAddr + ",所属楼栋:" + buildingName);
|
||||
// if (deviceType.equals("热泵")) {
|
||||
// String strState = nowDataService.selectState(buildingId, deviceAddr);
|
||||
// if (strState != null && strState.equals("离线")) { //采集到数据
|
||||
// nowDataService.updateRunState(buildingId, deviceAddr, "不运行", buildingName); //监控界面状态表热泵在线状态
|
||||
// }
|
||||
// }
|
||||
// try {
|
||||
// if ((dataStr.length() == 18 || dataStr.length() == 70 || dataStr.length() == 44) && deviceType.equals("水表")) {
|
||||
// rtData = analysisReceiveOrder485.analysisWtMeterOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if ((dataStr.length() == 36 || dataStr.length() == 44 || dataStr.length() == 40 || dataStr.length() == 50) && deviceType.equals("电表")) {
|
||||
// rtData = analysisReceiveOrder485.analysisMeterOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("压变")) {
|
||||
// rtData = analysisReceiveOrder485.analysisPressureOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if ((dataStr.length() == 30) && deviceType.equals("状态检测")) {//五路状态读取,兼容旧版系统
|
||||
// analysisReceiveOrder485.analysisStateOrder485(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("水位开关") && (registerAddr.equals("0018") || registerAddr.equals("0017"))) {
|
||||
// rtData = analysisReceiveOrder485.analysisRelayOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("热泵")) {
|
||||
// rtData = analysisReceiveOrder485.analysisPumpOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("时控")) {
|
||||
// rtData = analysisReceiveOrder485.analysisTimeSetOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("水位开关") && registerAddr.equals("0010")) { //热泵状态
|
||||
// rtData = analysisReceiveOrder485.analysisPumpStateOrder2(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("温度变送器")) {
|
||||
// rtData = analysisReceiveOrder485.analysisMulTempOrder4852(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// } else if (deviceType.equals("热泵状态")) {
|
||||
// rtData = analysisReceiveOrder485.analysisPumpStateOrder2(dataStr, registerAddr, brand, buildingId, buildingName);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.error(deviceCodeParamEntity.getDeviceType() + "单抄保存数据库失败!");
|
||||
// return "fail";
|
||||
// }
|
||||
// Thread.sleep(500);
|
||||
// log.info("-----------------------------" + serialPort + "单抄结束!-----------------------------");
|
||||
// return rtData;
|
||||
// } catch (Exception e) {
|
||||
// log.error("前端设置出现异常==>", e);
|
||||
// return "fail";
|
||||
// } finally {
|
||||
// if (null != serialPort) {
|
||||
// serialPortUtil.closePort(serialPort);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 字节数组转16进制字符串
|
||||
// *
|
||||
// * @param b 字节数组
|
||||
// * @return 16进制字符串
|
||||
// */
|
||||
// public static String printHexString(byte[] b) {
|
||||
// StringBuilder sbf = new StringBuilder();
|
||||
// for (byte value : b) {
|
||||
// String hex = Integer.toHexString(value & 0xFF);
|
||||
// if (hex.length() == 1) {
|
||||
// hex = '0' + hex;
|
||||
// }
|
||||
// sbf.append(hex.toUpperCase()).append(" ");
|
||||
// }
|
||||
// return sbf.toString().trim();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 十六进制字符串转byte[]
|
||||
// *
|
||||
// * @param hex 十六进制字符串
|
||||
// * @return byte[]
|
||||
// */
|
||||
// public static byte[] hexStr2Byte(String hex) {
|
||||
// if (hex == null) {
|
||||
// return new byte[]{};
|
||||
// }
|
||||
//
|
||||
// // 奇数位补0
|
||||
// if (hex.length() % 2 != 0) {
|
||||
// hex = "0" + hex;
|
||||
// }
|
||||
//
|
||||
// int length = hex.length();
|
||||
// ByteBuffer buffer = ByteBuffer.allocate(length / 2);
|
||||
// for (int i = 0; i < length; i++) {
|
||||
// String hexStr = hex.charAt(i) + "";
|
||||
// i++;
|
||||
// hexStr += hex.charAt(i);
|
||||
// byte b = (byte) Integer.parseInt(hexStr, 16);
|
||||
// buffer.put(b);
|
||||
// }
|
||||
// return buffer.array();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 16进制转换成为string类型字符串
|
||||
// *
|
||||
// * @param s 待转换字符串
|
||||
// */
|
||||
// public static String hexStringToString(String s) {
|
||||
// if (s == null || "".equals(s)) {
|
||||
// return null;
|
||||
// }
|
||||
// s = s.replace(" ", "");
|
||||
// byte[] baKeyword = new byte[s.length() / 2];
|
||||
// for (int i = 0; i < baKeyword.length; i++) {
|
||||
// try {
|
||||
// baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// try {
|
||||
// s = new String(baKeyword, StandardCharsets.UTF_8);
|
||||
// } catch (Exception e1) {
|
||||
// e1.printStackTrace();
|
||||
// }
|
||||
// return s;
|
||||
// }
|
||||
//}
|
||||
|
Loading…
Reference in new issue