增加定时任务,实现设备离线判断、数据库定期清理等功能
This commit is contained in:
parent
a34b83c1a2
commit
588a47e94f
@ -1,10 +1,14 @@
|
||||
package com.imdroid.secapi.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
|
||||
@Mapper
|
||||
public interface GnssMsgMapper extends BaseMapper<GnssMsg> {
|
||||
|
||||
@Delete({"delete from gnssmsg where createtime <= #{t}"})
|
||||
int deleteTimeBefore(Timestamp t);
|
||||
}
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
package com.imdroid.secapi.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@Mapper
|
||||
public interface GnssRawDataMapper extends BaseMapper<GnssRawData> {
|
||||
|
||||
@Delete({"delete from gnssrawdata where createtime <= #{t}"})
|
||||
int deleteTimeBefore(Timestamp t);
|
||||
}
|
||||
|
||||
@ -32,5 +32,6 @@ public class GnssStatusJoin {
|
||||
Short warning;
|
||||
|
||||
Short devicetype;
|
||||
Integer group_id;
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,9 @@ package com.imdroid.secapi.dto;
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Mapper
|
||||
@ -12,6 +15,12 @@ public interface GnssStatusMapper extends MPJBaseMapper<GnssStatus> {
|
||||
@Select({"select * from gnssstatus where deviceid = #{deviceId} limit 1"})
|
||||
GnssStatus getByDeviceId(String deviceId);
|
||||
|
||||
@Update({"update gnssstatus set state=0,warning=#{warning},warningcode=#{warningcode} where deviceid=#{deviceid}"})
|
||||
int setOfflineByDeviceId(GnssStatusJoin status);
|
||||
|
||||
@Select({"select s.*, d.devicetype, d.group_id from gnssstatus s ,gnssdevices d where s.deviceid=d.deviceid and s.state <> 0"})
|
||||
List<GnssStatusJoin> queryOnline();
|
||||
|
||||
// 需要关联设备类型
|
||||
@Select({"select s.*, d.devicetype from gnssstatus s ,gnssdevices d where s.deviceid=d.deviceid and s.deviceid = #{deviceId}"})
|
||||
GnssStatusJoin queryByDeviceId(String deviceId);
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
package com.imdroid.secapi.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
|
||||
@Mapper
|
||||
public interface GnssStatusMsgMapper extends BaseMapper<GnssStatusMsg> {
|
||||
|
||||
@Delete({"delete from gnssstatusmsg where createtime <= #{t}"})
|
||||
int deleteTimeBefore(Timestamp t);
|
||||
}
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
package com.imdroid.secapi.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
|
||||
@Mapper
|
||||
public interface GnssTrxMsgMapper extends BaseMapper<GnssTrxMsg> {
|
||||
|
||||
@Delete({"delete from gnsstrxmsg where createtime <= #{t}"})
|
||||
int deleteTimeBefore(Timestamp t);
|
||||
}
|
||||
|
||||
@ -0,0 +1,113 @@
|
||||
package com.imdroid.beidou.config;
|
||||
|
||||
import com.imdroid.secapi.dto.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/***
|
||||
* 定时任务:
|
||||
* 1、定时清理消息表(缺省保留3个月)
|
||||
* 2、定时清理原始数据表(缺省保留1个月)
|
||||
* 3、定时清理操作日志(缺省保留1年)
|
||||
* 3、定时检查设备是否在线
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class SchedulingConfig {
|
||||
@Autowired
|
||||
GnssStatusMapper gnssStatusMapper;
|
||||
@Autowired
|
||||
GnssMsgMapper gnssMsgMapper;
|
||||
@Autowired
|
||||
GnssStatusMsgMapper statusMsgMapper;
|
||||
@Autowired
|
||||
GnssTrxMsgMapper trxMsgMapper;
|
||||
@Autowired
|
||||
GnssRawDataMapper rawDataMapper;
|
||||
|
||||
@Autowired
|
||||
GnssGroupMapper groupMapper;
|
||||
|
||||
@Autowired
|
||||
WarningMsgMapper warningMsgMapper;
|
||||
|
||||
|
||||
//cron表达式格式:
|
||||
//{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
|
||||
|
||||
@Scheduled(cron = "0 10 0 * * *") // 每天凌晨执行一次(0:10:0)
|
||||
//@Scheduled(cron = "0 */2 * * * ?") // 每60分钟执行一次
|
||||
//@Scheduled(cron = "*/5 * * * * ?") // 每5秒执行一次
|
||||
public void dayTask() {
|
||||
checkMsgDataset();
|
||||
checkRawDataset();
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 18 * * * ?") // 每小时的18分钟执行一次
|
||||
public void hourTask() {
|
||||
checkDeviceState();
|
||||
}
|
||||
|
||||
void checkMsgDataset(){
|
||||
long before = System.currentTimeMillis() -
|
||||
(long)90 * 24 * 3600 * 1000;
|
||||
Timestamp t = new Timestamp(before);
|
||||
int count = gnssMsgMapper.deleteTimeBefore(t);
|
||||
log.info("clean msg dataset num: "+count);
|
||||
count = statusMsgMapper.deleteTimeBefore(t);
|
||||
log.info("clean status msg dataset num: "+count);
|
||||
count = trxMsgMapper.deleteTimeBefore(t);
|
||||
log.info("clean trx msg dataset num: "+count);
|
||||
}
|
||||
|
||||
void checkRawDataset(){
|
||||
long before = System.currentTimeMillis() -
|
||||
(long)30 * 24 * 3600 * 1000;
|
||||
Timestamp t = new Timestamp(before);
|
||||
int count = rawDataMapper.deleteTimeBefore(t);
|
||||
log.info("clean raw dataset num: "+count);
|
||||
}
|
||||
|
||||
|
||||
void checkDeviceState(){
|
||||
List<GnssGroup> groupCfgs = groupMapper.selectList(null);
|
||||
HashMap<Integer,Integer> group_cycle_map=new HashMap<>();
|
||||
for(GnssGroup g:groupCfgs){
|
||||
group_cycle_map.put(g.getId(), g.getWork_cycle());
|
||||
}
|
||||
|
||||
long cur_time = System.currentTimeMillis();
|
||||
List<GnssStatusJoin> deviceStatuses = gnssStatusMapper.queryOnline();
|
||||
for(GnssStatusJoin status : deviceStatuses){
|
||||
//如果上次上线到现在超过两个周期,则认为掉线了
|
||||
if(cur_time>status.getUpdatetime().getTime()+
|
||||
group_cycle_map.get(status.getGroup_id())*1000*2){
|
||||
status.setState(GnssStatus.STATE_OFFLINE);
|
||||
if(status.getWarningcode()==null) status.setWarningcode(0);
|
||||
status.setWarningcode(status.getWarningcode() | WarningCfg.TYPE_DEVICE_OFF_LINE);
|
||||
status.setWarning(WarningCfg.LEVEL_2);
|
||||
gnssStatusMapper.setOfflineByDeviceId(status);
|
||||
//告警消息
|
||||
WarningMsg warningMsg = new WarningMsg();
|
||||
warningMsg.setDevicetype(WarningCfg.TYPE_GNSS);
|
||||
warningMsg.setTenantid(status.getTenantid());
|
||||
warningMsg.setDeviceid(status.getDeviceid());
|
||||
warningMsg.setCreatetime(new Date());
|
||||
warningMsg.setLevel(WarningCfg.LEVEL_2);
|
||||
warningMsg.setInfo(WarningCfg.TYPE_NAME_DEVICE_OFF_LINE);
|
||||
warningMsg.setCode(WarningCfg.TYPE_DEVICE_OFF_LINE);
|
||||
warningMsgMapper.insert(warningMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -27,21 +27,21 @@ public class APIController extends BasicController{
|
||||
@PostMapping(value = "/api/config_ack")
|
||||
@ResponseBody
|
||||
public String onConfigAck(String deviceId, String configAck) {
|
||||
GnssDevice device = deviceMapper.queryByDeviceId(deviceId);
|
||||
if(device == null) return null;
|
||||
|
||||
int msgType = Integer.parseInt(configAck.substring(0,4),16);
|
||||
// 配置是否成功
|
||||
if(msgType == 0xd311){
|
||||
//最后一个字节为1表示配置成功,0表示配置失败
|
||||
if(configAck.endsWith("01")){
|
||||
GnssDevice device = deviceMapper.queryByDeviceId(deviceId);
|
||||
if(device!=null){
|
||||
device.setSyn(true);
|
||||
deviceMapper.updateById(device);
|
||||
}
|
||||
device.setSyn(true);
|
||||
deviceMapper.updateById(device);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存
|
||||
saveMsg(deviceId, msgType, configAck);
|
||||
saveMsg(deviceId, device.getTenantid(),msgType, configAck);
|
||||
|
||||
// 命令行显示
|
||||
String rxInfo = "RX "+ dateFormat.format(System.currentTimeMillis())+
|
||||
@ -73,7 +73,7 @@ public class APIController extends BasicController{
|
||||
if(config != null){
|
||||
rtcmClient.config(deviceId, config);
|
||||
// 保存
|
||||
saveMsg(deviceId, 0xd311, config);
|
||||
saveMsg(deviceId, device.getTenantid(),0xd311, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,9 +89,10 @@ public class APIController extends BasicController{
|
||||
return null;
|
||||
}
|
||||
|
||||
void saveMsg(String deviceId, int msgType, String content){
|
||||
void saveMsg(String deviceId, int tenantId, int msgType, String content){
|
||||
GnssMsg gnssMsg = new GnssMsg();
|
||||
gnssMsg.setCreatetime(new Date());
|
||||
gnssMsg.setTenantid(tenantId);
|
||||
gnssMsg.setDeviceid(deviceId);
|
||||
gnssMsg.setMsgtype(msgType);
|
||||
if(content==null) content="";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user