|
@@ -0,0 +1,218 @@
|
|
|
+package com.ebei.screen.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.ebei.screen.common.response.ResponseBean;
|
|
|
+import com.ebei.screen.common.util.CommonUtil;
|
|
|
+import com.ebei.screen.modules.po.CameraDevice;
|
|
|
+import com.ebei.screen.modules.po.Project;
|
|
|
+import com.ebei.screen.modules.po.YsAccessToken;
|
|
|
+import com.ebei.screen.service.CameraDeviceService;
|
|
|
+import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.*;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @version v1.0
|
|
|
+ * @ProjectName: screen
|
|
|
+ * @ClassName: CameraDeviceServiceImpl
|
|
|
+ * @Description:
|
|
|
+ * @Author: ch
|
|
|
+ * @Date: 2021-01-20 9:40
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class CameraDeviceServiceImpl implements CameraDeviceService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ private static Long _id = 1L;
|
|
|
+ private static String appKey = "d63cdf8387254760a0c75adeb97a4ce9";
|
|
|
+ private static String appSecret = "3c2e436abfbb6a4f5da9ba05f76a5605";
|
|
|
+ private static String code = "Jn5201314";
|
|
|
+ private static String ysGetTokenUrl = "https://open.ys7.com/api/lapp/token/get";
|
|
|
+ private static String ysGetDeviceWithPage = "https://open.ys7.com/api/lapp/device/list";
|
|
|
+ private static String ysGetLiveAddress = "https://open.ys7.com/api/lapp/v2/live/address/get";
|
|
|
+
|
|
|
+ public String getYsAccessToken() throws Exception {
|
|
|
+ //accesstoken有效期为7天,缓存到mongodb中,每6天重新获取一次
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ Query query = new Query();
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ criteria.where("_id").is(_id);
|
|
|
+ query.addCriteria(criteria);
|
|
|
+ YsAccessToken ysAccessToken = mongoTemplate.findById(_id, YsAccessToken.class, "YsAccessToken");
|
|
|
+ if(CommonUtil.isEmpty(ysAccessToken) || ysAccessToken.getCreateTime().plusDays(6).isBefore(now)) {
|
|
|
+ ysAccessToken = new YsAccessToken();
|
|
|
+ //没有缓存过或者缓存的已经过期需要重新获取
|
|
|
+ //请求萤石接口
|
|
|
+ Map<String,Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("appKey",appKey);
|
|
|
+ paramMap.put("appSecret",appSecret);
|
|
|
+ String result = HttpUtil.post(ysGetTokenUrl, paramMap);
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ if("200".equals(jsonObject.get("code"))) {
|
|
|
+ Object temp = jsonObject.get("data");
|
|
|
+ JSONObject data = JSONUtil.parseObj(temp);
|
|
|
+ String accessToken = data.get("accessToken").toString();
|
|
|
+ ysAccessToken.setToken(accessToken);
|
|
|
+ } else {
|
|
|
+ log.error("获取萤石accesstoken失败:"+jsonObject.get("msg"));
|
|
|
+ throw new Exception("获取萤石accesstoken失败:"+jsonObject.get("msg"));
|
|
|
+ }
|
|
|
+ //保存到mongodb
|
|
|
+ Query tempQuery = new Query();
|
|
|
+ tempQuery.addCriteria(Criteria.where("_is").is(_id));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set("token",ysAccessToken.getToken());
|
|
|
+ update.set("createTime",now);
|
|
|
+ mongoTemplate.upsert(query,update,YsAccessToken.class,"YsAccessToken");
|
|
|
+ }
|
|
|
+ return ysAccessToken.getToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Override
|
|
|
+ public void syncAllYsDevice() throws Exception {
|
|
|
+ log.info("----------------开始同步萤石监控设备----------------------");
|
|
|
+ List<CameraDevice> ysDeviceList = getYsDeviceList();
|
|
|
+ if(CommonUtil.isEmpty(ysDeviceList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ //查询mongodb中存在的全部设备
|
|
|
+ List<CameraDevice> cameraDevice = mongoTemplate.findAll(CameraDevice.class, "CameraDevice");
|
|
|
+ Map<String, CameraDevice> deviceMap = cameraDevice.stream().collect(Collectors.toMap(CameraDevice::getDeviceSerial, i -> i));
|
|
|
+ //处理直播地址
|
|
|
+ if(CommonUtil.isNotEmpty(ysDeviceList)) {
|
|
|
+ //查询直播地址
|
|
|
+ ysDeviceList.forEach( i -> {
|
|
|
+ CameraDevice temp = deviceMap.get(i.getDeviceSerial());
|
|
|
+ if(CommonUtil.isEmpty(temp)) {
|
|
|
+ //不存在的需要生成id
|
|
|
+ i.setId(CommonUtil.getSnowId());
|
|
|
+ i.setCreateTime(now);
|
|
|
+ } else {
|
|
|
+ i.setId(temp.getId());
|
|
|
+ }
|
|
|
+ if(i.getStatus().equals(1)) {
|
|
|
+ //设备在线的查询直播地址
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ try {
|
|
|
+ map.put("accessToken",this.getYsAccessToken());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取萤石accessToken失败!",e);
|
|
|
+ }
|
|
|
+ map.put("deviceSerial",i.getDeviceSerial());
|
|
|
+ map.put("code",code);
|
|
|
+ String result = HttpUtil.post(ysGetLiveAddress, map);
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ if("200".equals(jsonObject.get("code"))) {
|
|
|
+ i.setUrl(JSONUtil.parseObj(jsonObject.get("data")).get("url").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //更新或插入mongodb
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("_id").is(i.getId()));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set("status",i.getStatus());
|
|
|
+ update.set("deivceName",i.getDeviceName());
|
|
|
+ update.set("deviceSerial",i.getDeviceSerial());
|
|
|
+ update.set("deviceType",i.getDeviceType());
|
|
|
+ update.set("isDelete",0);
|
|
|
+ update.set("url",i.getUrl());
|
|
|
+ update.set("createTime",i.getCreateTime());
|
|
|
+ update.set("updateTime",i.getUpdateTime());
|
|
|
+ mongoTemplate.upsert(query,update,CameraDevice.class,"CameraDevice");
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ log.info("----------------同步萤石监控设备结束----------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseBean queryProjectPage(Integer page, Integer pageSize, String name, String deviceSerial, String status) {
|
|
|
+ Query query = new Query();
|
|
|
+ if(CommonUtil.isNotEmpty(name)) {
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ Pattern pattern=Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
|
|
|
+ criteria.where("deviceName").regex(pattern);
|
|
|
+ query.addCriteria(criteria);
|
|
|
+ }
|
|
|
+ if(CommonUtil.isNotEmpty(deviceSerial)) {
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ Pattern pattern=Pattern.compile("^.*"+deviceSerial+".*$", Pattern.CASE_INSENSITIVE);
|
|
|
+ criteria.where("deviceSerial").regex(pattern);
|
|
|
+ query.addCriteria(criteria);
|
|
|
+ }
|
|
|
+ if(CommonUtil.isNotEmpty(status)) {
|
|
|
+ query.addCriteria(Criteria.where("status").is(status));
|
|
|
+ }
|
|
|
+ //计算总数
|
|
|
+ long total = mongoTemplate.count(query, CameraDevice.class);
|
|
|
+ //分页查询
|
|
|
+ Pageable pageable = PageRequest.of(page, pageSize);
|
|
|
+ query.with(pageable);
|
|
|
+ query.with(Sort.by(Sort.Direction.DESC, "createTime"));
|
|
|
+ query.addCriteria(Criteria.where("isDelete").is(0));
|
|
|
+ List<CameraDevice> result = mongoTemplate.find(query, CameraDevice.class);
|
|
|
+ Page<CameraDevice> pageResult = new PageImpl<>(result, pageable, total);
|
|
|
+ return ResponseBean.success(pageResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<CameraDevice> getYsDeviceList() throws Exception {
|
|
|
+ //请求萤石接口获取全部设备
|
|
|
+ Integer pageStart = 0;
|
|
|
+ Integer pageSize = 50;
|
|
|
+ Map<String,Object> paramMap = new HashMap<>();
|
|
|
+ try {
|
|
|
+ paramMap.put("accessToken",this.getYsAccessToken());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步设备失败:获取萤石accessToken失败",e);
|
|
|
+ throw new Exception("同步设备失败:获取萤石accessToken失败");
|
|
|
+ }
|
|
|
+ paramMap.put("pageStart", pageStart);
|
|
|
+ paramMap.put("pageSize", pageSize);
|
|
|
+ String result = HttpUtil.post(ysGetDeviceWithPage, paramMap);
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ Object temp = jsonObject.get("page");
|
|
|
+ JSONObject page = JSONUtil.parseObj(temp);
|
|
|
+ Object data = jsonObject.get("data");
|
|
|
+ List<CameraDevice> cameraDevices = JSONUtil.toList(JSONUtil.parseArray(data), CameraDevice.class);
|
|
|
+ List<CameraDevice> totalDevice = new ArrayList<>();
|
|
|
+ totalDevice.addAll(cameraDevices);
|
|
|
+ while(totalDevice.size() < Integer.valueOf(page.get("total").toString())) {
|
|
|
+ //继续获取并存入totalDevice
|
|
|
+ pageStart++;
|
|
|
+ paramMap.put("pageStart",pageStart);
|
|
|
+ String resultTemp = HttpUtil.post(ysGetDeviceWithPage, paramMap);
|
|
|
+ JSONObject jsonObjectTemp = JSONUtil.parseObj(resultTemp);
|
|
|
+ Object tempPage = jsonObjectTemp.get("page");
|
|
|
+ page = JSONUtil.parseObj(tempPage);
|
|
|
+ Object dataTemp = jsonObjectTemp.get("data");
|
|
|
+ List<CameraDevice> cameraDevicesTemp = JSONUtil.toList(JSONUtil.parseArray(dataTemp), CameraDevice.class);
|
|
|
+ totalDevice.addAll(cameraDevicesTemp);
|
|
|
+ }
|
|
|
+ return totalDevice;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|