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.fileupload.version>1.3.1</commons.fileupload.version>
<commons.io.version>2.5</commons.io.version> <commons.io.version>2.5</commons.io.version>
<commons.codec.version>1.10</commons.codec.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> <joda.time.version>2.9.9</joda.time.version>
<lombok.version>1.18.4</lombok.version> <lombok.version>1.18.4</lombok.version>
<swagger.version>2.7.0</swagger.version> <swagger.version>2.7.0</swagger.version>

View File

@ -208,6 +208,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){ if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson); JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim(); String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){ if(errLine.equals("empty")){
for (TenRecordEntity recordEntity : updateRecords) { for (TenRecordEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1); recordEntity.setXaSyncImage(1);
@ -226,10 +227,20 @@ 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);
}
}
}
} }
} }
private void processRecordCarEnterImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) { private void processRecordCarEnterImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) {
List<TenPackRecordEnterEntity> records = this.tenPackRecordEnterService.getNotSyncImage(cellId); List<TenPackRecordEnterEntity> records = this.tenPackRecordEnterService.getNotSyncImage(cellId);
List<XaTCCTCSBZPXX> syncRecords = new ArrayList(); List<XaTCCTCSBZPXX> syncRecords = new ArrayList();
List<TenPackRecordEnterEntity> updateRecords = new ArrayList<>(); List<TenPackRecordEnterEntity> updateRecords = new ArrayList<>();
@ -345,6 +356,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){ if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson); JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim(); String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){ if(errLine.equals("empty")){
for (TenPackRecordEnterEntity recordEntity : updateRecords) { for (TenPackRecordEnterEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1); recordEntity.setXaSyncImage(1);
@ -363,13 +375,23 @@ 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);
}
}
}
} }
} }
private void processRecordCarExitImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) { private void processRecordCarExitImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) {
List<TenPackRecordExitEntity> records = this.tenPackRecordExitService.getNotSyncImage(cellId); List<TenPackRecordExitEntity> records = this.tenPackRecordExitService.getNotSyncImage(cellId);
List<XaTCCTCSBZPXXEXIT> syncRecords = new ArrayList(); List<XaTCCTCSBZPXXEXIT> syncRecords = new ArrayList();
List<TenPackRecordExitEntity> updateRecords = new ArrayList<>(); List<TenPackRecordExitEntity> updateRecords = new ArrayList<>();
@ -491,6 +513,7 @@ public class XaImageTask implements ITask {
if(resJson.contains("ErrorLineParameter")){ if(resJson.contains("ErrorLineParameter")){
JSONObject resJsonObject = JSONObject.parseObject(resJson); JSONObject resJsonObject = JSONObject.parseObject(resJson);
String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim(); String errLine = resJsonObject.getJSONObject("sta").getString("ErrorLineParameter").trim();
JSONArray datas = resJsonObject.getJSONArray("datas");
if(errLine.equals("empty")){ if(errLine.equals("empty")){
for (TenPackRecordExitEntity recordEntity : updateRecords) { for (TenPackRecordExitEntity recordEntity : updateRecords) {
recordEntity.setXaSyncImage(1); recordEntity.setXaSyncImage(1);
@ -509,11 +532,21 @@ 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);
}
}
}
} }
} }
private void processRealPersonImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) { private void processRealPersonImage(String appId, String appSecret, String xqid, String fwikUrl, Long cellId) {
List<TenPersonEntity> persons = this.tenPersonService.getNotSyncImage(cellId); List<TenPersonEntity> persons = this.tenPersonService.getNotSyncImage(cellId);
List<XaRYRKPHOTO> syncPersons = new ArrayList(); List<XaRYRKPHOTO> syncPersons = new ArrayList();
List<TenPersonEntity> updatePersons = new ArrayList<>(); List<TenPersonEntity> updatePersons = new ArrayList<>();

View File

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

View File

@ -13,5 +13,6 @@ import java.util.Map;
public interface SysDeviceService extends IService<SysDeviceEntity> { public interface SysDeviceService extends IService<SysDeviceEntity> {
PageUtils queryPage(Map<String, Object> params); 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.entity.TenDeviceEntity;
import net.shapelight.modules.ten.service.*; import net.shapelight.modules.ten.service.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Map; import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -75,4 +76,11 @@ public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceDao, SysDeviceEnt
return new PageUtils(page); 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.io.Serializable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
@ -120,8 +117,6 @@ public class TenDeviceServiceImpl extends ServiceImpl<TenDeviceDao, TenDeviceEnt
// }else{ // }else{
// dev.setStatus(0); // dev.setStatus(0);
// } // }
Boolean flag = deviceApiService.isOnline(dev.getSn()); Boolean flag = deviceApiService.isOnline(dev.getSn());
if (flag) { if (flag) {
dev.setStatus(1); dev.setStatus(1);
@ -129,7 +124,25 @@ public class TenDeviceServiceImpl extends ServiceImpl<TenDeviceDao, TenDeviceEnt
dev.setStatus(0); 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); return new PageUtils(page);
} }

View File

@ -143,6 +143,9 @@ public class TenRecordServiceImpl implements TenRecordService {
@Override @Override
public PageUtils queryPageRoomRecord(Map<String, Object> params) { public PageUtils queryPageRoomRecord(Map<String, Object> params) {
Long tenantId = (Long)params.get("tenantId");
Page pageParam = new Page(); Page pageParam = new Page();
pageParam.setCurrent(Long.parseLong((String) params.get("page"))); pageParam.setCurrent(Long.parseLong((String) params.get("page")));
pageParam.setSize(Long.parseLong((String) params.get("limit"))); 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) { public static String httpPOSTJson(String url, String json, String serviceId, String serviceValue, String appid, String secre, String tranId) {
String data = ""; String data = "";
HttpURLConnection conn = null;
try { try {
URL dataurl = new URL(url); URL dataurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) dataurl.openConnection(); conn = (HttpURLConnection) dataurl.openConnection();
try { try {
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
String currdate = new SimpleDateFormat("yyyyMMdd").format(new Date()); String currdate = new SimpleDateFormat("yyyyMMdd").format(new Date());
@ -121,6 +122,12 @@ public class XaApi {
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setDoInput(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"); // String params = "appid=" + URLEncoder.encode(appid, "UTF-8") + "&appsecret=" + URLEncoder.encode(secre, "UTF-8");
// OutputStream outputStream = conn.getOutputStream(); // OutputStream outputStream = conn.getOutputStream();
// outputStream.write(params.getBytes()); // outputStream.write(params.getBytes());
@ -146,7 +153,12 @@ public class XaApi {
is.close(); is.close();
dis.close(); dis.close();
} catch (Exception e) { } catch (Exception e) {
log.error("请求失败"+url+"\n"+e.getMessage());
data = ""; data = "";
} finally {
if(conn!=null){
conn.disconnect();
}
} }
return data; return data;
} }

View File

@ -381,7 +381,9 @@
select r.*,t.name from ten_record_${params.tenantId} r select r.*,t.name from ten_record_${params.tenantId} r
left join ten_person t on r.person_id = t.person_id 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!=''"> <if test="params.cellId != null and params.cellId!=''">
and r.cell_id = #{params.cellId} and r.cell_id = #{params.cellId}
</if> </if>
@ -398,6 +400,7 @@
and `record_time` &lt; #{params.recordTimeEnd} and `record_time` &lt; #{params.recordTimeEnd}
</if> </if>
order by record_time desc order by record_time desc
</select> </select>