package com.newfiber.api.pc.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.hikvision.artemis.sdk.ArtemisHttpUtil; import com.hikvision.artemis.sdk.config.ArtemisConfig; import com.newfiber.api.core.commons.CustomException; import com.newfiber.api.core.commons.PageRequestObject; import com.newfiber.api.core.commons.PageResultObject; import com.newfiber.api.core.commons.ResultCode; import com.newfiber.api.core.utils.DateUtils; import com.newfiber.api.pc.dao.CameraResourceMapper; import com.newfiber.api.pc.dao.RegionStationMapper; import com.newfiber.api.pc.dto.ResourcesRequest; import com.newfiber.api.pc.model.entity.CameraResource; import com.newfiber.api.pc.model.entity.CameraStat; import com.newfiber.api.pc.model.entity.CamerasRequest; import com.newfiber.api.pc.model.hikvsion.*; import com.newfiber.api.pc.model.vo.CameraRegionCountVo; import com.newfiber.api.pc.model.vo.OneMapGisResult; import com.newfiber.api.pc.model.vo.OneMapInteraction; import com.newfiber.api.pc.service.CameraResourceService; import com.newfiber.api.pc.service.ConfigCameraRegionCodeService; import com.newfiber.api.pc.service.RegisionStationService; import com.newfiber.api.pc.model.monitor.Sl6512014; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Service; import org.springframework.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * @ClassName CameraResourceServiceImpl * @Description TODO * @Author 张鸿志 * @Date 2021年3月4日16:53:26 16:53 * Version 1.0 **/ @Service @Slf4j public class CameraResourceServiceImpl extends ServiceImpl<CameraResourceMapper, CameraResource> implements CameraResourceService { @Autowired private CameraResourceMapper cameraResourceMapper; @Autowired private RegionStationMapper regionStationMapper; @Autowired private RegisionStationService regisionStationService; @Autowired private MongoTemplate mongoTemplate; @Autowired private ConfigCameraRegionCodeService configCameraRegionCodeService; /** * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数. */ static { /** *地址:172.16.40.121 * AK:22293881 * SK:12Fj7DSxrH8zbQP3USnB */ // ArtemisConfig.host = "10.50.10.136:443";// 平台门户/nginx的IP和端口(必须使用https协议,https端口默认为443) // ArtemisConfig.appKey = "26204365"; // 秘钥appkey // ArtemisConfig.appSecret = "SfJKM3KLd75p3mAm82mA";// 秘钥appSecret String osName = System.getProperty("os.name"); //线上环境的key if(osName.equalsIgnoreCase("linux")){ ArtemisConfig.host = "192.168.101.246:14430";// 平台门户/nginx的IP和端口(必须使用https协议,https端口默认为443) ArtemisConfig.appKey = "21517856"; // 秘钥appkey ArtemisConfig.appSecret = "OGwk6a1PX0gU9b7n8DWL";// 秘钥appSecret }else{ //本地测试环境的key ArtemisConfig.host = "172.16.12.30:443";// 平台门户/nginx的IP和端口(必须使用https协议,https端口默认为443) ArtemisConfig.appKey = "26520492"; // 秘钥appkey ArtemisConfig.appSecret = "bo4dlF9nIbFIbAL49tdd";// 秘钥appSecret } } /** * STEP2:设置OpenAPI接口的上下文 */ private static final String ARTEMIS_PATH = "/artemis"; private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public PageResultObject<CameraResource> queryPage(PageRequestObject<String> pageRequestObject) { if(StringUtils.isEmpty(pageRequestObject)){ throw new CustomException(ResultCode.PARAM_NULL); } int current = pageRequestObject.getCurrent(); int size = pageRequestObject.getSize(); String name = pageRequestObject.getObject(); EntityWrapper<CameraResource> wrapper = new EntityWrapper<>(); if(!StringUtils.isEmpty(name)){ wrapper.like("name",name); } int count = this.selectCount(wrapper); wrapper.orderDesc(Arrays.asList(new String[]{"id"})); Page<CameraResource> page = new Page<>(current,size); Page<CameraResource> resourcePage = this.selectPage(page, wrapper); return new PageResultObject<>(current,size,(long)count,resourcePage.getRecords()); } @Override public void updateIsTop(Integer id) { if(StringUtils.isEmpty(id)){ throw new CustomException(ResultCode.PARAM_NULL); } cameraResourceMapper.updateIsTop(id); } @Override public void updateIsShow(Integer id) { if(StringUtils.isEmpty(id)){ throw new CustomException(ResultCode.PARAM_NULL); } cameraResourceMapper.updateIsShow(id); } @Override public List<CameraResource> queryShowCamera() { EntityWrapper<CameraResource> wrapper = new EntityWrapper<>(); wrapper.eq("is_show",1); wrapper.orderDesc(Arrays.asList(new String[]{"is_top"})); return this.selectList(wrapper); } @Override public void configIsHighSpot(Integer id) { if(StringUtils.isEmpty(id)){ throw new CustomException(ResultCode.PARAM_NULL); } cameraResourceMapper.configIsHighSpot(id); } @Override public void sync() throws ParseException { CamerasRequest camerasRequest = new CamerasRequest(); camerasRequest.setPageNo(1); camerasRequest.setPageSize(1000); camerasRequest.setTreeCode("0"); // 分页获取所有的 String camerasDataApi = ARTEMIS_PATH +"/api/irds/v2/deviceResource/resources"; //分页获取区域信息 String areaRegions = ARTEMIS_PATH + "/api/resource/v1/regions"; String cameraStatus = ARTEMIS_PATH + "/api/nms/v1/online/camera/get"; String body=JSON.toJSONString(camerasRequest); Map<String,String> path1 = new HashMap<String,String>(2){ { put("https://",areaRegions); } }; //区域数据 String areaResult =ArtemisHttpUtil.doPostStringArtemis(path1,body,null,null,"application/json"); //同步区域数据 SyncResultObj areaSync = JSON.parseObject(areaResult, SyncResultObj.class); HikvsionSyncPageObj areaData = areaSync.getData(); List<JSONObject> areaObjs = JSONArray.parseArray(JSONObject.toJSONString(areaData.getList()), JSONObject.class); List<RegionStation> regionStations = new ArrayList<>(); //取出所有的数据插入 for (JSONObject areaObj : areaObjs) { RegionStation station = new RegionStation(); station.setName(areaObj.get("name").toString()); String regionIndexCode = areaObj.get("indexCode").toString(); //如果是主控中心或者市公安局的就过滤掉 if(regionIndexCode.equalsIgnoreCase("c6e476683e9740a3bffdf158363f0d99") || regionIndexCode.equalsIgnoreCase("41150001002160000001")){ continue; } station.setRegionIndexCode(regionIndexCode); station.setParentIndexCode(areaObj.get("parentIndexCode").toString()); station.setTreeCode(areaObj.get("treeCode").toString()); regionStations.add(station); } //查询本地已有的区域中父级为水务的所有子集 EntityWrapper<RegionStation> wrapper = new EntityWrapper<>(); wrapper.eq("parent_index_code","5d67a9e9a24c4a0b93d85f2651b4d8d9"); //查询得到自己配置的区域信息 List<RegionStation> oldConfigObj = regionStationMapper.selectList(wrapper); oldConfigObj.forEach(s -> s.setId(null)); //删除所有的区域列表数据 regionStationMapper.truncate(); //添加保留的数据 regionStations.addAll(oldConfigObj); if(regionStations.size() > 0){ regisionStationService.insertBatch(regionStations); } List<HikvsionReourceNew> totalArray = new ArrayList<>(); int pageNo = 1; while (true){ Map<String,String> path = new HashMap<String,String>(2){ { put("https://",camerasDataApi); } }; ResourcesRequest resourcesRequest = new ResourcesRequest(); resourcesRequest.setPageNo(pageNo); resourcesRequest.setPageSize(1000); resourcesRequest.setResourceType("camera"); String resourceRequestJson = JSON.toJSONString(resourcesRequest); String result =ArtemisHttpUtil.doPostStringArtemis(path,resourceRequestJson,null,null,"application/json"); // String artemis = ArtemisHttpUtil.doPostStringArtemis(path, jsonObject.toJSONString(), null, null, "application/json"); SyncResultObj syncResultObj = JSON.parseObject(result, SyncResultObj.class); HikvsionSyncPageObj pageObj = syncResultObj.getData(); List<HikvsionReourceNew> jsonObjects = JSONArray.parseArray(JSONObject.toJSONString(pageObj.getList()), HikvsionReourceNew.class); totalArray.addAll(jsonObjects); if(jsonObjects.size() < 1000){ break; } pageNo++; } // 之前查询 是用来 像这张表里面添加分类数据,比如排口、高点之类的 config_camera_region_code List<CameraResource> cameraResources = this.selectList(null); List<CameraResource> remoteList = new ArrayList<>(); //用来保存新增水务摄像头资源时默认添加到其他类别下的集合 // List<ConfigCameraRegionCode> regionCodeList = new ArrayList<>(); for(HikvsionReourceNew hikvsionReource : totalArray){ // if(hikvsionReource.getUnitIndexCode().equalsIgnoreCase("c6e476683e9740a3bffdf158363f0d99") || hikvsionReource.getUnitIndexCode().equalsIgnoreCase("41150001002160000001")){ // continue; // } CameraResource resource = new CameraResource(); resource.setCreateTime(hikvsionReource.getCreateTime()); resource.setIndexCode(hikvsionReource.getIndexCode()); resource.setIsShow(1); resource.setIsTop(0); resource.setStatus(StringUtils.isEmpty(hikvsionReource.getOnlineStatus()) ? 0 : hikvsionReource.getOnlineStatus()); resource.setName(hikvsionReource.getName()); resource.setX(hikvsionReource.getLongitude()); resource.setY(hikvsionReource.getLatitude()); resource.setRegionIndexCode(hikvsionReource.getRegionIndexCode()); resource.setUpdateTime(hikvsionReource.getUpdateTime()); resource.setCameraAddress(hikvsionReource.getInstallPlace()); remoteList.add(resource); } //将新拿到的与已有的进行比较,赋值,这样能保证保存原来的配置和更新数据 if(cameraResources.size() > 0){ for (CameraResource cameraResource : remoteList) { for(int i = 0; i < cameraResources.size(); i++){ CameraResource resource = cameraResources.get(i); //如果两个摄像头编号相等,那么就将数据库中已有的is_top is_show is_high_sopt camera_address字段赋值给新拿到的数据 if(cameraResource.getIndexCode().equalsIgnoreCase(resource.getIndexCode())){ cameraResource.setIsTop(resource.getIsTop()); cameraResource.setIsShow(resource.getIsShow()); cameraResource.setIsHighSpot(resource.getIsHighSpot()); break; } } } } pageNo = 1; List<CameraStatus> cameraStatusTotalList = new ArrayList<>(); while (true){ //查询摄像头状态 Map<String,String> statusPath = new HashMap<String,String>(2){ { put("https://",cameraStatus); } }; StatusRequest statusRequest = new StatusRequest(); statusRequest.setPageNo(pageNo); statusRequest.setPageSize(1000); statusRequest.setStatus("1"); String statusRequestJson = JSON.toJSONString(statusRequest); String statusResult =ArtemisHttpUtil.doPostStringArtemis(statusPath,statusRequestJson,null,null,"application/json"); SyncResultObj syncResultObjStatus = JSON.parseObject(statusResult, SyncResultObj.class); HikvsionSyncPageObj pageObjStatus = syncResultObjStatus.getData(); List<CameraStatus> cameraStatusList = JSONArray.parseArray(JSONObject.toJSONString(pageObjStatus.getList()), CameraStatus.class); cameraStatusTotalList.addAll(cameraStatusList); if(cameraStatusList.size() < 1000){ break; } pageNo++; } for (CameraResource cameraResource : remoteList) { for (CameraStatus status : cameraStatusTotalList) { if(cameraResource.getIndexCode().equals(status.getIndexCode())){ cameraResource.setStatus(status.getOnline()); break; } } } //清空表数据 cameraResourceMapper.truncateTable(); //批量新增 this.insertBatch(remoteList); // regionStationMapper.truncate(); // regisionStationService.insertBatch(regionStations); //将同步过来的摄像头资源排除已有的摄像头资源,剩下的就是新增的,新增的水务类别下的资源就添加到其他类别下 // if(remoteList.removeAll(cameraResources)){ // if(remoteList.size() > 0){ // for (CameraResource cameraResource : remoteList) { // if(cameraResource.getRegionIndexCode().equals("5d67a9e9a24c4a0b93d85f2651b4d8d9")){ // ConfigCameraRegionCode regionCode = new ConfigCameraRegionCode(); // regionCode.setCameraIndexCode(cameraResource.getIndexCode()); // regionCode.setRegionIndexCode("4b1680f9-235d-4f47-b5a7-6b09c338a33c"); // regionCode.setRemark("其他"); // regionCodeList.add(regionCode); // } // } // } // } // // if(regionCodeList.size() > 0){ // configCameraRegionCodeService.insertBatch(regionCodeList); // // log.info("在同步摄像头资源中插入了水务类别中的其他类别,请自行在表中改为某一种类别:{}",JSON.toJSONString(regionCodeList)); // } } @Override public List<String> getCameraCode(String siteNo) { List<String> result = cameraResourceMapper.getCameraCode(siteNo); return result; } @Override public Map<String, Object> getBlackRiverWaterData(String siteNo) { Query query = new Query(); Criteria criteria = Criteria.where(""); criteria.andOperator( //大于开始时间 Criteria.where("tt").gte(DateUtils.getBeforeDay()), //小于结束时间 Criteria.where("tt").lte(new Date()) ); query.addCriteria(criteria); List<Sl6512014> list = mongoTemplate.find(query, Sl6512014.class, siteNo); List<String> time = new ArrayList<>(); List<String> level = new ArrayList<>(); if(!list.isEmpty()){ for (Sl6512014 sl6512014 : list) { if(sl6512014.getZ()!=null && sl6512014.getTt()!=null){ level.add(sl6512014.getZ().toString()); time.add(DateUtils.format(sl6512014.getTt(),"yyyy-MM-dd")); } } } Map<String,Object> result = new HashMap<>(); result.put("time",time); result.put("level",level); return result; } @Override public CameraStat queryCameraStat() { CameraStat cameraStat = cameraResourceMapper.queryCameraStat(); cameraStat.setOnlineCount(cameraStat.getTotal() - cameraStat.getNoOnlineCOunt()); return cameraStat; } @Override public List<CameraRegionCountVo> queryRegionCount() { return cameraResourceMapper.queryRegionCount(); } @Override public OneMapGisResult selectCamreaData() { OneMapGisResult oneMapGisResult = new OneMapGisResult(); oneMapGisResult.setLabel("camrea"); oneMapGisResult.setLableTitle("应 急 指 挥 一 张 图"); List<OneMapInteraction> list = cameraResourceMapper.selectCamreaData(); List<OneMapInteraction> collect = list.stream().filter(s -> !StringUtils.isEmpty(s.getLon())).collect(Collectors.toList()); oneMapGisResult.setOneMapInteraction(collect); return oneMapGisResult; } }