media_player_client/src/common/RemoteDataExangeProcesser.ts

407 lines
13 KiB
TypeScript
Raw Normal View History

2021-08-26 15:42:34 +08:00
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;
2021-08-26 15:42:34 +08:00
constructor(options: OptionsType) {
this.options = options;
EventBus.getInstance().on(EventNamesDefine.WebSocketClose, () => {
if (this.plan_notify) {
this.plan_notify(void 0);
this.plan_notify = null;
}
});
2021-08-26 15:42:34 +08:00
}
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) {
(<any>GlobalData.getInstance().applicationConfig)[temp.key] =
temp.value;
$store.commit(
"setWallCol",
global_data.applicationConfig.wall_col
);
$store.commit(
"setWallRow",
global_data.applicationConfig.wall_row
);
2021-12-15 16:42:19 +08:00
$store.commit(
"setDeviceScreenWidth",
global_data.applicationConfig.screen_width
);
$store.commit(
"setDeviceScreenHeight",
global_data.applicationConfig.screen_height
);
$store.commit(
"setPowerOnPlan",
global_data.applicationConfig.power_on_plan
);
2021-08-26 15:42:34 +08:00
}
}
}
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;
}
}
2021-08-26 15:42:34 +08:00
}
}
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.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;
2022-01-18 08:52:51 +08:00
case Protocol.Commands.kFanTemperature:
{
const temp = JSON.parse(
notify.data
) as Protocol.FanTemperatureNotifyEntity;
if (temp) {
$store.commit("setFanTemp", temp.fan_temp);
}
}
break;
2021-08-26 15:42:34 +08:00
}
} catch {}
}
private unRegisteEvents() {
EventBus.getInstance().off(
EventNamesDefine.NotifyMessage,
this.onNotifyMessage
);
}
disable() {
if (this.flag) {
this.unRegisteEvents();
}
}
}