zhgdyunapp/utils/sos-uniapp-wrapper.js
2025-09-11 18:14:57 +08:00

383 lines
9.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* SOS SDK uniapp包装器
* 专门为uniapp环境设计的SOS SDK包装器解决DOM兼容性问题
*/
// 首先加载DOM polyfill
require('./dom-polyfill.js')
// 修复tmedia_api_add_js_scripts未定义的问题
if (typeof window.tmedia_api_add_js_scripts === 'undefined') {
window.tmedia_api_add_js_scripts = function(position, ...scriptPaths) {
console.log('tmedia_api_add_js_scripts called:', position, scriptPaths);
return true;
};
}
// 确保其他必要的全局变量存在
if (typeof window.__b_release_mode === 'undefined') {
window.__b_release_mode = true;
}
// 确保URL对象存在
if (typeof window.URL === 'undefined') {
window.URL = window.URL || window.webkitURL || {};
}
// 确保performance对象存在
if (typeof window.performance === 'undefined') {
window.performance = window.performance || {};
window.performance.now = window.performance.now || (function() {
var start = Date.now();
return function() {
return Date.now() - start;
};
})();
}
// 加载SOS库
require('./sos-v1.0.17.js');
// 等待SOS库加载完成
setTimeout(() => {
if (typeof window.SOS !== 'undefined' && typeof window.SOS.SDK === 'function') {
console.log('SOS.SDK loaded successfully in uniapp environment');
} else {
console.error('SOS.SDK not found');
}
}, 100);
/**
* uniapp环境下的SOS SDK包装类
* 提供uniapp兼容的接口
*/
class SOSUniAppWrapper {
constructor(config = {}) {
if (typeof window.SOS === 'undefined' || typeof window.SOS.SDK !== 'function') {
throw new Error('SOS.SDK is not available. Please ensure sos-v1.0.17.js is loaded correctly.');
}
// 为uniapp环境创建必要的DOM元素
this.createRequiredElements();
// 创建SDK实例
this.sdk = new window.SOS.SDK(config);
this.isInitialized = false;
this.eventListeners = new Map();
}
/**
* 创建SOS SDK需要的DOM元素
*/
createRequiredElements() {
// 创建音频元素
const audioElement = document.createElement('audio');
audioElement.id = 'sos-audio-element';
audioElement.style.display = 'none';
document.body.appendChild(audioElement);
// 创建视频元素
const videoElement = document.createElement('video');
videoElement.id = 'sos-video-element';
videoElement.style.display = 'none';
document.body.appendChild(videoElement);
// 创建canvas元素
const canvasElement = document.createElement('canvas');
canvasElement.id = 'sos-canvas-element';
canvasElement.style.display = 'none';
document.body.appendChild(canvasElement);
}
/**
* 初始化SDK
*/
init() {
return new Promise((resolve, reject) => {
try {
this.sdk.init();
this.isInitialized = true;
resolve();
} catch (error) {
reject(error);
}
});
}
/**
* 销毁SDK
*/
destroy() {
if (this.sdk && this.isInitialized) {
this.sdk.destroy();
this.isInitialized = false;
}
// 清理事件监听器
this.eventListeners.clear();
}
/**
* 添加事件监听器
*/
addEventListener(event, callback) {
if (this.sdk) {
this.sdk.addEventListener(event, callback);
// 保存事件监听器以便后续清理
if (!this.eventListeners.has(event)) {
this.eventListeners.set(event, []);
}
this.eventListeners.get(event).push(callback);
}
}
/**
* 移除事件监听器
*/
removeEventListener(event) {
if (this.sdk) {
this.sdk.removeEventListener(event);
this.eventListeners.delete(event);
}
}
/**
* 获取设备列表
*/
deviceList(callback) {
if (this.sdk) {
return this.sdk.deviceList(callback);
}
}
/**
* 开始音频对讲
*/
audioSpeakStart(deviceId, callback) {
if (this.sdk) {
// 在uniapp中可能需要特殊处理音频元素
const audioElement = document.getElementById('sos-audio-element');
if (audioElement) {
// 设置音频元素属性
audioElement.setAttribute('autoplay', 'true');
audioElement.setAttribute('playsinline', 'true');
}
return this.sdk.audioSpeakStart(deviceId, callback);
}
}
/**
* 停止音频对讲
*/
audioSpeakStop(deviceId, callback) {
if (this.sdk) {
return this.sdk.audioSpeakStop(deviceId, callback);
}
}
/**
* 开始音频监听
*/
audioListenStart(deviceId, callback) {
if (this.sdk) {
const audioElement = document.getElementById('sos-audio-element');
if (audioElement) {
audioElement.setAttribute('autoplay', 'true');
audioElement.setAttribute('playsinline', 'true');
}
return this.sdk.audioListenStart(deviceId, callback);
}
}
/**
* 停止音频监听
*/
audioListenStop(deviceId, callback) {
if (this.sdk) {
return this.sdk.audioListenStop(deviceId, callback);
}
}
/**
* 开始视频监控
*/
videoMonitorStart(deviceId, callback) {
if (this.sdk) {
const videoElement = document.getElementById('sos-video-element');
if (videoElement) {
videoElement.setAttribute('autoplay', 'true');
videoElement.setAttribute('playsinline', 'true');
videoElement.setAttribute('muted', 'true');
}
return this.sdk.videoMonitorStart(deviceId, callback);
}
}
/**
* 停止视频监控
*/
videoMonitorStop(deviceId, callback) {
if (this.sdk) {
return this.sdk.videoMonitorStop(deviceId, callback);
}
}
/**
* 开始录音
*/
audioRecordStart(deviceId, callback) {
if (this.sdk) {
return this.sdk.audioRecordStart(deviceId, callback);
}
}
/**
* 停止录音
*/
audioRecordStop(callback) {
if (this.sdk) {
return this.sdk.audioRecordStop(callback);
}
}
/**
* 开始录像
*/
videoRecordStart(deviceId, callback) {
if (this.sdk) {
return this.sdk.videoRecordStart(deviceId, callback);
}
}
/**
* 停止录像
*/
videoRecordStop(deviceId, callback) {
if (this.sdk) {
return this.sdk.videoRecordStop(deviceId, callback);
}
}
/**
* 获取版本信息
*/
version() {
if (this.sdk) {
return this.sdk.version();
}
}
/**
* 检查是否为测试模式
*/
isTest() {
if (this.sdk) {
return this.sdk.isTest();
}
return false;
}
/**
* 获取设备基础信息
*/
deviceBaseInfoGet(deviceId, callback) {
if (this.sdk) {
return this.sdk.deviceBaseInfoGet(deviceId, callback);
}
}
/**
* 修改设备基础信息
*/
deviceBaseInfoMod(deviceId, data, callback) {
if (this.sdk) {
return this.sdk.deviceBaseInfoMod(deviceId, data, callback);
}
}
/**
* 获取设备告警记录
*/
deviceAlarmRecordList(deviceId, callback) {
if (this.sdk) {
return this.sdk.deviceAlarmRecordList(deviceId, callback);
}
}
/**
* 处理设备告警
*/
deviceAlarmDone(deviceId, callback) {
if (this.sdk) {
return this.sdk.deviceAlarmDone(deviceId, callback);
}
}
/**
* 接受设备告警
*/
deviceAlarmAccept(deviceId, callback) {
if (this.sdk) {
return this.sdk.deviceAlarmAccept(deviceId, callback);
}
}
/**
* 获取设备音量
*/
deviceVolumeGet(deviceId, callback) {
if (this.sdk) {
return this.sdk.deviceVolumeGet(deviceId, callback);
}
}
/**
* 设置设备音量
*/
deviceVolumeSet(deviceId, volume, callback) {
if (this.sdk) {
return this.sdk.deviceVolumeSet(deviceId, volume, callback);
}
}
/**
* 开始多播
*/
microphoneMutilBoradcastStart(deviceIds, callback) {
if (this.sdk) {
return this.sdk.microphoneMutilBoradcastStart(deviceIds, callback);
}
}
/**
* 停止多播
*/
microphoneMutilBoradcastStop(deviceIds, callback) {
if (this.sdk) {
return this.sdk.microphoneMutilBoradcastStop(deviceIds, callback);
}
}
/**
* 获取功能权限
*/
getFeaturePermission(callback) {
if (this.sdk) {
return this.sdk.getFeaturePermission(callback);
}
}
}
// 导出SOS SDK
module.exports = {
SDK: SOSUniAppWrapper,
// 也可以直接导出原始的SOS.SDK
OriginalSDK: typeof window.SOS !== 'undefined' ? window.SOS.SDK : null,
// 导出SOS对象
SOS: typeof window.SOS !== 'undefined' ? window.SOS : null
};
console.log('SOS uniapp wrapper loaded successfully');