作者 钟来

模块整理

@@ -97,6 +97,7 @@ @@ -97,6 +97,7 @@
97 <artifactId>sqlite-jdbc</artifactId> 97 <artifactId>sqlite-jdbc</artifactId>
98 <version>3.21.0.1</version> 98 <version>3.21.0.1</version>
99 </dependency> 99 </dependency>
  100 +
100 <!-- <dependency>--> 101 <!-- <dependency>-->
101 <!-- <groupId>mysql</groupId>--> 102 <!-- <groupId>mysql</groupId>-->
102 <!-- <artifactId>mysql-connector-java</artifactId>--> 103 <!-- <artifactId>mysql-connector-java</artifactId>-->
@@ -2,14 +2,15 @@ package com.zhonglai.luhui.smart.feeder; @@ -2,14 +2,15 @@ package com.zhonglai.luhui.smart.feeder;
2 2
3 import com.ruoyi.framework.config.ResourcesConfig; 3 import com.ruoyi.framework.config.ResourcesConfig;
4 import com.zhonglai.luhui.smart.feeder.config.OpenCVConfig; 4 import com.zhonglai.luhui.smart.feeder.config.OpenCVConfig;
5 -import com.zhonglai.luhui.smart.feeder.controller.CameraController;  
6 -import com.zhonglai.luhui.smart.feeder.controller.ConfigController; 5 +import com.zhonglai.luhui.smart.feeder.config.v2apibug.ResponseFilter;
7 import org.springframework.boot.SpringApplication; 6 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.boot.autoconfigure.SpringBootApplication;
9 8
10 import org.slf4j.Logger; 9 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 10 import org.slf4j.LoggerFactory;
12 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 11 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  12 +import org.springframework.boot.web.servlet.FilterRegistrationBean;
  13 +import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.ComponentScan; 14 import org.springframework.context.annotation.ComponentScan;
14 import org.springframework.context.annotation.FilterType; 15 import org.springframework.context.annotation.FilterType;
15 import org.springframework.scheduling.annotation.EnableScheduling; 16 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -32,4 +33,5 @@ public class Main { @@ -32,4 +33,5 @@ public class Main {
32 OpenCVConfig.loadOpenCv(args); 33 OpenCVConfig.loadOpenCv(args);
33 SpringApplication.run(Main.class,args); 34 SpringApplication.run(Main.class,args);
34 } 35 }
  36 +
35 } 37 }
1 package com.zhonglai.luhui.smart.feeder.config; 1 package com.zhonglai.luhui.smart.feeder.config;
2 2
3 import com.ruoyi.common.config.RuoYiConfig; 3 import com.ruoyi.common.config.RuoYiConfig;
  4 +import com.zhonglai.luhui.smart.feeder.config.v2apibug.ResponseFilter;
4 import io.swagger.annotations.ApiOperation; 5 import io.swagger.annotations.ApiOperation;
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.boot.web.servlet.FilterRegistrationBean;
6 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.Configuration;
8 import springfox.documentation.builders.ApiInfoBuilder; 10 import springfox.documentation.builders.ApiInfoBuilder;
@@ -48,5 +50,17 @@ public class SwaggerConfig { @@ -48,5 +50,17 @@ public class SwaggerConfig {
48 .version("版本号:" + ruoyiConfig.getVersion()) 50 .version("版本号:" + ruoyiConfig.getVersion())
49 .build(); 51 .build();
50 } 52 }
51 - 53 +
  54 + /**
  55 + * 解决/v2/api-docs返回多了一层value的问题
  56 + * @return
  57 + */
  58 + @Bean
  59 + public FilterRegistrationBean someFilterRegistration() {
  60 + FilterRegistrationBean registration = new FilterRegistrationBean();
  61 + registration.setFilter(new ResponseFilter());
  62 + // 过滤的地址
  63 + registration.addUrlPatterns("/v2/api-docs");
  64 + return registration;
  65 + }
52 } 66 }
  1 +package com.zhonglai.luhui.smart.feeder.config.v2apibug;
  2 +
  3 +import com.google.gson.Gson;
  4 +
  5 +import javax.servlet.*;
  6 +import javax.servlet.http.HttpServletResponse;
  7 +import java.io.IOException;
  8 +import java.util.HashMap;
  9 +import java.util.Map;
  10 +
  11 +public class ResponseFilter implements Filter {
  12 +
  13 + @Override
  14 + public void init(FilterConfig filterConfig) throws ServletException {
  15 + Filter.super.init(filterConfig);
  16 + }
  17 +
  18 + @Override
  19 + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  20 + // 这里需要重写ResponseWrapper,因为原方法是没有获取返回值的功能
  21 + ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse) response);
  22 + // 这里只拦截返回,直接让请求过去,如果在请求前有处理,可以在这里处理
  23 + chain.doFilter(request, wrapperResponse);
  24 + byte[] content = wrapperResponse.getContent();//获取返回值
  25 + // 判断是否有值
  26 + if (content.length > 0) {
  27 + // 这里是返回的内容
  28 + String str = new String(content, "UTF-8");
  29 + System.out.println("拦截的返回值:" + str);
  30 + try {
  31 + if(str.startsWith("{\"value\""))
  32 + {
  33 + Gson gson = new Gson();
  34 + Map<String,Object> map = gson.fromJson(str, HashMap.class);
  35 + response.getWriter().println(map.get("value"));
  36 + }else{
  37 + response.getWriter().write(str);
  38 + }
  39 + } catch (Exception e) {
  40 + e.printStackTrace();
  41 + }finally {
  42 + response.getWriter().flush();
  43 + }
  44 +
  45 + }
  46 + }
  47 +
  48 + @Override
  49 + public void destroy() {
  50 + Filter.super.destroy();
  51 + }
  52 +}
  1 +package com.zhonglai.luhui.smart.feeder.config.v2apibug;
  2 +
  3 +import javax.servlet.ServletOutputStream;
  4 +import javax.servlet.WriteListener;
  5 +import javax.servlet.http.HttpServletResponse;
  6 +import javax.servlet.http.HttpServletResponseWrapper;
  7 +import java.io.ByteArrayOutputStream;
  8 +import java.io.IOException;
  9 +
  10 +public class ResponseWrapper extends HttpServletResponseWrapper {
  11 +
  12 + private ByteArrayOutputStream buffer;
  13 +
  14 + private ServletOutputStream out;
  15 +
  16 + public ResponseWrapper(HttpServletResponse httpServletResponse) {
  17 + super(httpServletResponse);
  18 + buffer = new ByteArrayOutputStream();
  19 + out = new WrapperOutputStream(buffer);
  20 + }
  21 +
  22 + @Override
  23 + public ServletOutputStream getOutputStream()
  24 + throws IOException {
  25 + return out;
  26 + }
  27 +
  28 + @Override
  29 + public void flushBuffer()
  30 + throws IOException {
  31 + if (out != null) {
  32 + out.flush();
  33 + }
  34 + }
  35 +
  36 + public byte[] getContent()
  37 + throws IOException {
  38 + flushBuffer();
  39 + return buffer.toByteArray();
  40 + }
  41 +
  42 + class WrapperOutputStream extends ServletOutputStream {
  43 + private ByteArrayOutputStream bos;
  44 +
  45 + public WrapperOutputStream(ByteArrayOutputStream bos) {
  46 + this.bos = bos;
  47 + }
  48 +
  49 + @Override
  50 + public void write(int b)
  51 + throws IOException {
  52 + bos.write(b);
  53 + }
  54 +
  55 + @Override
  56 + public boolean isReady() {
  57 +
  58 + // TODO Auto-generated method stub
  59 + return false;
  60 +
  61 + }
  62 +
  63 + @Override
  64 + public void setWriteListener(WriteListener arg0) {
  65 +
  66 + // TODO Auto-generated method stub
  67 +
  68 + }
  69 + }
  70 +}
1 package com.zhonglai.luhui.smart.feeder.dto; 1 package com.zhonglai.luhui.smart.feeder.dto;
2 2
  3 +import cn.hutool.core.util.ArrayUtil;
3 import com.fasterxml.jackson.core.JsonProcessingException; 4 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper; 5 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.ruoyi.common.utils.ByteUtil; 6 import com.ruoyi.common.utils.ByteUtil;
6 -import com.ruoyi.common.utils.GsonConstructor;  
7 import lombok.Data; 7 import lombok.Data;
8 import org.apache.commons.lang3.ArrayUtils; 8 import org.apache.commons.lang3.ArrayUtils;
9 9
10 import java.io.Serializable; 10 import java.io.Serializable;
11 -import java.util.HashMap;  
12 -import java.util.Map; 11 +import java.util.*;
13 12
14 /** 13 /**
15 * Modbus协议 14 * Modbus协议
@@ -17,11 +16,26 @@ import java.util.Map; @@ -17,11 +16,26 @@ import java.util.Map;
17 @Data 16 @Data
18 public class ModbusDto implements Serializable { 17 public class ModbusDto implements Serializable {
19 private static final long serialVersionUID = -6008279428004571734L; 18 private static final long serialVersionUID = -6008279428004571734L;
20 - protected String hstr; 19 + private String hstr;
21 protected Integer address; //地址位 20 protected Integer address; //地址位
22 protected Integer commdcode; //功能码 21 protected Integer commdcode; //功能码
23 protected byte[] data; //数据 22 protected byte[] data; //数据
24 protected String crc; //16CRC 码 23 protected String crc; //16CRC 码
  24 +
  25 + public ModbusDto(Integer address,Integer commdcode,byte[] data)
  26 + {
  27 + this.address = address;
  28 + this.commdcode = commdcode;
  29 + this.data = data;
  30 +
  31 + byte[] heardbyte = new byte[2];
  32 + heardbyte[0] = address.byteValue();
  33 + heardbyte[1] = commdcode.byteValue();
  34 + byte[] notlrcdata = ArrayUtil.addAll(heardbyte,data);
  35 + this.crc = generateLRC(notlrcdata);
  36 + this.hstr = ByteUtil.toHexString(notlrcdata)+this.crc;
  37 + }
  38 +
25 public ModbusDto() 39 public ModbusDto()
26 { 40 {
27 41
@@ -48,6 +62,12 @@ public class ModbusDto implements Serializable { @@ -48,6 +62,12 @@ public class ModbusDto implements Serializable {
48 new ModbusDto(ByteUtil.hexStringToByte(str)); 62 new ModbusDto(ByteUtil.hexStringToByte(str));
49 } 63 }
50 64
  65 +
  66 + public byte[] generateCommd()
  67 + {
  68 + return ByteUtil.hexStringToByte(hstr);
  69 + }
  70 +
51 // 计算CRC校验码 71 // 计算CRC校验码
52 public static String generateLRC(byte[] data) 72 public static String generateLRC(byte[] data)
53 { 73 {
@@ -63,6 +83,8 @@ public class ModbusDto implements Serializable { @@ -63,6 +83,8 @@ public class ModbusDto implements Serializable {
63 tmp += 1; 83 tmp += 1;
64 return (byte) tmp; 84 return (byte) tmp;
65 } 85 }
  86 +
  87 +
66 public static void main(String[] args) { 88 public static void main(String[] args) {
67 String hexData = "01 03 8E 00 01 00 04 FF E0 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 07 00 03 00 00 00 1D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00".replace(" ",""); 89 String hexData = "01 03 8E 00 01 00 04 FF E0 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 07 00 03 00 00 00 1D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00".replace(" ","");
68 String crc = ByteUtil.getCRC16(ByteUtil.hexStringToByte(hexData)); 90 String crc = ByteUtil.getCRC16(ByteUtil.hexStringToByte(hexData));
@@ -3,7 +3,6 @@ package com.zhonglai.luhui.smart.feeder.dto.commd; @@ -3,7 +3,6 @@ package com.zhonglai.luhui.smart.feeder.dto.commd;
3 import com.ruoyi.common.utils.ByteUtil; 3 import com.ruoyi.common.utils.ByteUtil;
4 import org.apache.commons.lang3.ArrayUtils; 4 import org.apache.commons.lang3.ArrayUtils;
5 5
6 -import java.io.Serializable;  
7 import java.util.HashMap; 6 import java.util.HashMap;
8 import java.util.Map; 7 import java.util.Map;
9 8
  1 +package com.zhonglai.luhui.smart.feeder.dto.commd;
  2 +
  3 +
  4 +public class FeederCommd03Response implements FeederCommd{
  5 + private static final long serialVersionUID = -5498638326172560045L;
  6 + private Integer start_char;
  7 + private Integer char_lenth;
  8 +
  9 + public FeederCommd03Response(Integer start_char,Integer char_lenth)
  10 + {
  11 + this.start_char = start_char;
  12 + this.char_lenth = char_lenth;
  13 + }
  14 +
  15 + public Integer getStart_char() {
  16 + return start_char;
  17 + }
  18 +
  19 + public void setStart_char(Integer start_char) {
  20 + this.start_char = start_char;
  21 + }
  22 +
  23 + public Integer getChar_lenth() {
  24 + return char_lenth;
  25 + }
  26 +
  27 + public void setChar_lenth(Integer char_lenth) {
  28 + this.char_lenth = char_lenth;
  29 + }
  30 +}
@@ -3,14 +3,12 @@ package com.zhonglai.luhui.smart.feeder.dto.commd; @@ -3,14 +3,12 @@ package com.zhonglai.luhui.smart.feeder.dto.commd;
3 import com.zhonglai.luhui.smart.feeder.dto.ModbusDto; 3 import com.zhonglai.luhui.smart.feeder.dto.ModbusDto;
4 import lombok.Data; 4 import lombok.Data;
5 5
6 -import java.io.Serializable;  
7 import java.util.HashMap; 6 import java.util.HashMap;
8 import java.util.Map; 7 import java.util.Map;
9 8
10 /** 9 /**
11 * 投料机协议Modbus 10 * 投料机协议Modbus
12 */ 11 */
13 -@Data  
14 public class FeederCommdDto extends ModbusDto { 12 public class FeederCommdDto extends ModbusDto {
15 13
16 private static final long serialVersionUID = -2783135648395348130L; 14 private static final long serialVersionUID = -2783135648395348130L;
@@ -19,19 +17,33 @@ public class FeederCommdDto extends ModbusDto { @@ -19,19 +17,33 @@ public class FeederCommdDto extends ModbusDto {
19 17
20 private FeederCommd feederCommd; 18 private FeederCommd feederCommd;
21 19
22 - public FeederCommdDto(byte[] bytes) {  
23 - super(bytes);  
24 -  
25 - switch (commdcode) 20 + public static FeederCommdDto initRead(byte[] requesBytes)
  21 + {
  22 + FeederCommdDto feederCommdDto = new FeederCommdDto(requesBytes);
  23 + switch (feederCommdDto.getCommdcode())
26 { 24 {
27 case 0x03: 25 case 0x03:
28 - feederCommd = new FeederCommd03Request(this.data); 26 + feederCommdDto.setFeederCommd(new FeederCommd03Request(feederCommdDto.getData()));
29 break; 27 break;
30 case 0x06: 28 case 0x06:
31 break; 29 break;
32 case 0x10: 30 case 0x10:
33 break; 31 break;
34 } 32 }
  33 + return feederCommdDto;
  34 + }
  35 +
  36 + public FeederCommdDto initWrite(FeederCommd03Response feederCommdResponse)
  37 + {
  38 + this.feederCommd = feederCommdResponse;
  39 + feederCommdResponse.getStart_char();
  40 + feederCommdResponse.getChar_lenth();
  41 +
  42 + return new FeederCommdDto(requesBytes);
  43 + }
  44 +
  45 + public FeederCommdDto(byte[] requesBytes) {
  46 + super(requesBytes);
35 } 47 }
36 48
37 public FeederCommdDto() { 49 public FeederCommdDto() {
@@ -41,4 +53,28 @@ public class FeederCommdDto extends ModbusDto { @@ -41,4 +53,28 @@ public class FeederCommdDto extends ModbusDto {
41 public FeederCommdDto(String str) { 53 public FeederCommdDto(String str) {
42 super(str); 54 super(str);
43 } 55 }
  56 +
  57 + public Integer getQuantity() {
  58 + return quantity;
  59 + }
  60 +
  61 + public void setQuantity(Integer quantity) {
  62 + this.quantity = quantity;
  63 + }
  64 +
  65 + public Map<Integer, byte[]> getValue() {
  66 + return value;
  67 + }
  68 +
  69 + public void setValue(Map<Integer, byte[]> value) {
  70 + this.value = value;
  71 + }
  72 +
  73 + public FeederCommd getFeederCommd() {
  74 + return feederCommd;
  75 + }
  76 +
  77 + public void setFeederCommd(FeederCommd feederCommd) {
  78 + this.feederCommd = feederCommd;
  79 + }
44 } 80 }
1 package com.zhonglai.luhui.smart.feeder.service; 1 package com.zhonglai.luhui.smart.feeder.service;
2 2
  3 +import com.zhonglai.luhui.smart.feeder.Main;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
3 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
5 8
6 import javax.annotation.PostConstruct; 9 import javax.annotation.PostConstruct;
  10 +import java.io.IOException;
7 import java.util.concurrent.ScheduledExecutorService; 11 import java.util.concurrent.ScheduledExecutorService;
8 import java.util.concurrent.TimeUnit; 12 import java.util.concurrent.TimeUnit;
9 13
@@ -12,16 +16,28 @@ import java.util.concurrent.TimeUnit; @@ -12,16 +16,28 @@ import java.util.concurrent.TimeUnit;
12 */ 16 */
13 @Service 17 @Service
14 public class DateListenService { 18 public class DateListenService {
  19 + private static final Logger logger = LoggerFactory.getLogger(DateListenService.class);
15 @Autowired 20 @Autowired
16 private ScheduledExecutorService scheduledExecutorService; 21 private ScheduledExecutorService scheduledExecutorService;
17 22
  23 + @Autowired
  24 + private DeviceService deviceService;
  25 +
18 @PostConstruct 26 @PostConstruct
19 public void run() 27 public void run()
20 { 28 {
21 - scheduledExecutorService.scheduleAtFixedRate(new Runnable() {  
22 - @Override  
23 - public void run() { 29 + scheduledExecutorService.scheduleAtFixedRate(() -> {
  30 + try {
  31 + deviceService.openDefaultSerialPort();
  32 + } catch (Exception e) {
  33 + logger.error("串口打开失败",e);
  34 + return;
  35 + }
  36 + try {
  37 + deviceService.sendData("01 03 00 00 00 47 05 F8".replace(" ","").trim());
24 38
  39 + } catch (Exception e) {
  40 + logger.error("数据采集失败",e);
25 } 41 }
26 },1,60, TimeUnit.SECONDS); 42 },1,60, TimeUnit.SECONDS);
27 } 43 }
@@ -182,7 +182,7 @@ public class DeviceService { @@ -182,7 +182,7 @@ public class DeviceService {
182 SerialTool.addListener(serialPortEvent -> { 182 SerialTool.addListener(serialPortEvent -> {
183 try { 183 try {
184 Thread.sleep(500); 184 Thread.sleep(500);
185 - FeederCommdDto commdDto = new FeederCommdDto(SerialTool.readFromPort(serialPort)); 185 + FeederCommdDto commdDto = FeederCommdDto.initRead (SerialTool.readFromPort(serialPort));
186 dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据 186 dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
187 } catch (Exception e) { 187 } catch (Exception e) {
188 logger.error("返回数据处理异常",e); 188 logger.error("返回数据处理异常",e);
@@ -190,6 +190,11 @@ public class DeviceService { @@ -190,6 +190,11 @@ public class DeviceService {
190 }, serialPort); 190 }, serialPort);
191 } 191 }
192 192
  193 + public void openDefaultSerialPort() throws Exception {
  194 + SerialPortConfig serialPortConfig = (SerialPortConfig) configurationParameterService.getConfig(ConfigurationParameter.SerialPortConfig);
  195 + openSerialPort(serialPortConfig.getPortName(),serialPortConfig.getBaudrate(),serialPortConfig.getDataBits(),serialPortConfig.getStopBits(),serialPortConfig.getParity());
  196 + }
  197 +
193 /** 198 /**
194 * 发送数据 199 * 发送数据
195 * @param hexStr 200 * @param hexStr
  1 +package com.zhonglai.luhui.smart.feeder.service;
  2 +
  3 +import org.springframework.stereotype.Service;
  4 +
  5 +@Service
  6 +public class FeederDeviceService {
  7 +
  8 +}
@@ -54,6 +54,8 @@ public class TerminalService { @@ -54,6 +54,8 @@ public class TerminalService {
54 public void startMqttListenerService() throws MqttException{ 54 public void startMqttListenerService() throws MqttException{
55 log.info("-----------开始启动mqtt监听服务--------------------"); 55 log.info("-----------开始启动mqtt监听服务--------------------");
56 init(); 56 init();
  57 + connect();
  58 + subscribe();
57 } 59 }
58 60
59 61