This commit is contained in:
2025-10-10 10:08:05 +08:00
parent ed0527bf27
commit 204dd4781b
6 changed files with 235 additions and 816 deletions

View File

@@ -3,7 +3,7 @@ import { EventBuilderImpl } from '@/events/impl/EventBuilderImpl'
import type { IEventBuilder } from '@/events/IEventBuilder'
import type {
EventCommunicationEvents,
WindowFormDataUpdateParams,
WindowFormDataUpdateParams
} from './EventCommunicationService'
import type { ResourceType } from './ResourceService'
@@ -19,36 +19,26 @@ import { externalAppDiscovery } from './ExternalAppDiscovery'
* 系统服务配置接口
*/
export interface SystemServiceConfig {
debug?: boolean
maxMemoryUsage?: number
maxCpuUsage?: number
enablePerformanceMonitoring?: boolean
enableSecurityAudit?: boolean
autoCleanup?: boolean
cleanupInterval?: number
debug?: boolean // 是否开启调试模式
autoCleanup?: boolean // 是否自动清理
cleanupInterval?: number // 自动清理间隔
}
/**
* 系统状态接口
*/
export interface SystemStatus {
initialized: boolean
running: boolean
initialized: boolean // 系统是否初始化完成
running: boolean // 系统是否运行中
servicesStatus: {
windowService: boolean
resourceService: boolean
eventService: boolean
sandboxEngine: boolean
lifecycleManager: boolean
windowService: boolean // 窗体服务是否启动
resourceService: boolean // 资源服务是否启动
eventService: boolean // 事件服务是否启动
sandboxEngine: boolean // 沙箱引擎是否启动
lifecycleManager: boolean // 生命周期管理器是否启动
}
performance: {
memoryUsage: number
cpuUsage: number
activeApps: number
activeWindows: number
}
uptime: number
lastError?: string
uptime: number // 系统运行时间
lastError?: string // 最后一次错误
}
/**
@@ -88,15 +78,9 @@ export class SystemServiceIntegration {
resourceService: false,
eventService: false,
sandboxEngine: false,
lifecycleManager: false,
lifecycleManager: false
},
performance: {
memoryUsage: 0,
cpuUsage: 0,
activeApps: 0,
activeWindows: 0,
},
uptime: 0,
uptime: 0
})
// 性能监控
@@ -106,13 +90,9 @@ export class SystemServiceIntegration {
constructor(config: SystemServiceConfig = {}) {
this.config = {
debug: false,
maxMemoryUsage: 1024, // 1GB
maxCpuUsage: 80, // 80%
enablePerformanceMonitoring: true,
enableSecurityAudit: true,
autoCleanup: true,
cleanupInterval: 5 * 60 * 1000, // 5分钟
...config,
...config
}
this.startTime = new Date()
@@ -141,11 +121,6 @@ export class SystemServiceIntegration {
// 设置SDK消息处理
this.setupSDKMessageHandling()
// 启动性能监控
if (this.config.enablePerformanceMonitoring) {
this.startPerformanceMonitoring()
}
// 启动自动清理
if (this.config.autoCleanup) {
this.startAutoCleanup()
@@ -167,7 +142,7 @@ export class SystemServiceIntegration {
// 发送系统就绪事件
this.eventService.sendMessage('system', 'system-ready', {
timestamp: new Date(),
services: Object.keys(this.systemStatus.servicesStatus),
services: Object.keys(this.systemStatus.servicesStatus)
})
} catch (error) {
console.error('系统服务初始化失败:', error)
@@ -180,7 +155,6 @@ export class SystemServiceIntegration {
* 获取系统状态
*/
getSystemStatus(): SystemStatus {
this.updateSystemStatus()
return { ...this.systemStatus }
}
@@ -240,7 +214,7 @@ export class SystemServiceIntegration {
return {
success: true,
data: result,
requestId,
requestId
}
} catch (error) {
console.error('SDK调用失败:', error)
@@ -248,7 +222,7 @@ export class SystemServiceIntegration {
return {
success: false,
error: error instanceof Error ? error.message : String(error),
requestId,
requestId
}
}
}
@@ -324,8 +298,6 @@ export class SystemServiceIntegration {
console.log('系统服务已关闭')
}
// 私有方法
/**
* 初始化所有服务
*/
@@ -356,7 +328,7 @@ export class SystemServiceIntegration {
this.windowService,
this.resourceService,
this.eventService,
this.sandboxEngine,
this.sandboxEngine
)
this.systemStatus.servicesStatus.lifecycleManager = true
}
@@ -380,15 +352,15 @@ export class SystemServiceIntegration {
'onWindowStateChanged',
(windowId: string, newState: string, oldState: string) => {
console.log(
`[SystemIntegration] 接收到窗体状态变化事件: ${windowId} ${oldState} -> ${newState}`,
`[SystemIntegration] 接收到窗体状态变化事件: ${windowId} ${oldState} -> ${newState}`
)
this.eventService.sendMessage('system', 'window-state-change', {
windowId,
newState,
oldState,
oldState
})
console.log(`[SystemIntegration] 已发送 window-state-change 消息到事件通信服务`)
},
}
)
// 监听窗体关闭事件,自动停止对应的应用
@@ -412,24 +384,13 @@ export class SystemServiceIntegration {
// 监听窗体数据更新事件
this.eventBus.addEventListener('onWindowFormDataUpdate', (data: WindowFormDataUpdateParams) => {
console.log(`[SystemIntegration] 接收到窗体数据更新事件:`, data)
// 只有在有订阅者时才发送消息
if (this.eventService.getChannelSubscriberCount('window-form-data-update') > 0) {
this.eventService.sendMessage('system', 'window-form-data-update', data)
console.log(`[SystemIntegration] 已发送 window-form-data-update 消息到事件通信服务`)
} else {
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-data-update 消息`)
}
this.eventService.sendMessage('system', 'window-form-data-update', data)
})
// 监听窗体调整尺寸开始事件
this.eventBus.addEventListener('onWindowFormResizeStart', (windowId: string) => {
console.log(`[SystemIntegration] 接收到窗体调整尺寸开始事件: ${windowId}`)
// 只有在有订阅者时才发送消息
if (this.eventService.getChannelSubscriberCount('window-form-resize-start') > 0) {
this.eventService.sendMessage('system', 'window-form-resize-start', { windowId })
} else {
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resize-start 消息`)
}
this.eventService.sendMessage('system', 'window-form-resize-start', { windowId })
})
// 监听窗体调整尺寸过程中事件
@@ -438,43 +399,21 @@ export class SystemServiceIntegration {
(windowId: string, width: number, height: number) => {
console.log(`[SystemIntegration] 接收到窗体调整尺寸过程中事件: ${windowId}`, {
width,
height,
height
})
// 只有在有订阅者时才发送消息
if (this.eventService.getChannelSubscriberCount('window-form-resizing') > 0) {
this.eventService.sendMessage('system', 'window-form-resizing', {
windowId,
width,
height,
})
} else {
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resizing 消息`)
}
},
this.eventService.sendMessage('system', 'window-form-resizing', {
windowId,
width,
height
})
}
)
// 监听窗体调整尺寸结束事件
this.eventBus.addEventListener('onWindowFormResizeEnd', (windowId: string) => {
console.log(`[SystemIntegration] 接收到窗体调整尺寸结束事件: ${windowId}`)
// 只有在有订阅者时才发送消息
if (this.eventService.getChannelSubscriberCount('window-form-resize-end') > 0) {
this.eventService.sendMessage('system', 'window-form-resize-end', { windowId })
} else {
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resize-end 消息`)
}
this.eventService.sendMessage('system', 'window-form-resize-end', { windowId })
})
// 监听资源配额超出
this.eventBus.addEventListener(
'onResourceQuotaExceeded',
(appId: string, resourceType: ResourceType) => {
console.log(`[SystemIntegration] 接收到资源配额超出事件: ${appId} - ${resourceType}`)
this.eventService.sendMessage('system', 'resource-quota-exceeded', {
appId,
resourceType,
})
},
)
}
/**
@@ -503,9 +442,9 @@ export class SystemServiceIntegration {
iframe.contentWindow?.postMessage(
{
type: 'system:response',
...result,
...result
},
'*',
'*'
)
}
}
@@ -567,9 +506,9 @@ export class SystemServiceIntegration {
type: 'system:storage-response',
requestId,
result,
success,
success
},
'*',
'*'
)
}
}
@@ -660,7 +599,7 @@ export class SystemServiceIntegration {
const windowData = this.windowService.getWindow(windowId)
return {
width: windowData?.config.width,
height: windowData?.config.height,
height: windowData?.config.height
}
default:
@@ -714,7 +653,7 @@ export class SystemServiceIntegration {
status: response.status,
statusText: response.statusText,
headers: {} as Record<string, string>, // 简化headers处理
url: response.url,
url: response.url
}
: null
@@ -726,7 +665,7 @@ export class SystemServiceIntegration {
return {
requestCount: requests.length,
failureCount: requests.filter((r) => r.status && r.status >= 400).length,
averageTime: 0, // 需要实现时间统计
averageTime: 0 // 需要实现时间统计
}
default:
@@ -750,7 +689,7 @@ export class SystemServiceIntegration {
this.sandboxEngine.sendMessage(app.sandboxId, {
type: 'system:event',
subscriptionId: data.subscriptionId,
message,
message
})
}
})
@@ -800,10 +739,10 @@ export class SystemServiceIntegration {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
screenResolution: {
width: screen.width,
height: screen.height,
height: screen.height
},
colorDepth: screen.colorDepth,
pixelRatio: window.devicePixelRatio,
pixelRatio: window.devicePixelRatio
}
case 'getAppInfo':
@@ -815,7 +754,7 @@ export class SystemServiceIntegration {
version: app.version,
permissions: app.manifest.permissions,
createdAt: app.installedAt,
lastActiveAt: app.lastActiveAt,
lastActiveAt: app.lastActiveAt
}
: null
@@ -851,32 +790,6 @@ export class SystemServiceIntegration {
return null
}
/**
* 开始性能监控
*/
private startPerformanceMonitoring(): void {
this.performanceInterval = setInterval(() => {
this.updateSystemStatus()
// 检查性能阈值
if (this.systemStatus.performance.memoryUsage > this.config.maxMemoryUsage!) {
this.eventService.sendMessage('system', 'performance-alert', {
type: 'memory',
usage: this.systemStatus.performance.memoryUsage,
limit: this.config.maxMemoryUsage,
})
}
if (this.systemStatus.performance.cpuUsage > this.config.maxCpuUsage!) {
this.eventService.sendMessage('system', 'performance-alert', {
type: 'cpu',
usage: this.systemStatus.performance.cpuUsage,
limit: this.config.maxCpuUsage,
})
}
}, 10000) // 每10秒检查一次
}
/**
* 开始自动清理
*/
@@ -894,20 +807,6 @@ export class SystemServiceIntegration {
}, this.config.cleanupInterval!)
}
/**
* 更新系统状态
*/
private updateSystemStatus(): void {
this.systemStatus.uptime = Date.now() - this.startTime.getTime()
this.systemStatus.performance.activeApps = this.lifecycleManager?.getRunningApps().length || 0
this.systemStatus.performance.activeWindows = this.windowService?.getAllWindows().length || 0
// 简化的内存和CPU使用率计算
this.systemStatus.performance.memoryUsage =
(performance as any).memory?.usedJSHeapSize / 1024 / 1024 || 0
this.systemStatus.performance.cpuUsage = Math.random() * 20 // 模拟CPU使用率
}
/**
* 检查是否已初始化
*/
@@ -927,7 +826,7 @@ export class SystemServiceIntegration {
})
window.addEventListener('unhandledrejection', (event) => {
console.error('未处理的Promise拒绝:', event.reason)
// console.error('未处理的Promise拒绝:', event.reason)
this.systemStatus.lastError = event.reason?.message || '未处理的Promise拒绝'
})
}