Browse Source

优化采集控制逻辑

prod_202403
mh 10 months ago
parent
commit
faa17b1a93
  1. 85
      user-service/src/main/java/com/mh/user/controller/TestController.java
  2. 57
      user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java
  3. 6
      user-service/src/main/java/com/mh/user/job/DealDataJob.java
  4. 12
      user-service/src/main/java/com/mh/user/mapper/NowDataMapper.java
  5. 2
      user-service/src/main/java/com/mh/user/mapper/NowPublicDataMapper.java
  6. 8
      user-service/src/main/java/com/mh/user/serialport/SerialPortSendReceive2.java
  7. 6
      user-service/src/main/java/com/mh/user/serialport/SerialPortSingle2.java
  8. 3
      user-service/src/main/java/com/mh/user/serialport/SerialTool.java
  9. 7
      user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java
  10. 4
      user-service/src/main/java/com/mh/user/utils/ComThreadPoolService.java

85
user-service/src/main/java/com/mh/user/controller/TestController.java

@ -1,17 +1,16 @@
package com.mh.user.controller;
import com.mh.common.utils.DateTimeUtils;
import com.mh.user.constants.Constant;
import com.mh.user.dynamic.datasource.DataSourceContextHolder;
import com.mh.user.dynamic.datasource.DataSourceObject;
import com.mh.user.entity.DBEntity;
import com.mh.user.manage.QuartzManager;
import com.mh.user.mapper.NowDataMapper;
import com.mh.user.serialport.SerialTool;
import com.mh.user.service.NowDataService;
import com.mh.user.service.SysUserService;
import com.mh.user.utils.AESUtil;
import com.mh.user.utils.AnalysisReceiveOrder485;
import com.mh.user.utils.GetReadOrder485;
import com.mh.user.utils.TimedTask2;
import com.mh.user.utils.*;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
@ -19,6 +18,9 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import purejavacomm.CommPort;
import purejavacomm.CommPortIdentifier;
import purejavacomm.PortInUseException;
import javax.annotation.Resource;
@ -88,4 +90,79 @@ public class TestController {
}
@GetMapping
public String testCollection(String type) throws Exception {
purejavacomm.SerialPort serialPort1 = null;
System.out.println("开始时间==>" + DateTimeUtils.getDateTime());
String returnStr = "未采集到数据";
Constant.WEB_FLAG = true;
try {
if ("0".equals(type)) {
// 采集水表
// 采集水表
try {
serialPort1 = SerialTool.openPort("COM12",
2400,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_NONE);
} catch (PortInUseException e) {
System.out.println("进入异常==>");
if (null != serialPort1) {
serialPort1.close();
}
serialPort1 = SerialTool.openPort("COM12",
2400,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_NONE);
}
//从串口读取数据 SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort, sendStr, deviceType);
String sendStr = "FE FE FE 68 10 39 87 10 81 00 00 00 01 03 90 1F 00 7C 16".replace(" ","");
SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort1, sendStr, "水表");
Thread.sleep(888);
byte[] bytes = SerialTool.readFromPort(serialPort1);
assert bytes != null;
returnStr = ExchangeStringUtil.parseByte2HexStr(bytes);
System.out.println("控制读取回来的水表报文==>" + returnStr);
serialPort1.close();
}
if ("1".equals(type)) {
// 采集电表
// 采集电表
try {
serialPort1 = SerialTool.openPort("COM12",
1200,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_EVEN);
} catch (Exception e) {
System.out.println("进入异常==>");
if (null != serialPort1) {
serialPort1.close();
}
serialPort1 = SerialTool.openPort("COM12",
1200,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_EVEN);
}
//从串口读取数据 SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort, sendStr, deviceType);
String sendStr = "FE FE FE 68 69 04 00 70 01 08 68 01 02 43 C3 BF 16".replace(" ","");
SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort1, sendStr, "电表");
Thread.sleep(888);
byte[] bytes = SerialTool.readFromPort(serialPort1);
assert bytes != null;
returnStr = ExchangeStringUtil.parseByte2HexStr(bytes);
System.out.println("控制读取回来的电表报文==>" + returnStr);
serialPort1.close();
}
} finally {
Constant.WEB_FLAG = false;
}
System.out.println("结束时间==>" + DateTimeUtils.getDateTime());
return returnStr;
}
}

57
user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java

@ -5,13 +5,13 @@ import com.mh.user.entity.AddCronJobReq;
import com.mh.user.manage.QuartzManager;
import com.mh.user.serialport.SerialPortListener;
import com.mh.user.serialport.SerialPortUtil;
import com.mh.user.serialport.SerialTool;
import com.mh.user.service.DeviceCodeParamService;
import com.mh.user.utils.CacheUtil;
import com.mh.user.utils.ExchangeStringUtil;
import com.mh.user.utils.GetReadOrder485;
import com.mh.user.utils.TimedTask2;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@ -44,6 +44,59 @@ public class CollectionLoopRunner implements ApplicationRunner {
// Constant.WEB_FLAG=false; //恢复采集
// 初始化指令设备参数
initialDeviceCodeParams();
// 模拟采集
//simulationCollection();
}
private void simulationCollection() throws Exception {
purejavacomm.SerialPort serialPort1 = null;
do {
if (!Constant.WEB_FLAG) {
try {
// 采集水表
serialPort1 = SerialTool.openPort("COM12",
2400,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_NONE);
//从串口读取数据 SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort, sendStr, deviceType);
// 81108739
String sendStr = "FE FE FE 68 10 39 87 10 81 00 00 00 01 03 90 1F 00 7C 16".replace(" ", "");
SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort1, sendStr, "水表");
Thread.sleep(888);
byte[] bytes = SerialTool.readFromPort(serialPort1);
assert bytes != null;
String returnStr = ExchangeStringUtil.parseByte2HexStr(bytes);
System.out.println("响应报文==>" + returnStr);
serialPort1.close();
if (!Constant.WEB_FLAG) {
continue;
}
// 采集电表
serialPort1 = SerialTool.openPort("COM12",
1200,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_EVEN);
//从串口读取数据 SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort, sendStr, deviceType);
// 080170000469
sendStr = "FE FE FE 68 69 04 00 70 01 08 68 01 02 43 C3 BF 16".replace(" ", "");
SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort1, sendStr, "电表");
Thread.sleep(888);
bytes = SerialTool.readFromPort(serialPort1);
assert bytes != null;
returnStr = ExchangeStringUtil.parseByte2HexStr(bytes);
System.out.println("响应报文==>" + returnStr);
serialPort1.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != serialPort1) {
serialPort1.close();
}
}
}
} while (true);
}
private void initialDeviceCodeParams() {

6
user-service/src/main/java/com/mh/user/job/DealDataJob.java

@ -78,6 +78,9 @@ public class DealDataJob {
Constant.FLAG = true;
log.info("------Constant.WEB_FLAG==" + false + "------");
for (int i = 1; i <= 4; i++) {
if (!Constant.WEB_FLAG) {
break;
}
String threadName;
if (i == 1 || i == 3) {
threadName = "1";
@ -91,6 +94,9 @@ public class DealDataJob {
}
CountDownLatch countDownLatch = new CountDownLatch(10);
for (int j = 1; j < 11; j++) {
if (!Constant.WEB_FLAG) {
break;
}
SerialPortThread myThread = new SerialPortThread();
myThread.setName(threadName, String.valueOf(j), countDownLatch);
Thread thread = new Thread(myThread);

12
user-service/src/main/java/com/mh/user/mapper/NowDataMapper.java

@ -237,8 +237,13 @@ public interface NowDataMapper {
//初始化结束-----------------------------------------------------------------------------------------
//求热泵平均温度
@Select("select Convert(decimal(18,1),avg(CAST(water_temp as FLOAT))) from now_data where building_id=#{buildingId}")
String selectAve(@Param("buildingId") String buildingId);
@Select("<script>" +
"select Convert(decimal(18,1),avg(CAST(water_temp as FLOAT))) from now_data where building_id=#{buildingId} " +
"<if test='temp != null'>" +
" and water_temp >= #{temp} " +
"</if>" +
"</script>")
String selectAve(@Param("buildingId") String buildingId, @Param("temp") Integer temp);
//求单个热泵温度
@Select("select water_temp from now_data where pump_id=#{pumpId} and building_id=#{buildingId}")
@ -415,4 +420,7 @@ public interface NowDataMapper {
@Param("seat") String seat,
@Param("buildingId") String buildingId,
@Param("curDate") String curDate);
@Select("select count(1) from now_data where pump_name like '%热泵%' ")
int selectPumpCount(@Param("buildingId") String buildingId);
}

2
user-service/src/main/java/com/mh/user/mapper/NowPublicDataMapper.java

@ -87,7 +87,7 @@ public interface NowPublicDataMapper {
@Result(property ="singleTemp",column ="single_temp"),
@Result(property ="avgTemp",column ="use_water_temp")
})
@Select("select building_id,building_name,use_water_temp,single_temp from now_public_data")
@Select("select building_id,building_name,use_water_temp+3,single_temp+3 from now_public_data order by building_id ")
List<TempModel> queryWtTemp();
//查询单个楼栋水箱平均温度

8
user-service/src/main/java/com/mh/user/serialport/SerialPortSendReceive2.java

@ -70,7 +70,7 @@ public class SerialPortSendReceive2 {
String comName = deviceManageEntityList.get(i).getDataCom();
if (StringUtils.isBlank(comName)) {
Constant.WEB_FLAG = false;//可以采集的状态
Constant.FLAG = false;//可以采集的状态
log.info("-------------串口:" + comName + "不存在!-------------");
continue;
}
@ -101,7 +101,7 @@ public class SerialPortSendReceive2 {
continue;
}
} catch (Exception e) {
Constant.WEB_FLAG = false;//可以采集的状态
Constant.FLAG = false;//可以采集的状态
log.error("发送窗口数据异常==>", e);
}
@ -111,7 +111,7 @@ public class SerialPortSendReceive2 {
SerialTool.closePort(serialPort);
log.info("串口" + serialPort + "没有数据返回!" + i);
log.info("----------------" + deviceType + "离线,设备号:" + deviceAddr + ",所属楼栋:" + buildingName + "----------------");
Constant.WEB_FLAG = false;//可以采集的状态
Constant.FLAG = false;//可以采集的状态
String time1 = deviceInstallService.selectLastDate(deviceType, deviceAddr, buildingId);
Date date1 = new Date();
String time2 = DateUtil.dateToString(date1, "yyyy-MM-dd HH:mm:ss");;
@ -132,7 +132,7 @@ public class SerialPortSendReceive2 {
dealReceiveData(dateStr, serialPort, i, deviceAddr, deviceType, registerAddr, brand, buildingId, buildingName, bytes);
}
} catch (Exception e) {
Constant.WEB_FLAG = false;//可以采集的状态
Constant.FLAG = false;//可以采集的状态
log.error("-------------串口采集异常!----------->>", e);
}
}

6
user-service/src/main/java/com/mh/user/serialport/SerialPortSingle2.java

@ -51,7 +51,11 @@ public class SerialPortSingle2 {
if (serialPort != null) {
String sendStr = SendOrderUtils.getSendStr(deviceCodeParamEntity);
SerialTool.sendToPort(SerialTool.HexString2Bytes(sendStr), serialPort, sendStr, deviceCodeParamEntity.getDeviceType());
Thread.sleep(2000);
if (deviceCodeParamEntity.getDeviceType().equals("热泵")) {
Thread.sleep(4000);
} else {
Thread.sleep(2000);
}
}
} catch (Exception e) {
log.info("单抄串口" + serialPort + "异常,没有数据返回!关闭串口", e);

3
user-service/src/main/java/com/mh/user/serialport/SerialTool.java

@ -84,8 +84,6 @@ public class SerialTool {
throw e;
}
return serialPort;
} else {
throw new Exception();
}
} catch (NoSuchPortException e) {
@ -95,6 +93,7 @@ public class SerialTool {
log.error("串口使用中:",e);
throw e;
}
return null;
}

7
user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java

@ -363,7 +363,12 @@ public class NowDataServiceImpl implements NowDataService {
//从监视表查询热泵温度平均值
@Override
public String selectAve(String buildingId) {
return nowDataMapper.selectAve(buildingId);
// int pumpCount = nowDataMapper.selectPumpCount(buildingId);
// Integer temp = null;
// if (pumpCount > 1) {
// temp = 40;
// }
return nowDataMapper.selectAve(buildingId, null);
}
@Override

4
user-service/src/main/java/com/mh/user/utils/ComThreadPoolService.java

@ -17,13 +17,13 @@ public class ComThreadPoolService {
public static final int CORE_POOL_SIZE = 11;
/** 线程池最大线程数 */
public static final int MAX_POOL_SIZE = 50;
public static final int MAX_POOL_SIZE = 20;
/** 空闲线程回收时间 */
public static final int KEEP_ALIVE_TIME = 30000;
/** 线程池等待队列 */
public static final int BLOCKING_QUEUE_SIZE = 500;
public static final int BLOCKING_QUEUE_SIZE = 100;
// 私有化构造器
private ComThreadPoolService(){}

Loading…
Cancel
Save