添加接口调用异常的短信提示

wushunjie
吴顺杰 2 months ago
parent 50d7f0a07f
commit 92e01d90e5

@ -22,6 +22,7 @@
<dependency> <dependency>
<groupId>com.yingji</groupId> <groupId>com.yingji</groupId>
<artifactId>utils</artifactId> <artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

@ -28,5 +28,12 @@ public interface AlarmMapper extends BaseMapper<Alarm> {
* @param list * @param list
*/ */
int saveAll(List<Alarm> list); int saveAll(List<Alarm> list);
/**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

@ -6,6 +6,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.utils.SmsUtil;
import com.yingji.entity.Alarm; import com.yingji.entity.Alarm;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.entity.dto.request.AlarmRequest; import com.yingji.entity.dto.request.AlarmRequest;
@ -18,6 +19,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
@ -62,14 +64,23 @@ public class AlarmQuartz {
addList(idList, token); addList(idList, token);
// 110算法接口 // 110算法接口
if (CollectionUtil.isNotEmpty(idList)) { if (CollectionUtil.isNotEmpty(idList)) {
emergencyAlgorithm(); try {
emergencyAlgorithm();
} catch (Exception e) {
// 调用短信接口
SmsUtil.sendSms("110调用算法接口异常", null);
}
} }
quartzLog.setStatus(1); quartzLog.setStatus(1);
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
log.error("============110算法接口获取token失败============="); // 调用短信接口
SmsUtil.sendSms("110原始接口获取token失败", null);
log.error("============110原始接口获取token失败=============");
} }
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口
SmsUtil.sendSms("110原始接口调用失败", null);
quartzLog.setStatus(2); quartzLog.setStatus(2);
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -148,6 +159,17 @@ public class AlarmQuartz {
if (StrUtil.isEmpty(token)) { if (StrUtil.isEmpty(token)) {
return idList; return idList;
} }
// 获取数据库中最新的创建时间
LocalDateTime newTime = alarmService.findNewTime();
// 获取当前时间
LocalDateTime currentTime = LocalDateTime.now();
// 计算两者之间的持续时间
Duration duration = Duration.between(newTime, currentTime);
// 判断时间差是否大于一小时
if (duration.toHours() > 1) {
// 调用短信接口
SmsUtil.sendSms("110原始接口超过1小时无数据", null);
}
// 获取数据库中最新的时间 // 获取数据库中最新的时间
LocalDateTime nowTime = alarmService.findNowTime(); LocalDateTime nowTime = alarmService.findNowTime();
log.info("==========================获取数据库中最新的时间{}", nowTime); log.info("==========================获取数据库中最新的时间{}", nowTime);
@ -167,7 +189,9 @@ public class AlarmQuartz {
req.setPageIndex(pageIndex); req.setPageIndex(pageIndex);
req.setPageSize(pageSize); req.setPageSize(pageSize);
List<String> data = alarmService.findDataIdList(req, token); List<String> data = alarmService.findDataIdList(req, token);
idList.addAll(data); if (CollectionUtil.isNotEmpty(data)) {
idList.addAll(data);
}
/* while (true) { /* while (true) {
List<String> data = alarmService.findDataIdList(req, token); List<String> data = alarmService.findDataIdList(req, token);
if (data == null) { if (data == null) {

@ -54,5 +54,12 @@ public interface AlarmService extends IService<Alarm> {
* @param list * @param list
*/ */
int saveAll(List<Alarm> list); int saveAll(List<Alarm> list);
/**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

@ -72,6 +72,9 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
.execute().body(); .execute().body();
log.info("==========================================="); log.info("===========================================");
log.info("++++++++++++++++" + body + "++++++++++++++"); log.info("++++++++++++++++" + body + "++++++++++++++");
if (body.contains("Time-out")) {
return null;
}
JSONObject json = JSONObject.parse(body); JSONObject json = JSONObject.parse(body);
Object response = json.get("response"); Object response = json.get("response");
if (BeanUtil.isNotEmpty(response)) { if (BeanUtil.isNotEmpty(response)) {
@ -183,5 +186,15 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements
public int saveAll(List<Alarm> list) { public int saveAll(List<Alarm> list) {
return baseMapper.saveAll(list); return baseMapper.saveAll(list);
} }
/**
*
*
* @return
*/
@Override
public LocalDateTime findNewTime() {
return baseMapper.findNewTime();
}
} }

@ -35,4 +35,11 @@
limit 1 limit 1
</select> </select>
<select id="findNewTime" resultType="java.time.LocalDateTime">
select create_time
from alarm
order by create_time desc
limit 1
</select>
</mapper> </mapper>

@ -22,6 +22,7 @@
<dependency> <dependency>
<groupId>com.yingji</groupId> <groupId>com.yingji</groupId>
<artifactId>utils</artifactId> <artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

@ -3,6 +3,7 @@ package com.yingji.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
/** /**
@ -20,5 +21,12 @@ public interface FireMapper extends BaseMapper<Fire> {
* @param list * @param list
*/ */
void saveAll(List<Fire> list); void saveAll(List<Fire> list);
/**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.utils.SmsUtil;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.service.FireService; import com.yingji.service.FireService;
@ -15,6 +16,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -45,6 +47,17 @@ public class FireQuartz {
@Scheduled(cron = "0 */5 * * * ? ") @Scheduled(cron = "0 */5 * * * ? ")
public void savaData() { public void savaData() {
log.info("119接口开始" + LocalDateTime.now()); log.info("119接口开始" + LocalDateTime.now());
// 获取数据库中最新的创建时间
LocalDateTime newTime = fireService.findNewTime();
// 获取当前时间
LocalDateTime currentTime = LocalDateTime.now();
// 计算两者之间的持续时间
Duration duration = Duration.between(newTime, currentTime);
// 判断时间差是否大于6小时
if (duration.toHours() > 6) {
// 调用短信接口
SmsUtil.sendSms("119原始接口超过6小时无数据", null);
}
QuartzLog quartzLog = new QuartzLog(); QuartzLog quartzLog = new QuartzLog();
quartzLog.setInterfaceName(2); quartzLog.setInterfaceName(2);
quartzLog.setCreateTime(LocalDateTime.now()); quartzLog.setCreateTime(LocalDateTime.now());
@ -57,18 +70,27 @@ public class FireQuartz {
// 110算法接口 // 110算法接口
if (CollectionUtil.isNotEmpty(list)) { if (CollectionUtil.isNotEmpty(list)) {
fireService.saveAll(list); fireService.saveAll(list);
for (Fire fire : list) { try {
link119Algorithm(fire.getId()); for (Fire fire : list) {
link119Algorithm(fire.getId());
}
} catch (Exception e) {
// 调用短信接口
SmsUtil.sendSms("119调用算法接口异常", null);
} }
} }
quartzLog.setStatus(1); quartzLog.setStatus(1);
log.info("119接口结束" + LocalDateTime.now()); log.info("119接口结束" + LocalDateTime.now());
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口
SmsUtil.sendSms("119原始接口获取token失败", null);
log.error("============119算法接口获取token失败119接口结束============="); log.error("============119算法接口获取token失败119接口结束=============");
} }
} catch (Exception e) { } catch (Exception e) {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口
SmsUtil.sendSms("119原始接口调用失败", null);
} finally { } finally {
quartzLogService.save(quartzLog); quartzLogService.save(quartzLog);
} }

@ -3,6 +3,7 @@ package com.yingji.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
/** /**
@ -17,7 +18,6 @@ public interface FireService extends IService<Fire> {
/** /**
* *
* *
* @param req
* @param token token * @param token token
* @return List * @return List
*/ */
@ -37,5 +37,12 @@ public interface FireService extends IService<Fire> {
* @param list * @param list
*/ */
void saveAll(List<Fire> list); void saveAll(List<Fire> list);
/**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

@ -70,6 +70,9 @@ public class FireServiceImpl extends ServiceImpl<FireMapper, Fire> implements Fi
.execute().body(); .execute().body();
log.info("==============================="); log.info("===============================");
log.info("119body数据为:" + body); log.info("119body数据为:" + body);
if (body.contains("Time-out")) {
return null;
}
try { try {
JSONObject json = JSONObject.parse(body); JSONObject json = JSONObject.parse(body);
Object response = json.get("response"); Object response = json.get("response");
@ -135,5 +138,15 @@ public class FireServiceImpl extends ServiceImpl<FireMapper, Fire> implements Fi
public void saveAll(List<Fire> list) { public void saveAll(List<Fire> list) {
baseMapper.saveAll(list); baseMapper.saveAll(list);
} }
/**
*
*
* @return
*/
@Override
public LocalDateTime findNewTime() {
return baseMapper.findNewTime();
}
} }

@ -18,4 +18,11 @@
</foreach> </foreach>
</insert> </insert>
<select id="findNewTime" resultType="java.time.LocalDateTime">
select create_time
from fire
order by create_time desc
limit 1
</select>
</mapper> </mapper>

@ -22,6 +22,7 @@
<dependency> <dependency>
<groupId>com.yingji</groupId> <groupId>com.yingji</groupId>
<artifactId>utils</artifactId> <artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

@ -3,6 +3,7 @@ package com.yingji.controller;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.utils.SmsUtil;
import com.yingji.base.controller.BaseController; import com.yingji.base.controller.BaseController;
import com.yingji.base.domain.AjaxResult; import com.yingji.base.domain.AjaxResult;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
@ -66,6 +67,15 @@ public class FireController extends BaseController {
return success(fireService.getById(id)); return success(fireService.getById(id));
} }
/**
*
*/
@GetMapping("/sendSms")
@ApiOperation(value = "发送短信")
public AjaxResult sendSms() {
SmsUtil.sendSms("发送短信测试", "17638176947");
return success();
}
} }

@ -92,36 +92,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build>
<!--打包成jar包时的名字-->
<finalName>algorithms</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.yingji.YingjiAlgorithmsApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

@ -22,6 +22,7 @@
<dependency> <dependency>
<groupId>com.yingji</groupId> <groupId>com.yingji</groupId>
<artifactId>utils</artifactId> <artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

@ -1,6 +1,7 @@
package com.yingji.quartz; package com.yingji.quartz;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.utils.SmsUtil;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.service.AccidentService; import com.yingji.service.AccidentService;
import com.yingji.service.QuartzLogService; import com.yingji.service.QuartzLogService;
@ -11,6 +12,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
/** /**
@ -31,8 +33,19 @@ public class AccidentQuartz {
private QuartzLogService quartzLogService; private QuartzLogService quartzLogService;
@Async @Async
@Scheduled(cron = "*/10 * * * * ? ") @Scheduled(cron = "0 */1 * * * ? ")
public void savaData() { public void savaData() {
// 获取数据库中最新的创建时间
LocalDateTime newTime = accidentService.findNewTime();
// 获取当前时间
LocalDateTime currentTime = LocalDateTime.now();
// 计算两者之间的持续时间
Duration duration = Duration.between(newTime, currentTime);
// 判断时间差是否大于24小时
if (duration.toHours() > 24) {
// 调用短信接口
SmsUtil.sendSms("120原始接口超过24小时无数据", null);
}
QuartzLog quartzLog = new QuartzLog(); QuartzLog quartzLog = new QuartzLog();
quartzLog.setInterfaceName(3); quartzLog.setInterfaceName(3);
quartzLog.setCreateTime(LocalDateTime.now()); quartzLog.setCreateTime(LocalDateTime.now());
@ -44,9 +57,13 @@ public class AccidentQuartz {
log.info("=================120定时任务结束==============="); log.info("=================120定时任务结束===============");
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口
SmsUtil.sendSms("120原始接口获取token失败", null);
log.info("=================120定时任务获取token失败==============="); log.info("=================120定时任务获取token失败===============");
} }
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口
SmsUtil.sendSms("120原始接口调用失败", null);
e.printStackTrace(); e.printStackTrace();
quartzLog.setStatus(2); quartzLog.setStatus(2);
} finally { } finally {

@ -1,5 +1,7 @@
package com.yingji.service; package com.yingji.service;
import java.time.LocalDateTime;
/** /**
* 120 * 120
* *
@ -26,5 +28,10 @@ public interface AccidentService {
void getAccidentUrl(String accessToken); void getAccidentUrl(String accessToken);
void text(); /**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

@ -7,6 +7,7 @@ import com.yingji.entity.dto.response.EventPageRequest;
import com.yingji.entity.dto.response.EventsFindResponse; import com.yingji.entity.dto.response.EventsFindResponse;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
/** /**
* (Event) * (Event)
@ -32,5 +33,12 @@ public interface EventService extends IService<Event> {
* @return * @return
*/ */
EventsFindResponse findById(Serializable id); EventsFindResponse findById(Serializable id);
/**
*
*
* @return
*/
LocalDateTime findNewTime();
} }

File diff suppressed because one or more lines are too long

@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -102,6 +103,20 @@ public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements
return res; return res;
} }
/**
*
*
* @return
*/
@Override
public LocalDateTime findNewTime() {
QueryWrapper<Event> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("create_time")
.last("LIMIT 1");
Event event = this.getOne(wrapper);
return event.getCreateTime();
}
} }

@ -2,20 +2,11 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.yingji</groupId> <groupId>com.yingji</groupId>
<artifactId>yingjiAlgorithms</artifactId> <artifactId>yingjiAlgorithms</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>utils</artifactId> <artifactId>utils</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project> </project>

@ -4,6 +4,8 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -15,6 +17,8 @@ import java.util.Map;
* @since 2024/9/24 10:18 * @since 2024/9/24 10:18
*/ */
public class SmsUtil { public class SmsUtil {
public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
/** /**
* token * token
@ -31,6 +35,8 @@ public class SmsUtil {
.execute() .execute()
.body(); .body();
JSONObject responseJson = JSONUtil.parseObj(responseStr); JSONObject responseJson = JSONUtil.parseObj(responseStr);
log.error("----------------------------短信token======================");
log.error("短信token: " + JSONUtil.toJsonStr(responseJson));
return (String) responseJson.get("data"); return (String) responseJson.get("data");
} }
@ -52,13 +58,16 @@ public class SmsUtil {
} else { } else {
bodyMap.put("tel", "18870257135,18261462112,15850922852,18112760590"); bodyMap.put("tel", "18870257135,18261462112,15850922852,18112760590");
} }
bodyMap.put("content", "市应急局提醒: " + content); bodyMap.put("content", "市应急局提醒" + content);
String bodyJson = JSONUtil.toJsonStr(bodyMap); String bodyJson = JSONUtil.toJsonStr(bodyMap);
log.error("bodyJson: " + bodyJson);
String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/send") String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/send")
.header("Authorization", token) .header("Authorization", "Bearer " + token)
.body(bodyJson) .body(bodyJson)
.execute() .execute()
.body(); .body();
log.error("----------------------------短信响应======================");
log.error("短信: " + JSONUtil.toJsonStr(responseStr));
return JSONUtil.parseObj(responseStr); return JSONUtil.parseObj(responseStr);
} }
} }

Loading…
Cancel
Save