import { Protocol } from "src/entities/WSProtocol"; import { NotifyMessage } from "./ClientConnection"; import EventBus, { EventNamesDefine } from "./EventBus"; import GlobalData from "./GlobalData"; import OptionsType from "./OptionsType"; export default class RemoteDataExangeProcesser { private flag = false; private options: OptionsType; private plan_notify: any = null; constructor(options: OptionsType) { this.options = options; EventBus.getInstance().on(EventNamesDefine.WebSocketClose, () => { if (this.plan_notify) { this.plan_notify(void 0); this.plan_notify = null; } }); } enable() { if (!this.flag) { this.registeEvent(); } } private registeEvent() { EventBus.getInstance().on(EventNamesDefine.NotifyMessage, (notify) => this.onNotifyMessage(notify) ); } private onNotifyMessage(notify: NotifyMessage) { const $store = this.options.$store; const $t = this.options.$t; const $q = this.options.$q; try { switch (notify.packet.command) { case Protocol.Commands.kSetApplicationConfig: { let temp = JSON.parse( notify.data ) as Protocol.ApplicationConfigChangeNotifyEntity; if (temp) { let global_data = GlobalData.getInstance(); if (global_data && global_data.applicationConfig) { if (temp.key == "registered") { global_data.applicationConfig.registered = JSON.parse( temp.value ); } else { (global_data.applicationConfig)[temp.key] = temp.value; $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 ); } } } } break; case Protocol.Commands.kCurrentRunningPlanStateChanged: { const temp = JSON.parse( notify.data ) as Protocol.PlanRunningStateChangeNotifyEntity; if (temp && temp.plan) { if (temp.running) { $store.commit("setCurrentRunningPlan", temp.plan.uuid); if (this.plan_notify) { this.plan_notify(void 0); this.plan_notify = null; } this.plan_notify = $q.notify({ position: "center", color: "positive", timeout: 0, // we want to be in control when it gets dismissed spinner: true, message: $t.t("plan") + ": " + temp.plan.name + " " + $t.t("running now") + "!", }); } else { $store.commit("setCurrentRunningPlan", ""); if (this.plan_notify) { this.plan_notify(void 0); this.plan_notify = null; } } } } break; case Protocol.Commands.kRpcAddMode: { const temp = JSON.parse( notify.data ) as Protocol.ModeAddNotifyEntity; if (temp) { GlobalData.getInstance().modes.push(temp.mode); $store.commit("addModeTreeItem", { parent: temp.mode.group_uuid, is_group: false, item_data: temp.mode, }); } } break; case Protocol.Commands.kRpcDeleteMode: { const temp = JSON.parse( notify.data ) as Protocol.ModeDeleteNotifyEntity; if (temp) { let pos = GlobalData.getInstance().modes.findIndex( (element) => element && element.uuid == temp.uuid ); if (pos != -1) { GlobalData.getInstance().modes.splice(pos, 1); } $store.commit("deleteModeTreeItem", { is_group: false, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditMode: { const temp = JSON.parse( notify.data ) as Protocol.ModeEditNotifyEntity; if (temp) { let pos = GlobalData.getInstance().modes.findIndex( (element) => element && element.uuid == temp.mode.uuid ); if (pos != -1) { GlobalData.getInstance().modes[pos] = temp.mode; } $store.commit("setModeTreeItem", { is_group: false, item_data: temp.mode, }); } } break; case Protocol.Commands.kRpcAddModeGroup: { const temp = JSON.parse( notify.data ) as Protocol.ModeGroupAddNotifyEntity; if (temp) { $store.commit("addModeTreeItem", { parent: temp.mode_group.parent_uuid, is_group: true, item_data: temp.mode_group, }); } } break; case Protocol.Commands.kRpcDeleteModeGroup: { const temp = JSON.parse( notify.data ) as Protocol.ModeGroupDeleteNotifyEntity; if (temp) { $store.commit("deleteModeTreeItem", { is_group: true, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditModeGroup: { const temp = JSON.parse( notify.data ) as Protocol.ModeGroupEditNotifyEntity; if (temp) { $store.commit("setModeTreeItem", { is_group: true, item_data: temp.mode_group, }); } } break; case Protocol.Commands.kRpcAddPlan: { const temp = JSON.parse( notify.data ) as Protocol.PlanAddNotifyEntity; if (temp) { GlobalData.getInstance().plans.push(temp.plan); $store.commit("addPlanTreeItem", { parent: temp.plan.group_uuid, is_group: false, item_data: temp.plan, }); } } break; case Protocol.Commands.kRpcDeletePlan: { const temp = JSON.parse( notify.data ) as Protocol.PlanDeleteNotifyEntity; if (temp) { let pos = GlobalData.getInstance().plans.findIndex( (element) => element && element.uuid == temp.uuid ); if (pos != -1) { GlobalData.getInstance().plans.splice(pos, 1); } $store.commit("deletePlanTreeItem", { is_group: false, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditPlan: { const temp = JSON.parse( notify.data ) as Protocol.PlanEditNotifyEntity; if (temp) { let pos = GlobalData.getInstance().plans.findIndex( (element) => element && element.uuid == temp.plan.uuid ); if (pos != -1) { GlobalData.getInstance().plans[pos] = temp.plan; } $store.commit("setPlanTreeItem", { is_group: false, item_data: temp.plan, }); } } break; case Protocol.Commands.kRpcAddPlanGroup: { const temp = JSON.parse( notify.data ) as Protocol.PlanGroupAddNotifyEntity; if (temp) { $store.commit("addPlanTreeItem", { parent: temp.plan_group.parent_uuid, is_group: true, item_data: temp.plan_group, }); } } break; case Protocol.Commands.kRpcDeletePlanGroup: { const temp = JSON.parse( notify.data ) as Protocol.PlanGroupDeleteNotifyEntity; if (temp) { $store.commit("deletePlanTreeItem", { is_group: true, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditPlanGroup: { const temp = JSON.parse( notify.data ) as Protocol.PlanGroupEditNotifyEntity; if (temp) { $store.commit("setPlanTreeItem", { is_group: true, item_data: temp.plan_group, }); } } break; case Protocol.Commands.kRpcAddPolling: { const temp = JSON.parse( notify.data ) as Protocol.PollingAddNotifyEntity; if (temp) { GlobalData.getInstance().pollings.push(temp.polling); $store.commit("addPollingTreeItem", { parent: temp.polling.group_uuid, is_group: false, item_data: temp.polling, }); } } break; case Protocol.Commands.kRpcDeletePolling: { const temp = JSON.parse( notify.data ) as Protocol.PollingDeleteNotifyEntity; if (temp) { let pos = GlobalData.getInstance().pollings.findIndex( (element) => element && element.uuid == temp.uuid ); if (pos != -1) { GlobalData.getInstance().pollings.splice(pos, 1); } $store.commit("deletePollingTreeItem", { is_group: false, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditPolling: { const temp = JSON.parse( notify.data ) as Protocol.PollingEditNotifyEntity; if (temp) { let pos = GlobalData.getInstance().pollings.findIndex( (element) => element && element.uuid == temp.polling.uuid ); if (pos != -1) { GlobalData.getInstance().pollings[pos] = temp.polling; } $store.commit("setPollingTreeItem", { is_group: false, item_data: temp.polling, }); } } break; case Protocol.Commands.kRpcAddPollingGroup: { const temp = JSON.parse( notify.data ) as Protocol.PollingGroupAddNotifyEntity; if (temp) { $store.commit("addPollingTreeItem", { parent: temp.polling_group.parent_uuid, is_group: true, item_data: temp.polling_group, }); } } break; case Protocol.Commands.kRpcDeletePollingGroup: { const temp = JSON.parse( notify.data ) as Protocol.PollingGroupDeleteNotifyEntity; if (temp) { $store.commit("deletePollingTreeItem", { is_group: true, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditPollingGroup: { const temp = JSON.parse( notify.data ) as Protocol.PollingGroupEditNotifyEntity; if (temp) { $store.commit("setPollingTreeItem", { is_group: true, item_data: temp.polling_group, }); } } break; case Protocol.Commands.kRpcAddSignalSource: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceAddNotifyEntity; if (temp) { GlobalData.getInstance().signal_source.push(temp.signal_source); $store.commit("addSignalSourceTreeItem", { parent: temp.signal_source.group_uuid, is_group: false, item_data: temp.signal_source, }); } } break; case Protocol.Commands.kRpcDeleteSignalSource: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceDeleteNotifyEntity; if (temp) { $store.commit("deleteSignalSourceTreeItem", { is_group: false, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditSignalSource: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceEditNotifyEntity; if (temp) { $store.commit("setSignalSourceTreeItem", { is_group: false, item_data: temp.signal_source, }); } } break; case Protocol.Commands.kRpcAddSignalSourceGroup: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceGroupAddNotifyEntity; if (temp) { $store.commit("addSignalSourceTreeItem", { parent: temp.signal_source_group.parent_uuid, is_group: true, item_data: temp.signal_source_group, }); } } break; case Protocol.Commands.kRpcDeleteSignalSourceGroup: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceGroupDeleteNotifyEntity; if (temp) { $store.commit("deleteSignalSourceTreeItem", { is_group: true, uuid: temp.uuid, }); } } break; case Protocol.Commands.kRpcEditSignalSourceGroup: { const temp = JSON.parse( notify.data ) as Protocol.SignalSourceGroupEditNotifyEntity; if (temp) { $store.commit("setSignalSourceTreeItem", { is_group: true, item_data: temp.signal_source_group, }); } } break; case Protocol.Commands.kFanTemperature: { const temp = JSON.parse( notify.data ) as Protocol.FanTemperatureNotifyEntity; if (temp) { $store.commit("setFanTemp", temp.fan_temp); } } break; case Protocol.Commands.kDesktopResolutionChangedNotify: { const temp = JSON.parse( notify.data ) as Protocol.DesktopResolutionChangedNotifyEntity; if (temp) { $store.commit("setDeviceScreenConnectState", true); $store.commit("setDeviceScreenWidth", temp.width); $store.commit("setDeviceScreenHeight", temp.height); $store.commit("setDeviceScreenRefreshRate", temp.refresh_rate); } } break; case Protocol.Commands.kDesktopDisconnectNotify: { const temp = JSON.parse( notify.data ) as Protocol.DesktopDisconnectNotifyEntity; if (temp) { $store.commit("setDeviceScreenConnectState", false); } } break; } } catch {} } private unRegisteEvents() { EventBus.getInstance().off( EventNamesDefine.NotifyMessage, this.onNotifyMessage ); } disable() { if (this.flag) { this.unRegisteEvents(); } } }