pico-engine 1.2.0 → 1.3.0
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/README.md +2 -0
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -0
- package/krl/io.picolabs.did-o.krl +325 -0
- package/krl/io.picolabs.pico-engine-ui.krl +9 -6
- package/krl/io.picolabs.wrangler.krl +484 -473
- package/krl/test/didtestdid.krl +78 -0
- package/package.json +5 -5
- package/src/cli.ts +1 -1
- package/src/index.ts +188 -187
|
@@ -1,473 +1,484 @@
|
|
|
1
|
-
/* Rules with a public facing point of entry are camelCase, internal rules are snake_case
|
|
2
|
-
For skyQuery and parent-child trees:
|
|
3
|
-
wrangler is built so that the parent can skyQuery its children for info at any time,
|
|
4
|
-
but children only get information from their direct parent through event-passing to prevent race conditions
|
|
5
|
-
*/
|
|
6
|
-
ruleset io.picolabs.wrangler {
|
|
7
|
-
meta {
|
|
8
|
-
name "Wrangler Core"
|
|
9
|
-
description <<
|
|
10
|
-
Wrangler Core Module,
|
|
11
|
-
use example: "use module io.picolabs.wrangler alias wrangler".
|
|
12
|
-
This Ruleset/Module provides a developer interface to the pico (persistent compute object).
|
|
13
|
-
When a pico is created this ruleset will be installed to provide essential services.
|
|
14
|
-
>>
|
|
15
|
-
author "BYU Pico Lab"
|
|
16
|
-
|
|
17
|
-
provides skyQuery, picoQuery,
|
|
18
|
-
channels, createChannel, deleteChannel, //channel
|
|
19
|
-
rulesetConfig, rulesetMeta, installedRIDs, //ruleset
|
|
20
|
-
children, parent_eci, name, myself //pico
|
|
21
|
-
|
|
22
|
-
shares skyQuery, picoQuery,
|
|
23
|
-
channels, //channel
|
|
24
|
-
rulesetConfig, rulesetMeta,installedRIDs, //ruleset
|
|
25
|
-
children, parent_eci, name, myself, id //pico
|
|
26
|
-
}
|
|
27
|
-
global {
|
|
28
|
-
__testing = { "queries": [ {"name": "name"},
|
|
29
|
-
{"name": "myself"},
|
|
30
|
-
{ "name": "channels", "args":["tags"] },
|
|
31
|
-
{ "name": "installedRIDs"},
|
|
32
|
-
{"name":"skyQuery", "args":["eci", "mod", "func", "params","_host","_path","_root_url"]},
|
|
33
|
-
{"name":"children", "args":[]}
|
|
34
|
-
],
|
|
35
|
-
"events": [
|
|
36
|
-
{ "domain": "wrangler", "name": "new_child_request",
|
|
37
|
-
"attrs": [ "name", "backgroundColor"] },
|
|
38
|
-
{ "domain": "wrangler", "name": "child_deletion_request",
|
|
39
|
-
"attrs": [ "eci"] },
|
|
40
|
-
{ "domain": "wrangler", "name": "new_channel_request",
|
|
41
|
-
"attrs": [ "tags", "eventPolicy", "queryPolicy" ] },
|
|
42
|
-
{ "domain": "wrangler", "name": "channel_deletion_request",
|
|
43
|
-
"attrs": [ "eci" ] },
|
|
44
|
-
{ "domain": "wrangler", "name": "install_ruleset_request",
|
|
45
|
-
"attrs": [ "url" ] },
|
|
46
|
-
{ "domain": "wrangler", "name": "install_ruleset_request",
|
|
47
|
-
"attrs": [ "absoluteURL","rid" ] },
|
|
48
|
-
{ "domain": "wrangler", "name": "uninstall_ruleset_request",
|
|
49
|
-
"attrs": [ "rid" ] },
|
|
50
|
-
] }
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// ********************************************************************************************
|
|
54
|
-
// *** ***
|
|
55
|
-
// *** FUNCTIONS ***
|
|
56
|
-
// *** ***
|
|
57
|
-
// ********************************************************************************************
|
|
58
|
-
|
|
59
|
-
/*
|
|
60
|
-
picoQuery is used to programmatically call function inside of other picos from inside a rule.
|
|
61
|
-
parameters;
|
|
62
|
-
eci - The eci of the pico which contains the function to be called
|
|
63
|
-
mod - The ruleset ID or alias of the module
|
|
64
|
-
func - The name of the function in the module
|
|
65
|
-
params - The parameters to be passed to function being called
|
|
66
|
-
optional parameters
|
|
67
|
-
_host - The host of the pico-engine being queried.
|
|
68
|
-
Note this must include protocol (http:// or https://) being used and port number if not 80.
|
|
69
|
-
For example "http://localhost:8080", which also is the default.
|
|
70
|
-
_path - The sub path of the url which does not include mod or func.
|
|
71
|
-
For example "/sky/cloud/", which also is the default.
|
|
72
|
-
_root_url - The entire url except eci, mod, func.
|
|
73
|
-
For example, dependent on _host and _path is
|
|
74
|
-
"http://localhost:8080/sky/cloud/", which also is the default.
|
|
75
|
-
picoQuery on success (if status code of request is 200) returns results of the called function.
|
|
76
|
-
picoQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
|
|
77
|
-
error - general error message.
|
|
78
|
-
httpStatus - status code returned from http get command.
|
|
79
|
-
picoQueryError - The value of the "error key", if it exist, aof the function results.
|
|
80
|
-
picoQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
|
|
81
|
-
picoQueryReturnValue - The function call results.
|
|
82
|
-
*/
|
|
83
|
-
QUERY_SELF_INVALID_HTTP_MAP = {"status_code": 400,
|
|
84
|
-
"status_line":"HTTP/1.1 400 Pico should not query itself",
|
|
85
|
-
"content": "{\"error\":\"Pico should not query itself\"}"};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
buildWebHook = function(eci, mod, func, _host, _path, _root_url) {
|
|
89
|
-
createRootUrl = function (_host,_path){
|
|
90
|
-
host = _host || meta:host;
|
|
91
|
-
path = _path || "/sky/cloud/";
|
|
92
|
-
root_url = host+path;
|
|
93
|
-
root_url
|
|
94
|
-
};
|
|
95
|
-
root_url = _root_url || createRootUrl(_host,_path);
|
|
96
|
-
root_url + eci + "/"+mod+"/" + func;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
processHTTPResponse = function(response) {
|
|
100
|
-
status = response{"status_code"};// pass along the status
|
|
101
|
-
error_info = {
|
|
102
|
-
"error": "sky query request was unsuccesful.",
|
|
103
|
-
"httpStatus": {
|
|
104
|
-
"code": status,
|
|
105
|
-
"message": response{"status_line"}
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
// clean up http return
|
|
109
|
-
response_content = response{"content"}.decode();
|
|
110
|
-
response_error = (response_content.typeof() == "Map" && (not response_content{"error"}.isnull())) => response_content{"error"} | 0;
|
|
111
|
-
response_error_str = (response_content.typeof() == "Map" && (not response_content{"error_str"}.isnull())) => response_content{"error_str"} | 0;
|
|
112
|
-
error = error_info.put({"picoQueryError": response_error,
|
|
113
|
-
"picoQueryErrorMsg": response_error_str,
|
|
114
|
-
"picoQueryReturnValue": response_content});
|
|
115
|
-
is_bad_response = (response_content.isnull() || (response_content == "null") || response_error || response_error_str);
|
|
116
|
-
// if HTTP status was OK & the response was not null and there were no errors...
|
|
117
|
-
(status == 200 && not is_bad_response) => response_content | error.klog("error: ")
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
skyQuery = function(eci, mod, func, params,_host,_path,_root_url) { // path must start with "/"", _host must include protocol(http:// or https://)
|
|
121
|
-
//.../sky/cloud/<eci>/<rid>/<name>?name0=value0&...&namen=valuen
|
|
122
|
-
thisPico = ctx:channels.any(function(c){c{"id"}==eci})
|
|
123
|
-
web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
|
|
124
|
-
|
|
125
|
-
response = (not thisPico) => http:get(web_hook, {}.put(params)) | QUERY_SELF_INVALID_HTTP_MAP;
|
|
126
|
-
processHTTPResponse(response)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
picoQuery() is an improved skyQuery() that automatically queries picos on the same engine using ctx:query() to avoid
|
|
131
|
-
unnecessary HTTP calls and channel policy issues (family channels can't be accessed over HTTP).
|
|
132
|
-
|
|
133
|
-
The parameters are the same.
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
// this does nothing now, but it may be that we need to return error codes.
|
|
137
|
-
processQueryResponse = function(response) {
|
|
138
|
-
response
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
picoQuery = function(eci, mod, func, params,_host,_path,_root_url) {
|
|
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
|
-
.put("
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
wrangler_children
|
|
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
|
-
fired {
|
|
445
|
-
ent:
|
|
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
|
-
|
|
1
|
+
/* Rules with a public facing point of entry are camelCase, internal rules are snake_case
|
|
2
|
+
For skyQuery and parent-child trees:
|
|
3
|
+
wrangler is built so that the parent can skyQuery its children for info at any time,
|
|
4
|
+
but children only get information from their direct parent through event-passing to prevent race conditions
|
|
5
|
+
*/
|
|
6
|
+
ruleset io.picolabs.wrangler {
|
|
7
|
+
meta {
|
|
8
|
+
name "Wrangler Core"
|
|
9
|
+
description <<
|
|
10
|
+
Wrangler Core Module,
|
|
11
|
+
use example: "use module io.picolabs.wrangler alias wrangler".
|
|
12
|
+
This Ruleset/Module provides a developer interface to the pico (persistent compute object).
|
|
13
|
+
When a pico is created this ruleset will be installed to provide essential services.
|
|
14
|
+
>>
|
|
15
|
+
author "BYU Pico Lab"
|
|
16
|
+
|
|
17
|
+
provides skyQuery, picoQuery,
|
|
18
|
+
channels, createChannel, deleteChannel, //channel
|
|
19
|
+
rulesetConfig, rulesetMeta, installedRIDs, //ruleset
|
|
20
|
+
children, parent_eci, name, myself //pico
|
|
21
|
+
|
|
22
|
+
shares skyQuery, picoQuery,
|
|
23
|
+
channels, //channel
|
|
24
|
+
rulesetConfig, rulesetMeta,installedRIDs, //ruleset
|
|
25
|
+
children, parent_eci, name, myself, id //pico
|
|
26
|
+
}
|
|
27
|
+
global {
|
|
28
|
+
__testing = { "queries": [ {"name": "name"},
|
|
29
|
+
{"name": "myself"},
|
|
30
|
+
{ "name": "channels", "args":["tags"] },
|
|
31
|
+
{ "name": "installedRIDs"},
|
|
32
|
+
{"name":"skyQuery", "args":["eci", "mod", "func", "params","_host","_path","_root_url"]},
|
|
33
|
+
{"name":"children", "args":[]}
|
|
34
|
+
],
|
|
35
|
+
"events": [
|
|
36
|
+
{ "domain": "wrangler", "name": "new_child_request",
|
|
37
|
+
"attrs": [ "name", "backgroundColor"] },
|
|
38
|
+
{ "domain": "wrangler", "name": "child_deletion_request",
|
|
39
|
+
"attrs": [ "eci"] },
|
|
40
|
+
{ "domain": "wrangler", "name": "new_channel_request",
|
|
41
|
+
"attrs": [ "tags", "eventPolicy", "queryPolicy" ] },
|
|
42
|
+
{ "domain": "wrangler", "name": "channel_deletion_request",
|
|
43
|
+
"attrs": [ "eci" ] },
|
|
44
|
+
{ "domain": "wrangler", "name": "install_ruleset_request",
|
|
45
|
+
"attrs": [ "url" ] },
|
|
46
|
+
{ "domain": "wrangler", "name": "install_ruleset_request",
|
|
47
|
+
"attrs": [ "absoluteURL","rid" ] },
|
|
48
|
+
{ "domain": "wrangler", "name": "uninstall_ruleset_request",
|
|
49
|
+
"attrs": [ "rid" ] },
|
|
50
|
+
] }
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
// ********************************************************************************************
|
|
54
|
+
// *** ***
|
|
55
|
+
// *** FUNCTIONS ***
|
|
56
|
+
// *** ***
|
|
57
|
+
// ********************************************************************************************
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
picoQuery is used to programmatically call function inside of other picos from inside a rule.
|
|
61
|
+
parameters;
|
|
62
|
+
eci - The eci of the pico which contains the function to be called
|
|
63
|
+
mod - The ruleset ID or alias of the module
|
|
64
|
+
func - The name of the function in the module
|
|
65
|
+
params - The parameters to be passed to function being called
|
|
66
|
+
optional parameters
|
|
67
|
+
_host - The host of the pico-engine being queried.
|
|
68
|
+
Note this must include protocol (http:// or https://) being used and port number if not 80.
|
|
69
|
+
For example "http://localhost:8080", which also is the default.
|
|
70
|
+
_path - The sub path of the url which does not include mod or func.
|
|
71
|
+
For example "/sky/cloud/", which also is the default.
|
|
72
|
+
_root_url - The entire url except eci, mod, func.
|
|
73
|
+
For example, dependent on _host and _path is
|
|
74
|
+
"http://localhost:8080/sky/cloud/", which also is the default.
|
|
75
|
+
picoQuery on success (if status code of request is 200) returns results of the called function.
|
|
76
|
+
picoQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
|
|
77
|
+
error - general error message.
|
|
78
|
+
httpStatus - status code returned from http get command.
|
|
79
|
+
picoQueryError - The value of the "error key", if it exist, aof the function results.
|
|
80
|
+
picoQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
|
|
81
|
+
picoQueryReturnValue - The function call results.
|
|
82
|
+
*/
|
|
83
|
+
QUERY_SELF_INVALID_HTTP_MAP = {"status_code": 400,
|
|
84
|
+
"status_line":"HTTP/1.1 400 Pico should not query itself",
|
|
85
|
+
"content": "{\"error\":\"Pico should not query itself\"}"};
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
buildWebHook = function(eci, mod, func, _host, _path, _root_url) {
|
|
89
|
+
createRootUrl = function (_host,_path){
|
|
90
|
+
host = _host || meta:host;
|
|
91
|
+
path = _path || "/sky/cloud/";
|
|
92
|
+
root_url = host+path;
|
|
93
|
+
root_url
|
|
94
|
+
};
|
|
95
|
+
root_url = _root_url || createRootUrl(_host,_path);
|
|
96
|
+
root_url + eci + "/"+mod+"/" + func;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
processHTTPResponse = function(response) {
|
|
100
|
+
status = response{"status_code"};// pass along the status
|
|
101
|
+
error_info = {
|
|
102
|
+
"error": "sky query request was unsuccesful.",
|
|
103
|
+
"httpStatus": {
|
|
104
|
+
"code": status,
|
|
105
|
+
"message": response{"status_line"}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
// clean up http return
|
|
109
|
+
response_content = response{"content"}.decode();
|
|
110
|
+
response_error = (response_content.typeof() == "Map" && (not response_content{"error"}.isnull())) => response_content{"error"} | 0;
|
|
111
|
+
response_error_str = (response_content.typeof() == "Map" && (not response_content{"error_str"}.isnull())) => response_content{"error_str"} | 0;
|
|
112
|
+
error = error_info.put({"picoQueryError": response_error,
|
|
113
|
+
"picoQueryErrorMsg": response_error_str,
|
|
114
|
+
"picoQueryReturnValue": response_content});
|
|
115
|
+
is_bad_response = (response_content.isnull() || (response_content == "null") || response_error || response_error_str);
|
|
116
|
+
// if HTTP status was OK & the response was not null and there were no errors...
|
|
117
|
+
(status == 200 && not is_bad_response) => response_content | error.klog("error: ")
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
skyQuery = function(eci, mod, func, params,_host,_path,_root_url) { // path must start with "/"", _host must include protocol(http:// or https://)
|
|
121
|
+
//.../sky/cloud/<eci>/<rid>/<name>?name0=value0&...&namen=valuen
|
|
122
|
+
thisPico = ctx:channels.any(function(c){c{"id"}==eci})
|
|
123
|
+
web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
|
|
124
|
+
|
|
125
|
+
response = (not thisPico) => http:get(web_hook, {}.put(params)) | QUERY_SELF_INVALID_HTTP_MAP;
|
|
126
|
+
processHTTPResponse(response)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
picoQuery() is an improved skyQuery() that automatically queries picos on the same engine using ctx:query() to avoid
|
|
131
|
+
unnecessary HTTP calls and channel policy issues (family channels can't be accessed over HTTP).
|
|
132
|
+
|
|
133
|
+
The parameters are the same.
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
// this does nothing now, but it may be that we need to return error codes.
|
|
137
|
+
processQueryResponse = function(response) {
|
|
138
|
+
response
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
picoQuery = function(eci, mod, func, params,_host,_path,_root_url) {
|
|
142
|
+
isDid = eci.match(re#^did:#)
|
|
143
|
+
thisPico = ctx:channels.any(function(c){c{"id"}==eci})
|
|
144
|
+
thisHost = _host.isnull() || _host == meta:host;
|
|
145
|
+
web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
|
|
146
|
+
|
|
147
|
+
response = thisPico => QUERY_SELF_INVALID_HTTP_MAP |
|
|
148
|
+
thisHost => processQueryResponse(ctx:query(eci, mod, func, {}.put(params))) |
|
|
149
|
+
isDid => dido:prepareQuery(eci, {"rid": mod, "name": func, "args": params}) |
|
|
150
|
+
processHTTPResponse(http:get(web_hook, {}.put(params)))
|
|
151
|
+
|
|
152
|
+
response
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
// ********************************************************************************************
|
|
158
|
+
// *** Rulesets ***
|
|
159
|
+
// ********************************************************************************************
|
|
160
|
+
|
|
161
|
+
rulesetByRID = function(rid) {
|
|
162
|
+
ctx:rulesets.filter(function(rs){rs{"rid"}==rid}).head()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
rulesetConfig = function(rid) {
|
|
166
|
+
rulesetByRID(rid){"config"}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
rulesetMeta = function(rid) {
|
|
170
|
+
rulesetByRID(rid){["meta","krlMeta"]}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
installedRIDs = function() {
|
|
174
|
+
ctx:rulesets.map(function(rs){rs{"rid"}})
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ********************************************************************************************
|
|
178
|
+
// *** Channels ***
|
|
179
|
+
// ********************************************************************************************
|
|
180
|
+
|
|
181
|
+
channels = function(tags){
|
|
182
|
+
all_channels = ctx:channels
|
|
183
|
+
str_tags = tags.typeof()=="Array" => tags.join(",") | tags
|
|
184
|
+
cf_tags = tags => str_tags.lc().split(",").sort().join(",") | null
|
|
185
|
+
cf_tags.isnull() => all_channels
|
|
186
|
+
| all_channels.filter(function(c){c{"tags"}.sort().join(",")==cf_tags})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
deleteChannel = defaction(eci) {
|
|
190
|
+
ctx:delChannel(eci)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
createChannel = defaction(tags,eventPolicy,queryPolicy){
|
|
194
|
+
ctx:newChannel(
|
|
195
|
+
tags=tags,
|
|
196
|
+
eventPolicy=eventPolicy,
|
|
197
|
+
queryPolicy=queryPolicy
|
|
198
|
+
) setting(channel)
|
|
199
|
+
return channel
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ********************************************************************************************
|
|
203
|
+
// *** Picos ***
|
|
204
|
+
// ********************************************************************************************
|
|
205
|
+
|
|
206
|
+
myself = function(){
|
|
207
|
+
{
|
|
208
|
+
"name":ent:name,
|
|
209
|
+
"id":ent:id,
|
|
210
|
+
"eci":ent:eci
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
children = function() {
|
|
215
|
+
convert = function(eci){
|
|
216
|
+
pico = {}
|
|
217
|
+
.put("eci",eci)
|
|
218
|
+
.put("name",ctx:query(eci,ctx:rid,"name"))
|
|
219
|
+
.put("parent_eci",ctx:query(eci,ctx:rid,"parent_eci"))
|
|
220
|
+
pico
|
|
221
|
+
}
|
|
222
|
+
wrangler_children = ctx:children
|
|
223
|
+
.map(convert)
|
|
224
|
+
wrangler_children
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
parent_eci = function() {
|
|
228
|
+
ent:parent_eci
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
name = function() {
|
|
232
|
+
ent:name
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
id = function() {
|
|
236
|
+
meta:picoId
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ********************************************************************************************
|
|
242
|
+
// *** ***
|
|
243
|
+
// *** System ***
|
|
244
|
+
// *** ***
|
|
245
|
+
// ********************************************************************************************
|
|
246
|
+
/* NOT USED IN 1.0.0 */
|
|
247
|
+
rule systemOnLine {
|
|
248
|
+
select when system online
|
|
249
|
+
foreach children() setting(child)
|
|
250
|
+
event:send({ "eci" : child{"eci"},
|
|
251
|
+
"domain": "system", "type": "online",
|
|
252
|
+
"attrs" : event:attrs })
|
|
253
|
+
}
|
|
254
|
+
// ********************************************************************************************
|
|
255
|
+
// *** ***
|
|
256
|
+
// *** Rulesets ***
|
|
257
|
+
// *** ***
|
|
258
|
+
// ********************************************************************************************
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
rule install_ruleset_relatively {
|
|
262
|
+
select when wrangler install_ruleset_request
|
|
263
|
+
absoluteURL re#(.+)# // required
|
|
264
|
+
rid re#(.+)# // required
|
|
265
|
+
setting(absoluteURL,rid)
|
|
266
|
+
pre {
|
|
267
|
+
parts = absoluteURL.split("/")
|
|
268
|
+
url = parts.splice(parts.length()-1,1,rid+".krl").join("/")
|
|
269
|
+
}
|
|
270
|
+
fired {
|
|
271
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
272
|
+
event:attrs
|
|
273
|
+
.delete("absoluteURL") // so this rule won't select again!
|
|
274
|
+
.put({"url":url})
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
rule install_ruleset_absolutely {
|
|
279
|
+
select when wrangler install_ruleset_request
|
|
280
|
+
url re#(.+)# // required
|
|
281
|
+
setting(url)
|
|
282
|
+
pre {
|
|
283
|
+
config = event:attr("config") || {}
|
|
284
|
+
}
|
|
285
|
+
every {
|
|
286
|
+
ctx:flush(url=url)
|
|
287
|
+
ctx:install(url=url, config=config) setting(rid)
|
|
288
|
+
}
|
|
289
|
+
fired {
|
|
290
|
+
raise wrangler event "ruleset_installed" attributes
|
|
291
|
+
event:attrs.put({"rids": [rid]})
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
rule uninstall_one_ruleset {
|
|
296
|
+
select when wrangler uninstall_ruleset_request
|
|
297
|
+
rid re#(.+)# setting(rid)
|
|
298
|
+
ctx:uninstall(rid)
|
|
299
|
+
fired {
|
|
300
|
+
raise wrangler event "ruleset_uninstalled" attributes event:attrs
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
rule flush_all_rulesets {
|
|
305
|
+
select when wrangler rulesets_need_flushing
|
|
306
|
+
foreach ctx:rulesets setting(rs)
|
|
307
|
+
ctx:flush(rs{"url"})
|
|
308
|
+
fired {
|
|
309
|
+
raise wrangler event "rulesets_flushed" on final
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ********************************************************************************************
|
|
314
|
+
// *** Channels ***
|
|
315
|
+
// ********************************************************************************************
|
|
316
|
+
|
|
317
|
+
rule createChannel {
|
|
318
|
+
select when wrangler new_channel_request
|
|
319
|
+
pre {
|
|
320
|
+
tags = event:attrs{"tags"}
|
|
321
|
+
eventPolicy = event:attrs{"eventPolicy"}
|
|
322
|
+
queryPolicy = event:attrs{"queryPolicy"}
|
|
323
|
+
}
|
|
324
|
+
if tags && eventPolicy && queryPolicy then
|
|
325
|
+
createChannel(tags,eventPolicy,queryPolicy) setting(channel)
|
|
326
|
+
fired {
|
|
327
|
+
raise wrangler event "channel_created"
|
|
328
|
+
attributes event:attrs.put({"channel":channel})
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
rule deleteChannel {
|
|
333
|
+
select when wrangler channel_deletion_request
|
|
334
|
+
eci re#^(.+)$# // required
|
|
335
|
+
setting(eci)
|
|
336
|
+
deleteChannel(eci)
|
|
337
|
+
fired {
|
|
338
|
+
raise wrangler event "channel_deleted" attributes event:attrs
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ********************************************************************************************
|
|
343
|
+
// *** Picos ***
|
|
344
|
+
// ********************************************************************************************
|
|
345
|
+
//-------------------- Picos initializing ----------------------
|
|
346
|
+
|
|
347
|
+
rule createChild {
|
|
348
|
+
select when wrangler new_child_request
|
|
349
|
+
name re#.+# // required
|
|
350
|
+
backgroundColor re#.*# // optional
|
|
351
|
+
pre {
|
|
352
|
+
name = event:attrs{"name"}
|
|
353
|
+
backgroundColor = event:attrs{"backgroundColor"} || "#87CEFA"
|
|
354
|
+
engine_ui_rid = "io.picolabs.pico-engine-ui"
|
|
355
|
+
engine_ui_ruleset = function(){
|
|
356
|
+
the_ruleset = ctx:rulesets
|
|
357
|
+
.filter(function(r){r.get("rid")==engine_ui_rid})
|
|
358
|
+
.head();
|
|
359
|
+
{ "url":the_ruleset.get("url"),
|
|
360
|
+
"config":the_ruleset.get("config")
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
every {
|
|
365
|
+
ctx:newPico(rulesets=[
|
|
366
|
+
engine_ui_ruleset(),
|
|
367
|
+
{ "url": ctx:rid_url, "config": {} }
|
|
368
|
+
]) setting(newEci)
|
|
369
|
+
ctx:eventQuery(
|
|
370
|
+
eci=newEci,
|
|
371
|
+
domain="engine_ui",
|
|
372
|
+
name="setup",
|
|
373
|
+
rid=engine_ui_rid,
|
|
374
|
+
queryName="uiECI"
|
|
375
|
+
) setting(newUiECI)
|
|
376
|
+
ctx:event(
|
|
377
|
+
eci=newEci,
|
|
378
|
+
domain="wrangler",
|
|
379
|
+
name="pico_created",
|
|
380
|
+
attrs=event:attrs
|
|
381
|
+
)
|
|
382
|
+
ctx:event(
|
|
383
|
+
eci=newUiECI,
|
|
384
|
+
domain="engine_ui",
|
|
385
|
+
name="box",
|
|
386
|
+
attrs={
|
|
387
|
+
"name": name,
|
|
388
|
+
"backgroundColor": backgroundColor
|
|
389
|
+
}
|
|
390
|
+
)
|
|
391
|
+
}
|
|
392
|
+
fired {
|
|
393
|
+
raise wrangler event "new_child_created"
|
|
394
|
+
attributes event:attrs.put({"eci":newEci})
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//
|
|
399
|
+
// NOTE: the next two rules run in a newly created child pico
|
|
400
|
+
//
|
|
401
|
+
rule initialize_child_after_creation {
|
|
402
|
+
select when wrangler pico_created
|
|
403
|
+
pre {
|
|
404
|
+
subscription_ruleset_url = function(){
|
|
405
|
+
subscription_rid = "io.picolabs.subscription"
|
|
406
|
+
parts = ctx:rid_url.split("/")
|
|
407
|
+
parts.splice(parts.length()-1,1,subscription_rid+".krl").join("/")
|
|
408
|
+
}
|
|
409
|
+
subs_url = subscription_ruleset_url()
|
|
410
|
+
|
|
411
|
+
did_o_ruleset_url = function(){
|
|
412
|
+
did_o_rid = "io.picolabs.did-o"
|
|
413
|
+
parts = ctx:rid_url.split("/")
|
|
414
|
+
parts.splice(parts.length()-1,1,did_o_rid+".krl").join("/")
|
|
415
|
+
}
|
|
416
|
+
did_o_url = did_o_ruleset_url()
|
|
417
|
+
}
|
|
418
|
+
fired {
|
|
419
|
+
raise wrangler event "install_ruleset_request"
|
|
420
|
+
attributes {"url":subs_url}
|
|
421
|
+
raise wrangler event "install_ruleset_request"
|
|
422
|
+
attributes {"url":did_o_url}
|
|
423
|
+
ent:parent_eci := ctx:parent
|
|
424
|
+
ent:name := event:attr("name")
|
|
425
|
+
ent:id := ctx:picoId
|
|
426
|
+
ent:eci := event:eci
|
|
427
|
+
raise wrangler event "pico_initialized"
|
|
428
|
+
attributes event:attrs.put("eci",event:eci)
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
rule finish_child_initialization {
|
|
433
|
+
select when wrangler pico_initialized
|
|
434
|
+
event:send({ "eci" : ent:parent_eci,
|
|
435
|
+
"domain": "wrangler", "type": "child_initialized",
|
|
436
|
+
"attrs" : event:attrs })
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// this pico is the primary pico
|
|
440
|
+
|
|
441
|
+
rule pico_root_created {
|
|
442
|
+
select when engine_ui setup
|
|
443
|
+
if ent:id.isnull() && ent:parent_eci.isnull() then noop()
|
|
444
|
+
fired {
|
|
445
|
+
ent:id := ctx:picoId
|
|
446
|
+
ent:eci := channels("system,self").head(){"id"}
|
|
447
|
+
ent:name := "Pico"
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
rule take_note_of_name_change {
|
|
452
|
+
select when wrangler name_changed
|
|
453
|
+
name re#(.+)# // required
|
|
454
|
+
setting(name)
|
|
455
|
+
fired {
|
|
456
|
+
ent:name := name
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
//-------------------- PARENT PERSPECTIVE ----------------------
|
|
461
|
+
rule deleteOneChild {
|
|
462
|
+
select when wrangler child_deletion_request
|
|
463
|
+
eci re#^(.+)$# setting(eci)
|
|
464
|
+
ctx:delPico(eci)
|
|
465
|
+
fired {
|
|
466
|
+
raise wrangler event "child_deleted"
|
|
467
|
+
attributes event:attrs
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
//-------------------- CHILD PERSPECTIVE ----------------------
|
|
472
|
+
|
|
473
|
+
rule child_initiates_deletion {
|
|
474
|
+
select when wrangler ready_for_deletion
|
|
475
|
+
pre {
|
|
476
|
+
my_eci = channels("system,child").head().get("id")
|
|
477
|
+
}
|
|
478
|
+
if my_eci then
|
|
479
|
+
event:send({"eci":ent:parent_eci,
|
|
480
|
+
"domain":"wrangler", "type":"child_deletion_request",
|
|
481
|
+
"attrs":{"eci":my_eci}})
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
}//end ruleset
|