1. 修改发送功能

This commit is contained in:
黄昕 2023-08-30 11:27:34 +08:00
parent 90b6c51460
commit b99e08f20a
4 changed files with 64 additions and 44 deletions

View File

@ -59,9 +59,8 @@ public class AuthUser {
new ArrayList<>(),
new ArrayList<>(),
new ArrayList<>(Arrays.asList(0, 1, 2, 3)));
Long id = baidouAdapterService.getAllAdapter().get(0).getId();
baidouAdapterService.sendCommond(id, sensorPro);
baidouAdapterService.sendCommandToAllAdapter(sensorPro);
return "{\"result\": 0}";
}

View File

@ -8,10 +8,14 @@ import java.util.List;
public interface BaidouAdapterService {
void startBeidouAdapter();
List<Long> getActiveAdapter();
void sendCommond(Long adapterId, Object command);
void sendCommond(Channel ch, Object command);
void sendCommandToAllAdapter(Object command);
List<BeidouAdapterDevice> getAllAdapter();
void addNewBeidouAdapter(BeidouAdapterDevice adapter);

View File

@ -33,6 +33,7 @@ import com.zjyr.beidouservice.pojo.dto.task.AlarmTaskReqDTO;
import com.zjyr.beidouservice.pojo.vo.PageInfoVO;
import com.zjyr.beidouservice.service.AdapterProtocolService;
import com.zjyr.beidouservice.service.AlarmTaskService;
import com.zjyr.beidouservice.service.BaidouAdapterService;
import com.zjyr.beidouservice.service.DeviceService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@ -55,6 +56,9 @@ import java.util.stream.Collectors;
public class AlarmTaskServiceImpl implements AlarmTaskService {
@Resource
AdapterProtocolService adapterProtocolService;
@Resource
BaidouAdapterService baidouAdapterService;
@Resource
DeviceService deviceService;
@ -110,7 +114,7 @@ public class AlarmTaskServiceImpl implements AlarmTaskService {
// 刷新设备告警状态
deviceService.refreshDeviceAlarmStatus(alarmDeviceTaskDOS);
//执行控制任,默认只有一种通道方式
adapterProtocolService.createSensorControlProtocol(
baidouAdapterService.sendCommandToAllAdapter(adapterProtocolService.createSensorControlProtocol(
generatedId.intValue(),
CommonEnumHandler.codeOf(AlarmModeName.class, alarmTaskReqDTO.getAlarmMode()),
CommonEnumHandler.codeOf(AlarmControlTypeName.class, alarmTaskReqDTO.getAlarmType()),
@ -120,7 +124,7 @@ public class AlarmTaskServiceImpl implements AlarmTaskService {
deviceInfo.getCityCodes(),
deviceInfo.getCountyCodes(),
deviceInfo.getBeidouIds()
);
));
return MyResp.success("执行任务成功");

View File

@ -33,7 +33,8 @@ import static com.zjyr.beidouservice.common.impl.SocketEventName.SOCKET_EVT_CONN
@Service
@Slf4j
public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implements BaidouAdapterService, ApplicationListener<T> {
public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implements BaidouAdapterService,
ApplicationListener<T> {
private final ConcurrentHashMap<Long, ControlAdapterSocketCtx> ctxMap = new ConcurrentHashMap<>();
@Resource
@ -56,6 +57,13 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
tcpServer.start();
}
@Override
public List<Long> getActiveAdapter() {
List<Long> adapterList = new ArrayList<>();
ctxMap.forEach((key, value) -> adapterList.add(key));
return adapterList;
}
@Override
public void sendCommond(Long adapterId, Object command) {
ControlAdapterSocketCtx ctx = ctxMap.get(adapterId);
@ -72,24 +80,29 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
//}
}
@Override
public void sendCommandToAllAdapter(Object command) {
getActiveAdapter().forEach(k -> sendCommond(k, command));
}
@Override
public void onApplicationEvent(@Nonnull T evtContent) {
if (evtContent instanceof SocketNotifyEvent event) {
InetSocketAddress sa = (InetSocketAddress) event.getCtxChannel().remoteAddress();
Long devId = Optional.ofNullable(getAdapterByAddr(sa.getAddress().getHostAddress()))
.orElse(BeidouAdapterDevice.builder()
.id(-1)
.build())
.getId();
.orElse(BeidouAdapterDevice.builder()
.id(-1)
.build())
.getId();
switch (event.getSocketEvent()) {
case SOCKET_EVT_CONNECT, SOCKET_EVT_IDLE_TIMEOUT -> {
sendCommond(event.getCtxChannel(), adapterProtocolService.createHeartProtocol());
if (event.getSocketEvent() == SOCKET_EVT_CONNECT) {
BeidouAdapterDevice a = BeidouAdapterDevice.builder()
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
.build();
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
.build();
addNewBeidouAdapter(a);
ControlAdapterSocketCtx sockCtx = new ControlAdapterSocketCtx(a.getId(), event.getCtxChannel());
@ -108,19 +121,19 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
case SOCKET_EVT_RECV -> {
if (event.getEvtMessage() instanceof ControllerStatus status) {
log.info("+++{}, {}, {}",
status.getWirelessStatus(),
status.getTelphoneStatus(),
HelperUtils.bytesToHexString(status.getBeidouSignalStrength()));
status.getWirelessStatus(),
status.getTelphoneStatus(),
HelperUtils.bytesToHexString(status.getBeidouSignalStrength()));
BeidouAdapterDevice a = BeidouAdapterDevice.builder()
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
.sendEnabled(status.getSendStatus().getValue())
.beidouStatus(status.getBeidouStatus().getValue())
.beidouChannelStatus(HelperUtils.bytesToHexString(status.getBeidouFreq()))
.beidouSignalStrength(HelperUtils.bytesToHexString(status.getBeidouSignalStrength()))
.wirelessStatus(status.getWirelessStatus().getValue())
.telphoneStatus(status.getTelphoneStatus().getValue())
.build();
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
.sendEnabled(status.getSendStatus().getValue())
.beidouStatus(status.getBeidouStatus().getValue())
.beidouChannelStatus(HelperUtils.bytesToHexString(status.getBeidouFreq()))
.beidouSignalStrength(HelperUtils.bytesToHexString(status.getBeidouSignalStrength()))
.wirelessStatus(status.getWirelessStatus().getValue())
.telphoneStatus(status.getTelphoneStatus().getValue())
.build();
addNewBeidouAdapter(a);
} else if (event.getEvtMessage() instanceof SensorTaskAck ack) {
log.info("+++{}, {}, {}", ack.getTaskId(), ack.getAckTimestamp(), ack.getSensorStatus().size());
@ -143,16 +156,16 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
for (ControlDevice c : controlDevices) {
adapterDevices.add(BeidouAdapterDevice.builder()
.id(c.getId())
.adapterType(c.getDeviceType())
.adapterAddr(c.getDeviceAddr())
.sendEnabled(Optional.ofNullable(c.getSendEnabled()).orElse(0))
.beidouStatus(Optional.ofNullable(c.getBeidouStatus()).orElse(0))
.beidouChannelStatus(Optional.ofNullable(c.getBeidouChannelStatus()).orElse(""))
.beidouSignalStrength(Optional.ofNullable(c.getBeidouSignalStrength()).orElse(""))
.wirelessStatus(Optional.ofNullable(c.getWirelessStatus()).orElse(0))
.telphoneStatus(Optional.ofNullable(c.getTelphoneStatus()).orElse(0))
.build());
.id(c.getId())
.adapterType(c.getDeviceType())
.adapterAddr(c.getDeviceAddr())
.sendEnabled(Optional.ofNullable(c.getSendEnabled()).orElse(0))
.beidouStatus(Optional.ofNullable(c.getBeidouStatus()).orElse(0))
.beidouChannelStatus(Optional.ofNullable(c.getBeidouChannelStatus()).orElse(""))
.beidouSignalStrength(Optional.ofNullable(c.getBeidouSignalStrength()).orElse(""))
.wirelessStatus(Optional.ofNullable(c.getWirelessStatus()).orElse(0))
.telphoneStatus(Optional.ofNullable(c.getTelphoneStatus()).orElse(0))
.build());
}
return adapterDevices;
@ -164,16 +177,16 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
@Override
public void addNewBeidouAdapter(BeidouAdapterDevice adapter) {
ControlDevice device = ControlDevice.builder()
.id(adapter.getId())
.deviceType(adapter.getAdapterType())
.deviceAddr(adapter.getAdapterAddr())
.beidouStatus(adapter.getBeidouStatus())
.sendEnabled(adapter.getSendEnabled())
.beidouChannelStatus(adapter.getBeidouChannelStatus())
.beidouSignalStrength(adapter.getBeidouSignalStrength())
.wirelessStatus(adapter.getWirelessStatus())
.telphoneStatus(adapter.getTelphoneStatus())
.build();
.id(adapter.getId())
.deviceType(adapter.getAdapterType())
.deviceAddr(adapter.getAdapterAddr())
.beidouStatus(adapter.getBeidouStatus())
.sendEnabled(adapter.getSendEnabled())
.beidouChannelStatus(adapter.getBeidouChannelStatus())
.beidouSignalStrength(adapter.getBeidouSignalStrength())
.wirelessStatus(adapter.getWirelessStatus())
.telphoneStatus(adapter.getTelphoneStatus())
.build();
BeidouAdapterDevice dev = getAdapterByAddr(adapter.getAdapterAddr());
if (dev == null) {
controlDeviceMapper.addControlDevice(device);