How to send an email automatically after a limit violation?

How to send an email automatically after a limit violation?
Requirements:
- Installation of SPM (SENTRON Powermanager) V2.0
- Knowledge of writing scripts in SPM.
- EXPERT license of SPM
- SMTP server via the emails are sent
This is just an example how a script for an email alert could be written. This is not the
way it has to be.
This document shows the script that can be used if you want to send an email
automatically after a defined limit was violated.
The script is a global one and is running with its own manager.
1. Creating the script
Please open the GEDI via the system settings in your PM explorer. This can be found
after a right click on “System1” and the “Graphics Editor” Button.
After opening the GEDI please create a new CTRL script.
Below you will find the script in detail. Please note that the used names of the variables
are not fixed. You can also use your own names. The ones used are just for this example.
The script for sending an email has to contain of the following parts:
- Loading the email sending functionality of SPM that is written in the “email.ctl”config
o #uses “email.ctl”
-
-
-
-
In the main function the data point has to be defined and the address where it is
located. You can use the datapoint selector under the “Tools” menu to select the
datapoint to want to control.
o string sDP = “datapoint”;
The script works with global variables for the last value (g_LastValue) and the
limit (g_LimitforValue) you want to check. If these variables don’t exist they
need to be created.
o if
(!globalExists(“g_LastValue”))
addGlobal(“g_LastValue”,
FLOAT_VAR);
o if (!globalExists("g_LimitforValue")) addGlobal("g_LimitforValue",
FLOAT_VAR);
Then you have to assign the limit of the value you want to control.
o g_LimitforValue = xxx.xx;
The value of the data point and the global variable need to be read
o Dpget(sDP, g_LastValue);
Then connect to the checking of the value function.
o dpConnect(”ValueChanged”,sDP);
The checking value function has the following parts:
o The function itself with the used variables
 ValueChanged(string dp, float fNewValue)
o If the last value is under the limit and the new value is above the limit than
we have to open the email sending function to send the email.
 if ((g_LastValue < g_LimitforValue) && (fNewValue >=
g_LimitforValue))
email(fNewValue)
o updating the last value with the current value for the next check
 g_LastValue = fNewValue;
executing the email sending function with the needed parameters
o email(float value)
 detailed description please see below
2. Email function “emSendMail”
To send an email we have to use the function “emSendMail”. This is a predefined
function and is available after using the “email.ctl”-library.
This function connects to a SMTP server and sends an email based on SMTP. After the
email was send the connection will be closed.
The function has the following structure and parameters:
emSendMail (string smtp_host, string name, dyn_string email, int ret);
smtp_host: host name or IP address of SMTP server
name: domain name or IP address of the client (identifies the client at the server)
email: dyn string contains information of email (reciever, sender, subject,
message body
ret: return value 0 for OK or -1 for errors
This is the structure how a function for sending emails can look like:
Email()
{
int ret;
dyn_string email_cont;
email_cont[1] = “[email protected];[email protected]";
email_cont[2] = "[email protected]";
email_cont[3] = "Test";
email_cont[4] = "Hello World";
emSendMail(“SMTP_server", “Client_IP", email_cont, ret);
}
The first field of the array email_cont contains the information about the receiver, the
second one about the sender of the email. In the third one you find the subject and the
fourth contains the message body of your email.
3. Final script
Please see below how the script looks like in the final version:
First check the script with the “Syntax-Check-button”. Then save the script in the
“scripts”-folder of your project with a click on the “save”-button. Name it like you want
and give the ending .ctl.
4. Adding the Control Manager
To run the script you have to add a new manager to the console. This manager is a
control manager.
Please navigate to the Console and add a new manager with the button on the right side.
Select the Control Manager in the manager list add the option “-script scriptname” and
set the start mode to always.
After the manager is created please start it.