1 | var extend = require('extend'); |
||
2 | |||
3 | // ------------------------------------------ |
||
4 | // CONSTRUCTOR |
||
5 | // ------------------------------------------ |
||
6 | |||
7 | var init = function(){ |
||
8 | info('Starting ScriptManager ...'); |
||
9 | return ScriptManager; |
||
10 | } |
||
11 | |||
12 | // ------------------------------------------ |
||
13 | // RUN / CALL |
||
14 | // ------------------------------------------ |
||
15 | |||
16 | var last = function(callback){ |
||
17 | if (_name && _last){ |
||
18 | run(_name, _last, callback); |
||
19 | } |
||
20 | } |
||
21 | var _name, _last; |
||
22 | var run = function(name, options, callback, backup){ |
||
23 | |||
24 | // Last Backup |
||
25 | if (backup){ |
||
26 | _name = name; _last = {}; |
||
27 | extend(true, _last, options); |
||
28 | ScriptManager.lastContext = _last; // to be available everywhere |
||
29 | } |
||
30 | |||
31 | var data = {}; |
||
32 | extend(true, data, options); |
||
33 | |||
34 | // 3. Finish by calling back |
||
35 | var next = function(json){ |
||
36 | if (json){ extend(true, data, json); } |
||
37 | if (callback) { callback(data); } |
||
38 | } |
||
39 | |||
40 | // 2. Dispatch to next script |
||
41 | var dispatch = function(json){ |
||
42 | if (json){ extend(true, data, json); } |
||
43 | |||
44 | // 1.3 Call script |
||
45 | SARAH.RuleEngine.dispatch('after', data); |
||
0 ignored issues
–
show
|
|||
46 | |||
47 | // Dispatch |
||
48 | SARAH.RuleEngine.dispatch(name, data, next); |
||
49 | } |
||
50 | |||
51 | // 1.1 Call before |
||
52 | SARAH.RuleEngine.dispatch('before', data); |
||
53 | |||
54 | // 1.2 Call script |
||
55 | call(name, data, dispatch); |
||
56 | } |
||
57 | |||
58 | var call = function(name, options, callback){ |
||
59 | |||
60 | // Find Plugin |
||
61 | var plugin = SARAH.find(name); |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
62 | if (!plugin){ |
||
63 | warn('call('+name+') ', "Can't find plugin"); |
||
64 | if (callback){ callback(); } return; |
||
65 | } |
||
66 | |||
67 | // Get instance outside of a zone; |
||
68 | var plug = plugin.getInstance(); |
||
69 | |||
70 | // Set callback |
||
71 | var next = function(data){ |
||
72 | if (timeout){ clearTimeout(timeout); } else { return; } |
||
73 | |||
74 | var end = (new Date()).getTime(); |
||
75 | info('call('+name+') in ', (end-start)+'ms'); |
||
76 | |||
77 | if (data && data.error){ |
||
78 | error('call('+name+') ', data.error); |
||
79 | } |
||
80 | |||
81 | if (callback){ callback(data); } |
||
82 | } |
||
83 | |||
84 | // Set timeout |
||
85 | var timeout = setTimeout(function(){ |
||
86 | warn('action('+name+') as timeout ! Check plugin\'s callback()'); |
||
87 | next(); |
||
88 | }, Config.http.timeout); |
||
0 ignored issues
–
show
The variable
Config seems to be never declared. If this is a global, consider adding a /** global: Config */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
89 | |||
90 | // Run script |
||
91 | var start = (new Date()).getTime(); |
||
92 | try { |
||
93 | var instance = plugin.getInstance(); |
||
94 | plugin.getInstance().action(options, next, Config, SARAH); |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
95 | } |
||
96 | catch(ex){ |
||
97 | error('call('+name+') ', ex.message); |
||
98 | next(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // ------------------------------------------ |
||
103 | // HOOKS |
||
104 | // ------------------------------------------ |
||
105 | |||
106 | var standBy = function(motion, device){ |
||
107 | var plugins = SARAH.PluginManager.getList(); |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
108 | for (var i = 0 ; i < plugins.length ; i++){ |
||
109 | var plugin = plugins[i].getInstance(); |
||
110 | if (plugin && plugin.standBy) plugin.standBy(motion, device); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | var speak = function(tts, async){ |
||
115 | if (!tts){ return tts; } |
||
116 | |||
117 | // Answer |
||
118 | if (tts == 'answer'){ |
||
119 | var answers = Config.bot.answers.split('|'); |
||
0 ignored issues
–
show
The variable
Config seems to be never declared. If this is a global, consider adding a /** global: Config */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
120 | tts = answers[ Math.floor(Math.random() * answers.length)]; |
||
121 | } |
||
122 | |||
123 | // Dispatch to all plugins |
||
124 | var plugins = SARAH.PluginManager.getList(); |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
125 | for (var i = 0 ; i < plugins.length ; i++){ |
||
126 | var plugin = plugins[i].getInstance(); |
||
127 | if (plugin && plugin.speak){ |
||
128 | tts = plugin.speak(tts, async); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | // Replace Name |
||
133 | if (Profile.last && Profile.last.face && Profile.last.face != "Unknow"){ |
||
0 ignored issues
–
show
The variable
Profile seems to be never declared. If this is a global, consider adding a /** global: Profile */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
134 | tts = tts.replace("[name]", Profile.last.face); |
||
135 | } |
||
136 | tts = tts.replace('[name]',''); |
||
137 | |||
138 | return tts; |
||
139 | } |
||
140 | |||
141 | // ------------------------------------------ |
||
142 | // PUBLIC |
||
143 | // ------------------------------------------ |
||
144 | |||
145 | var ScriptManager = { |
||
146 | 'init' : init, |
||
147 | 'run' : run, |
||
148 | 'call' : call, |
||
149 | 'last' : last, |
||
150 | 'standBy' : standBy, |
||
151 | 'speak' : speak, |
||
152 | 'lastContext' : {} |
||
153 | } |
||
154 | |||
155 | // Exports Manager |
||
156 | exports.init = ScriptManager.init; |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.