-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction.js
More file actions
131 lines (109 loc) · 3.52 KB
/
Copy pathinstruction.js
File metadata and controls
131 lines (109 loc) · 3.52 KB
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
// First we'll need to compile the rule
// Parse the rule
// Get the default rule to execute
// analyse the WHAT
// analyse the type
// this is mainly for future use; i.e the NLP tags that will need to map against
// if it starts w/ a "rule" then execute the referenced Rule and populate the values for the WHAT field
// else that is 'what' nayya will hv to ask
// analyse the WHEN
// if it starts w/ a "rule" then execute the referenced Rule and populate the values for the WHEN field
// else check the tags
// if its 'daily', check time of day
// if its in, fire up in the next array item duration
// analyse the WHO
// if it starts w/ a "rule" then execute the referenced Rule and populate the values for the WHO field
// else check the tags
// match the nick name to a slack user id/channel
// analyse rules
// the rules will be executed sequentially after the current rule is executed
var fs = require('fs');
var rule = require('./rule.js');
var schedule = require('node-schedule');
var instructionObject;
var ruleAtHead = 0;
var rulesOnMainThread = {};
var rulesOnRefThread = {};
var __onComplete;
var scheduledJob;
var runRule = function()
{
var keys = Object.keys(rulesOnMainThread),
length = keys.length,
i = ruleAtHead,
prop,
value;
var message = {
username : "nayya",
channel : '',
text : '',
runNow : true
};
// while (i < length) {
prop = keys[i];
if(rulesOnMainThread[prop].what.isRule)
;// call the Rule
else
message.text = rulesOnMainThread[prop].what.toAsk;
if(rulesOnMainThread[prop].when.isRule)
;// call the Rule
else
; //message.text = rulesOnMainThread[prop].what.toAsk;
if(rulesOnMainThread[prop].who.isRule)
;// call the Rule
else
message.channel = rulesOnMainThread[prop].who.toAsk;
scheduledJob = schedule.scheduleJob(rulesOnMainThread[prop].when.toRecur, function() {
var string = '{"username" : "nayya", "channel" : "' + message.channel + '", "text" : "' + message.text + '"}';
console.log(string);
});
// i++;
// }
}
instruction.prototype.compile = function()
{
// Main Thread - loop thru all the rules and compile them
for(var item in instructionObject.mainThread)
{
console.log('compiling ' + item);
rulesOnMainThread[item] = new rule(instructionObject.mainThread[item]);
}
// Ref Thread - loop thru all the rules and compile them
for(var item in instructionObject.refThread)
{
console.log('compiling ' + item);
rulesOnRefThread[item] = new rule(instructionObject.refThread[item]);
}
runRules();
}
instruction.prototype.load = function(filename, __callback)
{
// Read from the file
fs.readFile(filename, (err, data) => {
if (err) throw err;
// Call the callback
__load(data, __callback);
});
}
function __load(data, __callback)
{
// Clear the object
instructionObject = null;
try {
instructionObject = JSON.parse(data);
} catch (err) {
console.log(err, err.stack);
return;
}
typeof (__callback) == 'function' ? __callback() : true;
}
function instruction(instructionFile)
{
// Load the instruction
this.__onComplete = this.run;
this.load('reminder/groceries.json', this.compile);
}
// Unit Testing
//instruction();
var instruct = new instruction();
module.exports = instruction;