import { Protocol } from "src/entities/WSProtocol"; import GlobalData from "src/common/GlobalData"; import { Common } from "./Common"; import EventBus, { EventNamesDefine } from "./EventBus"; import OptionsType from "./OptionsType"; export default class Initializer { private options: OptionsType; constructor(options: OptionsType) { this.options = options; } private async getSignalSources() { const global_data = GlobalData.getInstance(); return (await global_data .getCurrentClient() ?.getSignalSources()) as Protocol.GetSignalSourcesResponse; } private async getModes() { const global_data = GlobalData.getInstance(); return (await global_data .getCurrentClient() ?.getModes()) as Protocol.GetModesResponseEntity; } private async getPlans() { const global_data = GlobalData.getInstance(); return (await global_data .getCurrentClient() ?.getPlans()) as Protocol.GetPlansResponseEntity; } private async getPollings() { const global_data = GlobalData.getInstance(); return (await global_data .getCurrentClient() ?.getPollings()) as Protocol.GetPollingsResponseEntity; } private async initSignalSourceTree() { const options = this.options; const $store = options?.$store; if ($store) { try { let response = await this.getSignalSources(); if (response) { $store.commit("buildSignalSourceTree", { options, response }); $store.commit("setArrayValue", { name: "signal_sources", value: response.signal_sources, }); GlobalData.getInstance().signal_source = response.signal_sources; } } catch (e) { console.error(e); } } } private async initModeTree() { const options = this.options; const $store = options.$store; if ($store) { try { let response = await this.getModes(); if (response) { $store.commit("buildModeTree", { options, response }); $store.commit("setArrayValue", { name: "modes", value: response.modes, }); GlobalData.getInstance().modes = response.modes; } } catch (e) { console.error(e); } } } private async initPlanTree() { const options = this.options; const $store = options.$store; if ($store) { try { let response = await this.getPlans(); if (response) { $store.commit("buildPlanTree", { options, response }); $store.commit("setArrayValue", { name: "plans", value: response.plans, }); GlobalData.getInstance().plans = response.plans; } } catch (e) { console.error(e); } } } private async initPollingTree() { const options = this.options; const $store = options.$store; if ($store) { try { let response = await this.getPollings(); if (response) { $store.commit("buildPollingTree", { options, response }); $store.commit("setArrayValue", { name: "pollings", value: response.pollings, }); GlobalData.getInstance().pollings = response.pollings; } } catch (e) { console.error(e); } } } private async getApplicationConfig() { const global_data = GlobalData.getInstance(); global_data.applicationConfig = ( await global_data.getCurrentClient()?.getApplicationSettins() )?.config; const options = this.options; let $store = options.$store; if (global_data.applicationConfig && $store) { $store.commit("setWallCol", global_data.applicationConfig.wall_col); $store.commit("setWallRow", global_data.applicationConfig.wall_row); $store.commit( "setPowerOnPlan", global_data.applicationConfig.power_on_plan ); } } private async getWindows() { const options = this.options; const global_data = GlobalData.getInstance(); let windows = (await global_data.getCurrentClient()?.getWindows())?.windows; let $store = options.$store; if (windows && $store) { $store.commit("clearWindows"); $store.commit("setWindows", windows); } } private async getCurrentRunningPlan() { const plan_response = await GlobalData.getInstance() .getCurrentClient() ?.getCurrentRunningPlan(); if (plan_response && plan_response.running && plan_response.plan) { const packet = new Protocol.PacketEntity(); packet.flag = Protocol.PacketEntity.FLAG_NOTIFY; packet.command = Protocol.Commands.kCurrentRunningPlanStateChanged; EventBus.getInstance().emit(EventNamesDefine.NotifyMessage, { packet, data: JSON.stringify(plan_response), }); } } async initialize() { const global_data = GlobalData.getInstance(); let client = global_data.getCurrentClient(); if (client) { while (!client.is_login) { await Common.waitFor(100); } // get device attribute { await this.getApplicationConfig(); GlobalData.getInstance() .getCurrentClient() ?.getDeviceAttribute() .then((response) => { if (response && typeof response.attribute != "undefined") { this.options.$store.commit( "setDeviceAttribute", response.attribute ); } }); } this.initSignalSourceTree(); this.initModeTree(); this.initPlanTree(); this.initPollingTree(); this.getCurrentRunningPlan(); this.getWindows(); const support_resolutions = await GlobalData.getInstance() .getCurrentClient() ?.getSupportResolutions(); if (support_resolutions) { this.options.$store.commit( "setDeviceScreenWidth", support_resolutions.screen_width ); this.options.$store.commit( "setDeviceScreenHeight", support_resolutions.screen_height ); this.options.$store.commit( "setDeviceScreenRefreshRate", support_resolutions.screen_refresh_rate ); } } } }