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.
The bare essentials of a class :
class 'Name'
declares a class (class 'History'
).:
colon is used to refer to a class method (History:Add
)..
dot is used to refer to a class field or variable (History.Num_Lines
).__init
is the constructor, it creates (instantiates) a class and initialises
the internals (History.__init
).self
is used to refer to its own methods and fields (self.Show()
,
self.Lines
)
--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 ()
end
function History:Clear ()
for I=1,self.Num_Lines
do
self.Lines [I] = ''
end
self:Show ()
end
function History:Show ()
for I=1,self.Num_Lines
do
self.App:updateView(self.Label_Id..I, 'text', self.Lines[I])
end
end
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 ()
end
end
function History:Log (Line, NoShow)
local Time = os.date ('%Y/%m/%d %H:%M:%S ')
self:Add (Time..Line, NoShow)
end
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.
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.
function History:__init (App, Label_Id, Num_Lines)
This instantiates the class. The parameters are
App
: The QuickApp to show the history on.Label_Id
: The Prefix of the Label to write on.Num_Lines
: The number of lines to display.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.
History.shared.version='1.0'
-- defines a class New inherited from History
class 'New'(History)
-- need to call the original constructor
function New:__init (arg)
History.__init (self, arg)
end