langjie-m-play 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "langjie-m-play",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
- "main": "./index.js",
6
- "types": "./index.d.ts",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc"
9
9
  },
@@ -4,7 +4,50 @@ export declare class MPlay {
4
4
  private readonly serviceType;
5
5
  private readonly ctrlIndex;
6
6
  private dataGramCallBack;
7
+ private atsHandleNameMap;
8
+ /**
9
+ * Connect to the MPlay server.
10
+ *
11
+ * @param url The URL of the MPlay server.
12
+ */
13
+ static connect(url: string): Promise<void>;
14
+ /**
15
+ * Create a new MPlay instance.
16
+ *
17
+ * @param instId The instance ID of the MPlay instance.
18
+ * @param serviceType The service type of the MPlay instance.
19
+ * @param ctrlIndex The control index of the MPlay instance.
20
+ */
7
21
  constructor(instId: string | null, serviceType: ServiceTypeEnum | null, ctrlIndex: number | null);
22
+ /**
23
+ * Subscribe datagram to the MPlay server.
24
+ *
25
+ * @param tags The channel tags to subscribe.
26
+ * @param fn The callback function to handle the datagram.
27
+ */
8
28
  subscribeForDatagram(tags: string, fn: (data: Array<Array<string>>) => void): Promise<void>;
29
+ /**
30
+ * Unsubscribe datagram from the MPlay server.
31
+ */
9
32
  unsubscribeForDatagram(): Promise<void>;
33
+ /**
34
+ * Load ATS script to the MPlay server.
35
+ *
36
+ * @param atsScriptName The name of the ATS script to load.
37
+ * @return atsHandle The handle of the ATS script.
38
+ */
39
+ load(atsScriptName: string): Promise<number>;
40
+ /**
41
+ * Start ATS script to the MPlay server by ATS handle.
42
+ *
43
+ * @param atsHandle The handle of the ATS script.
44
+ */
45
+ start(atsHandle: number): Promise<void>;
46
+ /**
47
+ * Trigger ATS script button to the MPlay server by ATS handle.
48
+ *
49
+ * @param atsHandle The handle of the ATS script.
50
+ * @param buttonName The name of the button to trigger.
51
+ */
52
+ pushButton(atsHandle: number, buttonName: string): Promise<void>;
10
53
  }
package/service/MPlay.js CHANGED
@@ -12,9 +12,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MPlay = void 0;
13
13
  const GtcClient_1 = require("./GtcClient");
14
14
  const ServiceTypeEnum_1 = require("../domain/enums/ServiceTypeEnum");
15
+ const PubSubUtil_1 = require("../utils/PubSubUtil");
16
+ const SubKeyEnum_1 = require("../utils/enums/SubKeyEnum");
17
+ const TaskSynchronizer_1 = require("../utils/TaskSynchronizer");
15
18
  class MPlay {
19
+ /**
20
+ * Connect to the MPlay server.
21
+ *
22
+ * @param url The URL of the MPlay server.
23
+ */
24
+ static connect(url) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ yield GtcClient_1.GtcClient.init(url);
27
+ });
28
+ }
29
+ /**
30
+ * Create a new MPlay instance.
31
+ *
32
+ * @param instId The instance ID of the MPlay instance.
33
+ * @param serviceType The service type of the MPlay instance.
34
+ * @param ctrlIndex The control index of the MPlay instance.
35
+ */
16
36
  constructor(instId, serviceType, ctrlIndex) {
17
37
  this.ctrlIndex = 0;
38
+ this.atsHandleNameMap = {};
18
39
  if (instId) {
19
40
  this.instId = instId;
20
41
  }
@@ -35,17 +56,102 @@ class MPlay {
35
56
  this.dataGramCallBack(data.data.DataGramList);
36
57
  }
37
58
  });
59
+ GtcClient_1.GtcClient.listenEventMsg(this.instId, this.serviceType, this.ctrlIndex, (msg) => {
60
+ // console.log(msg);
61
+ // console.log(ctrlIndex + "<<>>" + msg.msgId.toString(16));
62
+ if (msg.msgId === 0x2000) {
63
+ // PubSubUtil.publish(SubKeyEnum.LAUNCH_OK, { instId, ctrlIndex });
64
+ }
65
+ else if (msg.msgId >= 0x0620 && msg.msgId < 0x0630) {
66
+ // ats加载成功
67
+ // PubSubUtil.publish(SubKeyEnum.ATS_LOAD_OK, { instId, ctrlIndex });
68
+ }
69
+ else if (msg.msgId >= 0x0650 && msg.msgId < 0x0660) {
70
+ // ats启动成功
71
+ PubSubUtil_1.PubSubUtil.publish(SubKeyEnum_1.SubKeyEnum.ATS_START_OK, { instId, ctrlIndex });
72
+ }
73
+ else if (msg.msgId >= 0x0700 && msg.msgId < 0x0800) {
74
+ // ats跳转到指定步骤
75
+ // PubSubUtil.publish(SubKeyEnum.ATS_JUMP_ACTION, { instId, ctrlIndex });
76
+ }
77
+ else if (msg.msgId >= 0x0800 && msg.msgId < 0x0900) {
78
+ // ats异常停止
79
+ PubSubUtil_1.PubSubUtil.publish(SubKeyEnum_1.SubKeyEnum.ATS_TERMINATE_ABNORMAL, { instId, ctrlIndex });
80
+ const hAts = msg.msgId & 0x00F0 >> 4;
81
+ delete this.atsHandleNameMap[hAts];
82
+ }
83
+ else if (msg.msgId >= 0x0900 && msg.msgId < 0x0910) {
84
+ // ats正常停止
85
+ PubSubUtil_1.PubSubUtil.publish(SubKeyEnum_1.SubKeyEnum.ATS_TERMINATE, { instId, ctrlIndex });
86
+ const hAts = msg.msgId & 0x00F0 >> 4;
87
+ delete this.atsHandleNameMap[hAts];
88
+ }
89
+ });
38
90
  }
91
+ /**
92
+ * Subscribe datagram to the MPlay server.
93
+ *
94
+ * @param tags The channel tags to subscribe.
95
+ * @param fn The callback function to handle the datagram.
96
+ */
39
97
  subscribeForDatagram(tags, fn) {
40
98
  return __awaiter(this, void 0, void 0, function* () {
41
99
  yield GtcClient_1.GtcClient.subscribeForDatagram(this.instId, this.serviceType, tags);
42
100
  this.dataGramCallBack = fn;
43
101
  });
44
102
  }
103
+ /**
104
+ * Unsubscribe datagram from the MPlay server.
105
+ */
45
106
  unsubscribeForDatagram() {
46
107
  return __awaiter(this, void 0, void 0, function* () {
47
108
  yield GtcClient_1.GtcClient.unsubscribeForDatagram(this.instId, this.serviceType);
48
109
  });
49
110
  }
111
+ /**
112
+ * Load ATS script to the MPlay server.
113
+ *
114
+ * @param atsScriptName The name of the ATS script to load.
115
+ * @return atsHandle The handle of the ATS script.
116
+ */
117
+ load(atsScriptName) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const atsHandle = yield GtcClient_1.GtcClient.atsLoad(this.instId, this.serviceType, this.ctrlIndex, atsScriptName);
120
+ if (atsHandle === 0) {
121
+ throw new Error("Failed to load ATS script");
122
+ }
123
+ this.atsHandleNameMap[atsHandle] = atsScriptName;
124
+ return atsHandle;
125
+ });
126
+ }
127
+ /**
128
+ * Start ATS script to the MPlay server by ATS handle.
129
+ *
130
+ * @param atsHandle The handle of the ATS script.
131
+ */
132
+ start(atsHandle) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const atsStartSynchronizer = new TaskSynchronizer_1.TaskSynchronizer();
135
+ PubSubUtil_1.PubSubUtil.subscribe(SubKeyEnum_1.SubKeyEnum.ATS_START_OK, atsHandle.toString(), (eventMsg) => __awaiter(this, void 0, void 0, function* () { return atsStartSynchronizer.finishTaskKey(atsHandle.toString()); }));
136
+ atsStartSynchronizer.addTaskKey(atsHandle.toString());
137
+ yield GtcClient_1.GtcClient.atsStart(this.instId, this.serviceType, this.ctrlIndex, atsHandle, this.atsHandleNameMap[atsHandle]);
138
+ yield atsStartSynchronizer.waitAll();
139
+ PubSubUtil_1.PubSubUtil.unsubscribe(SubKeyEnum_1.SubKeyEnum.ATS_START_OK, atsHandle.toString());
140
+ });
141
+ }
142
+ /**
143
+ * Trigger ATS script button to the MPlay server by ATS handle.
144
+ *
145
+ * @param atsHandle The handle of the ATS script.
146
+ * @param buttonName The name of the button to trigger.
147
+ */
148
+ pushButton(atsHandle, buttonName) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ if (!this.atsHandleNameMap[atsHandle]) {
151
+ throw new Error("Please reload ATS script");
152
+ }
153
+ yield GtcClient_1.GtcClient.atsPushButton(this.instId, this.serviceType, this.ctrlIndex, atsHandle, buttonName);
154
+ });
155
+ }
50
156
  }
51
157
  exports.MPlay = MPlay;
@@ -0,0 +1,8 @@
1
+ export declare class TaskSynchronizer {
2
+ private resolveMap;
3
+ private taskMap;
4
+ private promiseList;
5
+ addTaskKey(key: string): void;
6
+ finishTaskKey(key: string): void;
7
+ waitAll(): Promise<any>;
8
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskSynchronizer = void 0;
4
+ class TaskSynchronizer {
5
+ constructor() {
6
+ this.resolveMap = {};
7
+ this.taskMap = {};
8
+ this.promiseList = [];
9
+ }
10
+ addTaskKey(key) {
11
+ this.taskMap[key] = new Promise(resolve => this.resolveMap[key] = resolve);
12
+ this.promiseList.push(this.taskMap[key]);
13
+ }
14
+ finishTaskKey(key) {
15
+ this.resolveMap[key]();
16
+ }
17
+ waitAll() {
18
+ return new Promise((resolve, reject) => {
19
+ Promise.all(this.promiseList).then(() => resolve(true)).catch(e => reject(e));
20
+ });
21
+ }
22
+ }
23
+ exports.TaskSynchronizer = TaskSynchronizer;