From 83ba7cd35454bc87bc60d59298210e0c95515c17 Mon Sep 17 00:00:00 2001 From: tongliran Date: Thu, 9 Jan 2025 18:18:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=9B=91=E6=8E=A7=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E6=8E=A5=E5=8F=A3=E6=94=B9=E4=B8=BA=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yingshiyun/service/YingshiyunService.java | 81 +++++++++++++++++-- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/yingshiyun/service/YingshiyunService.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/yingshiyun/service/YingshiyunService.java index cea3f99..fdf89e2 100644 --- a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/yingshiyun/service/YingshiyunService.java +++ b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/yingshiyun/service/YingshiyunService.java @@ -6,6 +6,12 @@ import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import com.fastbee.common.holder.ProjectHolder; +import com.fastbee.deviceData.domain.NgYingshiCloudIntegrationInfo; +import com.fastbee.deviceData.mapper.NgYingshiCloudIntegrationInfoMapper; +import com.fastbee.deviceInfo.domain.DeviceInformationMonitor; +import com.fastbee.deviceInfo.mapper.DeviceInformationMonitorMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; @@ -13,6 +19,7 @@ import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -22,16 +29,25 @@ public class YingshiyunService { @Autowired private StringRedisTemplate stringRedisTemplate; + @Autowired + private DeviceInformationMonitorMapper deviceInformationMonitorMapper; + @Autowired + private NgYingshiCloudIntegrationInfoMapper ngYingshiCloudIntegrationInfoMapper; //获取鉴权 - public String getAuth(){ - if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("yingshiyun:user:accessToken"))){ + public String getAuth(String appKey,String appSecret){ + /*if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("yingshiyun:user:accessToken"))){ + System.err.println(stringRedisTemplate.opsForValue().get("yingshiyun:user:accessToken")); return stringRedisTemplate.opsForValue().get("yingshiyun:user:accessToken"); + }*/ + if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("yingshiyun:user"+appKey+":"+appSecret+":accessToken"))){ + System.err.println(stringRedisTemplate.opsForValue().get("yingshiyun:user"+appKey+":"+appSecret+":accessToken")); + return stringRedisTemplate.opsForValue().get("yingshiyun:user"+appKey+":"+appSecret+":accessToken"); } // 鉴权请求 String AUTH_URL = "https://open.ys7.com/api/lapp/token/get"; - String appKey = "b21f910dc7044d668e7625a3c0392e62"; - String appSecret = "b4beff5f8f6694dd6993c8c5b618417b"; + /*String appKey = "b21f910dc7044d668e7625a3c0392e62"; + String appSecret = "b4beff5f8f6694dd6993c8c5b618417b";*/ // 构建请求体参数 String body = StrUtil.format("appKey={}&appSecret={}", appKey, appSecret); HttpResponse response = HttpRequest.post(AUTH_URL) @@ -45,21 +61,68 @@ public class YingshiyunService { JSONObject data = JSONUtil.parseObj(jsonObject.get("data")); String accessToken = data.get("accessToken").toString(); - stringRedisTemplate.opsForValue().set("yingshiyun:user:accessToken",accessToken, 60*60*24*6, TimeUnit.MILLISECONDS); + //stringRedisTemplate.opsForValue().set("yingshiyun:user:accessToken",accessToken, 60*60*24*6, TimeUnit.MILLISECONDS); + stringRedisTemplate.opsForValue().set("yingshiyun:user"+appKey+":"+appSecret+":accessToken",accessToken, 60*60*24*6, TimeUnit.MILLISECONDS); return accessToken; } //获取视频播放地址 public Map getVideoPlayUrl(String deviceSerial){ - String accessToken = getAuth(); -// System.err.println(accessToken); + /** + * 根据设备编码查找项目id,根据项目id查找萤石云对接账号信息 + */ + System.err.println("deviceSerial:"+deviceSerial); + Map map = new HashMap<>(); + List monitorList=new LambdaQueryChainWrapper<>(deviceInformationMonitorMapper) + .select(DeviceInformationMonitor::getProjectId) + .eq(DeviceInformationMonitor::getDeviceEncoding,deviceSerial) + .list(); + if(monitorList.size()==0) + { + System.err.println("设备信息未找到"); + return map; + } + Long projectId=monitorList.get(0).getProjectId(); + /*String projectId=ProjectHolder.getProjectInfo().getProjectId(); + System.err.println("projectId:"+projectId);*/ + List infoList=new LambdaQueryChainWrapper<>(ngYingshiCloudIntegrationInfoMapper) + .select(NgYingshiCloudIntegrationInfo::getAppkey,NgYingshiCloudIntegrationInfo::getAppsecret) + .eq(NgYingshiCloudIntegrationInfo::getProjectid,projectId) + .list(); + if(infoList.size()==0) + { + System.err.println("萤石云信息不存在"); + return map; + } + String accessToken = getAuth(infoList.get(0).getAppkey(),infoList.get(0).getAppsecret()); + System.err.println(accessToken); String VIDEO_PLAY_URL = "https://open.ys7.com/api/lapp/v2/live/address/get"; + String body = StrUtil.format("accessToken={}&deviceSerial={}&protocol={}",accessToken ,deviceSerial,"2"); HttpResponse response = HttpRequest.post(VIDEO_PLAY_URL) .body(body).execute(); JSONObject jsonObject = JSONUtil.parseObj(response.body()); - Map map = new HashMap<>(); + System.err.println("jsonObject:"+jsonObject); + + if(jsonObject.get("code").toString().equals("60019")) + { + //如果设备加密 + //解密 + String VIDEO_DECODE_URL = "https://open.ys7.com/api/lapp/device/encrypt/off"; + String decodeBody=StrUtil.format("accessToken={}&deviceSerial={}",accessToken ,deviceSerial); + HttpResponse decodeResponse=HttpRequest.post(VIDEO_DECODE_URL).body(decodeBody).execute(); + JSONObject decodeJsonObject=JSONUtil.parseObj(decodeResponse.body()); + System.err.println("decodeJsonObject:"+decodeJsonObject); + if(!decodeJsonObject.get("code").toString().equals("200")) + { + return map; + } + response = HttpRequest.post(VIDEO_PLAY_URL) + .body(body).execute(); + jsonObject = JSONUtil.parseObj(response.body()); + System.err.println("解密后jsonObject:"+jsonObject); + } if(!jsonObject.get("code").toString().equals("200")){ return map; } @@ -71,6 +134,8 @@ public class YingshiyunService { map.put("playUrl",playUrl); map.put("accessToken",accessToken); + System.err.println("playUrl:"+playUrl); + System.err.println("accessToken:"+accessToken); return map; }