@@ -6,30 +6,33 @@ import java.util.Arrays;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.TimeUnit ;
import java.util.stream.Collectors ;
import com.maibu.core.enums.AlarmType ;
import com.maibu.core.redis.RedisCache ;
import org.eclipse.paho.client.mqttv3.MqttException ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.util.CollectionUtils ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.maibu.core.business.Device ;
import com.maibu.core.business.alarm_center.Alarm ;
import com.maibu.core.business.alarm_center.AlarmMessage ;
import com.maibu.core.business.device.NettyDevice ;
import com.maibu.core.business.dto.AlarmHandleDTO ;
import com.maibu.core.business.dto.AlarmQueryDTO ;
import com.maibu.core.business.dto.WebOlineStatusMessageDTO ;
import com.maibu.core.business.inter.WebsocketMesDispather ;
import com.maibu.core.domain.model.LoginUser ;
import com.maibu.core.enums.AlarmType ;
import com.maibu.core.enums.ErrorSource ;
import com.maibu.core.page.TableDataInfo ;
import com.maibu.core.enums.MesType ;
import com.maibu.core.redis.RedisCache ;
import com.maibu.exception.ServiceException ;
import com.maibu.mapper.AlarmMapper ;
import com.maibu.mapper.DeviceMapper ;
import com.maibu.memory.DeviceSessionManager ;
import com.maibu.memory.GlobalMemory ;
import com.maibu.mqtt.MqttTopic ;
import com.maibu.service.IAlarmService ;
@@ -52,10 +55,15 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
@Autowired
private RedisCache redisCache ;
@Autowired
private DeviceSessionManager deviceSessionManager ;
@Autowired
private WebsocketMesDispather websocketMesDispather ;
@Value ( " ${mqtt.client-id} " )
private String mqttClientId ;
@Override
public List < Alarm > selectAlarmPage ( AlarmQueryDTO queryDTO ) {
// validateQueryParams(queryDTO);
@@ -82,7 +90,8 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
Map < String , Object > statistics = new HashMap < > ( ) ;
statistics . put ( " total " , alarms . size ( ) ) ;
statistics . put ( " pending " , alarms . stream ( ) . filter ( a - > a . getHandleStatus ( ) = = 1 ) . count ( ) ) ;
// statistics.put("processing", alarms.stream().filter(a -> a.getHandleStatus() == 2).count());
// statistics.put("processing", alarms.stream().filter(a -> a.getHandleStatus()
// == 2).count());
statistics . put ( " closed " , alarms . stream ( ) . filter ( a - > a . getHandleStatus ( ) = = 2 ) . count ( ) ) ;
statistics . put ( " levelStats " ,
@@ -240,10 +249,10 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
Alarm alarm = alarmMapper . selectById ( id ) ;
if ( alarm = = null )
throw new ServiceException ( " 告警不存在 " ) ;
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getDeviceId() != null) {
// validateDeviceAccess(alarm.getDeviceId());
// }
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getDeviceId() != null) {
// validateDeviceAccess(alarm.getDeviceId());
// }
return alarm ;
}
@@ -261,13 +270,13 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
@Override
public boolean updateAlarm ( Alarm alarm ) {
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getId() != null) {
// Alarm existAlarm = alarmMapper.selectById(alarm.getId());
// if (existAlarm != null && existAlarm.getDeviceId() != null) {
// validateDeviceAccess(existAlarm.getDeviceId());
// }
// }
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getId() != null) {
// Alarm existAlarm = alarmMapper.selectById(alarm.getId());
// if (existAlarm != null && existAlarm.getDeviceId() != null) {
// validateDeviceAccess(existAlarm.getDeviceId());
// }
// }
return alarmMapper . saveOrUpdate ( alarm ) ;
}
@@ -277,13 +286,14 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
if ( alarm = = null )
throw new ServiceException ( " 告警不存在 " ) ;
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getDeviceId() != null) {
// validateDeviceAccess(alarm.getDeviceId());
// }
// Long userId = SecurityUtils.getUserId();
// if (!SecurityUtils.isAdmin(userId) && alarm.getDeviceId() != null) {
// validateDeviceAccess(alarm.getDeviceId());
// }
// Alarm updateAlarm = new Alarm();
// updateAlarm.setId(handleDTO.getAlarmId());
// Alarm updateAlarm = new Alarm();
// updateAlarm.setId(handleDTO.getAlarmId());
alarm . setHandleStatus ( handleDTO . getHandleStatus ( ) ) ;
alarm . setHandleUserId ( SecurityUtils . getUserId ( ) ) ;
alarm . setHandleUserName ( SecurityUtils . getLoginUser ( ) . getUser ( ) . getNickName ( ) ) ;
@@ -296,24 +306,25 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
}
public void removeRedisAlarm ( String deviceId , String alarmNo ) {
if ( StringUtils . isEmpty ( deviceId ) | | StringUtils . isEmpty ( alarmNo ) ) return ;
if ( StringUtils . isEmpty ( deviceId ) | | StringUtils . isEmpty ( alarmNo ) )
return ;
String pushKey = " alarm: " + " dingtalk: " + deviceId + " : " + alarmNo ;
// Boolean exists = redisCache.hasKey(pushKey);
// if (exists != null && exists) {
// redisCache.deleteObject(pushKey);
// }
// Boolean exists = redisCache.hasKey(pushKey);
// if (exists != null && exists) {
// redisCache.deleteObject(pushKey);
// }
String pushKey2 = " alarm: " + " email: " + deviceId + " : " + alarmNo ;
// Boolean exists2 = redisCache.hasKey(pushKey2);
// if (exists2 != null && exists2) {
// redisCache.deleteObject(pushKey2);
// }
// Boolean exists2 = redisCache.hasKey(pushKey2);
// if (exists2 != null && exists2) {
// redisCache.deleteObject(pushKey2);
// }
String pushKey3 = " alarm: " + " mqtt: " + deviceId + " : " + alarmNo ;
// Boolean exists3 = redisCache.hasKey(pushKey3);
// if (exists3 != null && exists3) {
// redisCache.deleteObject(pushKey3);
// }
// Boolean exists3 = redisCache.hasKey(pushKey3);
// if (exists3 != null && exists3) {
// redisCache.deleteObject(pushKey3);
// }
redisCache . deleteObject ( pushKey ) ;
redisCache . deleteObject ( pushKey2 ) ;
@@ -382,8 +393,8 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
return statistics ;
}
public List < Alarm > pushAlertMes ( List < AlarmMessage > alarmMessages ) {
if ( alarmMessages = = null | | alarmMessages . isEmpty ( ) ) {
public List < Alarm > pushAlertMes ( List < AlarmMessage > alarmMessages , String deviceId , String deviceName ) {
if ( alarmMessages = = null | | alarmMessages . isEmpty ( ) | | StringUtils . isEmpty ( deviceId ) ) {
return null ;
}
List < Alarm > list = new ArrayList < > ( ) ;
@@ -402,18 +413,59 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
alarm . setTimeValues ( alarmMessage . getTimeValues ( ) ) ;
alarm . setSiteId ( alarmMessage . getSiteId ( ) ) ;
alarm . setAlarmTime ( LocalDateTime . now ( ) ) ;
pushErrorMessage ( alarmMessage . g etDeviceName ( ) , JsonUtils . toJsonString ( alarm ) ) ;
alarm . s etDeviceAlias ( deviceName ) ;
// 单条保存,数据库生成的 ID 会回填到 alarm
alarmMapper . insert ( alarm ) ;
pushErrorMessage (
alarmMessage . getDeviceName ( ) ,
JsonUtils . toJsonString ( alarm ) ) ;
list . add ( alarm ) ;
}
alarmMapper . saveOrUpdateBatch ( list ) ;
return list ;
// todo 推送websocket?
WebOlineStatusMessageDTO dto = new WebOlineStatusMessageDTO ( ) ;
dto . setAlarms ( list ) ;
dto . setDeviceId ( deviceId ) ;
dto . setType ( MesType . device_error_push ) ;
NettyDevice device = deviceSessionManager . getDevice ( deviceId ) ;
if ( device ! = null & & device . getDevice ( ) ! = null ) {
dto . setDeviceAlias ( device . getDevice ( ) . getDeviceAlias ( ) ) ;
}
List < NettyDevice > controlMasters = deviceSessionManager
. getAllSlaveControl ( deviceId ) ;
if ( ! CollectionUtils . isEmpty ( controlMasters ) ) {
for ( NettyDevice x : controlMasters ) {
try {
websocketMesDispather . dispather ( x . getConnectorId ( ) , JsonUtils . toJsonString ( dto ) ) ;
} catch ( Exception e ) {
log . error ( " 实时数据websocket推送失败: connectorId={}, error={} " , x . getConnectorId ( ) ,
e . getMessage ( ) ) ;
}
}
}
try {
if ( device ! = null & & device . getDevice ( ) ! = null ) {
Long siteId = device . getDevice ( ) . getSiteId ( ) ;
websocketMesDispather . mapMessageDispather ( siteId ,
JsonUtils . toJsonString ( dto ) ) ;
}
} catch ( Exception e ) {
log . error ( " 实时地图数据websocket推送失败: siteId={}, error={} " , e ) ;
}
return list ;
}
public void pushErrorMessage ( String deviceId , String dto ) {
try {
GlobalMemory . customMqttDeviceMonitor . publish ( mqttClientId , String . format ( MqttTopic . DEVICE_ERROR_PUSH_TOPIC , deviceId ) , dto ) ;
// GlobalMemory.mqttClientUtil.publish( String. format( MqttTopic. DEVICE_ERROR_PUSH_TOPIC, deviceId), dto) ;
GlobalMemory . customMqttDeviceMonitor . publish ( mqttClientId ,
String. format( MqttTopic. DEVICE_ERROR_PUSH_TOPIC, deviceId ) , dto ) ;
// GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_ERROR_PUSH_TOPIC,
// deviceId), dto);
// List<NettyDevice> controlMasters =
// sessionManager.getAllSlaveControl(deviceId);
// if (!CollectionUtils.isEmpty(controlMasters)) {