package cn.besteam.modules.medical.biz; import cn.besteam.common.constant.ErrorCode; import cn.besteam.common.exception.BusinessException; import cn.besteam.common.util.MD5Util; import cn.besteam.common.util.RedisUtil; import cn.besteam.config.DeviceConfig; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service @Slf4j public class MonitorDeviceApiNewService { @Autowired DeviceConfig deviceConfig; //登录 private final String LOGIN_URL = "/api/v1/login?username=%s&password=%s&url_token_only=%s"; //根据状态获取视频列表 private final String LIST_URL = "/api/v1/device/list?online=%s"; //查询设备通道列表接口(新) private final String channellist_URL = "/api/v1/device/channellist?serial=%s"; //根据设备号获取视频链接 private final String STREAM_URL = "/api/v1/stream/start?serial=%s&channel=%s"; //查询通道录像列表接口(新) private final String recordlist_URL = "/api/v1/playback/recordlist?serial=%s&channel=%s&starttime=%s&endtime=%s"; //开始回放接口(新) private final String playbackStart_URL = "/api/v1/playback/start?serial=%s&channel=%s&starttime=%s&endtime=%s"; //回放流停止接口(新) private final String playbackStop_URL = "/api/v1/playback/stop?streamid=%s"; //在线数据 Boolean url_token_only= true; //获取视频列表参数 Boolean online=true;//设备是否在线 @Autowired private RedisUtil redisUtil; /** * 登录 * type==1 重新调起登录 * @return */ public String getJSession(Integer type) { String key = "monitor:device:jsession:now"; String jsession = redisUtil.get(key); Long timeOut=604800L; if (jsession == null || type==1) { RestTemplate restTemplate = new RestTemplate(); String loginUrl = String.format( deviceConfig.getBaseUrl()+LOGIN_URL, deviceConfig.getUserName(), MD5Util.MD5Encode(deviceConfig.getPassword(),""),url_token_only); log.info("视频服务器:登录接口传参数据:{}", loginUrl); JSONObject result = restTemplate.getForObject(loginUrl, JSONObject.class); log.info("视频服务器:登录接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } if (result!=null) { timeOut = result.getLong("TokenTimeout");//缓存过期时间 jsession = result.getString("URLToken");//token } else { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } } redisUtil.set(key, jsession, timeOut); return jsession; } /** * 获取设备列表 * * * @return */ public List getDeviceList() { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); String completeUrl = String.format(deviceConfig.getBaseUrl()+LIST_URL, online); try { ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } List list=new ArrayList<>(); JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); JSONArray jsonArray=jsonBody.getJSONArray("DeviceList"); for(int i = 0; i < jsonArray.size() ; i++){ JSONObject jsonObject=jsonArray.getJSONObject(i); String ID=jsonObject.getString("ID"); list.add(ID); } return list; } catch (Exception e) { getJSession(1); return getDeviceList(); } } /** * 获取通道列表 * * * @return */ public List> getChannelList(String serial) { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); String completeUrl = String.format(deviceConfig.getBaseUrl()+channellist_URL, serial); try { ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } List> list=new ArrayList<>(); JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); JSONArray jsonArray=jsonBody.getJSONArray("ChannelList"); for(int i = 0; i < jsonArray.size() ; i++){ JSONObject jsonObject=jsonArray.getJSONObject(i); String channel=jsonObject.getString("Channel"); Map map = new HashMap<>(); map.put("channel",channel); list.add(map); } return list; } catch (Exception e) { getJSession(1); return getChannelList(serial); } } /** * 获取设备播放路径 * * @param devIdno * @return */ public Map getDeviceUrl(String devIdno,Integer channelNum) { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); if(channelNum == null){ channelNum = 1; } String completeUrl = String.format(deviceConfig.getBaseUrl()+STREAM_URL, devIdno,channelNum); System.out.println(completeUrl); ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); String flv=jsonBody.getString("FLV"); String snapURL=jsonBody.getString("SnapURL"); Map reMap = new HashMap<>(); reMap.put("flv",flv); reMap.put("snapURL",deviceConfig.getBaseUrl()+snapURL); return reMap; } /** * 查询通道录像列表 * * * @return */ public List> getPlaybackRecordlist(String serial,Integer channel,String starttime,String endtime) { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); String completeUrl = String.format(deviceConfig.getBaseUrl()+recordlist_URL, serial,channel,starttime,endtime); try { ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } List> list=new ArrayList<>(); JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); JSONArray jsonArray=jsonBody.getJSONArray("RecordList"); for(int i = 0; i < jsonArray.size() ; i++){ JSONObject jsonObject=jsonArray.getJSONObject(i); Map map = JSON.parseObject(jsonObject.toJSONString(),Map.class); list.add(map); } return list; } catch (Exception e) { getJSession(1); return null; //return getPlaybackRecordlist(serial,channel,starttime,endtime); } } /** * 开始回放接口 * * @param serial * @return */ public Map playbackStart(String serial,Integer channel,String starttime,String endtime) { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); if(channel == null){ channel = 1; } String completeUrl = String.format(deviceConfig.getBaseUrl()+playbackStart_URL, serial,channel,starttime,endtime); System.out.println(completeUrl); ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); String flv=jsonBody.getString("FLV"); String snapURL=jsonBody.getString("SnapURL"); String streamID=jsonBody.getString("StreamID"); Map reMap = new HashMap<>(); reMap.put("flv",flv); reMap.put("snapURL",deviceConfig.getBaseUrl()+snapURL); reMap.put("streamID",streamID); return reMap; } /** * 回放流停止接口 * * @param streamID * @return */ public Map playbackStop(String streamID) { String token=getJSession(0); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.add("token",token); HttpEntity request=new HttpEntity(httpHeaders); String completeUrl = String.format(deviceConfig.getBaseUrl()+playbackStop_URL, streamID); System.out.println(completeUrl); ResponseEntity resultEntity=restTemplate.exchange(completeUrl,HttpMethod.GET,request,JSONObject.class,""); if (resultEntity == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } JSONObject jsonBody=resultEntity.getBody(); //log.info("视频服务器:获取设备状态接口返回数据:{}", jsonBody); String playbackFileURL=jsonBody.getString("PlaybackFileURL"); Map reMap = new HashMap<>(); reMap.put("playbackFileURL",playbackFileURL); return reMap; } }