package com.ruoyi.quartz.task; import com.ruoyi.alarm.globalAlarm.service.IGlobalAlarmService; import com.ruoyi.alarm.stealAlarm.domain.ArdAlarmStealelec; import com.ruoyi.alarm.stealAlarm.service.IStealElecAlarmService; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.utils.LonlatConver; import com.ruoyi.common.websocket.util.WebSocketUtils; import com.ruoyi.system.service.ISysUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; import static com.ruoyi.common.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS; /** * @ClassName: AlarmTask * @Description: * @Author: Administrator * @Date: 2023年03月06日 11:13 * @Version: 1.0 **/ @Component("AlarmTask") @Slf4j(topic = "AlarmTask") public class AlarmTask { @Resource IStealElecAlarmService IStealElecAlarmService; @Resource ISysUserService sysUserService; @Resource IGlobalAlarmService globalAlarmService; /** * @描述 盗电报警实时推送任务 * @参数 [] * @返回值 void * @创建人 刘苏义 * @创建时间 2023/3/6 15:10 * @修改人和其它信息 */ public void stealElecAlarmPush() { log.info("盗电报警推送开始"); // 开始时间 long stime = System.currentTimeMillis(); try { for (String userId : ONLINE_USER_SESSIONS.keySet()) { SysUser sysUser = sysUserService.selectUserById(userId); List alarms = IStealElecAlarmService.getRealAlarm(sysUser.getDeptId()); Map map = new HashMap<>(); List objs = new ArrayList<>(); for (ArdAlarmStealelec data : alarms) { //经纬度火星转84 if (data.getLongitude() != 0 && data.getLatitude() != 0) { double[] wgs84 = LonlatConver.gcj02_To_Wgs84(data.getLongitude(), data.getLatitude());//火星转84 data.setLongitude(wgs84[0]); data.setLatitude(wgs84[1]); } //判断当前数据是否10S内 Date startTime = data.getStartTime(); SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//HH是24小时制,12小时用hh Calendar calendar = Calendar.getInstance(); //从当前时间减去10秒 calendar.add(Calendar.MINUTE, -30); String currentTime = formatTime.format(calendar.getTime()); int dateFlag = formatTime.format(startTime).compareTo(currentTime); if (dateFlag > 0) { objs.add(data); } data.setId(data.getDescribe() + "&" + data.getCommand()); } if (objs.size() > 0) { map.put("obj", objs); } else { map.put("obj", -1); } map.put("Command", 8000); map.put("count", objs.size()); map.put("total", alarms.size()); map.put("data", alarms); if (alarms.size() > 0) { WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(userId), map); } } } catch (Exception ex) { log.error("盗电报警推送异常:" + ex.getMessage()); } // 结束时间 long etime = System.currentTimeMillis(); // 计算执行时间 log.info("盗电报警推送结束:" + (etime - stime) + " 毫秒"); } /** * @描述 盗电报警api拉取任务 * @参数 [] * @返回值 void * @创建人 刘苏义 * @创建时间 2023/2/28 11:51 * @修改人和其它信息 */ public void stealElecAlarmPull() { log.info("盗电数据拉取开始"); // 开始时间 long stime = System.currentTimeMillis(); try { // String url = "http://iot.zhdk.net:8090/Warning/GetWarning?userName=cy4oil"; IStealElecAlarmService.getAlarmByApi(); } catch (Exception ex) { log.error("盗电数据拉取异常:" + ex.getMessage()); } // 结束时间 long etime = System.currentTimeMillis(); // 计算执行时间 log.info("盗电数据拉取结束:" + (etime - stime) + " 毫秒"); } public void globalAlarmCountPush() { Map stringIntegerMap = globalAlarmService.selectAlarmLogsCount(); if (ONLINE_USER_SESSIONS.size() > 0) { WebSocketUtils.sendMessageAll(stringIntegerMap); } } }