@indra.ai/deva 1.2.18 → 1.2.20
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/config.json +20 -10
- package/index.js +233 -202
- package/package.json +1 -1
package/config.json
CHANGED
|
@@ -30,6 +30,16 @@
|
|
|
30
30
|
"context": false,
|
|
31
31
|
"zone": "deva",
|
|
32
32
|
"zones": {
|
|
33
|
+
"init": "Initialize",
|
|
34
|
+
"start": "Start",
|
|
35
|
+
"stop": "Stop",
|
|
36
|
+
"enter": "Enter",
|
|
37
|
+
"exit": "Exit",
|
|
38
|
+
"finish": "Finish",
|
|
39
|
+
"done": "Done",
|
|
40
|
+
"question": "Question",
|
|
41
|
+
"ask": "Ask",
|
|
42
|
+
"answer": "Answer",
|
|
33
43
|
"deva": "Deva",
|
|
34
44
|
"client": "Client",
|
|
35
45
|
"agent": "Agent",
|
|
@@ -51,7 +61,6 @@
|
|
|
51
61
|
"comedy": "Comedy",
|
|
52
62
|
"drama": "Drama",
|
|
53
63
|
"scifi": "SciFi",
|
|
54
|
-
"finish": "Finish",
|
|
55
64
|
"help": "Help",
|
|
56
65
|
"error": "ERROR!"
|
|
57
66
|
},
|
|
@@ -66,6 +75,7 @@
|
|
|
66
75
|
"listen": "Listen",
|
|
67
76
|
"ask": "Ask",
|
|
68
77
|
"question": "Question",
|
|
78
|
+
"cmd": "Command",
|
|
69
79
|
"answer": "Answer",
|
|
70
80
|
"ready": "Ready",
|
|
71
81
|
"wait": "Waiting",
|
|
@@ -102,6 +112,8 @@
|
|
|
102
112
|
"put": "Put",
|
|
103
113
|
"push": "Push",
|
|
104
114
|
"pull": "Pull",
|
|
115
|
+
"set": "Set",
|
|
116
|
+
"response": "Response",
|
|
105
117
|
"invalid": "INVALID!",
|
|
106
118
|
"abort": "ABORT!",
|
|
107
119
|
"error": "ERROR!",
|
|
@@ -135,25 +147,23 @@
|
|
|
135
147
|
"list": "List items",
|
|
136
148
|
"reply": "Reply",
|
|
137
149
|
"set": "Set",
|
|
150
|
+
"get": "Get",
|
|
138
151
|
"return": "Return",
|
|
139
152
|
|
|
140
|
-
"question_ask": "
|
|
141
|
-
"question_ask_answer": "
|
|
153
|
+
"question_ask": "Ask question",
|
|
154
|
+
"question_ask_answer": "Answer question asked",
|
|
142
155
|
"question_cmd": "Question command",
|
|
143
156
|
"question_method": "Question method",
|
|
144
157
|
"question_talk": "Question talk",
|
|
145
|
-
"question_answer": "
|
|
158
|
+
"question_answer": "Answer question",
|
|
146
159
|
"question_done": "Question done",
|
|
147
160
|
|
|
148
161
|
"answer": "Answer",
|
|
149
|
-
"answer_talk": "Answer talk",
|
|
150
162
|
"ask": "Ask",
|
|
151
|
-
"ask_answer": "Ask answer",
|
|
152
163
|
|
|
153
|
-
"security": "
|
|
154
|
-
"support": "
|
|
155
|
-
"
|
|
156
|
-
"services": "SERVICES",
|
|
164
|
+
"security": "Get Security",
|
|
165
|
+
"support": "Get Support",
|
|
166
|
+
"services": "Get Services",
|
|
157
167
|
|
|
158
168
|
"Client": "Set client",
|
|
159
169
|
"Security": "Set Security",
|
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const { createHash, randomUUID, createCipheriv, createDecipheriv, randomBytes }
|
|
|
10
10
|
const config = require('./config.json').DATA // load the deva core configuration data.
|
|
11
11
|
class Deva {
|
|
12
12
|
constructor(opts) {
|
|
13
|
-
opts = opts || {};
|
|
13
|
+
opts = opts || {}; // set opts to provided opts or an empty object.
|
|
14
14
|
this._id = randomUUID(); // the unique id assigned to the agent at load
|
|
15
15
|
this._info = opts.info || false; // the deva information from the package file.
|
|
16
16
|
this._config = opts.config || {}; // local Config Object
|
|
@@ -20,17 +20,9 @@ class Deva {
|
|
|
20
20
|
this._security = false; // inherited Security features.
|
|
21
21
|
this._support = false; // inherited Support features.
|
|
22
22
|
this._services = false; // inherited Service features.
|
|
23
|
-
this.os =
|
|
24
|
-
this.fs =
|
|
25
|
-
this.path =
|
|
26
|
-
this.crypto = require('crypto'); // It is used to support cryptography for encryption and decryption.
|
|
27
|
-
this.zlib = require('zlib'); // provides compression functionality using Gzip, Deflate/Inflate, and Brotli.
|
|
28
|
-
this.dns = require('dns'); // It is used to lookup and resolve on domain names.
|
|
29
|
-
this.net = require('net'); // It used to create TCP server/client communicate using TCP protocol.
|
|
30
|
-
this.http = require('http'); // It is used to create Http server and Http client.
|
|
31
|
-
this.https = require('https'); // It is used to create Http server and Http client.
|
|
32
|
-
this.url = require('url'); // It is used for URL resolution and parsing.
|
|
33
|
-
this.assert = require('assert'); // It is used for testing itself.
|
|
23
|
+
this.os = os; // It is used to provide basic operating system related utility functions.
|
|
24
|
+
this.fs = fs; // this is so file system functions are in the core.
|
|
25
|
+
this.path = path; // this is so we can get path in the system.
|
|
34
26
|
this.events = opts.events || new EventEmitter({}); // Event Bus
|
|
35
27
|
this.libs = opts.libs || {}; // used for loading library functions
|
|
36
28
|
this.utils = opts.utils || {}; // parse function
|
|
@@ -109,7 +101,6 @@ class Deva {
|
|
|
109
101
|
return new Promise((resolve, reject) => {
|
|
110
102
|
try {
|
|
111
103
|
// set the default listeners for the states of the agent.
|
|
112
|
-
|
|
113
104
|
for (let state in this._states) {
|
|
114
105
|
if (typeof this[state] === 'function') {
|
|
115
106
|
this.events.on(`${this._agent.key}:${state}`, packet => {
|
|
@@ -119,15 +110,17 @@ class Deva {
|
|
|
119
110
|
}
|
|
120
111
|
|
|
121
112
|
// set the assigned listeners for the agent.
|
|
122
|
-
for (let listener in this.listeners) {
|
|
123
|
-
this.events.on(listener, packet => {
|
|
124
|
-
return this.listeners[listener](packet);
|
|
113
|
+
for (let listener in this.listeners) { // loop over the liteners
|
|
114
|
+
this.events.on(listener, packet => { // set the event listener
|
|
115
|
+
return this.listeners[listener](packet); // return the listener function
|
|
125
116
|
})
|
|
126
117
|
}
|
|
127
|
-
return resolve();
|
|
128
118
|
}
|
|
129
119
|
catch (e) {
|
|
130
|
-
return this.error(e, false, reject);
|
|
120
|
+
return this.error(e, false, reject); // pass errors to this.error
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
return resolve(); // resolve the function after everything is done.
|
|
131
124
|
}
|
|
132
125
|
});
|
|
133
126
|
}
|
|
@@ -208,7 +201,6 @@ class Deva {
|
|
|
208
201
|
if (isFunc) this.methods[y] = methods[y].bind(this);
|
|
209
202
|
}
|
|
210
203
|
}
|
|
211
|
-
// console.log('CLINET BEFORE COPY', client);
|
|
212
204
|
const _client = this.copy(client); // copy the client parameter
|
|
213
205
|
this._client = _client; // set local _client to this scope
|
|
214
206
|
return Promise.resolve();
|
|
@@ -352,9 +344,9 @@ class Deva {
|
|
|
352
344
|
/**************
|
|
353
345
|
func: listen
|
|
354
346
|
params:
|
|
355
|
-
- evt:
|
|
347
|
+
- evt: The vent label to listen for
|
|
356
348
|
- callback: The callback function to run when the event fires.
|
|
357
|
-
describe:
|
|
349
|
+
describe: setup a new event listener in the system.
|
|
358
350
|
***************/
|
|
359
351
|
listen(evt, callback) {
|
|
360
352
|
this.listeners[evt] = callback;
|
|
@@ -366,8 +358,7 @@ class Deva {
|
|
|
366
358
|
/**************
|
|
367
359
|
func: once
|
|
368
360
|
params:
|
|
369
|
-
- evt:
|
|
370
|
-
when waiting for a key response one time.
|
|
361
|
+
- evt: The event to listen to for a once call.
|
|
371
362
|
- callback: The callback functoin to run when the event fires.
|
|
372
363
|
describe:
|
|
373
364
|
***************/
|
|
@@ -378,10 +369,9 @@ class Deva {
|
|
|
378
369
|
/**************
|
|
379
370
|
func: ignore
|
|
380
371
|
params:
|
|
381
|
-
- evt:
|
|
372
|
+
- evt: The event you'd like to ignore.
|
|
382
373
|
- callback: a callback function to execute after removing the event from listerns.
|
|
383
|
-
describe:
|
|
384
|
-
The ignore function allow the removal of events that are in the existing devas lister group.
|
|
374
|
+
describe: The ignore function allow the removal of events in the listener group.
|
|
385
375
|
***************/
|
|
386
376
|
ignore(evt, callback) {
|
|
387
377
|
return this.events.removeListener(evt, callback);
|
|
@@ -399,10 +389,9 @@ class Deva {
|
|
|
399
389
|
question(TEXT=false, DATA=false) {
|
|
400
390
|
// check the active status
|
|
401
391
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
402
|
-
|
|
403
|
-
this.
|
|
404
|
-
const
|
|
405
|
-
const t_split = TEXT.split(' '); // split the text on spaces to get words.
|
|
392
|
+
this.zone('question'); // set the zone to question.
|
|
393
|
+
const id = this.uid(); // generate a unique id for transport.
|
|
394
|
+
const t_split = TEXT.split(' '); // split the text on spaces to get words.
|
|
406
395
|
|
|
407
396
|
// check to see if the string is an #ask string to talk to the other Deva.
|
|
408
397
|
const isAsk = t_split[0].startsWith(this.askChr);
|
|
@@ -411,79 +400,81 @@ class Deva {
|
|
|
411
400
|
const isCmd = t_split[0].startsWith(this.cmdChr);
|
|
412
401
|
|
|
413
402
|
// Format the packet for return on the request.
|
|
414
|
-
const data = DATA;
|
|
415
|
-
const packet = {
|
|
416
|
-
id,
|
|
417
|
-
q: false,
|
|
418
|
-
a: false,
|
|
419
|
-
created: Date.now(),
|
|
403
|
+
const data = DATA; // set the DATA to data
|
|
404
|
+
const packet = { // create the base q/a packet
|
|
405
|
+
id, // set the id into packet
|
|
406
|
+
q: false, // create empty q object in packet
|
|
407
|
+
a: false, // create empty a object in packet
|
|
408
|
+
created: Date.now(), // timestamp the packet
|
|
420
409
|
};
|
|
421
410
|
|
|
422
|
-
let text = TEXT,
|
|
423
|
-
params = false,
|
|
424
|
-
method = 'question',
|
|
425
|
-
key = this._agent.key;
|
|
411
|
+
let text = TEXT, // let TEXT is text for a manipulation variable
|
|
412
|
+
params = false, // params as false to build params string
|
|
413
|
+
method = 'question', // set the default method to question
|
|
414
|
+
key = this._agent.key; // set a temporary key from the agent key.
|
|
426
415
|
|
|
427
416
|
return new Promise((resolve, reject) => {
|
|
428
417
|
// resolve with the no text message if the client says nothing.
|
|
429
418
|
if (!TEXT) return this.finish(this._messages.notext, resolve);
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
419
|
+
let _action = 'question'; // set the initial _action to question.
|
|
420
|
+
this.state('question'); // set the state to question
|
|
421
|
+
try { // try to answer the question
|
|
422
|
+
if (isAsk) { // determine if hte question isAsk
|
|
423
|
+
this.state('ask');
|
|
435
424
|
_action = 'question_ask';
|
|
436
|
-
|
|
425
|
+
// if:isAsk split the agent key and remove first command character
|
|
426
|
+
key = t_split[0].substring(1);
|
|
437
427
|
//if:isAsk use text split index 1 as the parameter block
|
|
438
428
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
439
|
-
method = params[0];
|
|
440
|
-
text = t_split.slice(2).join(' ').trim();
|
|
429
|
+
method = params[0]; // the method to check is then params index 0
|
|
430
|
+
text = t_split.slice(2).join(' ').trim(); // rejoin the text with space
|
|
441
431
|
}
|
|
442
|
-
else if (isCmd) {
|
|
432
|
+
else if (isCmd) { // determine if the question is a command
|
|
433
|
+
this.state('cmd'); // set the state to cmd.
|
|
443
434
|
_action = 'question_cmd';
|
|
444
435
|
//if:isCmd use text split index 1 as the parameter block
|
|
445
436
|
params = t_split[0] ? t_split[0].split(':').slice(1) : false;
|
|
446
|
-
method = t_split[0].substring(1);
|
|
447
|
-
text = t_split.slice(1).join(' ').trim();
|
|
437
|
+
method = t_split[0].substring(1); // if:isCmd use the 0 index as the command
|
|
438
|
+
text = t_split.slice(1).join(' ').trim(); // if:isCmd rejoin the string on the space after removing first index
|
|
448
439
|
}
|
|
449
440
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
441
|
+
this.state('data');
|
|
442
|
+
packet.q = { // build packet.q container
|
|
443
|
+
id: this.uid(), // set the transport id for the question.
|
|
444
|
+
agent: this.agent(), // set the agent
|
|
445
|
+
client: this.client(), // set the client
|
|
446
|
+
meta: { // build the meta container
|
|
447
|
+
key, // set the key variable
|
|
448
|
+
method, // set method to track function use
|
|
449
|
+
params, // set any params that are associated
|
|
458
450
|
},
|
|
459
|
-
text,
|
|
460
|
-
data,
|
|
461
|
-
created: Date.now(),
|
|
451
|
+
text, // set the text for the packet.
|
|
452
|
+
data, // set the data object
|
|
453
|
+
created: Date.now(), // timestamp the question
|
|
462
454
|
}
|
|
463
455
|
|
|
464
456
|
// hash the question
|
|
465
457
|
packet.q.meta.hash = this.hash(packet.q);
|
|
466
458
|
|
|
467
|
-
this.action(_action);
|
|
459
|
+
this.action(_action); // set current action to what was defined by _action variable.
|
|
468
460
|
this.talk(config.events.question, this.copy(packet)); // global question event make sure to copy data.
|
|
469
461
|
|
|
470
|
-
if (isAsk) {
|
|
462
|
+
if (isAsk) { // isAsk check if the question isAsk and talk
|
|
471
463
|
// if: isAsk wait for the once event which is key'd to the packet ID for specified responses
|
|
472
464
|
this.talk(`${key}:ask`, packet);
|
|
473
465
|
this.once(`${key}:ask:${packet.id}`, answer => {
|
|
474
466
|
this.action('question_ask_answer');
|
|
475
|
-
|
|
476
467
|
this.talk(config.events.ask, this.copy(answer));
|
|
477
|
-
return this.finish(answer, resolve);
|
|
468
|
+
return this.finish(answer, resolve); // if:isAsk resolve the answer from the call
|
|
478
469
|
});
|
|
479
470
|
}
|
|
480
|
-
else {
|
|
471
|
+
else { // else: answer tue question locally
|
|
481
472
|
this.action('question_answer');
|
|
482
473
|
return this.answer(packet, resolve, reject);
|
|
483
474
|
}
|
|
484
475
|
}
|
|
485
|
-
catch(e) {
|
|
486
|
-
return this.error(e);
|
|
476
|
+
catch(e) {
|
|
477
|
+
return this.error(e); // if a overall error happens this witll call this.error
|
|
487
478
|
}
|
|
488
479
|
});
|
|
489
480
|
}
|
|
@@ -500,7 +491,9 @@ class Deva {
|
|
|
500
491
|
***************/
|
|
501
492
|
answer(packet, resolve, reject) {
|
|
502
493
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
503
|
-
this.
|
|
494
|
+
this.zone('answer');
|
|
495
|
+
const agent = this.agent();
|
|
496
|
+
const client = this.client();
|
|
504
497
|
// check if method exists and is of type function
|
|
505
498
|
const {method,params} = packet.q.meta;
|
|
506
499
|
const isMethod = this.methods[method] && typeof this.methods[method] == 'function';
|
|
@@ -509,7 +502,7 @@ class Deva {
|
|
|
509
502
|
}
|
|
510
503
|
// Call the local method to process the question based the extracted parameters
|
|
511
504
|
return this.methods[method](packet).then(result => {
|
|
512
|
-
this.
|
|
505
|
+
this.state('answer');
|
|
513
506
|
// check the result for the text, html, and data object.
|
|
514
507
|
// this is for when answers are returned from nested Devas.
|
|
515
508
|
const text = typeof result === 'object' ? result.text : result;
|
|
@@ -517,28 +510,25 @@ class Deva {
|
|
|
517
510
|
// if the data passed is NOT an object it will FALSE
|
|
518
511
|
const data = typeof result === 'object' ? result.data : false;
|
|
519
512
|
|
|
520
|
-
const
|
|
521
|
-
const client = this.client() || false;
|
|
522
|
-
const packet_answer = { // setup the packet.a container
|
|
513
|
+
const packet_answer = { // setup the packet.a container
|
|
523
514
|
id: this.uid(),
|
|
524
|
-
agent,
|
|
525
|
-
client,
|
|
526
|
-
meta: {
|
|
527
|
-
key: agent.key,
|
|
528
|
-
method,
|
|
529
|
-
params,
|
|
515
|
+
agent, // set the agent who answered the question
|
|
516
|
+
client, // set the client asking the question
|
|
517
|
+
meta: { // setup the answer meta container
|
|
518
|
+
key: agent.key, // set the agent key inot the meta
|
|
519
|
+
method, // set the method into the meta
|
|
520
|
+
params, // set the params into the meta
|
|
530
521
|
},
|
|
531
|
-
text,
|
|
532
|
-
html,
|
|
533
|
-
data,
|
|
534
|
-
created: Date.now(),
|
|
522
|
+
text, // set answer text
|
|
523
|
+
html, // set the answer html
|
|
524
|
+
data, // set the answer data
|
|
525
|
+
created: Date.now(), // set the created date for the answer
|
|
535
526
|
};
|
|
536
527
|
|
|
537
528
|
// create a hash for the answer and insert into answer meta.
|
|
538
529
|
packet_answer.meta.hash = this.hash(packet_answer);
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
this.action('answer_talk');
|
|
530
|
+
packet.a = packet_answer; // set the packet.a to the packet_answer
|
|
531
|
+
this.action('answer'); // set the action to answer
|
|
542
532
|
this.talk(config.events.answer, this.copy(packet)); // global talk event
|
|
543
533
|
return this.finish(packet, resolve); // resolve the packet to the caller.
|
|
544
534
|
}).catch(err => { // catch any errors in the method
|
|
@@ -564,7 +554,7 @@ class Deva {
|
|
|
564
554
|
ask(packet) {
|
|
565
555
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
566
556
|
|
|
567
|
-
this.
|
|
557
|
+
this.zone('ask');
|
|
568
558
|
|
|
569
559
|
const agent = this.agent();
|
|
570
560
|
const client = this.client();
|
|
@@ -587,14 +577,14 @@ class Deva {
|
|
|
587
577
|
try {
|
|
588
578
|
if (typeof this.methods[packet.q.meta.method] !== 'function') {
|
|
589
579
|
return setImmediate(() => {
|
|
590
|
-
this.action('invalid')
|
|
580
|
+
this.action('invalid');
|
|
591
581
|
this.talk(`${this._agent.key}:ask:${packet.id}`, this._methodNotFound(packet));
|
|
592
582
|
});
|
|
593
583
|
}
|
|
594
584
|
|
|
585
|
+
this.state('ask');
|
|
595
586
|
// The method is parsed and depending on what method is asked for it returns
|
|
596
587
|
// the response based on the passed through packet.
|
|
597
|
-
this.action('ask');
|
|
598
588
|
this.methods[packet.q.meta.method](packet).then(result => {
|
|
599
589
|
if (typeof result === 'object') {
|
|
600
590
|
packet_answer.text = result.text || false;
|
|
@@ -605,7 +595,7 @@ class Deva {
|
|
|
605
595
|
packet_answer.text = result;
|
|
606
596
|
}
|
|
607
597
|
packet_answer.meta.hash = this.hash(packet_answer);
|
|
608
|
-
this.action('
|
|
598
|
+
this.action('ask');
|
|
609
599
|
packet.a = packet_answer;
|
|
610
600
|
this.talk(config.events.answer, this.copy(packet)); // global talk event
|
|
611
601
|
this.talk(`${agent.key}:ask:${packet.id}`, packet);
|
|
@@ -652,7 +642,6 @@ class Deva {
|
|
|
652
642
|
}
|
|
653
643
|
_data.hash = this.hash(_data);
|
|
654
644
|
|
|
655
|
-
this.state('init');
|
|
656
645
|
return new Promise((resolve, reject) => {
|
|
657
646
|
this.events.setMaxListeners(this.maxListeners);
|
|
658
647
|
this._assignInherit().then(() => {
|
|
@@ -664,6 +653,7 @@ class Deva {
|
|
|
664
653
|
}).then(() => {
|
|
665
654
|
return this.Security();
|
|
666
655
|
}).then(() => {
|
|
656
|
+
this.zone('init');
|
|
667
657
|
const hasOnInit = this.onInit && typeof this.onInit === 'function';
|
|
668
658
|
return hasOnInit ? this.onInit(_data) : this.start(_data)
|
|
669
659
|
}).catch(err => {
|
|
@@ -683,6 +673,7 @@ class Deva {
|
|
|
683
673
|
usage: this.start('msg')
|
|
684
674
|
***************/
|
|
685
675
|
start(data) {
|
|
676
|
+
this.zone('start');
|
|
686
677
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
687
678
|
this.state('start');
|
|
688
679
|
data.value = 'start';
|
|
@@ -705,6 +696,7 @@ class Deva {
|
|
|
705
696
|
usage: this.enter('msg')
|
|
706
697
|
***************/
|
|
707
698
|
enter(data) {
|
|
699
|
+
this.zone('enter');
|
|
708
700
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
709
701
|
this.state('enter');
|
|
710
702
|
data.value = 'enter';
|
|
@@ -727,6 +719,7 @@ class Deva {
|
|
|
727
719
|
usage: this.done('msg')
|
|
728
720
|
***************/
|
|
729
721
|
done(data) {
|
|
722
|
+
this.zone('done');
|
|
730
723
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
731
724
|
this.state('done');
|
|
732
725
|
data.value = 'done';
|
|
@@ -742,20 +735,25 @@ class Deva {
|
|
|
742
735
|
params:
|
|
743
736
|
- packet: the data to pass to the resolve
|
|
744
737
|
- resolve: the finish resolve to pass back
|
|
745
|
-
describe:
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
usage:
|
|
749
|
-
this.finish(data, resolve)
|
|
738
|
+
describe: This function is use to relay the Agent ito a finish state when
|
|
739
|
+
resolving a question or data.
|
|
740
|
+
usage: this.finish(data, resolve)
|
|
750
741
|
***************/
|
|
751
742
|
finish(packet, resolve) {
|
|
743
|
+
this.zone('finish'); // set the zone to finish
|
|
752
744
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
753
|
-
|
|
745
|
+
|
|
746
|
+
this.state('finish'); // set the finish state
|
|
754
747
|
packet.hash = this.hash(packet);// hash the entire packet before finishing.
|
|
755
|
-
|
|
748
|
+
// check for agent on finish function in agent
|
|
749
|
+
const hasOnFinish = this.onFinish && typeof this.onFinish === 'function';
|
|
750
|
+
|
|
751
|
+
this.action('finish'); // set the finish action
|
|
752
|
+
// if: agent has on finish then return on finish
|
|
756
753
|
|
|
754
|
+
this.context('finish'); // set the context to finish
|
|
757
755
|
if (hasOnFinish) return this.onFinish(packet, resolve);
|
|
758
|
-
|
|
756
|
+
// return the provided resolve function or a promise resolve.
|
|
759
757
|
return resolve ? resolve(packet) : Promise.resolve(packet);
|
|
760
758
|
}
|
|
761
759
|
|
|
@@ -773,24 +771,21 @@ class Deva {
|
|
|
773
771
|
this.stop()
|
|
774
772
|
***************/
|
|
775
773
|
stop() {
|
|
774
|
+
this.zone('stop'); // set the zone to stop
|
|
776
775
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
value: this._messages.stop,
|
|
786
|
-
agent,
|
|
787
|
-
client,
|
|
788
|
-
created: Date.now(),
|
|
776
|
+
this.state('stop'); // set the state to stop
|
|
777
|
+
const data = { // build the stop data
|
|
778
|
+
id: this.uid(), // set the id
|
|
779
|
+
agent: this.agent(), // set the agent
|
|
780
|
+
client: this.client(), // set the client
|
|
781
|
+
key: 'stop', // set the key
|
|
782
|
+
value: this._messages.stop, // set the value
|
|
783
|
+
created: Date.now(), // set the created date
|
|
789
784
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
this.state('stop');
|
|
785
|
+
this.action('stop'); // set the stop action
|
|
786
|
+
// has stop function then set hasOnStop variable
|
|
793
787
|
const hasOnStop = this.onStop && typeof this.onStop === 'function';
|
|
788
|
+
// if: has on stop then run on stop function or return exit function.
|
|
794
789
|
return hasOnStop ? this.onStop(data) : this.exit(data)
|
|
795
790
|
}
|
|
796
791
|
|
|
@@ -811,9 +806,8 @@ class Deva {
|
|
|
811
806
|
***************/
|
|
812
807
|
exit() {
|
|
813
808
|
this.zone('exit');
|
|
814
|
-
this._active = false;
|
|
815
809
|
|
|
816
|
-
this.
|
|
810
|
+
this.state('exit');
|
|
817
811
|
const agent = this.agent();
|
|
818
812
|
const client = this.client();
|
|
819
813
|
|
|
@@ -834,7 +828,7 @@ class Deva {
|
|
|
834
828
|
this._support = false;
|
|
835
829
|
this._services = false;
|
|
836
830
|
|
|
837
|
-
this.
|
|
831
|
+
this.action('exit');
|
|
838
832
|
const hasOnExit = this.onExit && typeof this.onExit === 'function';
|
|
839
833
|
return hasOnExit ? this.onExit(data) : Promise.resolve(data)
|
|
840
834
|
}
|
|
@@ -845,27 +839,28 @@ class Deva {
|
|
|
845
839
|
/**************
|
|
846
840
|
func: state
|
|
847
841
|
params:
|
|
848
|
-
-
|
|
849
|
-
|
|
842
|
+
- value: The state value to set for the Deva that matches to this._states
|
|
843
|
+
- extra: any extra text to add ot the state change.
|
|
850
844
|
***************/
|
|
851
|
-
state(
|
|
845
|
+
state(value=false, extra=false) {
|
|
852
846
|
try {
|
|
853
|
-
if (!this._states[
|
|
854
|
-
this._state = state
|
|
855
|
-
const
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
847
|
+
if (!value || !this._states[value]) return; // return if no matching value
|
|
848
|
+
this._state = value; // set the local state variable.
|
|
849
|
+
const lookup = this._states[value]; // set the local states lookup
|
|
850
|
+
const text = extra ? `${lookup} ${extra}` : lookup; // set text from lookup with extra
|
|
851
|
+
const data = { // build the data object
|
|
852
|
+
id: this.uid(), // set the data id
|
|
853
|
+
agent: this.agent(), // set the agent
|
|
854
|
+
client: this.client(), // set the client
|
|
855
|
+
key: 'state', // set the key to state
|
|
856
|
+
value, // set the value to the passed in value
|
|
857
|
+
text, // set the text value of the data
|
|
858
|
+
created: Date.now(), // set the data created date.
|
|
864
859
|
};
|
|
865
|
-
data.hash = this.hash(data);
|
|
866
|
-
this.talk(config.events.state, data);
|
|
867
|
-
} catch (e) {
|
|
868
|
-
return this.error(e);
|
|
860
|
+
data.hash = this.hash(data); // hash the data
|
|
861
|
+
this.talk(config.events.state, data); // broadcasat the state event
|
|
862
|
+
} catch (e) { // catch any errors
|
|
863
|
+
return this.error(e); // return if an error happens
|
|
869
864
|
}
|
|
870
865
|
}
|
|
871
866
|
|
|
@@ -890,16 +885,20 @@ class Deva {
|
|
|
890
885
|
- st: The zone flag to set for the Deva that matches to this._zones
|
|
891
886
|
describe
|
|
892
887
|
***************/
|
|
893
|
-
zone(value) {
|
|
894
|
-
if (!this._zones[value] || value === this._zone) return;
|
|
888
|
+
zone(value=false, extra=false) {
|
|
889
|
+
if (!value || !this._zones[value] || value === this._zone) return;
|
|
895
890
|
try {
|
|
896
891
|
this._zone = value;
|
|
897
|
-
|
|
898
|
-
const
|
|
899
|
-
|
|
892
|
+
|
|
893
|
+
const lookup = this._zones[value]; // set the lookup value
|
|
894
|
+
const text = extra ? `${lookup} ${extra}` : lookup; // set the text value
|
|
895
|
+
|
|
896
|
+
const data = { // build the zone data
|
|
897
|
+
id: this.uid(), // set the packetid
|
|
898
|
+
agent: this.agent(),
|
|
899
|
+
client: this.client(),
|
|
900
900
|
key: 'zone',
|
|
901
901
|
value,
|
|
902
|
-
agent: this.agent(),
|
|
903
902
|
text,
|
|
904
903
|
created: Date.now(),
|
|
905
904
|
};
|
|
@@ -910,35 +909,46 @@ class Deva {
|
|
|
910
909
|
}
|
|
911
910
|
}
|
|
912
911
|
|
|
912
|
+
/**************
|
|
913
|
+
func: zones
|
|
914
|
+
params: none
|
|
915
|
+
describe: returns a listing of zones currently in the system.
|
|
916
|
+
***************/
|
|
913
917
|
zones() {
|
|
914
|
-
this.action('zones');
|
|
918
|
+
this.action('zones'); // set the action to zones
|
|
915
919
|
return {
|
|
916
|
-
id: this.uid(true),
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
+
id: this.uid(true), // set the uuid of the data
|
|
921
|
+
agent: this.agent(), // set the agent value
|
|
922
|
+
cleint: this.cleint(), // set the client value
|
|
923
|
+
key: 'zones', // set the key return value
|
|
924
|
+
value: this._zones, // set the list of zones
|
|
925
|
+
created: Date.now(), // set the created date of the object.
|
|
920
926
|
}
|
|
921
927
|
}
|
|
928
|
+
|
|
922
929
|
/**************
|
|
923
930
|
func: action
|
|
924
931
|
params:
|
|
925
|
-
-
|
|
932
|
+
- value: The state flag to set for the Deva that matches to this._states
|
|
933
|
+
- extra: Any extra text to send with the action value.
|
|
926
934
|
describe
|
|
927
935
|
***************/
|
|
928
|
-
action(
|
|
936
|
+
action(value=false, extra=false) {
|
|
929
937
|
try {
|
|
930
|
-
|
|
938
|
+
if (!value) return; // check feature value
|
|
939
|
+
this._action = value; // set the local action variable
|
|
931
940
|
// check local vars for custom actions
|
|
932
|
-
const var_action = this.vars.actions ? this.vars.actions[
|
|
933
|
-
// check action
|
|
934
|
-
const msg_action = this._actions[
|
|
935
|
-
const
|
|
941
|
+
const var_action = this.vars.actions ? this.vars.actions[value] : false;
|
|
942
|
+
// check the message action
|
|
943
|
+
const msg_action = var_action || this._actions[value];
|
|
944
|
+
const msg = msg_action || action; // set the correct message
|
|
945
|
+
const text = extra ? `${msg} ${extra}` : msg; // set the text of the action
|
|
936
946
|
const data = { // build the data object for the action.
|
|
937
947
|
id: this.uid(true), // generate a guid for the action transmitssion.
|
|
938
|
-
key: 'action', // the key for event to transmit action type
|
|
939
|
-
value: action, // the value key which is the action passed
|
|
940
948
|
agent: this.agent(), // the agent data to send with the action
|
|
941
949
|
client: this.client(), // the client data to send with the action
|
|
950
|
+
key: 'action', // the key for event to transmit action type
|
|
951
|
+
value, // the value key which is the action passed
|
|
942
952
|
text, // text of the action to send
|
|
943
953
|
created: Date.now(), // action time stamp
|
|
944
954
|
};
|
|
@@ -949,71 +959,90 @@ class Deva {
|
|
|
949
959
|
}
|
|
950
960
|
}
|
|
951
961
|
|
|
962
|
+
/**************
|
|
963
|
+
func: actions
|
|
964
|
+
params: none
|
|
965
|
+
describe: Returns a list of available actions in the system.
|
|
966
|
+
***************/
|
|
952
967
|
actions() {
|
|
953
968
|
this.action('actions');
|
|
954
969
|
return {
|
|
955
|
-
id: this.uid(true),
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
970
|
+
id: this.uid(true), // set the id with a uuid
|
|
971
|
+
agent: this.agent(), // set the agent value
|
|
972
|
+
client: this.client(), // set the client value
|
|
973
|
+
key: 'actions', // set the data key
|
|
974
|
+
value: this._actions, // set the value to the actions list
|
|
975
|
+
created: Date.now(), // set the data created date
|
|
959
976
|
}
|
|
960
977
|
}
|
|
961
978
|
|
|
962
979
|
/**************
|
|
963
980
|
func: feature
|
|
964
981
|
params:
|
|
965
|
-
-
|
|
982
|
+
- value: The feature flag to set for the Deva that matches to this._features
|
|
983
|
+
- extra: Any extra text to send with the feature value.
|
|
966
984
|
describe
|
|
967
985
|
***************/
|
|
968
|
-
feature(
|
|
986
|
+
feature(value=false, extra=false) {
|
|
969
987
|
try {
|
|
970
|
-
if (!this._features[
|
|
971
|
-
this._feature = feature
|
|
972
|
-
|
|
973
|
-
const
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
988
|
+
if (!value || !this._features[value]) return; // check feature value
|
|
989
|
+
this._feature = value; // set local feature variable
|
|
990
|
+
|
|
991
|
+
const lookup = this._features[value]; // set the lookup value
|
|
992
|
+
const text = extra ? `${lookup} ${extra}` : lookup; // set the text value
|
|
993
|
+
|
|
994
|
+
const data = { // build data object
|
|
995
|
+
id: this.uid(true), // set the id
|
|
996
|
+
agent: this.agent(), // set the agent transporting the packet.
|
|
997
|
+
key: 'feature', // set the key for transport
|
|
998
|
+
value, // set the value of the key
|
|
999
|
+
text, // set the text value
|
|
1000
|
+
created: Date.now(), // set the creation date
|
|
980
1001
|
};
|
|
981
|
-
|
|
982
|
-
this.talk(config.events.feature,
|
|
983
|
-
} catch (e) {
|
|
984
|
-
return this.error(e);
|
|
1002
|
+
data.hash = this.hash(data); // generate the hash value of the data packet
|
|
1003
|
+
this.talk(config.events.feature, data); // talk the feature event with data
|
|
1004
|
+
} catch (e) { // catch any errors
|
|
1005
|
+
return this.error(e); // retun this.error when an error is caught.
|
|
985
1006
|
}
|
|
986
1007
|
}
|
|
987
1008
|
|
|
1009
|
+
/**************
|
|
1010
|
+
func: features
|
|
1011
|
+
params: none
|
|
1012
|
+
describe: return a list of features that are available to the system.
|
|
1013
|
+
***************/
|
|
988
1014
|
features() {
|
|
989
|
-
this.action('features');
|
|
990
|
-
return {
|
|
991
|
-
id: this.uid(true),
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1015
|
+
this.action('features'); // set the action to features
|
|
1016
|
+
return { // return the data object
|
|
1017
|
+
id: this.uid(true), // set the object id
|
|
1018
|
+
agent: this.agent(), // set the agent value.
|
|
1019
|
+
client: this.client(), // set the client value.
|
|
1020
|
+
key: 'features', // set the key
|
|
1021
|
+
value: this._features, // set the value to the features list
|
|
1022
|
+
created: Date.now(), // set the created date.
|
|
995
1023
|
}
|
|
996
1024
|
}
|
|
997
1025
|
|
|
998
1026
|
/**************
|
|
999
1027
|
func: context
|
|
1000
1028
|
params:
|
|
1001
|
-
-
|
|
1029
|
+
- value: The context flag to set for the Deva that matches to this._contexts
|
|
1030
|
+
- extra: Any extra text that is sent with the context value.
|
|
1002
1031
|
describe
|
|
1003
1032
|
***************/
|
|
1004
1033
|
context(value=false, extra=false) {
|
|
1005
1034
|
try {
|
|
1006
|
-
if (!value) return
|
|
1035
|
+
if (!value) return;
|
|
1007
1036
|
this._context = value;
|
|
1008
1037
|
const lookup = this.vars.context[value] || value;
|
|
1009
1038
|
const text = extra ? `${lookup} ${extra}` : lookup;
|
|
1010
1039
|
|
|
1011
1040
|
const data = {
|
|
1012
1041
|
id: this.uid(true),
|
|
1013
|
-
key: 'context',
|
|
1014
|
-
value,
|
|
1015
1042
|
agent: this.agent(),
|
|
1016
1043
|
client: this.client(),
|
|
1044
|
+
key: 'context',
|
|
1045
|
+
value,
|
|
1017
1046
|
text,
|
|
1018
1047
|
created: Date.now(),
|
|
1019
1048
|
};
|
|
@@ -1028,6 +1057,8 @@ class Deva {
|
|
|
1028
1057
|
this.action('contexts');
|
|
1029
1058
|
return {
|
|
1030
1059
|
id: this.uid(true),
|
|
1060
|
+
agent: this.agent(),
|
|
1061
|
+
client: this.client(),
|
|
1031
1062
|
key: 'contexts',
|
|
1032
1063
|
value: this.vars.context || false,
|
|
1033
1064
|
created: Date.now(),
|
|
@@ -1037,29 +1068,25 @@ class Deva {
|
|
|
1037
1068
|
/**************
|
|
1038
1069
|
func: client
|
|
1039
1070
|
params: none
|
|
1040
|
-
describe:
|
|
1041
|
-
this function allows state management for when client prfioe is
|
|
1042
|
-
being accessed.
|
|
1071
|
+
describe: returns the current client values in the system.
|
|
1043
1072
|
usage: this.client();
|
|
1044
1073
|
***************/
|
|
1045
1074
|
client() {
|
|
1046
|
-
if (!this._active) return this._messages.offline;
|
|
1047
|
-
const client_copy = this.copy(this._client);
|
|
1048
|
-
return client_copy;
|
|
1075
|
+
if (!this._active) return this._messages.offline; // check the active status
|
|
1076
|
+
const client_copy = this.copy(this._client); // create a copy of the client data
|
|
1077
|
+
return client_copy; // return the copy of the client data.
|
|
1049
1078
|
}
|
|
1050
1079
|
|
|
1051
1080
|
/**************
|
|
1052
1081
|
func: agent
|
|
1053
1082
|
params: none
|
|
1054
|
-
describe:
|
|
1055
|
-
this function allows statement management for when client prfioe is
|
|
1056
|
-
being accessed.
|
|
1083
|
+
describe: returns the current agent values in the system.
|
|
1057
1084
|
usage: this.agent()
|
|
1058
1085
|
***************/
|
|
1059
1086
|
agent() {
|
|
1060
|
-
if (!this._active) return this._messages.offline;
|
|
1061
|
-
const agent_copy = this.copy(this._agent);
|
|
1062
|
-
return agent_copy;
|
|
1087
|
+
if (!this._active) return this._messages.offline; // check the active status
|
|
1088
|
+
const agent_copy = this.copy(this._agent); // create a copy of the agent data.
|
|
1089
|
+
return agent_copy; // return the copy of the agent data.
|
|
1063
1090
|
}
|
|
1064
1091
|
|
|
1065
1092
|
// FEATURE FUNCTIONS
|
|
@@ -1088,11 +1115,13 @@ class Deva {
|
|
|
1088
1115
|
support() {
|
|
1089
1116
|
this.feature('support'); // set the support state
|
|
1090
1117
|
if (!this._active) return this._messages.offline; // check the active status
|
|
1091
|
-
this.state('data');
|
|
1118
|
+
this.state('data'); // set the state to data
|
|
1092
1119
|
try {
|
|
1093
|
-
this.action('support');
|
|
1120
|
+
this.action('support'); // set the action to support.
|
|
1094
1121
|
return this.copy(this._support); // return the support feature
|
|
1095
|
-
} catch (e) {
|
|
1122
|
+
} catch (e) {
|
|
1123
|
+
return this.error(e); // return this.error when error catch
|
|
1124
|
+
}
|
|
1096
1125
|
}
|
|
1097
1126
|
|
|
1098
1127
|
/**************
|
|
@@ -1108,7 +1137,9 @@ class Deva {
|
|
|
1108
1137
|
try {
|
|
1109
1138
|
this.action('services'); // set the services state
|
|
1110
1139
|
return this.copy(this._services); // return the services feature
|
|
1111
|
-
} catch (e) {
|
|
1140
|
+
} catch (e) {
|
|
1141
|
+
return this.error(e); // return this.error when error catch
|
|
1142
|
+
}
|
|
1112
1143
|
}
|
|
1113
1144
|
|
|
1114
1145
|
/**************
|