98 lines
3.2 KiB
Java
98 lines
3.2 KiB
Java
|
package com.iot.modbus_rtcp.controller;
|
|||
|
|
|||
|
import com.iot.modbus_rtcp.dto.CommandTypeComparable;
|
|||
|
import com.iot.modbus_rtcp.dto.ModbusCommandDto;
|
|||
|
import com.iot.modbus_rtcp.netty.NettyServer;
|
|||
|
import com.iot.modbus_rtcp.utils.CRCUtil;
|
|||
|
import com.iot.modbus_rtcp.utils.HexUtil;
|
|||
|
import com.iot.modbus_rtcp.vo.Response;
|
|||
|
import jakarta.annotation.PreDestroy;
|
|||
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
import org.springframework.boot.ApplicationArguments;
|
|||
|
import org.springframework.boot.ApplicationRunner;
|
|||
|
import org.springframework.web.bind.annotation.PostMapping;
|
|||
|
import org.springframework.web.bind.annotation.RequestBody;
|
|||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
import org.springframework.web.bind.annotation.RestController;
|
|||
|
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
/**
|
|||
|
* Modbus-TCP协议API
|
|||
|
* 通信方式采用TCP的方式
|
|||
|
*/
|
|||
|
@Slf4j
|
|||
|
@RestController
|
|||
|
@RequestMapping("modbus-tcp")
|
|||
|
public class ModbusTCPController implements ApplicationRunner {
|
|||
|
private NettyServer nettyServer;
|
|||
|
|
|||
|
@PreDestroy
|
|||
|
private void destroy() {
|
|||
|
this.nettyServer.stop();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 采集命令API
|
|||
|
* 采集命令API使用异步访问的方式,设备响应数据后会将其推送到Kafka
|
|||
|
*
|
|||
|
* @param modbusCommandBoList
|
|||
|
* @return
|
|||
|
*/
|
|||
|
@PostMapping("/collect")
|
|||
|
public Response<String> collect(@RequestBody List<ModbusCommandDto> modbusCommandBoList) {
|
|||
|
log.debug("采集请求:{}", modbusCommandBoList);
|
|||
|
|
|||
|
try {
|
|||
|
modbusCommandBoList.stream().forEach(modbusCommandBo -> {
|
|||
|
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.HexStringToBytes(modbusCommandBo.getCommand())));
|
|||
|
|
|||
|
modbusCommandBo.setType(CommandTypeComparable.CommandType.COLLECTION);
|
|||
|
modbusCommandBo.setTimestamp(System.nanoTime());
|
|||
|
});
|
|||
|
|
|||
|
this.nettyServer.sender().send(modbusCommandBoList);
|
|||
|
} catch (Exception e) {
|
|||
|
log.error("", e);
|
|||
|
return Response.failed(e.getMessage());
|
|||
|
}
|
|||
|
|
|||
|
return Response.succeed();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 控制命令API
|
|||
|
* 控制命令API使用异步访问的方式,设备响应数据自动丢弃
|
|||
|
*
|
|||
|
* @param modbusCommandBoList
|
|||
|
* @return 设备响应返回
|
|||
|
*/
|
|||
|
@PostMapping("/control")
|
|||
|
public Response<String> control(@RequestBody List<ModbusCommandDto> modbusCommandBoList) {
|
|||
|
log.debug("控制请求:{}", modbusCommandBoList);
|
|||
|
|
|||
|
try {
|
|||
|
modbusCommandBoList.stream().forEach(modbusCommandBo -> {
|
|||
|
modbusCommandBo.setCommand(modbusCommandBo.getCommand() + CRCUtil.getCRC(HexUtil.HexStringToBytes(modbusCommandBo.getCommand())));
|
|||
|
|
|||
|
modbusCommandBo.setType(CommandTypeComparable.CommandType.CONTROL);
|
|||
|
modbusCommandBo.setTimestamp(System.nanoTime());
|
|||
|
});
|
|||
|
|
|||
|
this.nettyServer.sender().send(modbusCommandBoList);
|
|||
|
} catch (Exception e) {
|
|||
|
log.error("", e);
|
|||
|
return Response.failed(e.getMessage());
|
|||
|
}
|
|||
|
|
|||
|
return Response.succeed();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void run(ApplicationArguments args) throws Exception {
|
|||
|
this.nettyServer = new NettyServer(502, 10);
|
|||
|
this.nettyServer.start();
|
|||
|
}
|
|||
|
|
|||
|
}
|