1.优化心跳更新设备表sql慢的问题,用redis来存状态信息
This commit is contained in:
gaoben 2021-07-09 16:12:50 +08:00
parent 3ca221446f
commit 52866c212e
9 changed files with 94 additions and 15 deletions

View File

@ -38,7 +38,7 @@
<commons.fileupload.version>1.3.1</commons.fileupload.version>
<commons.io.version>2.5</commons.io.version>
<commons.codec.version>1.10</commons.codec.version>
<fastjson.version>1.2.58</fastjson.version>
<fastjson.version>1.2.75</fastjson.version>
<joda.time.version>2.9.9</joda.time.version>
<lombok.version>1.18.4</lombok.version>
<swagger.version>2.7.0</swagger.version>

View File

@ -208,6 +208,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){
for (TenRecordEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1);
@ -226,6 +227,16 @@ public class XaImageTask implements ITask {
}
}
}
if(datas!=null){
for(int i = 0;i<datas.size();i++){
JSONObject resData = (JSONObject) datas.get(i);
if(((String)resData.get("Result")).indexOf("请不要重复申报")>0){
TenRecordEntity recordEntity = updateRecords.get(i);
recordEntity.setXaSyncImage(1);
tenRecordService.updateById(recordEntity);
}
}
}
}
}
@ -345,6 +356,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){
for (TenPackRecordEnterEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1);
@ -363,6 +375,16 @@ public class XaImageTask implements ITask {
}
}
}
if(datas!=null){
for(int i = 0;i<datas.size();i++){
JSONObject resData = (JSONObject) datas.get(i);
if(((String)resData.get("Result")).indexOf("请不要重复申报")>0){
TenPackRecordEnterEntity recordEntity = updateRecords.get(i);
recordEntity.setXaSyncImage(1);
tenPackRecordEnterService.updateById(recordEntity);
}
}
}
}
}
@ -491,6 +513,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){
for (TenPackRecordExitEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1);
@ -509,6 +532,16 @@ public class XaImageTask implements ITask {
}
}
}
if(datas!=null){
for(int i = 0;i<datas.size();i++){
JSONObject resData = (JSONObject) datas.get(i);
if(((String)resData.get("Result")).indexOf("请不要重复申报")>0){
TenPackRecordExitEntity recordEntity = updateRecords.get(i);
recordEntity.setXaSyncImage(1);
tenPackRecordExitService.updateById(recordEntity);
}
}
}
}
}

View File

@ -59,6 +59,8 @@ public class DeviceApiServiceImpl implements DeviceApiService {
private SysDeviceService sysDeviceService;
@Autowired
private TenDoorCardService tenDoorCardService;
@Autowired
private RedisUtils redisUtils;
/*
@ -384,9 +386,12 @@ public class DeviceApiServiceImpl implements DeviceApiService {
}
//保存人脸个数
deviceEntity.setFaceCount(faceCount);
deviceEntity.setLastUpdateTime(new Date());
tenDeviceService.evictupdateById(deviceEntity);
// deviceEntity.setFaceCount(faceCount);
// deviceEntity.setLastUpdateTime(new Date());
// tenDeviceService.evictupdateById(deviceEntity);
//缓存
redisUtils.set("HearTBeat-"+sn,faceCount+"="+DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN),31536000l);//保存一年
TenCellEntity cellEntity = tenCellService.getById(deviceEntity.getCellId());
@ -521,6 +526,7 @@ public class DeviceApiServiceImpl implements DeviceApiService {
SysDeviceEntity sysDeviceEntity = sysDeviceService.getOne(new QueryWrapper<SysDeviceEntity>()
.eq("sn",sn));
// SysDeviceEntity sysDeviceEntity = sysDeviceService.getBySn(sn);
if(sysDeviceEntity.getState() == 0){
Result res = Result.error(202, "Device Is Stop");
String resContent = JSONObject.toJSONString(res);

View File

@ -13,5 +13,6 @@ import java.util.Map;
public interface SysDeviceService extends IService<SysDeviceEntity> {
PageUtils queryPage(Map<String, Object> params);
SysDeviceEntity getBySn(String sn);
}

View File

@ -15,6 +15,7 @@ import net.shapelight.modules.ten.dao.TenDeviceDao;
import net.shapelight.modules.ten.entity.TenDeviceEntity;
import net.shapelight.modules.ten.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -75,4 +76,11 @@ public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceDao, SysDeviceEnt
return new PageUtils(page);
}
@Override
@Cacheable(value = "SysDevice", key = "#sn")
public SysDeviceEntity getBySn(String sn) {
SysDeviceEntity sysDeviceEntity = this.getOne(new QueryWrapper<SysDeviceEntity>()
.eq("sn",sn));
return sysDeviceEntity;
}
}

View File

@ -19,10 +19,7 @@ import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -120,8 +117,6 @@ public class TenDeviceServiceImpl extends ServiceImpl<TenDeviceDao, TenDeviceEnt
// }else{
// dev.setStatus(0);
// }
Boolean flag = deviceApiService.isOnline(dev.getSn());
if (flag) {
dev.setStatus(1);
@ -129,7 +124,25 @@ public class TenDeviceServiceImpl extends ServiceImpl<TenDeviceDao, TenDeviceEnt
dev.setStatus(0);
}
//缓存
// redisUtils.set("HearTBeat-"+sn,faceCount+"="+DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN),31536000l);//保存一年
try {
if(redisUtils.get("HearTBeat-"+dev.getSn())!=null){
String devCache = (String)redisUtils.get("HearTBeat-"+dev.getSn());
if(devCache!=null){
String[] infs = devCache.split("=");
dev.setFaceCount(Integer.parseInt(infs[0]));
dev.setLastUpdateTime(DateUtils.stringToDate(infs[1],DateUtils.DATE_TIME_PATTERN));
}
}else{
dev.setFaceCount(null);
dev.setLastUpdateTime(null);
}
}catch (Exception e){
e.printStackTrace();
log.error(e.getMessage());
}
}
return new PageUtils(page);
}

View File

@ -143,6 +143,9 @@ public class TenRecordServiceImpl implements TenRecordService {
@Override
public PageUtils queryPageRoomRecord(Map<String, Object> params) {
Long tenantId = (Long)params.get("tenantId");
Page pageParam = new Page();
pageParam.setCurrent(Long.parseLong((String) params.get("page")));
pageParam.setSize(Long.parseLong((String) params.get("limit")));

View File

@ -97,10 +97,11 @@ public class XaApi {
*/
public static String httpPOSTJson(String url, String json, String serviceId, String serviceValue, String appid, String secre, String tranId) {
String data = "";
HttpURLConnection conn = null;
try {
URL dataurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) dataurl.openConnection();
conn = (HttpURLConnection) dataurl.openConnection();
try {
conn.setRequestMethod("POST");
String currdate = new SimpleDateFormat("yyyyMMdd").format(new Date());
@ -121,6 +122,12 @@ public class XaApi {
conn.setDoOutput(true);
conn.setDoInput(true);
//连接建立超时时间还有读取数据超时时间
conn.setConnectTimeout(600000);
conn.setReadTimeout(600000);
conn.connect();
// String params = "appid=" + URLEncoder.encode(appid, "UTF-8") + "&appsecret=" + URLEncoder.encode(secre, "UTF-8");
// OutputStream outputStream = conn.getOutputStream();
// outputStream.write(params.getBytes());
@ -146,7 +153,12 @@ public class XaApi {
is.close();
dis.close();
} catch (Exception e) {
log.error("请求失败"+url+"\n"+e.getMessage());
data = "";
} finally {
if(conn!=null){
conn.disconnect();
}
}
return data;
}

View File

@ -381,7 +381,9 @@
select r.*,t.name from ten_record_${params.tenantId} r
left join ten_person t on r.person_id = t.person_id
where 1=1
left join ten_device d on r.device_sn = d.sn
where d.tenant_id = #{params.tenantId}
and d.delete_flag = 0
<if test="params.cellId != null and params.cellId!=''">
and r.cell_id = #{params.cellId}
</if>
@ -398,6 +400,7 @@
and `record_time` &lt; #{params.recordTimeEnd}
</if>
order by record_time desc
</select>