Starting with Reactor 4, you have the possibility to create your own monitors by writing plugins in Lua. In other words, write your own plugins to fit your own needs.
In order to create plugins, you'd need to write them in Lua, which is a well-known scripting language. If programming is not your cup of tea, feel free to contact us with your requirements, or check the included plugins, a list that will keep growing.
Lua is a powerful and lightweight programming language. It supports general procedural programming as well as object-oriented and functional paradigms. It has automatic memory management (garbage collection) so you do not have to worry about allocating and deallocating memory.
-- Lua code example
print("Hello World!")
function factorial(n)
if n == 0 then
return 1
else
return n * factorial(n - 1)
end
end
Lua Plugins in Reactor
A Reactor plugin consists of a Lua script that implements a function named plugin. This function will be called every time the monitor is executed, and it will receive the parameters specified in the monitor URL, if any. The function must return -at least- a boolean value indicating if the monitoring was successful. Optionally, it can also return a responseText (string) and a responseCode (number) with extra details as a result of the execution.
function plugin(options) -- do some stuff return success, responseText, responseCode end
Brief Introduction to Lua
Lua is dynamically typed, meaning that values have types, but variables do not.There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. Although only the following 6 are normally used in Reactor plugins:
- nil: It represents the absence of a useful value, a kind of non-value.
- boolean: It has two possible values: true or false.
- number: The number type represents floating point (or real) numbers. Integer values can also be represented with this type.
- string: A sequence of characters. Each character has 8 bits with any numeric value [0..255]. This means that any binary data can also be stored in a string.
- table: A type that implements associative arrays and can also be used to store ordinary arrays, records and other data structures.
- function: The type of a code fragment that can be called with or without parameters. Functions are first-class values in Lua; they can be assigned to variables and passed to other functions.
Local variables are defined with the local keyword:
local message = "Hello world!"
Some basic notions:
- Expressions in Lua can be numeric constants and string literals, variables, unary and binary operations, and function calls.
- The usual arithmetic operators are supported: + (addition), - (subtraction), * (multiplication), / (division), % (modulo), and ^ (exponentiation).
- The relational operators in Lua are: == , ~= , < , > , <= and, >=
- The logical operators in Lua are and, or, and not.
- All logical operators consider both false and nil as false and anything else as true.
Lua provides you with the most commonly used control structures. The if instruction for conditionals and while, repeat, and for for iteration. All control structures have an explicit terminator: end terminates the if, for and while structures; and until terminates the repeat structure.
-- if condition then
if a < 0 then
a = 0
end
-- if condition then else
if a < b then
return a
else
return b
end
-- while
local i = 1
while not isProcessFinished() or (i > maxTries) do
tryAgain()
i = i + 1
end
-- repeat until
repeat
line = readData()
until line ~= ""
-- numeric for: for var=init, limit, step do
for i=100, 1 , -1 do
print(i)
end
-- generic for: for namelist in explist do
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
revDays = {}
for i,v in ipairs(days) do
revDays[v] = i
end
Finally, Lua supports multiple assignments in one statement. The syntax for assignment defines a list of variables on the left side and a list of expressions on the right side, separated by commas.
x, name, result = 42 , "Reactor" , falseThere is a specific concatenation operator, indicated by 2 dots ('..'), that is used for concatenating strings. The '+' operator is only for addition in Lua and using it with strings will cause a runtime error.
local searchWord = "Lua" local baseUrl = "http://en.wikipedia.org/w/index.php" local url = baseUrl .. "?title=" .. searchWord
Lua official website
Lua 5.1 Reference Manual
Programming in Lua (online version)
Lua Unofficial FAQ