修改控制指令代码,添加控制指令服务
This commit is contained in:
parent
f82b7d2d10
commit
83f8bec79c
@ -18,7 +18,7 @@ public interface SysAuthorityIds {
|
||||
Integer SYS_AUTHORITY_ID_DEVICE_VIEW = 103;
|
||||
|
||||
/**
|
||||
* 风机启停复位控制权限
|
||||
* 设备控制控制权限
|
||||
*/
|
||||
Integer SYS_AUTHORITY_ID_TURBINE_CTRL=104;
|
||||
Integer SYS_AUTHORITY_ID_DEVICE_CTRL=104;
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import com.das.modules.node.domain.dto.*;
|
||||
import com.das.modules.node.domain.vo.*;
|
||||
import com.das.modules.node.service.DataService;
|
||||
import com.das.modules.node.service.SysNodeService;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -239,23 +238,5 @@ public class SysNodeController {
|
||||
}
|
||||
return R.success("导入失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备遥控操作
|
||||
* @param deviceInfo 遥控信息
|
||||
*/
|
||||
@PostMapping("/link/command")
|
||||
public void deviceCommand(@RequestBody DeviceCommandDto deviceInfo) {
|
||||
sysNodeService.deviceCommand(deviceInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备遥调操作
|
||||
* @param deviceInfo 遥控信息
|
||||
*/
|
||||
@PostMapping("/link/setPoint")
|
||||
public void deviceSetPoint(@RequestBody DeviceSetPointDto deviceInfo) {
|
||||
sysNodeService.deviceSetPoint(deviceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,16 +54,4 @@ public interface SysNodeService {
|
||||
void exportMappingList(QueryTabMappingParamDto sysImptabmappingDto,HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
boolean importMappingList(String linkId, MultipartFile file);
|
||||
|
||||
/**
|
||||
* 设备遥控操作
|
||||
* @param deviceInfo 遥控具体的设备信息,设备id、遥控命令、遥控值
|
||||
*/
|
||||
void deviceCommand(DeviceCommandDto deviceInfo);
|
||||
|
||||
/**
|
||||
* 设备遥调操作
|
||||
* @param deviceInfo 遥调具体的设备信息,设备id、遥调命令、遥调值
|
||||
*/
|
||||
void deviceSetPoint(DeviceSetPointDto deviceInfo);
|
||||
}
|
||||
|
@ -466,83 +466,4 @@ public class SysNodeServiceImpl implements SysNodeService {
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deviceCommand(DeviceCommandDto device) {
|
||||
try {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
String cmd = NodeConstant.DEVICE_CONTROL;
|
||||
Long time = System.currentTimeMillis();
|
||||
|
||||
map.put("deviceId", device.getDeviceId());
|
||||
map.put("serviceName", device.getServiceName());
|
||||
map.put("opValue", device.getOpValue());
|
||||
|
||||
//激活的node节点Id
|
||||
long activeNodeId = getActiveNodeId();
|
||||
if(activeNodeId==0){
|
||||
throw new RuntimeException("找不到激活的节点信息");
|
||||
}
|
||||
|
||||
// 将 HashMap 转换为 JsonNode
|
||||
ObjectNode jsonNode = JSON_MAPPER.convertValue(map, ObjectNode.class);
|
||||
TerminalMessage configUpdate = TerminalMessage.builder()
|
||||
.cmd(cmd)
|
||||
.cmdId(IdUtil.nanoId())
|
||||
.time(time)
|
||||
.data(jsonNode)
|
||||
.build();
|
||||
terminalMessageEventHandler.sendTerminalMessageWithResult(activeNodeId, configUpdate);
|
||||
} catch (Exception e) {
|
||||
log.error("设备控制失败 ", e);
|
||||
throw new ServiceException("设备控制失败 "+ e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deviceSetPoint(DeviceSetPointDto device) {
|
||||
try {
|
||||
HashMap map = new HashMap<>();
|
||||
String cmd = NodeConstant.DEVICE_CONTROL;
|
||||
Long time = System.currentTimeMillis();
|
||||
|
||||
map.put("deviceId", device.getDeviceId());
|
||||
map.put("serviceName", device.getServiceName());
|
||||
map.put("opValue", device.getOpValue());
|
||||
|
||||
//激活的node节点Id
|
||||
long activeNodeId = getActiveNodeId();
|
||||
if(activeNodeId==0){
|
||||
throw new RuntimeException("找不到激活的节点信息");
|
||||
}
|
||||
|
||||
// 将 HashMap 转换为 JsonNode
|
||||
ObjectNode jsonNode = JSON_MAPPER.convertValue(map, ObjectNode.class);
|
||||
TerminalMessage configUpdate = TerminalMessage.builder()
|
||||
.cmd(cmd)
|
||||
.cmdId(IdUtil.nanoId())
|
||||
.time(time)
|
||||
.data(jsonNode)
|
||||
.build();
|
||||
terminalMessageEventHandler.sendTerminalMessageWithResult(activeNodeId, configUpdate);
|
||||
} catch (Exception e) {
|
||||
log.error("设备控制失败 ", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private long currentActiveNodeId = 0;
|
||||
/**
|
||||
* 获得当前激活的节点id
|
||||
* @return 节点id
|
||||
*/
|
||||
private long getActiveNodeId(){
|
||||
if(currentActiveNodeId==0){
|
||||
List<SysNodeVo> list = sysNodeMapper.querySysNodeList();
|
||||
if(!list.isEmpty()){
|
||||
currentActiveNodeId= list.get(0).getId();
|
||||
}
|
||||
}
|
||||
return currentActiveNodeId;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,78 @@
|
||||
package com.das.modules.operation.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.das.common.constant.MeasType;
|
||||
import com.das.common.constant.SysAuthorityIds;
|
||||
import com.das.common.result.R;
|
||||
import com.das.modules.operation.domain.CommandInfoDto;
|
||||
import com.das.modules.operation.service.OperationService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 手动操作
|
||||
*/
|
||||
@Slf4j
|
||||
@RequestMapping("/api/operation")
|
||||
@RestController
|
||||
public class ManualOperatorController {
|
||||
@Autowired
|
||||
OperationService optService;
|
||||
|
||||
/**
|
||||
* 设备遥控操作 下令
|
||||
* @param cmdInfo 遥控信息
|
||||
*/
|
||||
@PostMapping("/command")
|
||||
public R<Void> deviceCommand(HttpServletRequest request, @RequestBody CommandInfoDto cmdInfo) {
|
||||
cmdInfo.setMeasType(MeasType.TYPE_PSR_CONTROL);
|
||||
|
||||
//判断是否有权限
|
||||
boolean hasPermission = StpUtil.hasPermission(SysAuthorityIds.SYS_AUTHORITY_ID_DEVICE_CTRL.toString());
|
||||
if(!hasPermission){
|
||||
return R.fail("没有设备遥控权限");
|
||||
}
|
||||
|
||||
optService.executeOperation(request,cmdInfo);
|
||||
return R.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备遥调操作 下令
|
||||
* @param cmdInfo 遥控信息
|
||||
*/
|
||||
@PostMapping("/setPoint")
|
||||
public R<Void> deviceSetPoint(HttpServletRequest request,@RequestBody CommandInfoDto cmdInfo) {
|
||||
cmdInfo.setMeasType(MeasType.TYPE_PSR_SET_POINT);
|
||||
//判断是否有权限
|
||||
boolean hasPermission = StpUtil.hasPermission(SysAuthorityIds.SYS_AUTHORITY_ID_DEVICE_CTRL.toString());
|
||||
if(!hasPermission){
|
||||
return R.fail("没有设备遥控权限");
|
||||
}
|
||||
|
||||
optService.executeOperation(request,cmdInfo);
|
||||
return R.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备手工至位 不下令
|
||||
*/
|
||||
@PostMapping("/manualCommand")
|
||||
public R<Void> deviceSetPointByMrid(HttpServletRequest request,@RequestBody CommandInfoDto cmdInfo) {
|
||||
cmdInfo.setMeasType(MeasType.TYPE_PSR_CALCULATED_VALUE);
|
||||
|
||||
//判断是否有权限
|
||||
boolean hasPermission = StpUtil.hasPermission(SysAuthorityIds.SYS_AUTHORITY_ID_DEVICE_CTRL.toString());
|
||||
if(!hasPermission){
|
||||
return R.fail("没有设备遥控权限");
|
||||
}
|
||||
|
||||
optService.executeOperation(request,cmdInfo);
|
||||
return R.success();
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.das.modules.operation.domain;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CommandInfoDto {
|
||||
/**
|
||||
* 设备ID
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long deviceId;
|
||||
|
||||
/**
|
||||
* 命令类型
|
||||
* 遥控量 TYPE_PSR_CONTROL 147
|
||||
* 遥调量 TYPE_PSR_SET_POINT 146
|
||||
* 计算 TYPE_PSR_CALCULATED_VALUE 199(人工设位,不下令)
|
||||
*/
|
||||
private Integer measType;
|
||||
|
||||
/**
|
||||
* 命令编码
|
||||
*/
|
||||
private String serviceCode;
|
||||
|
||||
/**
|
||||
* 遥控名称
|
||||
*/
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 操作描述
|
||||
*/
|
||||
private String optDesc;
|
||||
/**
|
||||
*遥控值, 1或者0
|
||||
*/
|
||||
private Object opValue;
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
package com.das.modules.operation.service;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.das.common.config.SessionUtil;
|
||||
import com.das.common.constant.MeasType;
|
||||
import com.das.common.exceptions.ServiceException;
|
||||
import com.das.modules.auth.domain.vo.SysUserVo;
|
||||
import com.das.modules.node.constant.NodeConstant;
|
||||
import com.das.modules.node.disruptor.TerminalMessageEventHandler;
|
||||
import com.das.modules.node.domain.bo.TerminalMessage;
|
||||
import com.das.modules.node.domain.vo.SysNodeVo;
|
||||
import com.das.modules.node.mapper.SysNodeMapper;
|
||||
import com.das.modules.operation.domain.CommandInfoDto;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OperationService {
|
||||
|
||||
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private SysNodeMapper sysNodeMapper;
|
||||
@Autowired
|
||||
TerminalMessageEventHandler terminalMessageEventHandler;
|
||||
|
||||
/**
|
||||
* 执行操作命令
|
||||
* @param request HttpServletRequest
|
||||
* @param cmdInfo 命令信息
|
||||
*/
|
||||
public void executeOperation(HttpServletRequest request, CommandInfoDto cmdInfo) {
|
||||
SysUserVo sysUserVo = (SysUserVo) StpUtil.getTokenSession().get(SessionUtil.SESSION_USER_KEY);
|
||||
|
||||
if(cmdInfo.getMeasType().equals(MeasType.TYPE_PSR_CALCULATED_VALUE)){
|
||||
//TODO:待完善:人工置位,不需要下令
|
||||
// 1、只需要把数据保存到数据库 sys_manual_status表中
|
||||
// 2、更新redis RT实时数据值
|
||||
}else{
|
||||
sendCommand(cmdInfo); //发送命令消息
|
||||
}
|
||||
|
||||
//TODO:待完善,记录操作日志到数据库 表 sys_operation_log
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送命令
|
||||
* @param cmdInfo 命令
|
||||
*/
|
||||
private void sendCommand(CommandInfoDto cmdInfo) {
|
||||
try {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
String cmd = NodeConstant.DEVICE_CONTROL;
|
||||
Long time = System.currentTimeMillis();
|
||||
|
||||
map.put("deviceId", cmdInfo.getDeviceId());
|
||||
map.put("serviceName", cmdInfo.getServiceCode());
|
||||
if(cmdInfo.getMeasType().equals(MeasType.TYPE_PSR_CONTROL)){
|
||||
map.put("opValue", Integer.valueOf(cmdInfo.getOpValue().toString()));
|
||||
}else{
|
||||
map.put("opValue", Float.valueOf(cmdInfo.getOpValue().toString()));
|
||||
}
|
||||
|
||||
//激活的node节点Id
|
||||
long activeNodeId = getActiveNodeId();
|
||||
if(activeNodeId==0){
|
||||
throw new RuntimeException("找不到激活的节点信息");
|
||||
}
|
||||
|
||||
// 将 HashMap 转换为 JsonNode
|
||||
ObjectNode jsonNode = JSON_MAPPER.convertValue(map, ObjectNode.class);
|
||||
TerminalMessage configUpdate = TerminalMessage.builder()
|
||||
.cmd(cmd)
|
||||
.cmdId(IdUtil.nanoId())
|
||||
.time(time)
|
||||
.data(jsonNode)
|
||||
.build();
|
||||
terminalMessageEventHandler.sendTerminalMessageWithResult(activeNodeId, configUpdate);
|
||||
} catch (Exception e) {
|
||||
log.error("设备控制失败 ", e);
|
||||
throw new ServiceException("设备控制失败 "+ e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端用户ip地址
|
||||
* @param request HttpServletRequest
|
||||
* @return ip地址
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
private String getClientIp(HttpServletRequest request) throws UnknownHostException {
|
||||
String ipAddress = request.getRemoteAddr();
|
||||
// 在某些情况下(如使用反向代理),上面的方法可能返回代理服务器的IP地址。
|
||||
// 在这种情况下,你可以尝试从HTTP头中获取真实的客户端IP地址。
|
||||
String header = request.getHeader("X-Forwarded-For");
|
||||
if (header != null && !header.trim().isEmpty()) {
|
||||
// X-Forwarded-For可能包含多个IP地址,通常第一个是最原始的客户端IP。
|
||||
ipAddress = header.split(",")[0].trim();
|
||||
} else {
|
||||
// 如果没有X-Forwarded-For头,或者它是空的,则使用getRemoteAddr()返回的值。
|
||||
InetAddress inet = InetAddress.getLocalHost();
|
||||
// 这里只是为了演示如何获取本地主机信息,实际上不需要在这里调用它。
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
|
||||
private long currentActiveNodeId = 0;
|
||||
/**
|
||||
* 获得当前激活的节点id
|
||||
* @return 节点id
|
||||
*/
|
||||
private long getActiveNodeId(){
|
||||
if(currentActiveNodeId==0){
|
||||
List<SysNodeVo> list = sysNodeMapper.querySysNodeList();
|
||||
if(!list.isEmpty()){
|
||||
currentActiveNodeId= list.get(0).getId();
|
||||
}
|
||||
}
|
||||
return currentActiveNodeId;
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ import com.das.modules.equipment.domain.vo.SysEquipmentVo;
|
||||
import com.das.modules.equipment.mapper.SysEquipmentMapper;
|
||||
import com.das.modules.node.domain.dto.DeviceCommandDto;
|
||||
import com.das.modules.node.service.SysNodeService;
|
||||
import com.das.modules.operation.service.OperationService;
|
||||
import com.das.modules.page.domian.WindTurbinesPageVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -33,7 +34,7 @@ public class WindTurbinesPageService {
|
||||
private DataService dataService;
|
||||
|
||||
@Autowired
|
||||
SysNodeService sysNodeService;
|
||||
OperationService optService;
|
||||
|
||||
/**
|
||||
* 获取风机机组所属线路列表
|
||||
@ -119,7 +120,8 @@ public class WindTurbinesPageService {
|
||||
public void windTurbinesControl(List<DeviceCommandDto> controlList) {
|
||||
for (DeviceCommandDto item : controlList) {
|
||||
try {
|
||||
sysNodeService.deviceCommand(item);
|
||||
//TODO: 待完善 控制代码修改 换到 OperationService中实现
|
||||
//optService.executeOperation(item);
|
||||
} catch (Exception e) {
|
||||
log.error("下控失败", e);
|
||||
throw new ServiceException("下控失败" + e);
|
||||
|
Loading…
Reference in New Issue
Block a user