Commit 3e0454a1 by levphon

设备监控-新增设备有效位置校验

1 parent 17796168
package cn.com.glsx.devicemonitor.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "eshield")
public class EShieldConfig {
private String appId;
private String appSecret;
private String apiUrl;
}
package cn.com.glsx.devicemonitor.modules.components;
import cn.com.glsx.devicemonitor.config.EShieldConfig;
import cn.com.glsx.devicemonitor.modules.model.request.AbstractEShieldRequest;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
@Slf4j
@Component
public class EShieldComponent {
@Autowired
private EShieldConfig config;
// /**
// * 调用虎哥d盾
// */
// public <T> T access(String subRoute, Object req, Class<T> clazz) {
// String url = config.getApiUrl() + subRoute;
// return accessUrl(url, req, clazz);
// }
// /**
// * 调用虎哥d盾
// */
// private <T> T accessUrl(String url, Object req, Class<T> clazz) {
//
// AbstractEShieldRequest paramReq = (AbstractEShieldRequest) req;
//
// // 1. 补全全局参数
// complementedGlobalParams(paramReq);
//
// // 2. 校验具体业务参数
// verifyRequestData(paramReq);
//
// // 3. 网络请求,获取结果
// return realCall(url, paramReq, clazz);
// }
//
// /**
// * 补全公共参数值
// *
// * @param req
// */
// public void complementedGlobalParams(AbstractEShieldRequest req) {
//
// }
//
// /**
// * 校验请求实体的通用属性,比如appkey属性及sign属性。
// * 也可以通过 instanceof,针对特定请求实体进行校验操作。
// */
// private void verifyRequestData(AbstractEShieldRequest req) {
//
// }
//
// /**
// * 真正调用网络请求方法
// *
// * @param url
// * @param paramsMap
// * @param clazz
// * @param <T>
// * @return
// */
// private <T> T realCall(String url, Map<String, Object> paramsMap, Class<T> clazz) {
// log.info("【虎哥d盾】http请求信息—— url[{}] params[{}]", url, paramsMap.toString());
// String result = HttpUtils.post(url, paramsMap);
// log.info("【虎哥d盾】http响应信息—— result[{}]", result);
// return (T) JSONObject.toJavaObject(JSONObject.parseObject(result), clazz);
// }
}
package cn.com.glsx.devicemonitor.modules.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Data
@Table(name = "t_device_location")
public class DeviceLocation implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@Id
private Long id;
/**
* 设备号
*/
private String sn;
/**
* 最后gps时间
*/
@Column(name = "last_gps_time")
private Date lastGpsTime;
/**
* 最后基站时间
*/
@Column(name = "last_bs_time")
private Date lastBsTime;
/**
* 创建时间
*/
@Column(name = "created_time")
private Date createdTime;
/**
* 更新时间
*/
@Column(name = "updated_time")
private Date updatedTime;
}
\ No newline at end of file
package cn.com.glsx.devicemonitor.modules.mapper;
import cn.com.glsx.devicemonitor.modules.entity.DeviceLocation;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
@Mapper
public interface DeviceLocationMapper extends CommonBaseMapper<DeviceLocation> {
DeviceLocation selectBySn(@Param("sn") String sn);
int updateLastGpsTimeBySn(@Param("sn") String sn, @Param("lastGpsTime") Date lastGpsTime);
}
\ No newline at end of file
package cn.com.glsx.devicemonitor.modules.model.request;
import lombok.Data;
@Data
public class AbstractEShieldRequest {
}
package cn.com.glsx.devicemonitor.modules.model.response;
import lombok.Data;
@Data
public class AbstractEShieldResponse {
private String returnCode;
private String message;
}
package cn.com.glsx.devicemonitor.modules.model.response;
import lombok.Data;
@Data
public class GetDeviceClassifyNameResp extends AbstractEShieldResponse {
private String data;
}
package cn.com.glsx.devicemonitor.modules.model.response;
import cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO;
import lombok.Data;
@Data
public class GetEShieldTrackInfoResp extends AbstractEShieldResponse {
private TrackInfoDTO data;
}
package cn.com.glsx.devicemonitor.modules.service.impl;
import cn.com.glsx.devicemonitor.config.EShieldConfig;
import cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO;
import cn.com.glsx.devicemonitor.modules.model.response.GetDeviceClassifyNameResp;
import cn.com.glsx.devicemonitor.modules.model.response.GetEShieldTrackInfoResp;
import cn.com.glsx.devicemonitor.modules.utils.GopUtil;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.TreeMap;
@Slf4j
@Service
public class EShieldService {
@Autowired
private EShieldConfig config;
/**
* 调用e盾标准接口获取设备信息
*
* @param sn
*/
public TrackInfoDTO getDeviceBySn(String sn) {
String url = config.getApiUrl() + "/api/device/getInfoBySn";
TreeMap<String, Object> tparams = new TreeMap<>();
tparams.put("appKey", config.getAppId());
tparams.put("sn", sn);
//加密得到sign
String sign = GopUtil.sign(tparams, config.getAppSecret());
tparams.put("sign", sign);
String result = HttpUtils.get(url, tparams);
log.info("getInfoBySn result {}", result);
GetEShieldTrackInfoResp resp = JSONObject.parseObject(result, GetEShieldTrackInfoResp.class);
if (!"0".equals(resp.getReturnCode())) {
log.info("remote getInfoBysn fail! returnCode:" + resp.getReturnCode() + "; message:" + resp.getMessage());
return null;
}
return resp.getData();
}
/**
* 调用e盾标准接口获取设备型号
*
* @param sn 设备号
*/
public String getDeviceClassifyNameBySn(String sn) {
String url = config.getApiUrl() + "/api/device/getDeviceClassifyNameBysn";
TreeMap<String, Object> tparams = new TreeMap<>();
tparams.put("appKey", config.getAppId());
tparams.put("sn", sn);
//加密得到sign
String sign = GopUtil.sign(tparams, config.getAppSecret());
tparams.put("sign", sign);
String result = HttpUtils.get(url, tparams);
log.info("getDeviceClassifyNameBySn result " + result);
GetDeviceClassifyNameResp resp = JSONObject.parseObject(result, GetDeviceClassifyNameResp.class);
if (!"0".equals(resp.getReturnCode())) {
log.info("remote getInfoBysn fail! returnCode:" + resp.getReturnCode() + "; message:" + resp.getMessage());
return null;
}
return resp.getData();
}
}
......@@ -9,29 +9,34 @@ import cn.com.glsx.devicemonitor.api.enums.DeviceTypeRelationEnum;
import cn.com.glsx.devicemonitor.api.enums.GeoLocationEnum;
import cn.com.glsx.devicemonitor.modules.components.HdaoGpsComponent;
import cn.com.glsx.devicemonitor.modules.converter.DataLocationConvert;
import cn.com.glsx.devicemonitor.modules.entity.Device;
import cn.com.glsx.devicemonitor.modules.entity.DeviceLocation;
import cn.com.glsx.devicemonitor.modules.entity.inner.DataGpsLocation;
import cn.com.glsx.devicemonitor.modules.mapper.DeviceLocationMapper;
import cn.com.glsx.devicemonitor.modules.mapper.DeviceMapper;
import cn.com.glsx.devicemonitor.modules.model.BaseStationModel;
import cn.com.glsx.devicemonitor.modules.model.BsInfoModel;
import cn.com.glsx.devicemonitor.modules.model.MobileBaseModel;
import cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO;
import cn.com.glsx.devicemonitor.modules.service.GpsBsLocationService;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.context.utils.PropertiesUtils;
import com.glsx.plat.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
......@@ -45,9 +50,18 @@ import java.util.stream.Collectors;
@Service
public class GpsBsLocationServiceImpl implements GpsBsLocationService {
@Resource
private DeviceLocationMapper deviceLocationMapper;
@Autowired
private HdaoGpsComponent hdaoGpsComponent;
@Autowired
private EShieldService eShieldService;
@Autowired
private DeviceMapper deviceMapper;
@Override
public List<GpsBsData> getLastGpsBsBatch(GpsBsLastBatchRequest gpsBsLastBatchRequest) {
// 1、调用ICE批量获取数据
......@@ -114,12 +128,24 @@ public class GpsBsLocationServiceImpl implements GpsBsLocationService {
@Override
public List<GpsBsData> check(DeviceLocationRequest locationRequest) {
//设备信息校验
checkDevice(locationRequest.getDevices());
Map<String, String> snMap = locationRequest.getDevices().stream().collect(Collectors.toMap(DeviceLocationItem::getSn, DeviceLocationItem::getDeviceCode));
Map<String, GpsBsData> gpsBsDataMap = new HashMap<>();
for (DeviceLocationItem item : locationRequest.getDevices()) {
String sn = item.getSn();
TrackInfoDTO trackInfoDTO = eShieldService.getDeviceBySn(sn);
if (trackInfoDTO == null) {
throw BusinessException.create(String.format("设备%s不存在", sn));
}
if (trackInfoDTO.getIsActive() == 1) {
throw BusinessException.create(String.format("设备%s已激活", sn));
}
GpsBsData gpsBsData = gpsBsDataMap.get(sn);
if (gpsBsData == null) {
gpsBsData = new GpsBsData();
......@@ -140,23 +166,21 @@ public class GpsBsLocationServiceImpl implements GpsBsLocationService {
if (locationRequest.getVerifyTimeFlag()) {
//GPS有效定位验证
boolean gpsLocationVerify = isGpsBsDataValid(locationRequest.getGpsValidMinute(), gpsLocation);
log.info("{} GPS gpsLocationVerify:{}", sn, gpsLocationVerify);
//基站有效定位验证
boolean gmsLocationVerify = isGpsBsDataValid(locationRequest.getBsValidMinute(), bsLocation);
log.info("{} GMS gmsLocationVerify:{}", sn, gmsLocationVerify);
// TODO: 2021/12/15 待确认,新e盾激活是否也基站激活 ,回填GPS位置点等
if (!gpsLocationVerify) {
throw BusinessException.create("设备GPS定位验证失败");
}
if (!gmsLocationVerify) {
throw BusinessException.create("设备基站定位验证失败");
// 新e盾激活是否也基站激活 ,回填GPS位置点等
if (gpsLocationVerify || gmsLocationVerify) {
log.info("设备{} GPS验证{},基站验证{}", sn, gpsLocationVerify, gmsLocationVerify);
if (!gpsLocationVerify && locationRequest.getBsBackfillGps()) {
fillGpsData(sn, gpsBsData.getBsLocation());
}
}
}
}
//验证占没用到
if (locationRequest.getVerifyDistanceFlag()) {
//子母分离验证
boolean distanceVerify = isDistanceValid(locationRequest, gpsBsDataMap);
......@@ -169,6 +193,24 @@ public class GpsBsLocationServiceImpl implements GpsBsLocationService {
return new ArrayList<>(gpsBsDataMap.values());
}
/**
* 检验设备参数
*
* @param devices
* @return
*/
private void checkDevice(List<DeviceLocationItem> devices) {
if (CollectionUtils.isEmpty(devices)) {
throw BusinessException.create("传入的设备信息为空");
}
devices.forEach(dli -> {
Device device = deviceMapper.selectBySn(dli.getSn());
if (Objects.equals(device.getSource(), dli.getWireType())) {
throw BusinessException.create("设备有线无线类型校验有误");
}
});
}
private boolean isGpsBsDataValid(int validMinute, DataGpsLocation location) {
log.info("{} 位置上点时间:{}", location.getSn(), location.getDateTime());
ZonedDateTime nowTime = ZonedDateTime.now();
......@@ -188,6 +230,37 @@ public class GpsBsLocationServiceImpl implements GpsBsLocationService {
}
/**
* 基站激活时,gps位置点数据回填
*
* @param sn
* @param bsLocation
*/
@Async
public void fillGpsData(String sn, GpsLocation bsLocation) {
Date gpsBsTime = DateUtils.parse(bsLocation.getDateTime());
DeviceLocation location = deviceLocationMapper.selectBySn(sn);
if (location != null) {
deviceLocationMapper.updateLastGpsTimeBySn(sn, gpsBsTime);
} else {
location = new DeviceLocation();
location.setSn(sn);
location.setLastGpsTime(gpsBsTime);
location.setLastBsTime(gpsBsTime);
location.setCreatedTime(new Date());
deviceLocationMapper.insert(location);
}
String url = PropertiesUtils.getProperty("fill.gpsdata.tobigdata.url");//测试地址:"http://192.168.3.236:40100/gpsmsg/SetGpsByBs";
Map<String, Object> param = new TreeMap<>();
param.put("DeviceNO", sn);
param.put("lng", bsLocation.getBlng());
param.put("lat", bsLocation.getBlat());
param.put("time", String.valueOf(gpsBsTime.getTime() / 1000));
String fillResult = HttpUtils.get(url, param);
log.info(sn + " 大数据侧同步GPS位置点数据返回:" + fillResult);
}
/**
* WiFi定位数据
*
* @param sn
......
......@@ -45,8 +45,4 @@ spring.cloud.nacos.config.shared-configs[0].refresh=true
#\u6570\u636E\u5E93\u3001redis\u7B49\u516C\u5171\u914D\u7F6E
spring.cloud.nacos.config.shared-configs[1].group=NESHIELD_DEV_GROUP
spring.cloud.nacos.config.shared-configs[1].data-id=glsx-ne-shield-share-dev.properties
spring.cloud.nacos.config.shared-configs[1].refresh=true
swagger2.basePackage=cn.com.glsx.devicemonitor.modules
server.jetty.max-http-form-post-size=9000000B
\ No newline at end of file
spring.cloud.nacos.config.shared-configs[1].refresh=true
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.devicemonitor.modules.mapper.DeviceLocationMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.devicemonitor.modules.entity.DeviceLocation">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="sn" jdbcType="VARCHAR" property="sn"/>
<result column="last_gps_time" jdbcType="TIMESTAMP" property="lastGpsTime"/>
<result column="last_bs_time" jdbcType="TIMESTAMP" property="lastBsTime"/>
<result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
<result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
-->
id, sn, last_gps_time, last_bs_time, created_time, updated_time
</sql>
<select id="selectBySn" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_device_location
where sn = #{sn}
limit 1
</select>
<update id="updateLastGpsTimeBySn">
update t_device_location
set last_gps_time = #{lastGpsTime},
updated_time = sysdate()
where sn = #{sn}
</update>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!