作者 钟来

模块整理

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