/
PDS.krl
537 lines (485 loc) · 16.9 KB
/
PDS.krl
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
533
534
535
536
537
ruleset b506607x16 {
meta {
name "PDS"
description <<
Spime Data Services
>>
author "Phil Windley & Ed Orcutt & PICOLABS"
logging on
provides items, get_keys, // general
profile, // profile
settings, get_config_value, get_setting_data_value , config_value// settings
sharing on
}
// --------------------------------------------
// ent:profile
// {
// "name": "",
// "description": "",
// "location": "",
// "model": "",
// "model_description": "",
// "photo": "https://s3.amazonaws.com/k-mycloud/a169x672/unknown.png"
// }
// ent:general
// namespace : {
// key : <value>
// }
//
// ent:settings
// "<rulesetID>" : {
// "name" : "",
// "rID" : <rulesetID>,
// "data" : {},
// "schema" : []
// }
//
// --------------------------------------------
global {
/* // -for debugging???-------------------------------------------
get_all_items = function() {
ent:general;
};
*/
items = function (namespace, key){
item = function(namespace, keyvalue) {
ent:general{[namespace, keyvalue]}
};
multipleItems = function(namespace) {
ent:general{namespace}
};
return = (key.isnull()) => multipleItems( namespace) | item(namespace, key) ;
status = namespace.isnull() => "failed" | "success";
{
'status' : ( status ),
'general' : return
};
}
// set up pagination. look at fuse_fuel.krl allfillup -DEAD?
sorted_values = function(namespace, sort_opt, num_to_return) {
the_keys = this2that:transform(ent:general{[namespace]}, sort_opt); // get all the keys sorted by the key value provided in sort_opt
the_keys.isnull() => [] |
not num_to_return.isnull() => the_keys.slice(0,num_to_return-1) // only return how much we want
| the_keys
};
/*
trips = function(id, limit, offset) {
// x_id = id.isnull().klog(">>>> id >>>>>");
// x_limit = limit.klog(">>>> limit >>>>>");
// x_offset = offset.klog(">>>> offset >>>>>");
id.isnull() || id eq "" => allTrips(limit, offset)
| ent:trips_by_id{mkTid(id)};
};
allTrips = function(limit, offset) {
sort_opt = {
"path" : ["endTime"],
"reverse": true,
"compare" : "datetime"
};
max_returned = 25;
hard_offset = offset.isnull()
|| offset eq "" => 0 // default
| offset;
hard_limit = limit.isnull()
|| limit eq "" => 10 // default
| limit > max_returned => max_returned
| limit;
global_opt = {
"index" : hard_offset,
"limit" : hard_limit
};
sorted_keys = this2that:transform(ent:trip_summaries, sort_opt, global_opt.klog(">>>> transform using global options >>>> "));
sorted_keys.map(function(id){ ent:trip_summaries{id} })
};
*/
profile = function(key){
get_profile = function(k) {
ent:profile{k};
};
get_all_profile = function() {
ent:profile;
};
return = (key.isnull()) => get_all_profile() | get_profile(key);
// status update
{
'status' : ("success"),
'profile' : return
};
};
// --------------------------------------------dead
settings_names = function() {
foo = ent:settings.keys().map(function(setRID) {
setName = ent:settings{[setRID,"Name"]};
{
"setRID": setRID,
"setName": setName
}
});
foo
};
settings = function(Rid,Key,detail){ // do we need to have a default for detail?
//detail only will work with "Data" varible
get_setting_all = function() { //dead, rid should never be null
ent:settings
};
get_setting = function(rid) {
ent:settings{rid}
};
get_setting_value = function(rid, varible) {
ent:settings{[rid, varible]}
};
get_setting_value_default = function(rid, varible, value) {
ent:settings{[rid, "Data",value]}
};
return = (Key.isnull()) => (get_setting(Rid) ) | (
Rid.isnull() => "error" |
( (key eq "Data") => get_setting_value_default(Rid,Key,detail) | get_setting_value(Rid,Key)));
{
'status' : "success",// update
'settings' : return
};
}
// keep ,--------------------------------------------
setting_data = function(setRID) {
ent:settings{[setRID, "Data"]}
};
// --------------------------------------------
setting_schema = function(setRID) {
ent:settings{[setRID, "Schema"]}
};
// -------------------------------------------- I think sorting and filtering should be done by client or spime_management and not the server
setting_data_value = function(setRID, setKey) {
ent:settings{[setRID, "Data", setKey]}
};
config_value = function(setKey) {
setRID = meta:callingRID();
ent:settings{[setRID, "Data", setKey]}
};
defaultProfile = {
"name": "",
"notes": "",
"location": "",
"model": "",
"description": "",
"pohoto": "https://s3.amazonaws.com/k-mycloud/a169x672/unknown.png"
};
// defaultCloud = {
// "mySchemaName" : "Person",
// "myDoorbell" : "none"
// };
}
// Rules
//------------------------------- ent: general
rule add_item {
select when pds new_data_available
pre {
namespace = event:attr("namespace").defaultsTo("", "no namespace");
keyvalue = event:attr("key").defaultsTo("", "no key");
hash_path = [namespace, keyvalue]; //array of keys
value = event:attr("value").defaultsTo("", "no value");
}
always {
set ent:general{hash_path} value;
raise pds event data_added with
namespace = namespace and
keyvalue = keyvalue;
}
}
rule update_item {
select when pds updated_data_available
foreach(event:attr("value") || {}) setting(akey, avalue)
pre {
namespace = event:attr("namespace").defaultsTo("", "no namespace");
keyvalue = event:attr("key").defaultsTo("", "no key");
hash_path = [namespace, keyvalue, akey];
}
always {
set ent:general{ hash_path } avalue;
raise pds event data_updated with
namespace = namespace and
keyvalue = keyvalue if last;
}
}
rule remove_item {
select when pds remove_old_data
pre{
namespace = event:attr("namespace").defaultsTo("", "no namespace");
keyvalue = event:attr("key").defaultsTo("", "no key");
hash_path = [namespace, keyvalue];
}
always {
clear ent:general{hash_path};
raise pds event data_deleted with
namespace = namespace and
keyvalue = keyvalue;
}
}
rule remove_namespace {
select when pds remove_namespace
pre{
namespace = event:attr("namespace").defaultsTo("", "no namespace");
}
always {
clear ent:general{namespace};
raise pds event namespace_deleted with
namespace = namespace;
}
}
rule itemed_mapped {// should use decode??
select when pds map_item
pre{
namespace = event:attr("namespace").defaultsTo("", "no namespace");
mapvalues = event:attr("mapvalues").defaultsTo("", "no mapvalues").decode();
}
always {
set ent:general{namespace} mapvalues;
raise pds event new_map_added with
namespace = namespace and
mapvalues = mapvalues;
}
}
// should not be here !!!!!!!
rule add_spime_item { // uses different hash_path to add a varible
select when pds new_data2_available
pre {
namespace = event:attr("namespace").defaultsTo("", "no namespace");
spime = event:attr("spime").defaultsTo("", "no spime");
keyvalue = event:attr("keyvalue").defaultsTo("", "no keyvalue");
hash_path = [namespace, spime, keyvalue];
value = event:attr("value").defaultsTo("", "no value");
}
always {
set ent:general{hash_path} value;
}
}
// I dont think we need myCloud any more.
/*
rule SDS_init_mycloud {
select when web sessionReady
if (ent:general{"myCloud"} == 0) then { noop(); }
fired {
set ent:general{"myCloud"} defaultCloud;
}
}
// ------------------------------------------------------------------------
rule SDS_legacy_person {
select when web sessionReady
pre {
schema = ent:general{["myCloud", "mySchemaName"]};
}
if (schema eq "person") then { noop(); }
fired {
set ent:general{["myCloud", "mySchemaName"]} "Person";
}
}
*/
// profile
/*
rule init_profile { // should we combine with edit_profile? we only need to add created varible check in edit.
select when pds init_profile
pre {
profile = ent:profile;
buildProfile = function(){
created = time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"});
newProfile = event:attrs().defaultsTo(0, "no attrs");
ConstructedProfile = newProfile// does || work?
.put(["Name"], (newProfile{"Name"} || defaultProfile{"Name"}))
.put(["Description"], (newProfile{"Description"} || defaultProfile{"Description"}))
.put(["location"], (newProfile{"location"} || defaultProfile{"location"}))
.put(["model"], (newProfile{"model"} || defaultProfile{"model"}))
.put(["model_description"], (newProfile{"model_description"} || defaultProfile{"model_description"}))
.put(["Photo"], (newProfile{"Photo"} || defaultProfile{"Photo"}))
.put(["_created"], created)
.put(["_modified"], time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"}))
;
ConstructedProfile;
};
newly_constructed_profile = (profile == 0) =>
buildProfile() |
"profile exsist";
}
if (profile == 0) then {
noop();
}
fired {
set ent:profile newly_constructed_profile;
}
}
*/
rule update_profile {
select when pds updated_profile
pre {
profile = ent:profile || defaultProfile;
newProfile = event:attrs().defaultsTo(0, "no attrs");
created = function(){time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"});};
buildProfile = function(newProfile){
ConstructedProfile = newProfile// does || work?
.put(["name"], (newProfile{"name"} || profile{"name"}))
.put(["description"], (newProfile{"description"} || profile{"description"}))
.put(["location"], (newProfile{"location"} || profile{"location"}))
.put(["model"], (newProfile{"model"} || profile{"model"}))
.put(["model_description"], (profile{"model_description"} || profile{"model_description"}))
.put(["photo"], (newProfile{"photo"} || profile{"photo"}))
.put(["_created"], (profile{"_created"}||created()))
.put(["_modified"], time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"}))
;
ConstructedProfile;
};
newly_constructed_profile = (newProfile neq 0) => // as long as we have something to update
buildProfile(newProfile) |
"nothing to update";
}
if (newly_constructed_profile neq "nothing to update") then {
noop();
}
fired {
set ent:profile newly_constructed_profile;
raise pds event "profile_updated" attributes event:attrs();
}
}
/* rule SDS_update_profile { // do we need this rule?
select when sds new_profile_item_available
pre {
// get when sds was created.
created = profile("_created") || time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"});
newProfile = event:attrs();
newProfileWithImage = newProfile
.put(["myProfilePhoto"], (newProfile{"Photo"} || defaultProfile{"Photo"}))
.put(["_created"], created)
.put(["_modified"], time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"}))
;
}
always {
set ent:profile newProfileWithImage;
raise sds event "profile_updated" attributes newProfileWithImage;
}
}
// pass any number of key value pair
rule SDS_update_profile_partial {
select when sds updated_profile_item_available
foreach event:attrs() setting(profile_key, profile_value)
{
noop();
}
fired {
set ent:profile {} if not ent:profile; // creates a profile ent if not aready there
set ent:profile{profile_key} profile_value;
raise sds event "profile_updated" on last;
}
}
/*
rule SDS_new_profile_schema {
select when sds new_profile_schema
pre{
hash_path = ["myCloud", "mySchemaName"]; // whats my cloud for ???
mySchemaName = event:attr("mySchemaName").defaultsTo("", "no mySchemaName");
}
always {
set ent:general{hash_path} mySchemaName; // why is this stored in general and not profile?
}
}
rule SDS_update_doorbell {
select when sds new_doorbell_available
pre{
doorbell = event:attr("doorbell").defaultsTo("", "no doorbell");
hash_path = ["myCloud", "myDoorbell"];
}
always {// why do we put this in both profile and general ???
set ent:profile{"myDoorbell"} doorbell;
set ent:general{hash_path} doorbell;
}
}
*/
//----------------------------settings
// ent:settings
// "a169x222" : {
// "Name" : "",
// "RID" : "a169x222",
// "Data" : {},
// "Schema" : []
// }
rule settings_added{ // will this fire with out kre stopping the failed passed varibles
select when pds add_settings
pre {
setName = event:attr("Name").defaultsTo(0,"no Name");
setRID = event:attr("RID").defaultsTo(0,"no RID");
setSchema = event:attr("Schema").defaultsTo(0,"no Schema");
setData = event:attr("Data").defaultsTo(0,"no Data");
setAttr = event:attr("setAttr").defaultsTo(0,"no setAttr");
setValue = event:attr("Value").defaultsTo(0,"no Value");
}
always {
set ent:settings{[setRID, "Name"]} setName if not setName;
set ent:settings{[setRID, "RID"]} setRID if not setRID;
set ent:settings{[setRID, "Schema"]} setSchema if not setSchema;
//set ent:settings{[setRID, "Data"]} setData if not setData;
set ent:settings{[setRID, "Data", setAttr]} setValue if not setAttr;
raise pds event "settings_added" attributes event:attrs();
}
}
/*
rule SDS_add_settings_schema {
select when sds new_settings_schema
pre {
setName = event:attr("Name").defaultsTo("unknown","no Name");
setRID = event:attr("RID").defaultsTo("unknown","no RID");
setSchema = event:attr("Schema").defaultsTo([],"no Schema");
setData = event:attr("Data").defaultsTo({},"no Data");
gotData = ent:settings{[setRID, "setData"]};
}
always {
set ent:settings{[setRID, "Name"]} setName;
set ent:settings{[setRID, "RID"]} setRID;
set ent:settings{[setRID, "Schema"]} setSchema;
set ent:settings{[setRID, "Data"]} setData if not gotData;
}
}
rule SDS_add_settings_data {
select when sds new_settings_data
pre {
setRID = event:attr("RID").defaultsTo("unknown","no RID");
setData = event:attr("Data").defaultsTo({},"no Data");
hash_path = [setRID, "setData"];
}
always {
set ent:settings{hash_path} setData;
}
}
rule SDS_add_settings {
select when sds new_settings_available
pre {
setRID = event:attr("RID").defaultsTo("unknown","no RID");
setData = event:attr("Data").defaultsTo({},"no Data");
hash_path = [setRID, "setData"];
}
always {
set ent:settings{hash_path} setData.delete(["setRID"]); // why not use clear????
}
}
rule SDS_add_settings_attribute {
select when sds new_settings_attribute
pre {
setRID = event:attr("RID").defaultsTo("unknown","no RID");
setAttr = event:attr("setAttr").defaultsTo("unknown","no setAttr");
setValue = event:attr("Value").defaultsTo("unknown","no Value");
hash_path = [setRID, "setData", setAttr];
}
always {
set ent:settings{hash_path} setValue;
}
}
*/
rule SDS_spime_remove {
select when pds spime_uninstalled
pre{
}
always {
clear ent:general;
clear ent:profile;
clear ent:settings;
}
}
// ------------------------------------------------------------------------
// Beyond here there be dragons :)
// ------------------------------------------------------------------------
}