Basis van LUA
LUA is een dynamisch getypeerde imperatieve scripttaal die veel als geïntegreerde scripttaal in applicaties gebruikt wordt, maar ook los gebruikt kan worden. De scripts worden uitgevoerd op een virtuele machine met garbage collection. De taal heeft een eenvoudige syntaxis met enkele primitieve types (zoals booleans, doubles en strings) en tabellen, in essentie associatieve arrays, waarmee de bekende datastructuren zoals arrays, lijsten en hashmaps geconstrueerd kunnen worden.
De taal is ontwikkeld door Roberto Ierusalimschy, Waldemar Celes en Luiz Henrique de Figueiredo aan de PUC-Rio te Rio de Janeiro, Brazilië. De versies tot versie 5.0 zijn uitgebracht onder een licentie die vergelijkbaar is met de BSD-licentie. Vanaf versie 5.0 is Lua uitgebracht onder de MIT-licentie.
De volledige documentatie van de LUA programmeertaal kan teruggevonden worden op de officiele site: http://www.lua.org/manual/5.3/. Hieronder vindt men wat basis principes en elementen die men nodig heeft om succesvolle start met LUA te maken.
Structuur van de code
In LUA worden net zoals in andere programmeertalen de regels code onder elkaar getypt. Het heeft geen 'termination' teken nodig op het eind van elke regel code. Omdat in de programmeer wereld vaak in engelse termen gesproken wordt zal de korte uitleg verder op deze pagina engelstalig zijn.
Declaring local variables
If you want to declare a local variable, type:
local <variable_name>
Alternatively, we can assign an initial value when we declare the variable:
local <variable_name> = <value>
For example:
local var1 = "test" local var2 = "naam" local var3 = 17
Conditional Statements
Conditional statements have the form:
if (<conditions>) then <commands> elseif (<conditions>) then <commands> elseif (<conditions>) then <commands> else <commands> end
For example:
if ( a == 1) then b = 2 c = 3 elseif (a == 2) then b = 3 c = 4 else b = 4 c = 5 end
This example means:
“If the variable a has a value of 1, then assign the value 2 to the variable b and the value 3 to the variable c, otherwise, if the variable a has a value of 2, then assign the value 3 to variable b and the value 4 to the variable c, otherwise assign the value of 4 to variable b and the value of 5 to the variable c“.
Operators
== equal ~= different (not equal) > greater than < less than >= greater than or equal <= less than or equal
For example:
local a = 2 local b = 3 local c = 5 -- Displays "a less than or equal to b" if (a > b) then fibaro:debug('a greater than b'); else fibaro:debug('a less than or equal to b'); end -- Displays "c equal to 5" if (c ~= 5) then fibaro:debug('c not equal to 5'); else fibaro:debug('c equal to 5'); end
Arithmetic Operators:
+ addition - substraction * multiplication / division % the remainder (modulo)
For example:
local a = 2 local b = 3 local c = 5 local result1 = a + b + c local result2 = b / a local result3 = b * c local result4 = c % a -- Display: "a + b + c = 10" fibaro:debug('a + b + c =' .. result1) -- Display: "b / a = 1.5" fibaro:debug('b / a =' .. result2) -- Display: "b * c = 15" fibaro:debug('b * c =' .. result3) -- Display: "c % a = 3" fibaro:debug('c % a =' .. result4)
Logical Operators:
and conjunction or alternative not negation
For example:
local a = 2 local b = 3 local c = 5 -- Displays "true" if (a == 2 and b == 3) then fibaro:debug('true') else fibaro:debug('false') end -- Displays "false" -- because c is equal to 5 but we want to know if it's NOT the case) if (not (a == 5 or b == 5 or c == 5)) then fibaro:debug('true') else fibaro:debug('false') end
Concatenation Operators:
To combine strings (text variables) in LUA, we can use two dots ".."
For example:
local text1 = "I " local text2 = "love " local text3 = "Home Center 2!" local text4 = "My name is " local result = text1 .. text2 .. text3 local result2 = text4 .. 'Lili.' --Displays "I love Home Center 2!" fibaro:debug(result) --Displays "My name is Lili." fibaro:debug(result2)
Types of variables (numbers vs strings)
LUA does not explicitly declare the type of a variable (as is the case, for example, in C / C++). The type is dynamically determined based on the value that is assigned to the variable:
stringVariable = "3" numberVariable = 2
stringVariable will create a variable of type string and numberVariable will create a variable of type number. The coercion mechanism in LUA provides automatic type conversion if possible. This means that if you give the variable stringVariable to a function which expects a number (not text), it will be automatically converted. However, problems may arise when comparing variables of type number with variables of type string. In such cases, you should explicitly convert the string to a number value using the 'tonumber' function:
-- Not Valid if (stringVariable > numberVariable) then fibaro:debug('stringVariable is greater!') end -- Not Valid -- Assign the numerical value of the variable stringVariable -- to the variable a local a = tonumber(stringVariable) -- Now you can easily make a comparison if (a > numberVariable) then fibaro:debug('stringVariable is greater!') end -- Alternatively, we can do it without an additional variable if (tonumber(stringValue) > numberVariable) then fibaro:debug('stringVariable is greater!') end
The code on lines 1-5 will generate the following error: (line 2): attempt to compare number with string
Home Center 2 fibaro:functions
- fibaro:abort
- fibaro:calculateDistance
- fibaro:call
- fibaro:countScenes
- fibaro:debug
- fibaro:get
- fibaro:getGlobal
- fibaro:getGlobalValue
- fibaro:getGlobalModificationTime
- fibaro:getModificationTime
- fibaro:getName
- fibaro:getRoomID
- fibaro:getRoomName
- fibaro:getRoomNameByDeviceID
- fibaro:getSectionID
* Deze functies zijn alleen beschikbaar in Virtual Devices.
Home Center 2 JSON Functions
* Deze functies zijn alleen beschikbaar in Virtual Devices.