-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
185 lines (171 loc) · 6.25 KB
/
Copy pathmain.lua
File metadata and controls
185 lines (171 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--[[
Velux Active integration
@author ikubicki
@version 1.1.0
]]
function QuickApp:onInit()
self.config = Config:new(self)
self.velux = Velux:new(self.config)
self.i18n = i18n:new(api.get("/settings/info").defaultLanguage)
self:trace('')
self:trace(string.format(self.i18n:get('name'), self.name))
self:updateProperty('manufacturer', 'Velux')
self:updateProperty('model', 'Velux Active KIX 300')
self:updateView("button1", "text", self.i18n:get('stop'))
self:updateView("button2", "text", self.i18n:get('search-devices'))
self:updateView("label", "text", self.i18n:get('name'))
self:updateProperty("value", 99)
if string.len(self.config:getModuleID()) > 10 then
self:run()
end
end
function QuickApp:open()
self:setPosition(100)
end
function QuickApp:close()
self:setPosition(0)
end
function QuickApp:stop()
local callback = function(response)
self:pullDataFromCloud()
if self.properties.dead == true then
self:setDeadProperty(false)
end
end
local fallback = function (response)
self:updateView("label", "text", string.format('[%d] %s', response.status or 0, response.data or response))
if self.properties.dead == false then
self:setDeadProperty(true)
end
end
self.velux:stop(callback, fallback)
end
function QuickApp:setValue(value)
if value > 98 then
value = 100
end
self:setPosition(value)
end
function QuickApp:setPositionEvent(event)
local position = event.values[1]
self:setValue(position)
end
function QuickApp:stopEvent(event)
self:stop()
end
function QuickApp:refreshDataEvent(event)
self:updateView("label", "text", self.i18n:get('refreshing'))
self:pullDataFromCloud()
end
function QuickApp:setPosition(position)
-- self:debug(string.format("Setting new position (%d%%) ...", position))
if position < 1 then
position = 0
elseif position > 98 then
position = 100
end
local callback = function(response)
-- self:debug(string.format(self.i18n:get('position-set'), position))
self:pullDataFromCloud()
if self.properties.dead == true then
self:setDeadProperty(false)
end
end
local fallback = function (response)
self:updateView("label", "text", string.format('[%d] %s', response.status or 0, response.data or response))
if self.properties.dead == false then
self:setDeadProperty(true)
end
end
self.velux:setPosition(position, callback, fallback)
end
function QuickApp:updateValues(position, newPosition)
value = position
self:updateView("slider", "value", tostring(position))
if position < 1 then
value = 0
text = self.i18n:get('shutter-closed')
elseif position > 98 then
value = 99
text = self.i18n:get('shutter-opened')
else
text = string.format(self.i18n:get('shutter-half-open'), position)
end
local inMotion = false
if newPosition ~= nil and newPosition > position then
text = string.format(self.i18n:get('shutter-opening'), position)
inMotion = true
elseif newPosition ~= nil and newPosition < position then
text = string.format(self.i18n:get('shutter-closing'), position)
inMotion = true
end
if inMotion then
fibaro.setTimeout(3000, function()
self:pullDataFromCloud()
end)
end
self:updateView("label", "text", text)
self:updateProperty("value", value)
end
function QuickApp:run()
self:pullDataFromCloud()
local interval = self.config:getInterval()
if (interval > 0) then
fibaro.setTimeout(interval, function() self:run() end)
end
end
function QuickApp:pullDataFromCloud()
local callback = function(moduleData)
-- QuickApp:debug(json.encode(moduleData))
self:updateView("button3", "text", self.i18n:get('refresh'))
self:updateValues(moduleData.current_position, moduleData.target_position)
if self.properties.dead == true then
self:setDeadProperty(false)
end
self:updateView("label", "text", string.format(self.i18n:get('updated-at'), os.date('%Y-%m-%d %H:%M:%S')))
end
local fallback = function (response)
self:updateView("label", "text", string.format('[%d] %s', response.status or 0, response.data or response))
if self.properties.dead == false then
self:setDeadProperty(true)
end
end
self:updateView("button3", "text", self.i18n:get('refreshing'))
self.velux:sync(callback, fallback)
end
function QuickApp:searchDevicesEvent()
self:debug(self.i18n:get('searching-devices'))
self:updateView("button2", "text", self.i18n:get('searching-devices'))
local callback = function(homeData)
self:updateView("button2", "text", self.i18n:get('search-devices'))
-- printing results
QuickApp:trace(string.format(self.i18n:get('search-row-home'), homeData.name, homeData.id))
QuickApp:trace(string.format(self.i18n:get('search-row-home-gateways'), #homeData.gateways))
for _, gateway in ipairs(homeData.gateways) do
QuickApp:trace(string.format(self.i18n:get('search-row-gateway'), gateway.name, gateway.id))
QuickApp:trace(string.format(self.i18n:get('search-row-gateway-shutters'), #gateway.shutters))
for __, shutter in ipairs(gateway.shutters) do
QuickApp:trace(string.format(self.i18n:get('search-row-shutter'), shutter.id))
QuickApp:trace(string.format(self.i18n:get('search-row-shutter-position'), shutter.position))
end
end
self:updateView("label", "text", string.format(self.i18n:get('check-logs'), 'QUICKAPP' .. self.id))
if self.properties.dead == true then
self:setDeadProperty(false)
end
end
local fallback = function (response)
self:updateView("label", "text", string.format('[%d] %s', response.status or 0, response.data or response))
if self.properties.dead == false then
self:setDeadProperty(true)
end
end
self.velux:searchDevices(callback, fallback)
end
function QuickApp:setDeadProperty(value)
return api.put('/devices/' .. self.id, {
properties = {
dead = value
}
})
end