-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
299 lines (265 loc) · 8.62 KB
/
index.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
/*
使用scratch ui连接, 派生自 thingDemo
测试插件,方便做各种测试
todo 重置积木
关闭 adapter 插件,清理UI
清理get的一堆东西,使用下拉,避免被用户点击 一直查询
*/
const ArgumentType = require("../../extension-support/argument-type");
const BlockType = require("../../extension-support/block-type");
const formatMessage = require("format-message");
const AdapterBaseClient = require("../scratch3_eim/codelab_adapter_base.js");
const ScratchUIHelper = require("../scratch3_eim/scratch_ui_helper.js");
const blockIconURI = require("./icon.svg");
const menuIconURI = blockIconURI;
/**
* Enum for button parameter values.
* @readonly
* @enum {string}
*/
const SCRATCH_EXT_ID = "thingDemo"; //vm gui 与此一致
const NODE_NAME = `node_${SCRATCH_EXT_ID}`;
const NODE_ID = `eim/${NODE_NAME}`;
const NODE_MIN_VERSION = "1.0.0"; //node最低版本, 依赖
const HELP_URL = `https://adapter.codelab.club/extension_guide/${SCRATCH_EXT_ID}/`;
// 翻译
const FormHelp = {
en: "help",
"zh-cn": "帮助",
};
// Thing 对象内部可能出现意外错误,重置积木(client/Scratch)(重启整个插件)
const FormReset = {
en: "reset",
"zh-cn": "重置",
};
const FormSetEmitTimeout = {
en: "set wait timeout [emit_timeout]s",
"zh-cn": "设置等待超时时间[emit_timeout]秒",
};
const FormTakeOff = {
en: "takeoff",
"zh-cn": "起飞",
};
const FormLand = {
en: "land",
"zh-cn": "降落",
};
const Form_sendTopicMessageAndWait = {
en: "broadcast [content] and wait",
"zh-cn": "广播[content]并等待",
}
const Form_sendTopicMessageAndWait_REPORTER = {
en: "broadcast [content] and wait",
"zh-cn": "广播[content]并等待",
}
class AdapterClient {
onAdapterPluginMessage(msg) {
this.node_id = msg.message.payload.node_id;
if (this.node_id === this.NODE_ID) {
// json 数据, class
this.adapter_node_content_hat = msg.message.payload.content;
this.adapter_node_content_reporter = msg.message.payload.content;
console.log("content ->", msg.message.payload.content);
}
}
notify_callback(msg) {
// 使用通知机制直到自己退出
// todo 重置
if (msg.message === `${this.NODE_ID} stopped`) {
this.ScratchUIHelper.reset();
}
}
constructor(node_id, help_url, runtime) {
this.NODE_ID = node_id;
this.HELP_URL = help_url;
this.emit_timeout = 5000; //ms
this.adapter_base_client = new AdapterBaseClient(
null, // onConnect,
null, // onDisconnect,
null, // onMessage,
this.onAdapterPluginMessage.bind(this), // onAdapterPluginMessage,
null, // update_nodes_status,
null, // node_statu_change_callback,
this.notify_callback.bind(this),
null, // error_message_callback,
null // update_adapter_status
);
let list_timeout = 10000;
// 生成 UI 类
this.ScratchUIHelper = new ScratchUIHelper(
SCRATCH_EXT_ID,
NODE_NAME,
NODE_ID,
NODE_MIN_VERSION,
runtime,
this.adapter_base_client,
list_timeout
);
}
emit_with_messageid(NODE_ID, content) {
return this.adapter_base_client.emit_with_messageid(
NODE_ID,
content,
this.emit_timeout
);
}
}
class Scratch3ThingDemoBlocks {
constructor(runtime) {
runtime.registerPeripheralExtension(SCRATCH_EXT_ID, this);
this.client = new AdapterClient(NODE_ID, HELP_URL, runtime);
}
//以下都是复制张贴 应该使用一个类来做
scan() {
return this.client.ScratchUIHelper.scan();
}
connect(id) {
return this.client.ScratchUIHelper.connect(id);
}
disconnect() {
return this.client.ScratchUIHelper.disconnect();
}
reset() {
return this.client.ScratchUIHelper.reset();
}
isConnected() {
return this.client.ScratchUIHelper.isConnected();
}
/**
* The key to load & store a target's test-related state.
* @type {string}
*/
static get STATE_KEY() {
return `Scratch.${SCRATCH_EXT_ID}`;
}
_setLocale() {
let now_locale = "";
switch (formatMessage.setup().locale) {
case "en":
now_locale = "en";
break;
case "zh-cn":
now_locale = "zh-cn";
break;
default:
now_locale = "zh-cn";
break;
}
return now_locale;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
let the_locale = this._setLocale();
return {
id: SCRATCH_EXT_ID,
name: "Thing Demo",
colour: "#ff641d",
colourSecondary: "#c94f18",
colourTertiary: "#c94f18",
menuIconURI: menuIconURI,
blockIconURI: blockIconURI,
showStatusButton: true,
blocks: [
{
opcode: "open_help_url",
blockType: BlockType.COMMAND,
text: FormHelp[the_locale],
arguments: {},
},
{
opcode: "control_node",
blockType: BlockType.COMMAND,
text: FormReset[the_locale],
},
{
opcode: "set_emit_timeout",
blockType: BlockType.COMMAND,
text: FormSetEmitTimeout[the_locale],
arguments: {
emit_timeout: {
type: ArgumentType.NUMBER,
defaultValue: 5.0,
},
},
},
{
opcode: "takeoff",
blockType: BlockType.COMMAND,
text: FormTakeOff[the_locale],
},
{
opcode: "land",
blockType: BlockType.COMMAND,
text: FormLand[the_locale],
},
{
opcode: "broadcastTopicMessageAndWait",
blockType: BlockType.COMMAND,
text: Form_sendTopicMessageAndWait[the_locale],
arguments: {
content: {
type: ArgumentType.STRING,
defaultValue: "thing.move_forward(20)",
},
},
},
{
opcode: "broadcastTopicMessageAndWait_REPORTER",
blockType: BlockType.REPORTER,
text: Form_sendTopicMessageAndWait_REPORTER[the_locale],
arguments: {
content: {
type: ArgumentType.STRING,
defaultValue: "thing.query_height()",
},
},
},
],
menus: {
turn: {
acceptReporters: true,
items: ["start", "stop"],
},
},
};
}
takeoff(args, util) {
const content = "thing.takeoff()";
return this.client.emit_with_messageid(NODE_ID, content);
}
land(args, util) {
const content = "thing.land()";
return this.client.emit_with_messageid(NODE_ID, content);
}
open_help_url(args) {
window.open(HELP_URL);
}
set_emit_timeout(args) {
const timeout = parseFloat(args.emit_timeout) * 1000;
this.client.emit_timeout = timeout;
}
broadcastTopicMessageAndWait(args) {
// topic服务于消息功能, node_id承载业务逻辑(extension)
const content = args.content;
return this.client.emit_with_messageid(NODE_ID, content);
}
broadcastTopicMessageAndWait_REPORTER(args) {
// topic服务于消息功能, node_id承载业务逻辑(extension)
const content = args.content;
return this.client.emit_with_messageid(NODE_ID, content);
}
control_node(args) {
// ui是由结束通知完成的
const content = "stop";
const node_name = NODE_NAME;
return this.client.adapter_base_client.emit_with_messageid_for_control(
NODE_ID,
content,
node_name,
"node"
);
}
}
module.exports = Scratch3ThingDemoBlocks;