|
@ -0,0 +1,408 @@
|
|
|
package com.yihu.jw.care.service.device;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.care.config.YsConfig;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
import com.yihu.jw.utils.encode.Base64;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.security.Security;
|
|
|
import java.text.MessageFormat;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by Bing on 2021/6/1.
|
|
|
*/
|
|
|
@Service
|
|
|
public class YsDeviceService {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(YsDeviceService.class);
|
|
|
private Map<String,String> msgType = new HashMap<>();
|
|
|
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
|
|
|
/**
|
|
|
* 获取萤石设备assesToken
|
|
|
*/
|
|
|
public String getAccessToken() throws Exception {
|
|
|
if(redisTemplate.hasKey(YsConfig.redisKey)){
|
|
|
return redisTemplate.opsForValue().get(YsConfig.redisKey);
|
|
|
}
|
|
|
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("appKey",YsConfig.AppKey);
|
|
|
param.put("appSecret",YsConfig.Secret);
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.saasAssesToken,param, HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
String assToken = null;
|
|
|
if (responseBody.getInteger("code")==200){
|
|
|
assToken = responseBody.getJSONObject("data").getString("accessToken");
|
|
|
Long expireTime = responseBody.getJSONObject("data").getLong("expiresIn");//token有效期
|
|
|
redisTemplate.opsForValue().set(YsConfig.redisKey,assToken,expireTime, TimeUnit.MILLISECONDS);
|
|
|
}else {
|
|
|
throw new Exception("获取token失败");
|
|
|
}
|
|
|
return assToken;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取IOT开放平台assesToken
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String getIotAccessToken() throws Exception {
|
|
|
if(redisTemplate.hasKey(YsConfig.iotRedisKey)){
|
|
|
return redisTemplate.opsForValue().get(YsConfig.iotRedisKey);
|
|
|
}
|
|
|
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.iotAssesToken,param, HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
String assToken = null;
|
|
|
if (responseBody.getInteger("code")==200){
|
|
|
assToken = responseBody.getJSONObject("data").getString("ezOpenAccessToken");
|
|
|
Long expireTime = responseBody.getJSONObject("data").getLong("expireTime");//token有效期
|
|
|
redisTemplate.opsForValue().set(YsConfig.iotRedisKey,assToken,expireTime, TimeUnit.MILLISECONDS);
|
|
|
//设备解密密钥
|
|
|
String secretKey = responseBody.getJSONObject("data").getString("secretKey");
|
|
|
redisTemplate.opsForValue().set(YsConfig.secretKey,secretKey,expireTime, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
}else {
|
|
|
throw new Exception("获取token失败");
|
|
|
}
|
|
|
return assToken;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取设备解密密钥
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String getSecretKey() throws Exception {
|
|
|
if(redisTemplate.hasKey(YsConfig.secretKey)){
|
|
|
return redisTemplate.opsForValue().get(YsConfig.secretKey);
|
|
|
}
|
|
|
else {
|
|
|
getIotAccessToken();
|
|
|
return redisTemplate.opsForValue().get(YsConfig.secretKey);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 萤石摄像机获取通道详情
|
|
|
* @param deviceSerial
|
|
|
* @param channelNo
|
|
|
* @param isTrust
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject surveillanceWay(String deviceSerial,Integer channelNo,Integer isTrust,HttpServletRequest request) throws Exception {
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
param.put("deviceSerial",deviceSerial);
|
|
|
param.put("channelNo",channelNo);
|
|
|
param.put("isTrust",0);
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.surveillanceWay,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/os58z7#p9Tfd
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 视频加解密开关
|
|
|
* @param deviceSerial
|
|
|
* @param isEncrypt
|
|
|
* @param validateCode
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject deviceEncrypt(String deviceSerial,Integer isEncrypt,String validateCode,HttpServletRequest request) throws Exception {
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
param.put("deviceSerial",deviceSerial);
|
|
|
param.put("isEncrypt",isEncrypt);
|
|
|
if (0==isEncrypt){//PU4eJqa7j9GMst8P
|
|
|
validateCode = messageDecrypt(validateCode,getSecretKey());
|
|
|
param.put("validateCode",validateCode);
|
|
|
}
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.deviceEncrypt,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/os58z7#zfqzn
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设备抓拍
|
|
|
* @param deviceSerial
|
|
|
* @param channelNo
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject deviceCapture(String deviceSerial,Integer channelNo,HttpServletRequest request) throws Exception {
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
param.put("deviceSerial",deviceSerial);
|
|
|
param.put("channelNo",channelNo+"");
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.deviceCapture,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/os58z7#K2AHX
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取视频地址
|
|
|
*/
|
|
|
public JSONObject getDeviceLiveAddress(String deviceSerial,Integer channelNo,HttpServletRequest request) throws Exception {
|
|
|
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
|
|
|
param.add("accessToken",getIotAccessToken());
|
|
|
param.add("deviceSerial",deviceSerial);
|
|
|
param.add("channelNo",channelNo+"");
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.iotAssesTokenPostHttp(YsConfig.getDeviceLiveAddress,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://open.ys7.com/doc/zh/book/index/address.html#address-api2
|
|
|
|
|
|
/**
|
|
|
* {
|
|
|
* "msg":"操作成功!",
|
|
|
* "code":"200",
|
|
|
* "data":{
|
|
|
* "deviceSerial":"F56826706",
|
|
|
* "channelNo":1,
|
|
|
* "deviceName":"C6Wi(F56826706)",
|
|
|
* "liveAddress":"http://hls01open.ys7.com/openlive/99c146f11ab042849fd4b49f7b0d86cf.m3u8", HLS流畅直播地址
|
|
|
* "hdAddress":"http://hls01open.ys7.com/openlive/99c146f11ab042849fd4b49f7b0d86cf.hd.m3u8", HLS高清直播地址
|
|
|
* "rtmp":"rtmp://rtmp01open.ys7.com/openlive/99c146f11ab042849fd4b49f7b0d86cf", RTMP流畅直播地址
|
|
|
* "rtmpHd":"rtmp://rtmp01open.ys7.com/openlive/99c146f11ab042849fd4b49f7b0d86cf.hd", RTMP高清直播地址
|
|
|
* "flvAddress":"https://flvopen.ys7.com:9188/openlive/99c146f11ab042849fd4b49f7b0d86cf.flv", FLV流畅直播地址
|
|
|
* "hdFlvAddress":"https://flvopen.ys7.com:9188/openlive/99c146f11ab042849fd4b49f7b0d86cf.hd.flv", FLV高清直播地址
|
|
|
* "status":1,
|
|
|
* "exception":0,
|
|
|
* "beginTime":1622615659000,
|
|
|
* "endTime":1622615659000
|
|
|
* }
|
|
|
* }
|
|
|
*/
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 云台控制开始
|
|
|
* @param deviceSerial
|
|
|
* @param channelNo
|
|
|
* @param direction
|
|
|
* @param speed
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject ptzStart(String deviceSerial,Integer channelNo,Integer direction,Integer speed,HttpServletRequest request) throws Exception {
|
|
|
MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
|
|
|
param.add("accessToken",getIotAccessToken());
|
|
|
param.add("deviceSerial",deviceSerial);
|
|
|
param.add("channelNo",channelNo+"");
|
|
|
param.add("direction",direction+"");
|
|
|
param.add("speed",speed+"");
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.iotAssesTokenPostHttp(YsConfig.ptzStart,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://open.ys7.com/doc/zh/book/index/device_ptz.html#device_ptz-api1
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 云台控制结束
|
|
|
* @param deviceSerial
|
|
|
* @param channelNo
|
|
|
* @param direction
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject ptzStop(String deviceSerial,Integer channelNo,Integer direction,HttpServletRequest request) throws Exception {
|
|
|
MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
|
|
|
param.add("accessToken",getIotAccessToken());
|
|
|
param.add("deviceSerial",deviceSerial);
|
|
|
param.add("channelNo",channelNo+"");
|
|
|
param.add("direction",direction+"");
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.iotAssesTokenPostHttp(YsConfig.ptzStop,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://open.ys7.com/doc/zh/book/index/device_ptz.html#device_ptz-api2
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取上传录像授权信息
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject getVideoKey(HttpServletRequest request) throws Exception {
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.videoKey,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/nu22ks#Kj5dG
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 录像列表(无法区分用户)
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject videoList(String name,String type,Integer page,Integer count) throws Exception {
|
|
|
page = page>0?page-1:0;
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
if (StringUtils.isNotBlank(name)){
|
|
|
param.put("name",name);
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(type)){
|
|
|
param.put("type",type);
|
|
|
}
|
|
|
param.put("page",page);
|
|
|
param.put("count",count);
|
|
|
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.videoList,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/nu22ks#kF97n
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量获取录像详情
|
|
|
* @param vod_ids
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject videoDetailList(String[] vod_ids) throws Exception {
|
|
|
JSONArray arr = JSONArray.parseArray(JSON.toJSONString(vod_ids));
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
param.put("vod_ids",arr);
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.videoDetailList,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/nu22ks#60Fjf
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取录像详情
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public JSONObject videoDetail(String vod_id) throws Exception {
|
|
|
JSONObject param = new JSONObject();
|
|
|
param.put("accessToken",getAccessToken());
|
|
|
param.put("vod_id",vod_id);
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(YsConfig.videoDetail,param,HttpMethod.POST);
|
|
|
JSONObject responseBody = response.getBody();
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/nu22ks#xQCVK
|
|
|
}
|
|
|
|
|
|
public JSONObject deviceFailConfig(String deviceSerial,Integer type,HttpServletRequest request) throws Exception {
|
|
|
JSONObject responseBody = new JSONObject();
|
|
|
String url =MessageFormat.format(YsConfig.deviceFailConfig, deviceSerial,"1");
|
|
|
|
|
|
if (null==type){//查询
|
|
|
|
|
|
url += "?accessToken="+getIotAccessToken()+"&key=Alarm_DetectHumanCar";
|
|
|
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.iotAssesTokenPostHttp(url,param,HttpMethod.GET);
|
|
|
responseBody = response.getBody();
|
|
|
}
|
|
|
else {
|
|
|
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
|
|
|
param.add("accessToken",getIotAccessToken());
|
|
|
param.add("key","Alarm_DetectHumanCar");
|
|
|
param.add("value","{\"type\":"+type+"}");
|
|
|
|
|
|
HttpEntity<JSONObject> response = httpClientUtil.iotAssesTokenPostHttp(url,param,HttpMethod.PUT);
|
|
|
responseBody = response.getBody();
|
|
|
}
|
|
|
return responseBody;
|
|
|
//https://www.yuque.com/u1400669/kb/os58z7#YYazO
|
|
|
}
|
|
|
public JSONObject videoUpload(HttpServletRequest request){
|
|
|
|
|
|
|
|
|
return null;
|
|
|
//https://www.yuque.com/u1400669/kb/nu22ks#nQXzO
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 消息解密
|
|
|
* @param sSrc
|
|
|
* @param sKey
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String messageDecrypt(String sSrc, String sKey) throws Exception {
|
|
|
try {
|
|
|
byte[] raw = sKey.getBytes("utf-8");
|
|
|
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
|
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
|
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
|
|
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|
|
|
byte[] encrypted1 = Base64.decode(sSrc);//先用base64解密
|
|
|
byte[] original = cipher.doFinal(encrypted1);
|
|
|
return new String(original, "utf-8");
|
|
|
} catch (Exception ex) {
|
|
|
logger.error("AES解密异常:Str"+sSrc+"\nkey:"+sKey);
|
|
|
throw new Exception("解密失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) throws Exception {
|
|
|
// String mess = "9uFqSq7m0HCj0vboHqVI5XLmnlS+ZB4S2k2gH6CZyUMXIod2OP+UK7ibjP27v9OX";
|
|
|
// String skey="PU4eJqa7j9GMst8P";
|
|
|
// String response = messageDecrypt(mess,skey);
|
|
|
// System.out.println(response);
|
|
|
// JSONObject tmp = JSONObject.parseObject(response);
|
|
|
// System.out.println("1");
|
|
|
// }
|
|
|
|
|
|
{
|
|
|
msgType.put("10001","离岗检测消息");
|
|
|
msgType.put("10002","身份识别消息");
|
|
|
msgType.put("10003","回头客消息");
|
|
|
msgType.put("10004","门禁事件消息");
|
|
|
msgType.put("10005","设备报警消息");
|
|
|
msgType.put("10006","设备上线离线消息");
|
|
|
msgType.put("10007","门禁权限同步消息");
|
|
|
msgType.put("10008","消防设备消息");
|
|
|
msgType.put("10009","企业数据变更消息");
|
|
|
msgType.put("10010","智能联动消息");
|
|
|
msgType.put("10011","海康设备透传消息");
|
|
|
msgType.put("10012","通道关联状态上报消息");
|
|
|
msgType.put("10013","智能控制设备状态消息");
|
|
|
msgType.put("10014","托管设备增删消息");
|
|
|
msgType.put("10016","门铃消息");
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|