package com.zswl.dataservice.service.mqtt; import com.zswl.dataservice.dao.mqtt.DeviceInfoDao; import com.zswl.dataservice.dao.mqtt.GateWayInfoDao; import com.zswl.dataservice.domain.mqtt.DeviceInfo; import com.zswl.dataservice.domain.mqtt.GateWayInfo; import com.zswl.dataservice.event.DeviceSyncEvent; import com.zswl.dataservice.event.GateWaySyncEvent; import com.zswl.dataservice.httpRequest.ApiRequestService; import com.zswl.dataservice.service.base.SuperService; import com.zswl.dataservice.utils.result.ResultContent; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 物联网平台 设备 网关同步到全卡项目 * * @author TRX * @date 2024/6/26 */ @Slf4j @Service public class DeviceSyncFullCardService extends SuperService { @Autowired DeviceInfoDao deviceInfoDao; @Autowired GateWayInfoDao gateWayInfoDao; @Autowired OperationLogsService operationLogsService; @Autowired ApiRequestService apiRequestService; @Autowired ApplicationContext applicationContext; /** * 通知同步设备 * * @param deviceInfo * @return */ public ResultContent noticeSyncDevice(DeviceInfo deviceInfo) { if (ObjectUtils.isNotEmpty(deviceInfo)) { List deviceIds = new ArrayList<>(); deviceIds.add(deviceInfo.getDeviceId()); DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds); applicationContext.publishEvent(event); } return ResultContent.buildSuccess(); } /** * 通知同步设备 * * @param deviceInfos * @return */ public ResultContent noticeSyncDevice(List deviceInfos) { if (ObjectUtils.isNotEmpty(deviceInfos)) { List deviceIds = deviceInfos.stream().map(it -> it.getDeviceId()).collect(Collectors.toList()); DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds); applicationContext.publishEvent(event); } return ResultContent.buildSuccess(); } /** * 通知同步设备 * * @param deviceIds * @return */ public ResultContent noticeSyncDeviceIds(List deviceIds) { if (ObjectUtils.isNotEmpty(deviceIds)) { DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds); applicationContext.publishEvent(event); } return ResultContent.buildSuccess(); } /** * 接收到同步设备的事件 * * @param event */ @EventListener(classes = DeviceSyncEvent.class) @Async @SneakyThrows public void syncDeviceInfo(DeviceSyncEvent event) { List deviceIds = event.getDeviceIds(); log.info("event syncDeviceInfo: {}", deviceIds); } /** * 通知同步网关信息 * * @param gateWayInfo * @return */ public ResultContent noticeSyncGateWay(GateWayInfo gateWayInfo) { if (ObjectUtils.isNotEmpty(gateWayInfo)) { GateWaySyncEvent event = new GateWaySyncEvent(this, gateWayInfo.getGateWayId()); applicationContext.publishEvent(event); } return ResultContent.buildSuccess(); } /** * 同步网关信息 * * @param event */ @EventListener(classes = GateWaySyncEvent.class) @Async @SneakyThrows public void syncGateWayInfo(GateWaySyncEvent event) { String gatewayId = event.getGateWayId(); log.info("event syncGateWayInfo: {}", gatewayId); } }