|
...
|
...
|
@@ -4,6 +4,8 @@ import com.ruoyi.common.utils.GsonConstructor; |
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.dto.*;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.dto.commd.FeederCommdDto;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.FeederCommd06ResponseType;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.FeederCommdUtil;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.serial.SerialTool;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
...
|
...
|
@@ -13,6 +15,7 @@ import purejavacomm.SerialPort; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
...
|
...
|
@@ -26,8 +29,6 @@ import java.util.concurrent.TimeUnit; |
|
|
|
public class DeviceService {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(DeviceService.class);
|
|
|
|
|
|
|
|
private SerialPort serialPort; //串口
|
|
|
|
|
|
|
|
// 锁对象
|
|
|
|
private final Object lock = new Object();
|
|
|
|
// 用于存储串口返回的数据,使用线程安全的队列
|
|
...
|
...
|
@@ -43,6 +44,10 @@ public class DeviceService { |
|
|
|
|
|
|
|
private Integer nowGear; //当前档位
|
|
|
|
|
|
|
|
private Integer oldGear; //老的档位
|
|
|
|
|
|
|
|
private Double area; //面积
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ConfigurationParameterService configurationParameterService;
|
|
|
|
|
|
...
|
...
|
@@ -52,16 +57,41 @@ public class DeviceService { |
|
|
|
@Autowired
|
|
|
|
private EhCacheService ehCacheService;
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
private SerialPort serialPort;
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
//投料控制
|
|
|
|
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
|
|
|
if (((Boolean)ehCacheService.readFromCache(ConfigurationParameter.FeedingControl))) {
|
|
|
|
Map<Integer,String> map = (Map<Integer, String>) ehCacheService.readFromCache(ConfigurationParameter.gear_command);
|
|
|
|
if(null != map && StringUtils.isNotEmpty( map.get(nowGear)))
|
|
|
|
logger.info("当前档位{},以前的档位{},斜率{},斜率差值{},面积{},开关是否打开{}",nowGear,oldGear,slope,absValue,area,null == configurationParameterService.getStateData()?"未知":configurationParameterService.getStateData().getSwitch_status());
|
|
|
|
|
|
|
|
if(null != nowGear && oldGear != nowGear)
|
|
|
|
{
|
|
|
|
send485SerialData(map.get(nowGear));
|
|
|
|
if(nowGear>0 ) //如果档位有值
|
|
|
|
{
|
|
|
|
if(null !=configurationParameterService.getStateData() && 1==configurationParameterService.getStateData().getRunmode())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,0)); //,运行模式改成手动
|
|
|
|
}
|
|
|
|
if(null !=configurationParameterService.getStateData() && 0==configurationParameterService.getStateData().getSwitch_status())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,1)); //,开关是关的就先打开开关
|
|
|
|
}
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runspeed,nowGear));
|
|
|
|
oldGear = nowGear;
|
|
|
|
}else{
|
|
|
|
if(null !=configurationParameterService.getStateData() && 0==configurationParameterService.getStateData().getRunmode())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,1)); //,运行模式改成自动
|
|
|
|
}
|
|
|
|
if(null !=configurationParameterService.getStateData() && 1==configurationParameterService.getStateData().getSwitch_status())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,0)); //,开关是关的就先打开开关
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},1,1, TimeUnit.SECONDS);
|
|
...
|
...
|
@@ -86,6 +116,7 @@ public class DeviceService { |
|
|
|
*/
|
|
|
|
public double controlDevice(double area)
|
|
|
|
{
|
|
|
|
this.area = area;
|
|
|
|
if(null == backArea )
|
|
|
|
{
|
|
|
|
backArea = area;
|
|
...
|
...
|
@@ -182,12 +213,33 @@ public class DeviceService { |
|
|
|
SerialTool.addListener(serialPortEvent -> {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
FeederCommdDto commdDto = FeederCommdDto.initRead (SerialTool.readFromPort(serialPort));
|
|
|
|
FeederCommdDto commdDto = new FeederCommdDto (SerialTool.readFromPort(serialPort));
|
|
|
|
dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("返回数据处理异常",e);
|
|
|
|
}
|
|
|
|
}, serialPort);
|
|
|
|
logger.info("打开串口成功");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void nowOpenSerial(String portName, Integer baudrate, Integer dataBits, Integer stopBits,
|
|
|
|
Integer parity) throws Exception {
|
|
|
|
if(null != serialPort)
|
|
|
|
{
|
|
|
|
serialPort.removeEventListener();
|
|
|
|
serialPort.close();
|
|
|
|
}
|
|
|
|
serialPort = SerialTool.openPort(portName,baudrate,dataBits,stopBits,parity);
|
|
|
|
SerialTool.addListener(serialPortEvent -> {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
FeederCommdDto commdDto = new FeederCommdDto (SerialTool.readFromPort(serialPort));
|
|
|
|
dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("返回数据处理异常",e);
|
|
|
|
}
|
|
|
|
}, serialPort);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void openDefaultSerialPort() throws Exception {
|
|
...
|
...
|
@@ -215,5 +267,14 @@ public class DeviceService { |
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void close()
|
|
|
|
{
|
|
|
|
dataQueue.clear();
|
|
|
|
if(null != serialPort)
|
|
|
|
{
|
|
|
|
serialPort.removeEventListener();
|
|
|
|
serialPort.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|