-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPP_Control.lua
448 lines (371 loc) · 16.2 KB
/
FPP_Control.lua
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
--[[
Falcon Pi Player Monitor
Created by K. Rhodus
Version History:
1.0 - Initial Build
]]--
-----------------------------------------------------------------------------------------------------------------------------------------
-- Setup Script --
----------------------------------------------------------------------------------------------------------------------------------------
json = require("rapidjson")
--Tables
StatusState = { OK = 0, COMPROMISED = 1, FAULT = 2, NOTPRESENT = 3, MISSING = 4, INITIALIZING = 5 }
fppdstatus = {}
multisync = {}
Modes = {player = 6, remote = 8}
Controls.SetMode.Choices = {"player", "remote"}
playlists = {}
--Timers
Tick = Timer.New()
RebootTimeout = false
-----------------------------------------------------------------------------------------------------------------------------------------
-- Universal Tools --
-----------------------------------------------------------------------------------------------------------------------------------------
function univHTTPEventHandler(tbl, code, d, e)
if code == nil then code = "0" end
print( string.format("Response Code: %i\t\tErrors: %s\rData: %s",code, e or "None", d))
end
function rebootFPPD()
print ("Rebooting FPPD")
HttpClient.Download { Url = Controls.IPAddress.String.."/api/system/fppd/restart", Timeout = 30, EventHandler = univHTTPEventHandler }
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Download Player Info --
-----------------------------------------------------------------------------------------------------------------------------------------
--FPPD Status
function downloadfppdftn(tbl, code, data, err, headers)
if code == 200 then --Proper HTTP response
Tick:Start(30)
Controls.Status.Value = 0 --Set Status value
BadResponse = 0
local fppdstatus = json.decode(data)
--
print(json.encode(fppdstatus, {pretty = true}))
if fppdstatus.fppd == "Not Running" then
Controls.FPPDRunning.Boolean = false
Controls.FPPDRunning.Color = "#80FF0000"
if not FPPDTimer:IsRunning() then
FPPDTimer:Start(120)
Controls.Status.String = "FPPD Not Running - Countdown Timer Started"
end
else
FPPDTimer:Stop()
--Mode/FPPD Status
Controls.Mode.String = fppdstatus.mode_name
Controls.SetMode.String = fppdstatus.mode_name
Controls.FPPstatus.String = fppdstatus.status_name
Controls.FPPDRunning.Boolean = fppdstatus.fppd == "running"
Controls.FPPDRunning.Color = "#4080FF00"
--Currently Playing Section
Controls.Status.String = fppdstatus.mode_name .." "..fppdstatus.status_name
Controls.MediaName.String = fppdstatus.current_song
Controls.SequenceName.String = fppdstatus.current_sequence
--Controls.Time.Position = fppdstatus.seconds_played / (fppdstatus.seconds_played + fppdstatus.seconds_remaining)
Controls.ElapsedTime.String = fppdstatus.time_elapsed
--Sechedule Section
if fppdstatus.mode_name == "player" then
downloadplaylists()
Controls.CurrentPlaylist.String = fppdstatus.current_playlist.playlist
Controls.Playlists.String = fppdstatus.current_playlist.playlist
if fppdstatus.scheduler.status ~= "idle" and fppdstatus.scheduler.status ~= "manual" then --- More testing for Playlist Scheduling testing needed
Controls.ScheduledStartTime.String = fppdstatus.scheduler.currentPlaylist.scheduledStartTimeStr
Controls.ActualStartTime.String = fppdstatus.scheduler.currentPlaylist.actualStartTimeStr
Controls.CurrentStopTime.String = fppdstatus.scheduler.currentPlaylist.actualEndTimeStr
Controls.NextPlaylist.String = fppdstatus.scheduler.nextPlaylist.playlistName
Controls.NextStartTime.String = fppdstatus.scheduler.nextPlaylist.scheduledStartTimeStr
else
Controls.ScheduledStartTime.String = ""
Controls.ActualStartTime.String = ""
Controls.CurrentStopTime.String = ""
Controls.NextPlaylist.String = ""
Controls.NextStartTime.String = ""
end
--]]
end
--MultiSync Feedback
Controls.SendMultiSync.Boolean = fppdstatus.multisync
--Volume Feedback
Controls.Volume.Value = fppdstatus.volume
--Player Information
Controls.FPP_Version.String = fppdstatus.advancedView.Version
Controls.FPP_Hostname.String = fppdstatus.advancedView.HostName
Controls.FPP_Description.String = fppdstatus.advancedView.HostDescription
Controls.FPP_OSVersion.String = fppdstatus.advancedView.OSVersion
Controls.FPP_GitVersion.String = fppdstatus.advancedView.LocalGitVersion
Controls.FPP_HostType.String = fppdstatus.advancedView.Variant
Controls.FPP_Memory.Position = fppdstatus.advancedView.Utilization.Memory / 100
Controls.FPP_RootDisk.Position = fppdstatus.advancedView.Utilization.Disk.Root.Free / fppdstatus.advancedView.Utilization.Disk.Root.Total
Controls.FPP_MediaDisk.Position = fppdstatus.advancedView.Utilization.Disk.Media.Free / fppdstatus.advancedView.Utilization.Disk.Media.Total
Controls.UpgradeAvailable.Boolean = fppdstatus.advancedView.LocalGitVersion ~= fppdstatus.advancedView.RemoteGitVersion
Controls.Upgrade.IsDisabled = fppdstatus.advancedView.LocalGitVersion == fppdstatus.advancedView.RemoteGitVersion
end
--End of Status Parsing
else
BadResponse = BadResponse + 1
Controls.Status.String = "No/Bad Response - Starting Countdown"
if BadResponse >= 3 then
if RebootTimeout == false then
Controls.Status.Value = 6 --Set Status value
Controls.FPPDRunning.Boolean = false
Controls.FPPDRunning.Color = "#80FF0000"
Tick:Start(120)
end
end
end
end
function downloadfppd()
--print ("Downloading FPPD Status from: " .. address)
HttpClient.Download { Url = Controls.IPAddress.String.. "/api/system/status", Timeout = 360, EventHandler = downloadfppdftn }
end
pollplayer = function()
if #Controls.IPAddress.String>0 then
downloadfppd()
end
end
Tick.EventHandler = pollplayer
Controls.IPAddress.EventHandler = pollplayer
FPPDTimer = Timer.New()
FPPDTimer.EventHandler = function()
FPPDTimer:Stop()
Controls.Status.Value = 2
Controls.Status.String = "FPPD Not Running"
end
BadResponse = 0
-----------------------------------------------------------------------------------------------------------------------------------------
-- Set Test Mode --
-----------------------------------------------------------------------------------------------------------------------------------------
--TestMode Values Table
testmodetbl={
channelSet="1-8388608",
channelSetType="channelRange",
colorPattern="FF000000FF000000FF",
cycleMS=1000,
enabled=1,
mode="RGBCycle",
subMode="RGBCycle-RGBA"
}
testcolor = {mode="RGBFill",
color1=Controls.TestColor[1].Value,
color2=Controls.TestColor[2].Value,
color3=Controls.TestColor[3].Value,
enabled=1,
channelSet="1-8388608",
channelSetType="channelRange"
}
function testupload(data)
HttpClient.Upload {
Url = Controls.IPAddress.String.."/api/testmode",
Method = "POST",
Data = json.encode(data),
Headers = {
["Content-Type"] = "application/json",
},
EventHandler = univHTTPEventHandler -- The function to call upon response
}
end
Controls.TestMode.EventHandler = function(ctl)
testmodetbl.enabled = ctl.Value
testupload(testmodetbl)
end
for idx,ctl in ipairs(Controls.TestColor) do
ctl.EventHandler = function()
testcolor["color"..idx] = ctl.Value
testupload(testcolor)
Controls.TestMode.Boolean= true
end
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Player Mode *NEEDS UPDATED** --
----------------------------------------------------------------------------------------------------------------------------------------
Controls.SetMode.EventHandler = function(mode)
local setmode = 0
for idx,ctl in pairs(Modes) do
if idx == mode.String then
setmode = ctl
print(temp)
break
end
end
RebootTimeout = true
url = Controls.IPAddress.String.."/fppxml.php?command=setFPPDmode&mode="..setmode
playermodeupload(url)
rebootFPPD()
Timer.CallAfter(function() RebootTimeout = false end, 10)
end
function playermodeupload(url)
HttpClient.Upload {
Url = url,
Method = "POST",
Data = "",
Headers = {
["Content-Type"] = "application/json",
},
EventHandler = univHTTPEventHandler
}
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Playlist Functions --
----------------------------------------------------------------------------------------------------------------------------------------
--Get Playlists
function downloadplaylistsftn(tbl, code, data, err, headers)
if code == 200 then
playlists = json.decode(data)
Controls.Playlists.Choices = playlists
Controls.NotificationPlaylist.Choices = playlists
--print(json.encode(playlists, {pretty = true}))
end
end
function downloadplaylists()
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/api/playlists", Timeout = 30, EventHandler = downloadplaylistsftn }
end
Controls.Play.EventHandler = function()
local temp = {
command= "Start Playlist At Item",
args = {Controls.Playlists.String, 0,true,false}
}
HttpClient.Upload {
Url = Controls.IPAddress.String.."/api/command",
Method = "POST",
Data = json.encode(temp),
Headers = {
["Content-Type"] = "application/json"
},
EventHandler = univHTTPEventHandler
}
end
Controls.Stop.EventHandler = function()
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/api/playlists/stop", Timeout = 30, EventHandler = univHTTPEventHandler }
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- MultiSync Enabled --
----------------------------------------------------------------------------------------------------------------------------------------
Controls.SendMultiSync.EventHandler = function(ctl)
HttpClient.Upload {
Url = Controls.IPAddress.String.."/api/settings/MultiSyncEnabled/",
Method = "PUT",
Data = ctl.Value,
Headers = {
["Content-Type"] = "application/json"
},
EventHandler = univHTTPEventHandler
}
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/api/system/fppd/restart?quick=1", Timeout = 30, EventHandler = univHTTPEventHandler }
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Volume --
----------------------------------------------------------------------------------------------------------------------------------------
Controls.Volume.EventHandler = function(ctl)
local temp = {volume= ctl.Value}
HttpClient.Upload {
Url = Controls.IPAddress.String.."/api/system/volume/",
Method = "POST",
Data = json.encode(temp),
Headers = {
["Content-Type"] = "application/json"
},
EventHandler = univHTTPEventHandler
}
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Backup Controller --
----------------------------------------------------------------------------------------------------------------------------------------
function Backup()
HttpClient.Upload {
Url = Controls.IPAddress.String.."/api/backups/configuration",
Method = "POST",
Data = "Q-SYS Automated Backup - "..os.date("%Y-%m-%d %H:%M:%S"),
Headers = {
["Content-Type"] = "application/json"
},
EventHandler = BackupEH
}
end
function BackupEH(tbl, code, d, e)
if code == nil then code = "0" end
--print( string.format("Response Code: %i\t\tErrors: %s\rData: %s",code, e or "None", d))
print(d)
local temp = json.decode(d)
if temp ~= nil then
if temp.success == true then
path = string.sub(temp.backup_file_path, 32, -1)
print(path)
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/api/backups/configuration/JsonBackups/"..path, Timeout = 30, EventHandler = DLBackupEH }
end
end
end
function DLBackupEH(tbl, code, d, e)
if code == nil then code = "0" end
--print( string.format("Response Code: %i\t\tErrors: %s\rData: %s",code, e or "None", d))
print(d)
local temp = {
Command = "Commit",
Args = {
Filename = Controls.FPP_Hostname.String,
CommitMsg = "Q-SYS Automated Backup - "..os.date("%Y-%m-%d %H:%M:%S"),
Content = d
}
}
Timer.CallAfter(function() Notifications.Publish("ShowMonGit", temp) end, #Controls.IPAddress.String*math.random(10))
end
Controls.BackupController.EventHandler = Backup
function GitNotEH (name, data)
print(json.encode(data))
if data.Command == "Commit" and data.Results ~= nil then
if data.Results.Filename == Controls.FPP_Hostname.String then
Controls.LastBackupDate.String = data.Results.Code
end
end
end
--GitNotification = Notifications.Subscribe("ShowMonGit", GitNotEH)
-----------------------------------------------------------------------------------------------------------------------------------------
-- FPP Update --
----------------------------------------------------------------------------------------------------------------------------------------
Controls.Upgrade.EventHandler = function()
Controls.Upgrade.IsDisabled = true
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/manualUpdate.php", Timeout = 30, EventHandler = univHTTPEventHandler }
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Notifications --
----------------------------------------------------------------------------------------------------------------------------------------
function IncomingNotification (name, data)
print("**Incoming Notification- "..name.." - "..data.." **")
if data == "Test On" then
Controls.TestMode.Boolean = true
testmodetbl.enabled = 1
testupload(testmodetbl)
elseif data == "Test Off" then
Controls.TestMode.Boolean = false
testmodetbl.enabled = 0
testupload()
elseif data == "Show Start" then
if Controls.Mode.String == "player" then
local temp = {command= "Start Playlist At Item",args = {Controls.NotificationPlaylist.String, 0,true,false}}
HttpClient.Upload {Url = Controls.IPAddress.String.."/api/command",Method = "POST", Data = json.encode(temp),Headers = {["Content-Type"] = "application/json"},EventHandler = univHTTPEventHandler}
end
elseif data == "Show Stop" then
if Controls.Mode.String == "player" then
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/api/playlists/stop", Timeout = 30, EventHandler = univHTTPEventHandler }
end
elseif data == "Reset Mode" then
RebootTimeout = true
if Controls.SendMultiSync.Boolean then
playermodeupload(Controls.IPAddress.String.."/fppxml.php?command=setFPPDmode&mode=6")
else
playermodeupload(Controls.IPAddress.String.."/fppxml.php?command=setFPPDmode&mode=8")
end
rebootFPPD()
Timer.CallAfter(function() RebootTimeout = false end, 10)
elseif data == "Brightness Low" then
--Future Brightness Control
elseif data == "Update" then
HttpClient.Download { Url = "http://"..Controls.IPAddress.String.. "/manualUpdate.php", Timeout = 30, EventHandler = univHTTPEventHandler }
elseif data == "Backup" then
Backup()
end
end
FPPNotify = Notifications.Subscribe("FPPNotify", IncomingNotification)
-----------------------------------------------------------------------------------------------------------------------------------------
-- Startup Functions --
----------------------------------------------------------------------------------------------------------------------------------------
pollplayer()