/
h0tnanny
/
IotPlatform
Обзор
Документация
Войти
/
h0tnanny
/
IotPlatform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/runtime/Main.ts
159 строк
4 KB
h0tnanny
Добавление функциональных требований
08 фев 2026, 23:02
08 фев 2026, 23:02
a8a23c5
Код
Авторство
О чём код?
import { Workflow } from '../entities/Workflow'; import { Equipment } from '../entities/Equipment'; import { cloneField } from '../entities/Field'; import { LogService } from '../services/LogService'; import { TriggerScheduler } from './TriggerScheduler'; /** * Main Runtime - основной цикл выполнения платформы */ export class Main { private readonly workflowList: Map<string, Workflow> = new Map(); private readonly equipmentList: Map<string, Equipment> = new Map(); private readonly triggerScheduler = new TriggerScheduler(); private isRunning = false; private runtimeInterval: NodeJS.Timeout | null = null; constructor(private readonly tickRate: number = 1000) {} /** * Добавляет workflow в runtime */ addWorkflow(workflow: Workflow): void { this.workflowList.set(workflow.id, workflow); } /** * Удаляет workflow из runtime */ removeWorkflow(id: string): boolean { const workflow = this.workflowList.get(id); if (workflow) { this.triggerScheduler.cleanup(id); this.workflowList.delete(id); return true; } return false; } /** * Добавляет источник данных (equipment) в runtime */ addEquipment(equipment: Equipment): void { this.equipmentList.set(equipment.id, equipment); } /** * Удаляет источник данных (equipment) из runtime */ removeEquipment(id: string): boolean { const equipment = this.equipmentList.get(id); if (equipment) { equipment.stop(); this.equipmentList.delete(id); return true; } return false; } /** * Получает workflow по ID */ getWorkflow(id: string): Workflow | undefined { return this.workflowList.get(id); } /** * Получает equipment по ID */ getEquipment(id: string): Equipment | undefined { return this.equipmentList.get(id); } /** * Получает все workflow */ getAllWorkflows(): Workflow[] { return Array.from(this.workflowList.values()); } /** * Получает все equipment */ getAllEquipment(): Equipment[] { return Array.from(this.equipmentList.values()); } /** * Запускает runtime loop */ start(): void { if (this.isRunning) { return; } this.isRunning = true; console.log(`[App] Runtime запущен (Tick Rate: ${this.tickRate}ms)`); LogService.getInstance().info('system', `Runtime запущен (Tick Rate: ${this.tickRate}ms)`); this.runtimeInterval = setInterval(() => { this.tick(); }, this.tickRate); } /** * Останавливает runtime loop */ stop(): void { if (!this.isRunning) { return; } this.isRunning = false; if (this.runtimeInterval) { clearInterval(this.runtimeInterval); this.runtimeInterval = null; } // Останавливаем все equipment for (const equipment of this.equipmentList.values()) { equipment.stop(); } console.log('[App] Runtime остановлен'); LogService.getInstance().info('system', 'Runtime остановлен'); } /** * Одна итерация runtime loop */ private tick(): void { this.syncData(); this.triggerScheduler.checkTriggers(this.getAllWorkflows()); } /** * Синхронизирует данные из источников данных (Equipment) в Workflow */ private syncData(): void { for (const workflow of this.workflowList.values()) { if (!workflow.isActivated) { continue; } for (const equipment of this.equipmentList.values()) { const equipmentFields = equipment.getFieldsCopy(); for (const field of equipmentFields) { const existingField = workflow.variableList.get(field.name); if (existingField) { existingField.value = field.value; } else { workflow.addVariable(cloneField(field)); } } } } } }