-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy patheventp_processlist.php
204 lines (185 loc) · 8.67 KB
/
eventp_processlist.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
<?php
/*
Released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
EventProcesses module contributed by Nuno Chaveiro nchaveiro(at)gmail.com 2015
---------------------------------------------------------------------
Sponsored by http://archimetrics.co.uk/
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
// Schedule Processlist Module
class Eventp_ProcessList
{
private $log;
private $parentProcessModel;
// Module required constructor, receives parent as reference
public function __construct(&$parent)
{
$this->log = new EmonLogger(__FILE__);
$this->parentProcessModel = &$parent;
}
// / Below are functions of this module processlist, same name must exist on process_list()
public function process_list() {
textdomain("eventp_messages");
return array(
array(
"name"=>_("If rate >=, skip next"),
"short"=>"?rate>=",
"argtype"=>ProcessArg::VALUE,
"function"=>"ifRateGtEqualSkip",
"datafields"=>0,
"unit"=>"",
"group"=>_("Conditional - Event"),
"requireredis"=>true,
"nochange"=>true,
"description"=>_("<p>If value from last process has an absolute change from previous time it was calculated higher or equal to the specified value, processlist execution will skip the next process.</p>")
),
array(
"name"=>_("If rate <, skip next"),
"short"=>"?rate<",
"argtype"=>ProcessArg::VALUE,
"function"=>"ifRateLtSkip",
"datafields"=>0,
"unit"=>"",
"group"=>_("Conditional - Event"),
"requireredis"=>true,
"nochange"=>true,
"description"=>_("<p>If value from last process has an absolute change from previous time it was calculated lower than the specified value, processlist execution will skip the next process.</p>")
),
array(
"name"=>_("If Mute, skip next"),
"short"=>"?mute",
"argtype"=>ProcessArg::VALUE,
"function"=>"ifMuteSkip",
"datafields"=>0,
"unit"=>"",
"group"=>_("Conditional - Event"),
"requireredis"=>true,
"nochange"=>true,
"description"=>_("<p>A time elapsed dependent condition, first time a processlist passes here the flow is unchanged. Next times the same processlist passes here, if the specified value time (in seconds) has not elapsed, flow will skip next process.</p>")
),
array(
"name"=>_("If !Mute, skip next"),
"short"=>"?!mute",
"argtype"=>ProcessArg::VALUE,
"function"=>"ifNotMuteSkip",
"datafields"=>0,
"unit"=>"",
"group"=>_("Conditional - Event"),
"requireredis"=>true,
"nochange"=>true,
"description"=>_("<p>A time elapsed dependent condition, first time a processlist passes here the flow skips next. Next times the same processlist passes here, if the specified value time (in seconds) has elapsed, flow will skip next process.</p>")
),
array(
"name"=>_("Send Email"),
"short"=>"email",
"argtype"=>ProcessArg::TEXT,
"function"=>"sendEmail",
"datafields"=>0,
"unit"=>"",
"group"=>_("Event"),
"nochange"=>true,
"description"=>_("<p>Send an email to the user with the specified body. Email sent to user's email address or default set in config.</p><p>Supported template tags to customize body: {type}, {id}, {key}, {name}, {node}, {time}, {value}</p><p>Example body text: At {time} your {type} from {node} with key {key} named {name} had value {value}.</p>")
)
);
}
public function sendEmail($emailbody, $time, $value, $options) {
global $user, $session, $settings;
$timeformated = DateTime::createFromFormat("U", (int)$time);
if(!empty($this->parentProcessModel->timezone)) $timeformated->setTimezone(new DateTimeZone($this->parentProcessModel->timezone));
$timeformated = $timeformated->format("Y-m-d H:i:s");
$tag = array("{id}","{type}","{time}","{value}");
$replace = array($options['sourceid'],$options['sourcetype'],$timeformated, $value);
$emailbody = str_replace($tag, $replace, $emailbody);
if ($options['sourcetype'] == "INPUT"){
$inputdetails = $this->parentProcessModel->input->get_details($options['sourceid']);
$tag = array("{key}","{name}","{node}");
$replace = array($inputdetails['name'], $inputdetails['description'], $inputdetails['nodeid']);
$emailbody = str_replace($tag, $replace, $emailbody);
} elseif ($options['sourcetype'] == "VIRTUALFEED") {
// Not supported for VIRTUAL FEEDS
}
//need to get an email address from the config file or the form ?
$emailto = $settings['smtp']['default_emailto'];
if (!empty($emailto)) {
require_once "Lib/email.php";
$email = new Email();
//$email->from(from);
$email->to($emailto);
$email->subject('Emoncms event alert');
$email->body($emailbody);
$result = $email->send();
if (!$result['success']) {
$this->log->error("Email send returned error. message='" . $result['message'] . "'");
} else {
$this->log->info("Email sent to $emailto");
}
} else {
$this->log->error("No email address specified");
}
}
public function ifMuteSkip($ttl, $time, $value, $options)
{
global $redis;
if ($redis) {
$timenow = time();
$redispath = "process:ifMuteSkip:".$options['sourcetype'].":".$options['sourceid']."_".$this->parentProcessModel->proc_goto;
//$this->log->info("ifMuteSkip() timenow=$timenow ttl=$ttl redispath=$redispath");
if ($redis->exists($redispath)) {
$this->parentProcessModel->proc_skip_next = true;
} else {
$redis->set($redispath, $timenow);
$redis->expire($redispath, $ttl); // removed in $ttl seconds.
}
}
return $value;
}
public function ifNotMuteSkip($ttl, $time, $value, $options)
{
global $redis;
if ($redis) {
$timenow = time();
$redispath = "process:ifNotMuteSkip:".$options['sourcetype'].":".$options['sourceid']."_".$this->parentProcessModel->proc_goto;
//$this->log->info("ifNotMuteSkip() timenow=$timenow ttl=$ttl redispath=$redispath");
if (!$redis->exists($redispath)) {
$this->parentProcessModel->proc_skip_next = true;
$redis->set($redispath, $timenow);
$redis->expire($redispath, $ttl); // removed in $ttl seconds.
}
}
return $value;
}
public function ifRateGtEqualSkip($arg, $time, $value, $options)
{
global $redis;
if ($redis) {
$redispath = "process:ifRateGtEqualSkip:".$options['sourcetype'].":".$options['sourceid']."_".$this->parentProcessModel->proc_goto;
//$this->log->info("ifRateGtEqualSkip() time=$time value=$value redispath=$redispath");
if ($redis->exists($redispath)) {
$lastvalue = $redis->hmget($redispath,array('time','value'));
$change = abs($value - $lastvalue['value']);
if ($change >= $arg)
$this->parentProcessModel->proc_skip_next = true;
}
$redis->hMset($redispath, array('time' => $time, 'value' => $value));
}
return $value;
}
public function ifRateLtSkip($arg, $time, $value, $options)
{
global $redis;
if ($redis) {
$redispath = "process:ifRateLtSkip:".$options['sourcetype'].":".$options['sourceid']."_".$this->parentProcessModel->proc_goto;
//$this->log->info("ifRateLtSkip() time=$time value=$value redispath=$redispath");
if ($redis->exists($redispath)) {
$lastvalue = $redis->hmget($redispath,array('time','value'));
$change = abs(floatval($value) - floatval($lastvalue['value']));
if ($change < $arg)
$this->parentProcessModel->proc_skip_next = true;
}
$redis->hMset($redispath, array('time' => $time, 'value' => $value));
}
return $value;
}
}