106 lines
3.2 KiB
Java
106 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.*;
|
||
|
||
import java.util.List;
|
||
import java.util.Set;
|
||
|
||
/**
|
||
* Modbus-TCP协议API
|
||
* 通信方式采用TCP的方式
|
||
*/
|
||
@Slf4j
|
||
@RestController
|
||
@RequestMapping("modbus-tcp")
|
||
public class ModbusTCPController implements ApplicationRunner {
|
||
public static 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();
|
||
}
|
||
|
||
/**
|
||
* 在綫設備列表
|
||
*
|
||
* @return
|
||
*/
|
||
@GetMapping("/online")
|
||
public Response<Set<String>> online() {
|
||
return Response.succeed(this.nettyServer.getGroup().onlineGateway());
|
||
}
|
||
|
||
@Override
|
||
public void run(ApplicationArguments args) throws Exception {
|
||
this.nettyServer = new NettyServer(502, 10);
|
||
this.nettyServer.start();
|
||
}
|
||
|
||
}
|