-
Notifications
You must be signed in to change notification settings - Fork 8
/
codelab_adapter_base.js
532 lines (488 loc) · 19.6 KB
/
codelab_adapter_base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
const io = require("socket.io-client"); // yarn add socket.io-client@2.3.1
// import io from 'https://jspm.dev/socket.io-client@2.3.1'; // lively
const RateLimiter = require("./rateLimiter.js"); // 独立放一份到这里
// https://github.com/CodeLabClub/scratch3_eim/blob/v3/codelab_adapter_base.js
class AdapterBaseClient {
// fork for eim client
// onMessage,
// todo
constructor(
onConnect,
onDisconnect,
onMessage,
onAdapterPluginMessage,
update_nodes_status,
node_statu_change_callback,
notify_callback,
error_message_callback,
update_adapter_status,
SendRateMax = 60, // 每个插件每秒最多60条消息, eim/websocket 承载能力都远超这个值
runtime = null,
) {
this.debug_mode = false; //false
this.NOTIFICATION_lasttime = new Date().getTime();
this.runtime = runtime;
const ADAPTER_TOPIC = "adapter/nodes/data";
const EXTS_OPERATE_TOPIC = "core/exts/operate";
const NODES_STATUS_TOPIC = "core/nodes/status";
const NODE_STATU_CHANGE_TOPIC = "core/node/statu/change";
const NOTIFICATION_TOPIC = "core/notification";
const ADAPTER_STATUS_TOPIC = "core/status";
this.NODES_OPERATE_TOPIC = "core/nodes/operate";
this.GUI_TOPIC = "gui/operate";
this.NODES_STATUS_TRIGGER_TOPIC = "core/nodes/status/trigger";
this.SCRATCH_TOPIC = "scratch/extensions/command";
// Linda
this.LINDA_SERVER = "linda/server"; //target
this.LINDA_CLIENT = "linda/client";
this.plugin_topic_map = {
node: this.NODES_OPERATE_TOPIC,
extension: EXTS_OPERATE_TOPIC,
};
// this._requestID = 0; // todo uuid
this._promiseResolves = {};
this.SendRateMax = SendRateMax;
this._rateLimiter = new RateLimiter(SendRateMax);
const url = new URL(window.location.href);
let adapterHost = url.searchParams.get("adapter_host"); // 支持树莓派(分布式使用)
if (!adapterHost) {
adapterHost = window.__static
? "127.0.0.1"
: "codelab-adapter.codelab.club";
}
this.adapterHost = adapterHost;
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get("adapter_token");
this.token = urlParams.get("adapter_token");
this.socket = io(
`${window.__static ? "https:" : ""}//${adapterHost}:12358` +
`/test?token=${token}`,
{
transports: ["websocket"],
}
);
this.connected = false;
this.socket.on("connect", () => {
// 主动发起获取插件状态的请求,发出一则消息
// console.debug("socket connect ->", reason);
this.nodes_status_trigger();
// let onConnect = '';
if (typeof onConnect === "function") {
onConnect(); // 回调外部函数,onConnect可以是空的,忽视
} else {
console.debug("onConnect is not function");
}
this.connected = true;
});
this.socket.on("disconnect", (reason) => {
if (typeof onDisconnect === "function") {
onDisconnect(reason);
}
this.connected = false;
});
// on message
this.socket.on("sensor", (msg) => {
// actuator: to scratch
// console.debug("recv(all message):", msg.message);
if (typeof onMessage === "function") {
onMessage(msg);
}
const topic = msg.message.topic;
const content = msg.message.payload.content;
const message_id = msg.message.payload.message_id;
// console.debug('topic ->', topic);
switch (topic) {
case ADAPTER_STATUS_TOPIC: {
// if (msg.message.topic === this.ADAPTER_STATUS_TOPIC) {
console.debug("adapter core info:", content);
// this.version = content.version;
if (typeof update_adapter_status === "function") {
update_adapter_status(content);
}
break;
}
case NODES_STATUS_TOPIC: {
// 所有 plugin 的状态信息 初始化触发一次
// parents: this.adapter_client. trigger for nodes status
// console.debug("NODES_STATUS_TOPIC message");
if (typeof update_nodes_status === "function") {
// console.debug("callback update_nodes_status")
update_nodes_status(content);
}
// console.debug("NODES_STATUS_TOPIC messsage end");
break;
}
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/switch
// 如果没有break,则自动运行 已匹配到case的下一个 case
case NODE_STATU_CHANGE_TOPIC: {
// update extension status(start/stop open/close)
const extension_node_name = msg.message.payload.node_name; //node or extension
// extension or node
console.debug("extension_node_name:", extension_node_name);
if (typeof node_statu_change_callback === "function") {
node_statu_change_callback(
extension_node_name,
content
);
}
break;
}
case NOTIFICATION_TOPIC: {
// todo content.type
const type = msg.message.payload.type.toLowerCase();
const html = msg.message.payload.html;
console.debug("notification:", msg.message.payload);
// alert(content);
if (html == true) {
let notify_message = {
dangerouslyUseHTMLString: true,
message: content,
duration: 0,
};
if (typeof notify_callback === "function") {
notify_callback(notify_message);
}
return; //不再往下走
}
if (type === "error") {
// show error
let error_message = {
// html ?
showClose: true,
duration: 5000,
message: content,
type: type, // warning
};
if (typeof error_message_callback === "function") {
error_message_callback(error_message); // 为何要区分错误信息?
// notify_callback(notify_message);
}
} else {
let notify_message = {
message: content,
type: type, // warning
};
if (typeof notify_callback === "function") {
notify_callback(notify_message);
}
}
if (content == "download successfully!") {
this.nodes_status_trigger();
}
break;
}
case ADAPTER_TOPIC: {
// console.debug("ADAPTER_TOPIC message");
if (typeof onAdapterPluginMessage === "function") {
onAdapterPluginMessage(msg);
}
// window.message = msg; // to global
console.debug(
`ADAPTER_TOPIC message->`,
content
);
// 处理对应id的resolve
if (typeof message_id !== "undefined") {
if (this._promiseResolves[message_id]){
this._promiseResolves[message_id](content);
delete this._promiseResolves[message_id];
// console.debug({message_id:this._promiseResolves[message_id]});
}
}
break;
}
case this.LINDA_CLIENT: {
const tuple = msg.message.payload.tuple;
console.debug(
`LINDA_CLIENT message->`,
tuple
);
// 复用EIM位置参数
if (typeof onAdapterPluginMessage === "function") {
onAdapterPluginMessage(msg);
}
// todo 和 EIM 走同个逻辑管道
if (typeof message_id !== "undefined") {
if (this._promiseResolves[message_id]){
this._promiseResolves[message_id](tuple);
delete this._promiseResolves[message_id];
// console.debug({message_id:this._promiseResolves[message_id]});
}
}
break;
}
}
});
}
exit_adapter_app() {
let turn = "stop";
const message = {
topic: this.NODES_OPERATE_TOPIC,
payload: {
content: turn,
node_id: "adapter/app",
node_name: "_", // use id
token: this.token
},
};
this.socket.emit("actuator", message);
}
nodes_status_trigger() {
const message = {
topic: this.NODES_STATUS_TRIGGER_TOPIC,
payload: {
content: "UPDATE_UI",
token: this.token
},
};
this.socket.emit("actuator", message);
}
refresh_env() {
// todo 作为core的子集 而不是新的topic
const message = {
topic: this.NODES_STATUS_TRIGGER_TOPIC,
payload: {
content: "REFRESH_ENV",
token: this.token
},
};
console.debug("ready to refresh_env(send message)");
this.socket.emit("actuator", message);
}
download(plugin_url) {
const message = {
topic: this.GUI_TOPIC,
payload: {
content: "plugin_download",
plugin_url: plugin_url,
node_id: "adapter/app",
token: this.token
},
};
this.socket.emit("actuator", message);
// todo await
}
operate_node_extension(turn, node_name, pluginType) {
const message = {
topic: this.plugin_topic_map[pluginType],
payload: {
content: turn,
node_id: "_", // 不要使用它,避免bug(难以排查!)
node_name: node_name,
token: this.token
},
};
this.socket.emit("actuator", message); // actuator: from scratch
// todo: 确认完成之后才切换, 得到后端反馈(message id) json-rpc(scratch)
}
menu_action(val) {
// let _this = this;
if (val == "extensions_update") {
const message = {
topic: this.GUI_TOPIC,
payload: {
content: val,
node_id: "adapter/app",
token: this.token
},
};
this.socket.emit("actuator", message);
setTimeout(() => {
this.exit_adapter_app();
alert("更新成功,请重启 (Update successful, please restart.)");
}, 500);
} else if (val == "refresh_env") {
this.refresh_env();
} else {
const message = {
topic: this.GUI_TOPIC,
payload: {
content: val,
node_id: "adapter/app",
token: this.token
},
};
this.socket.emit("actuator", message);
}
}
get_reply_message(messageID, timeout=7000) {
return new Promise((resolve, reject) => {
this._promiseResolves[messageID] = resolve; // 抛到外部
setTimeout(() => {
if (this._promiseResolves[messageID]){
console.error(`timeout(${timeout/1000}s)`)
resolve(`timeout(${timeout/1000}s)`); // 不阻断接下来的积木
// reject(`reject: timeout(${timeout/1000}s)`); // reject 积木将中止
// todo 通知, 积木名字
// todo: https://github.com/LLK/scratch-vm/blob/acc2e6dba2e5a32668f0b26f0b2c4dfdecbe1023/src/util/jsonrpc.js#L91
this.runtime.emit('PUSH_NOTIFICATION', {content: `timeout(${timeout/1000}s)`, type: 'error'})
}
}, timeout);
});
}
get_uuid(){
// https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
check_limiter(){
if (this._rateLimiter.okayToSend()){
return true;
}
else{
let now = new Date().getTime(); // ms
// runtime.emit('PUSH_NOTIFICATION', {content: '通知内容', type: 'success | error | warning | info'})
console.error(`rate limit (${this.SendRateMax})`);
// 一秒内只发一次
if (this.runtime){
if (now - this.NOTIFICATION_lasttime > 1000 ){
// 如果不存在会如何?
// util.stopAll();
try{
console.debug(`PUSH_NOTIFICATION`);
this.runtime.emit('PUSH_NOTIFICATION', {content: `rate limit (${this.SendRateMax})`, type: 'error'})
// todo 灾难不要发生在全局,只是弹出提醒
// this.runtime.stopAll(); 弹出消息更细致 包括积木名字
}
catch (e) {
console.error(e)
}
this.NOTIFICATION_lasttime = new Date().getTime();
}
}
/*
window.antNotification.error({
message: 'Error',
description: `rate limit (${this.SendRateMax})`
});*/
return false;
}
}
emit_with_messageid(node_id, content, timeout=5000) {
// todo 添加积木信息,抛出错误
if (!this.check_limiter()) return Promise.resolve('rate limit');
// socket connected?
if (!this.connected) return Promise.resolve('not connected'); //reject?
const messageID = this.get_uuid();
const payload = {};
payload.node_id = node_id;
payload.content = content;
payload.message_id = messageID;
payload.token = this.token;
if (this.debug_mode){
payload.timestamp = new Date().getTime();
}
this.socket.emit("actuator", {
payload: payload,
topic: this.SCRATCH_TOPIC,
});
return this.get_reply_message(messageID, timeout);
}
send_to_linda(node_id, payload, timeout=5000) {
// send to linda server and wait
// 最好是只查询的,不要是有副作用的,永远,不要超时,或者超时,就发送一个目标tuple,抵消原来的请求
// todo 添加积木信息,抛出错误
// todo: 目前也走 EIM
if (!this.check_limiter()) return Promise.resolve('rate limit');
// socket connected?
if (!this.connected) return Promise.resolve('not connected');
// const payload = {};
payload.node_id = node_id;
// payload.tuple = linda_tuple; //tuple/list
const messageID = this.get_uuid();
payload.message_id = messageID;
payload.token = this.token;
if (this.debug_mode){
payload.timestamp = new Date().getTime();
}
this.socket.emit("actuator", {
payload: payload,
topic: this.LINDA_SERVER,
});
return messageID;
// return this.get_reply_message(messageID, timeout); //todo timeout
}
send_to_linda_and_wait(node_id, payload, timeout=5000) {
//返回promise
// 返回 promise reject?
if (!(Array.isArray(payload["tuple"]) && payload["tuple"].length >0)){return Promise.reject('input error')}
let messageID = this.send_to_linda(node_id, payload, timeout);
return this.get_reply_message(messageID, timeout); //todo timeout
}
// 1000
_linda_operate(operate, tuple, timeout=1000*3600*24){
let node_id = "linda/js/client";
let payload = {};
payload["operate"] = operate;
payload["tuple"] = tuple;
return this.send_to_linda_and_wait(node_id, payload, timeout)
}
linda_out(tuple, timeout=1000*3600*24){
return this._linda_operate("out", tuple, timeout);
}
linda_in(tuple, timeout=1000*3600*24){
return this._linda_operate("in", tuple, timeout);
}
linda_rd(tuple, timeout=1000*3600*24){
return this._linda_operate("rd", tuple, timeout);
}
linda_rdp(tuple, timeout=1000*3600*24){
return this._linda_operate("rdp", tuple, timeout);
}
linda_inp(tuple, timeout=1000*3600*24){
return this._linda_operate("inp", tuple, timeout);
}
// linda helper, 没有参数
linda_dump(){
let timeout = 1000*3600*24;
let tuple = ["dump"];
return this._linda_operate("dump", tuple, timeout);
}
linda_reboot(){
let timeout = 1000*3600*24;
let tuple = ["reboot"];
return this._linda_operate("reboot", tuple, timeout);
}
linda_status(){
let timeout = 1000*3600*24;
let tuple = ["status"];
return this._linda_operate("status", tuple, timeout);
}
emit_with_messageid_for_control(node_id, content, node_name, pluginType) {
const messageID = this.get_uuid();
const payload = {};
payload.node_id = node_id;
payload.content = content;
payload.message_id = messageID;
payload.node_name = node_name;
payload.token = this.token;
this.socket.emit("actuator", {
payload: payload,
topic: this.plugin_topic_map[pluginType],
});
return this.get_reply_message(messageID, 10000); // timeout 10000, 10s
}
emit_without_messageid(node_id, content) {
if (!this.check_limiter()) return Promise.resolve();
const payload = {};
payload.node_id = node_id;
payload.content = content;
payload.message_type = "nowait";
payload.token = this.token;
if (this.debug_mode){
payload.timestamp = new Date().getTime();
}
this.socket.emit("actuator", {
payload: payload,
topic: this.SCRATCH_TOPIC,
});
}
/*
rm_begin_end_quotation(x){
return x.replace(/^"|"$/g, '');
}*/
}
// window.AdapterBaseClient = AdapterBaseClient;
module.exports = AdapterBaseClient;