import axios from 'axios';
|
import { ElMessage } from 'element-plus';
|
import EZUIKit from 'ezuikit-js';
|
import { YingShiControl } from './types';
|
|
export type YingShiCameraOptions = {
|
/**
|
* @description 播放器容器 DOM 的 id
|
*/
|
domId: string;
|
/** @description 设备序列号 */
|
deviceSerial: string;
|
/**
|
* @description 视频宽度,默认值为容器容器 DOM 宽度
|
*/
|
width?: number;
|
/**
|
* @description 视频高度,默认值为容器容器 DOM 高度
|
*/
|
height?: number;
|
};
|
export class YingShiCamera {
|
domId: string;
|
deviceSerial: string;
|
width?: number;
|
height?: number;
|
|
accessToken: string;
|
liveUrl: string;
|
|
private static readonly APP_KEY = '4dd1ebea34544429b55d0e12fef86156';
|
|
private static readonly SECRET = '151b699b9150fe1939af7b2fca29503c';
|
|
private static readonly BASE_URL = 'https://open.ys7.com/api';
|
|
constructor(options: YingShiCameraOptions) {
|
this.domId = options.domId;
|
this.deviceSerial = options.deviceSerial;
|
this.width = options.width;
|
this.height = options.height;
|
}
|
|
private static async executeCommand(deviceSerial: string, accessToken: string, command: YingShiControl) {
|
const res = await axios({
|
headers: {
|
'Content-Type': 'application/x-www-form-urlencoded',
|
},
|
url: 'https://open.ys7.com/api/lapp/device/ptz/start',
|
method: 'post',
|
data: {
|
accessToken: accessToken,
|
deviceSerial: deviceSerial,
|
channelNo: 1,
|
...command,
|
},
|
});
|
const data = (res as any).data.data;
|
return data;
|
}
|
|
private static async stopCommand(deviceSerial: string, accessToken: string, command: Omit<YingShiControl, 'speed'>) {
|
const res = await axios({
|
headers: {
|
'Content-Type': 'application/x-www-form-urlencoded',
|
},
|
url: 'https://open.ys7.com/api/lapp/device/ptz/start',
|
method: 'post',
|
data: {
|
accessToken: accessToken,
|
deviceSerial: deviceSerial,
|
channelNo: 1,
|
...command,
|
},
|
});
|
const data = (res as any).data.data;
|
return data;
|
}
|
|
/**
|
* 获取 accessToken
|
* @returns Promise<string>
|
*/
|
private static async getAccessToken() {
|
const res = await axios({
|
method: 'post',
|
url: `${YingShiCamera.BASE_URL}/lapp/token/get`,
|
headers: {
|
'Content-Type': 'application/x-www-form-urlencoded',
|
},
|
data: {
|
appKey: YingShiCamera.APP_KEY,
|
appSecret: YingShiCamera.SECRET,
|
},
|
}).catch((err) => {
|
ElMessage.error(err);
|
});
|
const token = (res as any).data.data.accessToken;
|
return token;
|
}
|
|
/**
|
* 获取直播地址
|
* @param accessToken
|
* @param deviceSerial
|
* @returns
|
*/
|
private static async getLiveUrl(accessToken: string, deviceSerial: string) {
|
const res = await axios({
|
method: 'post',
|
url: `${YingShiCamera.BASE_URL}/lapp/v2/live/address/get`,
|
headers: {
|
'Content-Type': 'application/x-www-form-urlencoded',
|
},
|
data: {
|
accessToken: accessToken,
|
deviceSerial: deviceSerial, //设备序列号,一个序列号对应一个设备
|
},
|
});
|
const url = (res as any).data.data.url;
|
return url;
|
}
|
|
private async initEZUIKitPlayer() {
|
const UIKitDEMO = new EZUIKit.EZUIKitPlayer({
|
id: this.domId,
|
url: this.liveUrl,
|
accessToken: this.accessToken,
|
width: this.width,
|
height: this.height,
|
});
|
|
await UIKitDEMO.play().catch((err) => {
|
ElMessage.error(err);
|
});
|
UIKitDEMO.openPtz();
|
}
|
|
async init() {
|
this.accessToken = await YingShiCamera.getAccessToken();
|
this.liveUrl = await YingShiCamera.getLiveUrl(this.accessToken, this.deviceSerial);
|
this.initEZUIKitPlayer();
|
}
|
async control(command: YingShiControl) {
|
const data = await YingShiCamera.executeCommand(this.deviceSerial, this.accessToken, command);
|
}
|
|
async stopControl(command: Omit<YingShiControl, 'speed'>) {
|
const data = await YingShiCamera.stopCommand(this.deviceSerial, this.accessToken, command);
|
}
|
}
|