Pacific Blue Software Logo

How to get started on a Lua Class

Making sense of a new class in Lua

It is not easy to find a coherent introduction to classes in Lua. It also looks like the language is not standard and the process may differ across the spectrum. This uses Lua as implemented by Fibaro to create a class. This Lua class can be used in a QuickApp to have a history display of events, right on the mobile.

What is a class?

A class allows you to collect together and encapsulate all the data and functions of a process. Even though Lua is interpreted, this still has benefits.
  1. For example, you can easily replicate the entire class.
  2. You can inherit from a class and create a descendant class to enhance it

Explanation of Structure

The bare essentials of a class :

  1. class 'Name' declares a class (class 'History').
  2. : colon is used to refer to a class method (History:Add).
  3. . dot is used to refer to a class field or variable (History.Num_Lines).
  4. __init is the constructor, it creates (instantiates) a class and initialises the internals (History.__init).
  5. self is used to refer to its own methods and fields (self.Show(), self.Lines)

A sample class in Lua

Note that the indentation and style used is to make it clear what the code is doing.
--History Log and Display Class for Fibaro Quick Apps
class 'History'

function History:__init (App, Label_Id, Num_Lines)
  self.App = App
  self.Label_Id = Label_Id
  self.Num_Lines = Num_Lines
  self.Lines = {}
  self:Clear ()

function History:Clear ()
  for I=1,self.Num_Lines
    self.Lines [I] = ''
  self:Show ()

function History:Show ()
  for I=1,self.Num_Lines
    self.App:updateView(self.Label_Id..I, 'text', self.Lines[I])

function History:Add (Line, NoShow)
 table.remove (self.Lines, self.Num_Lines)
 table.insert (self.Lines, 1, Line)
 if (NoShow == nil) or (not NoShow)
 then self:Show ()

function History:Log (Line, NoShow)
  local Time = ('%Y/%m/%d %H:%M:%S  ')
  self:Add (Time..Line, NoShow)

What does it do?

This is a class to display a History of events in a QuickApp (A Virtual Device) on Fibaro HC3. It can be easily adapted for HC2, I no longer have one to make and test the changes.

How to use the code?

local History = History (App, 'Label_', 4)

This will create a class to display 4 lines for the QuickApp App using labels Label_1..Label_4

History:Add (Line)

This adds a line to the History or Log.

History:Log (Line)

This adds the line with a timestamp.

History:Add (Line,true)

This adds a line but does not display it.

How does the code work?

function History:__init (App, Label_Id, Num_Lines)

This instantiates the class. The parameters are

It saves the passed parameters to its own internal variables and creates an array called Lines and then calls the Clear method to clear the history and display.

function History:Clear ()

This goes through the array and assigns a space to each Line. It is important to not use a blank line, because that is the same as nil and it has unintended side effects. Then it calls the Show method to display the lines on the labels of the passed App.

function History:Show ()

This iterates through the array and calls the updateView method of the App to show the lines. Note the labels are assumed to be suffixed by "1", "2", etc.

function History:Add (Line, NoShow)

This adds a line to the Array by deleting the last line and inserting the new line at position 1. By default, it calls the Show method to display it right away. But you can override this by passing true as the second parameter.

function History:Log (Line, NoShow)

Normally, you would call this to display a timestamp at the front of each line. It prefixes the timestamp and calls the Add method.

Some advanced concepts

Sharing Data between instances of the same class


Inheriting a Class

-- defines a class New inherited from History
class 'New'(History)

-- need to call the original constructor
function New:__init (arg)
  History.__init (self, arg)

Download Code

A Sample class in Lua for the HC3

Back to Articles
Investigating the structure of the Virtual Device file used by HC2

If you found this useful, then please consider making a donation.

QR Code for donation Please donate if helpful