package com.ard.work.health.task; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.ard.common.core.utils.StringUtils; import com.ard.work.health.domain.ArdAlarmEqHealth; import com.ard.work.health.service.ArdAlarmEqHealthService; import com.ard.work.health.util.Global; import com.ard.work.health.util.LonlatConver; import com.ard.work.websocket.utils.JKWebSocketUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.socket.TextMessage; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @Slf4j(topic = "task") @RefreshScope public class HealthTask { @Value("${health.getAlarmAPITask}") Boolean EqHealthGetAlarmAPITaskEnable; @Value("${health.realTask}") Boolean EqHealthRealTaskEnable; @Autowired private ArdAlarmEqHealthService ardAlarmEqHealthService; /** * @描述 设备健康报警api接口查询定时任务 * @参数 [] * @返回值 void * @创建人 刘苏义 * @创建时间 2023/5/5 11:51 * @修改人和其它信息 */ @Scheduled(cron = "0/20 * * * * ?") public void GetAlarmEqHealthApiTask() { if (!EqHealthGetAlarmAPITaskEnable) { return; } log.debug("设备健康报警定时获取数据"); ardAlarmEqHealthService.getAlarmDataByApi(); } /** * @描述 设备健康报警上传前端定时任务 * @参数 [] * @返回值 void * @创建人 刘苏义 * @创建时间 2023/5/5 11:51 * @修改人和其它信息 */ @Scheduled(cron = "0/20 * * * * ?") public void EqHealthAlarmRealTask() { if (!EqHealthRealTaskEnable) { return; } log.debug("设备健康报警定时任务开始"); // 开始时间 long stime = System.currentTimeMillis(); try { /*获取数据库最新50条数据*/ List ardAlarmEqHealths = ardAlarmEqHealthService.GetRealAlarm(); Map map = new HashMap<>(); map.put("Command", 2001); int count = 0; List objs = new ArrayList<>(); for (ArdAlarmEqHealth data : ardAlarmEqHealths) { //判断DirectLE为空 赋值空字符串 if (data.getDirectle() == null) { data.setDirectle(""); } //经纬度火星转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]); } String alarmTime = data.getAlarmTime(); //将最新数据放入obj if (StringUtils.isEmpty(Global.EqHealthNewTime)) { /*如果临时最新时间为空,则取最近10秒内的最新数据*/ SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MINUTE, -10); Date time = calendar.getTime(); int dateFlag = alarmTime.compareTo(fmt.format(time)); if (dateFlag > 0) { objs.add(data); Global.EqHealthNewTime = alarmTime; } } else { /*否则判断当前数据的时间是否大于最新时间*/ int dateFlag = alarmTime.compareTo(Global.EqHealthNewTime); if (dateFlag > 0) { objs.add(data); Global.EqHealthNewTime = alarmTime; } } //统计所有countLE的总数 if (StringUtils.isEmpty(data.getDirectle())) { count++; } data.setId(data.getMeasureKey() + "&" + data.getCommand()); } if (objs.size() > 0) { map.put("obj", objs); } else { map.put("obj", -1); } map.put("count", count); map.put("data", ardAlarmEqHealths); if (ardAlarmEqHealths.size() > 0) { //推送 JKWebSocketUtils.sendMessageAll(Collections.singletonList(new TextMessage(JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect)))); // for (Integer userId : SessionCollection.getClientsessionmap().keySet()) { // // List webSocketSessions = SessionCollection.getClientsessionmap().get(userId); // for (WebSocketSession webSocketSession : webSocketSessions) { // if (webSocketSession != null && webSocketSession.isOpen()) { // synchronized (webSocketSession) { // try { // webSocketSession.sendMessage(new TextMessage(JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect))); // } catch (IOException e) { // continue; // } // // } // } // } // } } } catch (Exception ex) { log.error("设备健康实时数据定时任务异常:" + ex.getMessage()); } // 结束时间 long etime = System.currentTimeMillis(); // 计算执行时间 log.debug("设备健康报警定时任务结束:" + (etime - stime) + " 毫秒"); } }