/
process_model.php
210 lines (178 loc) · 8.96 KB
/
process_model.php
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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project: http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class ProcessError {
const NONE = 0;
const TOO_MANY_ITERATIONS = 1;
const ACCESS_FORBIDDEN = 2;
}
class ProcessOriginType {
const INPUT = 1;
const VIRTUALFEED = 2;
const TASK = 3;
}
class Process
{
public $mysqli;
public $input;
public $feed;
public $timezone = 'UTC';
public $proc_initialvalue; // save the input value at beginning of the processes list execution
public $proc_skip_next; // skip execution of next process in process list
public $proc_goto; // goto step in process list
public $runtime_error = ProcessError::NONE; // Errors that occured at runtime
private $log;
private $modules_functions = array();
private $process_list = array();
public $process_map = array();
public function __construct($mysqli,$input,$feed,$timezone)
{
$this->mysqli = $mysqli;
$this->input = $input;
$this->feed = $feed;
if (!($timezone === NULL)) $this->timezone = $timezone;
$this->log = new EmonLogger(__FILE__);
$this->process_list = $this->get_process_list(); // Load modules modules
// Build map of processids where set
foreach ($this->process_list as $k=>$v) {
if (isset($v['id_num'])) $this->process_map[$v['id_num']] = $k;
}
}
// Triggered when invoking inaccessible methods in this class context, it must be a module function then
public function __call($method, $args){
if (strpos($method, '__') === FALSE) {
$module = "process"; // default to core module 'process'
} else {
$mod_fun = explode('__',$method); // if method contains a '__', assume the format is module__function
$module = $mod_fun[0];
$method = $mod_fun[1];
}
if(isset($this->modules_functions[$module][$method])) {
$args[] = &$this;
return call_user_func_array(array($this->modules_functions[$module][$method], $method), $args);
} else {
$this->log->error("__call() Call to undefined module method. Missing function on module? method=$method ");
throw new Exception("ABORTED: Call to undefined module method. Missing function on module? method=$method");
}
}
public function get_process_list()
{
static $list = array(); // Array to hold the cache
if (empty($list) || empty($this->modules_functions)) { // Cache it now
$list=$this->load_modules();
}
return $list;
}
public function input($time, $value, $processList, $options = null)
{
//$this->log->info("input() received time=$time\tvalue=$value");
$this->proc_initialvalue = $value; // save the input value at beginning of the processes list execution
$this->proc_skip_next = false; // skip execution of next process in process list
$process_list = $this->get_process_list();
$pairs = explode(",",$processList);
$total = count($pairs);
$steps=0;
// if ($total>50) return false;
for ($this->proc_goto=0; $this->proc_goto<$total; $this->proc_goto++) {
$steps++;
$inputprocess = explode(":", $pairs[$this->proc_goto]); // Divide into process key and arg
$processkey = $inputprocess[0]; // Process id
// Map ids to process key names
if (isset($this->process_map[$processkey])) $processkey = $this->process_map[$processkey];
if (!isset($process_list[$processkey])) {
$this->log->error("input() Processor '".$processkey."' does not exists. Module missing?");
return false;
}
$arg = 0;
if (isset($inputprocess[1])) $arg = $inputprocess[1]; // Can be value or feed id
$process_function = $processkey; // get process key 'module.function'
if (strpos($processkey, '__') === FALSE) $process_function = $process_list[$processkey]["function"]; // Is this line needed??
$not_for_virtual_feeds = array('publish_to_mqtt','eventp__sendemail');
if (in_array($process_function, $not_for_virtual_feeds) && isset($options['sourcetype']) && $options['sourcetype']==ProcessOriginType::VIRTUALFEED) {
$this->log->error('Publish to MQTT and SendMail blocked for Virtual Feeds');
} else {
$value = $this->$process_function($arg,$time,$value,$options); // execute process function
}
if ($this->proc_skip_next) {
$this->proc_skip_next = false; $this->proc_goto++;
}
if ($steps > $total*2) {
// We are executing a looping processlist or too much gotos
// need to add 'error_found' process to this processList.
$this->runtime_error = ProcessError::TOO_MANY_ITERATIONS;
$this->log->error("input() DEACTIVATED processList due to too many steps. steps=$steps proc_goto=".$this->proc_goto." processkey=$processkey sourcetype=" . $options['sourcetype'] . " sourceid=" . $options['sourceid'] );
switch ($options['sourcetype']) {
case ProcessOriginType::INPUT:
$this->input->set_processlist($options['sourceid'],"process__error_found:0,".$processList);
break;
case ProcessOriginType::VIRTUALFEED:
$this->feed->set_processlist($options['sourceid'],"process__error_found:0,".$processList);
break;
case ProcessOriginType::TASK:
if (file_exists("Modules/task/task_model.php")) {
global $session, $redis;
require_once "Modules/task/task_model.php";
$this->task = new Task($this->mysqli, $redis, null);
$this->task->set_processlist($session['userid'], $options['sourceid'], "process__error_found:0," . $processList);
}
}
return false;
}
}
return $value;
}
private function load_modules() {
$list = array();
// Always load the process module processes first
$modules = array("process");
// Scan all other modules for process lists
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++) {
$module = $dir[$i];
if (filetype("Modules/$module")=='dir' || filetype("Modules/$module")=='link') {
if ($module!="process") $modules[] = $module;
}
}
// Load processes from selected modules
for ($i=0; $i<count($modules); $i++) {
$class = $this->get_module_class($modules[$i]);
if ($class != null) {
$mod_process_list = $class->process_list();
foreach($mod_process_list as $k => $v) {
$processkey = strtolower($modules[$i]."__".$v['function']);
$list[$processkey] = $v; // set list key as "module__function"
//$this->log->info("load_modules() module=$dir[$i] function=$v[2]");
}
}
}
return $list;
}
private function get_module_class($module_name){
/*
magic function __call (above) MUST BE USED with this.
Load additional processlist module files.
Looks in the folder Modules/modulename/ for a file modulename_processlist.php
(module_name all lowercase but class Modulename_ProcessList in php file that is with upper case first letter)
*/
$module_file = "Modules/".$module_name."/".$module_name."_processlist.php";
$module_class=null;
if(file_exists($module_file)){
require_once($module_file);
$module_class_name = ucfirst(strtolower($module_name)."_ProcessList");
$module_class = new $module_class_name($this); // passes this class as reference
$module_class_functions = get_class_methods($module_class);
foreach($module_class_functions as $key => $function_name){
if (substr($function_name, 0, 2) == "__" || $function_name == "process_list") continue;
$this->modules_functions[strtolower($module_name)][strtolower($function_name)] = &$module_class;
}
}
return $module_class;
}
}