media_player_client/src/common/ClientConnection.ts

700 lines
18 KiB
TypeScript
Raw Normal View History

import ReconnectingWebSocket from "reconnecting-websocket";
2021-08-09 10:59:16 +08:00
import NormalWindowRequestEntity from "src/entities/NormalWindowRequestEntity";
2021-08-26 10:43:31 +08:00
import { PlanEntity } from "src/entities/PlanEntity";
import { SignalSourceEntity } from "src/entities/SignalSourceEntity";
2021-09-29 14:21:33 +08:00
import SubtitleEntity from "src/entities/SubtitleEntity";
2021-08-05 14:23:45 +08:00
import { Protocol } from "src/entities/WSProtocol";
import EventBus, { EventNamesDefine } from "./EventBus";
2021-08-05 14:23:45 +08:00
class _RpcInfo {
send_timestamp: number;
timeout_timestamp: number;
callback: (is_fail: boolean, packet: Protocol.Commands, data: string) => void;
constructor(
timeout_timestamp: number,
callback: (
is_fail: boolean,
packet: Protocol.Commands,
data: string
) => void
) {
this.timeout_timestamp = timeout_timestamp;
this.send_timestamp = new Date().getTime();
this.callback = callback;
}
public reject() {
this.callback(true, new Protocol.Commands(), "");
}
public reslove(is_fail: boolean, packet: Protocol.Commands, data: string) {
this.callback(is_fail, packet, data);
}
}
2021-08-05 14:23:45 +08:00
export default class ClientConnection {
ws: ReconnectingWebSocket | null = null;
2021-08-05 14:23:45 +08:00
url = "";
user_name = "";
password = "";
_is_login = false;
_rpc_id_counter = 0;
rpc_map = new Map<number, _RpcInfo>();
2021-08-05 14:23:45 +08:00
public login_callback:
| ((this: ClientConnection, logined: boolean) => void)
| null = null;
constructor(
url: string,
user_name?: string | null,
password?: string | null
) {
this.url = url;
this.user_name = user_name ?? "";
this.password = password ?? "";
if (this.ws) {
this.ws.close();
}
this.ws = new ReconnectingWebSocket(url);
this.initializeWs();
setInterval(() => {
this.checkRpcTimeout();
}, 1000);
2021-08-05 14:23:45 +08:00
}
get is_connected() {
return this.ws && this.ws.readyState == WebSocket.OPEN;
}
get is_login() {
return this._is_login;
}
checkRpcTimeout() {
const current_datetime = new Date().getTime();
this.rpc_map.forEach((v, k, m) => {
if (current_datetime - v.send_timestamp > v.timeout_timestamp) {
v.reject();
m.delete(k);
}
});
}
2021-08-05 14:23:45 +08:00
initializeWs() {
if (this.ws) {
this.ws.onclose = (ev) => {
this.onClose(ev);
};
this.ws.onerror = (ev) => {
this.onError(ev);
};
this.ws.onopen = (ev) => {
this.onOpen(ev);
};
this.ws.onmessage = (ev) => {
this.onMessage(ev);
};
}
}
login() {
if (this.is_connected) {
const request = new Protocol.LoginRequest(this.user_name, this.password);
this.ws?.send(JSON.stringify(request));
}
}
onClose(ev: any) {
2021-08-05 14:23:45 +08:00
this._is_login = false;
2021-08-07 11:04:39 +08:00
EventBus.getInstance().emit(EventNamesDefine.WebSocketClose, this);
2021-08-05 14:23:45 +08:00
}
onError(ev: any) {
2021-08-05 14:23:45 +08:00
this._is_login = false;
2021-08-07 11:04:39 +08:00
EventBus.getInstance().emit(EventNamesDefine.WebSocketError, this);
}
onOpen(ev: any) {
2021-08-05 14:23:45 +08:00
this._is_login = false;
this.login();
}
onMessage(ev: MessageEvent) {
try {
const packet = JSON.parse(ev.data) as Protocol.PacketEntity;
2021-08-05 14:23:45 +08:00
if (packet) {
if (packet.has_exception) {
console.error(ev.data);
2021-12-22 08:24:26 +08:00
this.rpc_map.get(packet.rpc_id)?.reject();
this.rpc_map.delete(packet.rpc_id);
2021-08-05 14:23:45 +08:00
}
if (Protocol.Commands.AllCommands.has(packet.command)) {
if (
packet.flag == Protocol.PacketEntity.FLAG_RESPONSE ||
packet.flag == Protocol.PacketEntity.FLAG_NOTIFY
) {
if (packet.command == Protocol.Commands.kLogin) {
const login_response = JSON.parse(
ev.data
) as Protocol.LoginResponse;
if (login_response) {
this._is_login =
!login_response.has_exception && login_response.success;
2021-08-07 11:04:39 +08:00
if (this.is_login) {
EventBus.getInstance().emit(
EventNamesDefine.WebSocketConnected,
this
);
}
2021-08-05 14:23:45 +08:00
if (
this.login_callback &&
typeof this.login_callback == "function"
) {
this.login_callback(this._is_login);
}
}
2021-08-07 11:04:39 +08:00
} else if (this.rpc_map.has(packet.rpc_id)) {
this.rpc_map.get(packet.rpc_id)?.reslove(false, packet, ev.data);
this.rpc_map.delete(packet.rpc_id);
} else {
2021-08-12 08:28:52 +08:00
EventBus.getInstance().emit(
packet.flag == Protocol.PacketEntity.FLAG_NOTIFY
? EventNamesDefine.NotifyMessage
: packet.flag == Protocol.PacketEntity.FLAG_RESPONSE
? EventNamesDefine.ResponseMessage
: EventNamesDefine.UnKnow,
{
packet: packet,
data: ev.data,
}
);
2021-08-05 14:23:45 +08:00
}
}
} else {
console.error("unknow command: " + packet.command, packet);
2021-08-05 14:23:45 +08:00
}
}
} catch (e) {
console.error(e);
}
}
public async doRpc<_ResponseType>(
request: Protocol.PacketEntity
2021-08-05 17:26:27 +08:00
): Promise<_ResponseType | null> {
2021-08-05 14:23:45 +08:00
return new Promise((resolve, reject) => {
const rpc_id = ++this._rpc_id_counter;
if (this.rpc_map.has(rpc_id)) {
this.rpc_map.get(rpc_id)?.reject();
this.rpc_map.delete(rpc_id);
2021-08-05 14:23:45 +08:00
}
2021-08-05 17:26:27 +08:00
request.rpc_id = rpc_id;
2021-08-05 14:23:45 +08:00
this.rpc_map.set(
rpc_id,
new _RpcInfo(
request.timeout,
(is_fail: boolean, packet: Protocol.Commands, data: string) => {
if (is_fail) {
reject();
} else {
try {
const response = JSON.parse(data) as _ResponseType;
if (response) {
resolve(response);
} else {
reject();
}
} catch {
2021-08-05 14:23:45 +08:00
reject();
}
}
}
)
2021-08-05 14:23:45 +08:00
);
if (request) {
request.timeout;
}
this.ws?.send(JSON.stringify(request));
2021-08-05 14:23:45 +08:00
});
}
public async getSignalSources() {
2021-08-05 17:26:27 +08:00
try {
return await this.doRpc<Protocol.GetSignalSourcesResponse>(
new Protocol.GetSignalSourcesRequest()
);
2021-08-05 17:26:27 +08:00
} catch (e) {
console.error(e);
}
2021-08-05 14:23:45 +08:00
}
2021-08-25 17:30:02 +08:00
public async addSignalSourceGroup(parent_uuid: string, name: string) {
try {
return await this.doRpc<Protocol.AddSignalSourceGroupResponseEntity>(
new Protocol.AddSignalSourceGroupRequestEntity(0, parent_uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async editSignalSourceGroup(uuid: string, name: string) {
try {
return await this.doRpc<Protocol.EditSignalSourceGroupResponseEntity>(
new Protocol.EditSignalSourceGroupRequestEntity(0, uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async deleteSignalSourceGroup(uuid: string) {
try {
return await this.doRpc<Protocol.DeleteSignalSourceGroupResponseEntity>(
new Protocol.DeleteSignalSourceGroupRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
public async addSignalSource(item: SignalSourceEntity) {
try {
return await this.doRpc<Protocol.AddSignalSourceResponseEntity>(
new Protocol.AddSignalSourceRequestEntity(0, item)
);
} catch (e) {
console.error(e);
}
}
public async editSignalSource(item: SignalSourceEntity) {
try {
return await this.doRpc<Protocol.EditSignalSourceResponseEntity>(
new Protocol.EditSignalSourceRequestEntity(0, item)
);
} catch (e) {
console.error(e);
}
}
public async deleteSignalSource(uuid: string) {
try {
return await this.doRpc<Protocol.DeleteSignalSourceResponseEntity>(
new Protocol.DeleteSignalSourceRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
2021-08-24 15:20:01 +08:00
public async getModes() {
try {
return await this.doRpc<Protocol.GetModesResponseEntity>(
new Protocol.GetModesRequestEntity()
);
} catch (e) {
console.error(e);
}
}
2021-08-25 17:30:02 +08:00
public async addPlanGroup(group_uuid: string, name: string) {
try {
return await this.doRpc<Protocol.AddPlanGroupResponseEntity>(
new Protocol.AddPlanGroupRequestEntity(0, group_uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async editPlanGroup(uuid: string, name: string) {
try {
return await this.doRpc<Protocol.EditPlanGroupResponseEntity>(
new Protocol.EditPlanGroupRequestEntity(0, uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async deletePlanGroup(uuid: string) {
try {
return await this.doRpc<Protocol.DeletePlanGroupResponseEntity>(
new Protocol.DeletePlanGroupRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
2021-08-26 10:43:31 +08:00
public async addPlan(entity?: PlanEntity) {
2021-08-25 17:30:02 +08:00
try {
return await this.doRpc<Protocol.AddPlanResponseEntity>(
2021-08-26 10:43:31 +08:00
new Protocol.AddPlanRequestEntity(0, entity)
2021-08-25 17:30:02 +08:00
);
} catch (e) {
console.error(e);
}
}
2021-08-26 10:43:31 +08:00
public async editPlan(entity?: PlanEntity) {
2021-08-25 17:30:02 +08:00
try {
return await this.doRpc<Protocol.EditPlanResponseEntity>(
2021-08-26 10:43:31 +08:00
new Protocol.EditPlanRequestEntity(0, entity)
2021-08-25 17:30:02 +08:00
);
} catch (e) {
console.error(e);
}
}
public async deletePlan(uuid: string) {
try {
return await this.doRpc<Protocol.DeletePlanResponseEntity>(
new Protocol.DeletePlanRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
public async getCurrentRunningPlan() {
try {
return await this.doRpc<Protocol.GetCurrentRunningPlanResponseEntity>(
new Protocol.GetCurrentRunningPlanRequestEntity(0)
);
} catch (e) {
console.error(e);
}
}
public runPlan(uuid: string) {
this.ws?.send(JSON.stringify(new Protocol.RunPlanRequestEntity(uuid)));
}
2021-08-26 10:43:31 +08:00
public stopCurrentRunningPlan() {
this.ws?.send(
JSON.stringify(new Protocol.StopCurrentRunningPlanRequestEntity())
);
}
2021-08-25 17:30:02 +08:00
public async getPlans() {
try {
return await this.doRpc<Protocol.GetPlansResponseEntity>(
new Protocol.GetPlansRequestEntity()
);
} catch (e) {
console.error(e);
}
}
2021-08-24 15:20:01 +08:00
public async addModeGroup(group_uuid: string, name: string) {
try {
return await this.doRpc<Protocol.AddModeGroupResponseEntity>(
new Protocol.AddModeGroupRequestEntity(0, group_uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async editModeGroup(uuid: string, name: string) {
try {
return await this.doRpc<Protocol.EditModeGroupResponseEntity>(
new Protocol.EditModeGroupRequestEntity(0, uuid, name)
);
} catch (e) {
console.error(e);
}
}
public async deleteModeGroup(uuid: string) {
try {
return await this.doRpc<Protocol.DeleteModeGroupResponseEntity>(
new Protocol.DeleteModeGroupRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
2021-09-07 18:03:50 +08:00
public async addMode(group_uuid?: string, name?: string, index?: number) {
2021-08-24 15:20:01 +08:00
try {
return await this.doRpc<Protocol.AddModeResponseEntity>(
2021-09-07 18:03:50 +08:00
new Protocol.AddModeRequestEntity(0, name, group_uuid, index)
2021-08-24 15:20:01 +08:00
);
} catch (e) {
console.error(e);
}
}
2021-09-07 18:03:50 +08:00
public async editMode(uuid?: string, name?: string, index?: number) {
2021-08-24 15:20:01 +08:00
try {
return await this.doRpc<Protocol.EditModeResponseEntity>(
2021-09-07 18:03:50 +08:00
new Protocol.EditModeRequestEntity(0, name, uuid, index)
2021-08-24 15:20:01 +08:00
);
} catch (e) {
console.error(e);
}
}
public async deleteMode(uuid: string) {
try {
return await this.doRpc<Protocol.DeleteModeResponseEntity>(
new Protocol.DeleteModeRequestEntity(0, uuid)
);
} catch (e) {
console.error(e);
}
}
public callMode(uuid: string) {
this.ws?.send(JSON.stringify(new Protocol.CallModeRequestEntity(uuid)));
}
2021-08-05 14:23:45 +08:00
public async getApplicationSettins() {
try {
return await this.doRpc<Protocol.GetApplicationConfigResponseEntity>(
new Protocol.GetApplicationConfigRequestEntity()
);
2021-08-05 17:26:27 +08:00
} catch (e) {
console.error(e);
}
}
public async getWindows() {
try {
return await this.doRpc<Protocol.GetWindowsResponseEntity>(
new Protocol.GetWindowsRequestEntity()
);
2021-08-05 14:23:45 +08:00
} catch (e) {
console.error(e);
}
}
public moveWindow(window_id: number, x: number, y: number) {
this.ws?.send(
JSON.stringify(new Protocol.MoveWindowRequestEntity(window_id, x, y))
);
}
public resizeWindow(window_id: number, width: number, height: number) {
this.ws?.send(
JSON.stringify(
new Protocol.ResizeWindowRequestEntity(window_id, width, height)
)
);
}
public closeWindow(window_id: number) {
this.ws?.send(
JSON.stringify(new Protocol.CloseWindowRequestEntity(window_id))
);
}
2021-08-07 11:04:39 +08:00
public openWindow(data: Protocol.OpenWindowRequestEntity) {
this.ws?.send(JSON.stringify(data));
}
2021-08-09 10:59:16 +08:00
public focusIn(window_id: number) {
this.ws?.send(
JSON.stringify(
new NormalWindowRequestEntity(
Protocol.Commands.kFocuseWindow,
window_id
)
)
);
}
public setWallRowCol(wall_row: number, wall_col: number) {
this.ws?.send(
JSON.stringify(
new Protocol.SetApplicationConfigRequestEntity(
0,
"wall_row",
wall_row.toString()
)
)
);
this.ws?.send(
JSON.stringify(
new Protocol.SetApplicationConfigRequestEntity(
0,
"wall_col",
wall_col.toString()
)
)
);
}
public setPowerOnPlan(uuid: string) {
this.ws?.send(
JSON.stringify(
new Protocol.SetApplicationConfigRequestEntity(0, "power_on_plan", uuid)
)
);
}
public windowFitGrid(window_id: number) {
this.ws?.send(
JSON.stringify(
new NormalWindowRequestEntity(
Protocol.Commands.kWindowFitGrid,
window_id
)
)
);
}
2021-09-29 14:21:33 +08:00
public async getSubtitle() {
try {
return await this.doRpc<Protocol.GetSubtitleResponseEntity>(
new Protocol.GetSubtitleRequestEntity(0)
);
} catch (e) {
console.error(e);
}
}
public async setSubtitle(subtitle: SubtitleEntity) {
try {
return await this.doRpc<Protocol.SetSubtitleResponseEntity>(
new Protocol.SetSubtitleRequestEntity(0, subtitle)
);
} catch (e) {
console.error(e);
}
}
2021-12-18 16:41:47 +08:00
public async setSystemNetwork(
request: Protocol.SetSystemNetworkRequestEntity
) {
return await this.doRpc<Protocol.SetSystemNetworkResponseEntity>(request);
2021-12-18 16:41:47 +08:00
}
public async setSystemGraphics(
request: Protocol.SetSystemGraphicsRequestEntity
) {
return await this.doRpc<Protocol.SetSystemGraphicsResponseEntity>(request);
2021-12-18 16:41:47 +08:00
}
public async setSystemOther(request: Protocol.SetSystemOtherRequestEntity) {
return await this.doRpc<Protocol.SetSystemOtherResponseEntity>(request);
2021-12-18 16:41:47 +08:00
}
public async getSupportResolutions() {
try {
return await this.doRpc<Protocol.GetSupportResolutionsResponseEntity>(
new Protocol.GetSupportResolutionsRequestEntity(0)
);
} catch (e) {
console.error(e);
}
}
2021-12-20 10:34:56 +08:00
public async getOutputBoardSetting() {
try {
return await this.doRpc<Protocol.GetOutputBoardSettingResponseEntity>(
new Protocol.GetOutputBoardSettingRequestEntity(0)
);
} catch (e) {
console.error(e);
}
}
public async setOutputBoardSetting(
request: Protocol.SetOutputBoardSettingRequestEntity
) {
try {
return await this.doRpc<Protocol.SetOutputBoardSettingResponseEntity>(
request
);
} catch (e) {
console.error(e);
}
}
public async restoreOutputBoard() {
try {
return await this.doRpc<Protocol.RestoreOutputBoardResponseEntity>(
new Protocol.RestoreOutputBoardRequestEntity(0)
);
} catch (e) {
console.error(e);
}
}
2021-12-22 19:44:02 +08:00
public async setWindowVolume(window_id: number, volume: number) {
2022-01-18 16:09:15 +08:00
this.ws?.send(
JSON.stringify(
2021-12-22 19:44:02 +08:00
new Protocol.SetWindowVolumeRequestEntity(window_id, volume, 0)
2022-01-18 16:09:15 +08:00
)
);
2021-12-22 19:44:02 +08:00
}
2021-12-20 10:34:56 +08:00
2021-12-23 09:28:21 +08:00
public async unmuteWindow(window_id: number) {
2022-01-18 16:09:15 +08:00
this.ws?.send(
JSON.stringify(new Protocol.UnMuteWindowRequestEntity(window_id, 0))
);
2021-12-23 09:28:21 +08:00
}
public async muteWindow(window_id: number) {
2022-01-18 16:09:15 +08:00
this.ws?.send(
JSON.stringify(new Protocol.MuteWindowRequestEntity(window_id, 0))
);
}
public async playWindow(window_id: number) {
this.ws?.send(
JSON.stringify(new Protocol.PlayWindowRequestEntity(window_id, 0))
);
}
public async windowPlayNext(window_id: number) {
this.ws?.send(
JSON.stringify(new Protocol.WindowPlayNextRequestEntity(window_id, 0))
);
}
public async windowPlayPrev(window_id: number) {
this.ws?.send(
JSON.stringify(new Protocol.WindowPlayPrevRequestEntity(window_id, 0))
);
}
public async pauseWindow(window_id: number) {
this.ws?.send(
JSON.stringify(new Protocol.PauseWindowRequestEntity(window_id, 0))
);
}
public async getBuildInfo() {
return await this.doRpc<Protocol.GetBuildInfoResponseEntity>(
new Protocol.GetBuildInfoRequestEntity(0)
);
}
public restartDevice(delay_ms?: number) {
this.ws?.send(
JSON.stringify(new Protocol.RestartDeviceRequestEntity(delay_ms))
);
2021-08-16 09:42:35 +08:00
}
2021-08-07 11:04:39 +08:00
public destory() {
if (this.ws) {
this.ws.onclose = null;
this.ws.onerror = null;
this.ws.onopen = null;
this.ws.onmessage = null;
}
2021-08-07 11:04:39 +08:00
this.ws?.close();
this.ws = null;
}
2021-08-05 14:23:45 +08:00
}
2021-08-25 17:30:02 +08:00
export interface NotifyMessage {
packet: Protocol.PacketEntity;
data: string;
}