-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.lua
More file actions
488 lines (463 loc) · 14.7 KB
/
runtime.lua
File metadata and controls
488 lines (463 loc) · 14.7 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
rapidjson = require("rapidjson")
local NumberofInterfaces = Properties["Number Of Interfaces"].Value
local NumberofPinOnlyUCIs = Properties["Number Of Pin Only UCIs"].Value
local DatabaseDir = "media/NfcTags.json"
local Database = rapidjson.decode('{"Tags": []"}')
local BaseTag = {
["UID"] = "",
["AccessLevel"] = 0, -- 0=User, 1=Admin, 2=SuperAdmin
["UserName"] = "",
["Created"] = "",
["PIN"] = 123456, -- 4-12 digits
["History"] = {
["Time"] = os.time(),
["Location"] = ""
}
}
local ClearTime = 3 -- seconds
local PanelChoices = {"Off"}
local ModeChoices = {"Off", "Read", "Add", "Remove"}
local AccessLevelChoices = {"User", "Admin"}
Controls.LearningMode.Choices = ModeChoices
Controls.LearningMode.String = ModeChoices[1]
Controls.AccessLevel.Choices = AccessLevelChoices
Controls.AccessLevel.String = AccessLevelChoices[1]
Controls.UserEditAccessLevel.Choices = AccessLevelChoices
Controls.LearningPanel.String = PanelChoices[1]
function IndexOfKV(table, key, value)
for i, v in ipairs(table) do
if v[key] == value then
return i
end
end
return nil
end
function GetSensors()
local Choices = {}
for _, comp in pairs(Component.GetComponents()) do
if comp.Type == "touchscreen_sensors" then
table.insert(Choices, comp.Name)
end
end
for _, ctrl in ipairs(Controls.TSC_Sensor) do
ctrl.Choices = Choices
end
end
function GetStatus()
local Choices = {}
local UciChoices = {}
for _, comp in pairs(Component.GetComponents()) do
if comp.Type == "touch_screen_status" then
table.insert(Choices, comp.Name)
table.insert(UciChoices, comp.Name)
elseif comp.Type == "uci_viewer" then
table.insert(UciChoices, comp.Name)
end
end
for _, ctrl in ipairs(Controls.TSC_Status) do
ctrl.Choices = Choices
end
for _, ctrl in ipairs(Controls.Uci_Status) do
ctrl.Choices = UciChoices
end
end
function GetPanels()
local TscChoices = {}
local UciChoices = {}
for _, device in pairs(Design.GetInventory()) do
if device.Model:match("TSC%-%d+%-G3") then
table.insert(TscChoices, device.Name)
elseif device.Model:match("TSC") or device.Model:match("UCI") then
table.insert(UciChoices, device.Name)
end
end
for _, ctrl in ipairs(Controls.TSC_Name) do
ctrl.Choices = TscChoices
end
for _, ctrl in ipairs(Controls.Uci_Name) do
ctrl.Choices = UciChoices
end
end
function SetupSensors()
local LearningChoices = {"Off"}
for i = 1, #Controls.TSC_Name do
local name = Controls.TSC_Name[i].String
local Sensor = Component.New(Controls.TSC_Sensor[i].String)
local Status = Component.New(Controls.TSC_Status[i].String)
if name ~= "" and Sensor["nfc.clear"] ~= nil and Status["current.uci"] ~= nil then
print(
"Setting up NFC for " ..
name .. " with " .. Controls.TSC_Sensor[i].String .. " and " .. Controls.TSC_Status[i].String
)
table.insert(LearningChoices, name)
Sensor["nfc.clear"]:Trigger()
Sensor["nfc.uid"].EventHandler = function(ctrl)
if ctrl.String ~= "" then
local UID = ctrl.String
print(name .. ": " .. UID)
if name == Controls.LearningPanel.String then
ProcessTag(UID)
else
accessLevel, username = CheckTag(UID, name)
Login(accessLevel, username, Status["current.uci"].String)
end
Sensor["nfc.clear"]:Trigger()
end
end
end
end
Controls.LearningPanel.Choices = LearningChoices
end
function SetupPinAccess()
for i = 1, NumberofInterfaces do
local name = Controls.TSC_Name[i].String
local Status = Component.New(Controls.TSC_Status[i].String)
if name ~= "" and Status["current.uci"] ~= nil then
print("Setting up PinAccess for " .. name .. " with " .. Controls.TSC_Status[i].String)
Controls.PinEntry[i].EventHandler = function(ctrl)
if ctrl.String ~= "" then
local accessLevel, username = PinCheck(ctrl.String, name)
Login(accessLevel, username, Status["current.uci"].String)
ctrl.String = ""
end
end
else
Controls.PinEntry[i].EventHandler = nil
end
end
for i = 1, NumberofPinOnlyUCIs do
local name = Controls.Uci_Name[i].String
local Status = Component.New(Controls.Uci_Status[i].String)
if name ~= "" and Status["current.uci"] ~= nil then
print("Setting up PinAccess for " .. name .. " with " .. Controls.Uci_Status[i].String)
Controls.PinEntry[i + NumberofInterfaces].EventHandler = function(ctrl)
if ctrl.String ~= "" then
local accessLevel, username = PinCheck(ctrl.String, name)
Login(accessLevel, username, Status["current.uci"].String)
ctrl.String = ""
end
end
else
Controls.PinEntry[i + NumberofInterfaces].EventHandler = nil
end
end
end
function LoadDatabase()
if not System.IsEmulating then
print("Checking for Tag Database")
if
pcall(
function()
Database = rapidjson.load(DatabaseDir)
end
)
then
-- print(rapidjson.encode(Database))
print(#Database.Tags .. " Tags in Database")
else
print("Error loading database")
Database = {}
end
end
end
function ProcessTag(UID)
local mode = Controls.LearningMode.String
if mode == "Read" then
local _, _, tag = CheckTag(UID)
if tag then
Controls.UserName.String = tag.UserName
Controls.AccessLevel.String = (tag.AccessLevel == 0 and "User" or "Admin")
Controls.UID.String = tag.UID
Controls.BackupPin.String = tag.PIN or ""
Controls.Created.String = os.date("%e %b %Y %H:%M:%S%p", tag.Created)
local history = ""
for _, log in ipairs(tag.History) do
history = history .. log.Location .. ": " .. os.date("%e %b %Y %H:%M:%S%p", log.Time) .. "\n"
end
Controls.History.String = history
end
elseif mode == "Add" then
AddTag(
UID,
(Controls.AccessLevel.String == "Admin" and 1 or 0),
Controls.UserName.String,
tonumber(Controls.BackupPin.String)
)
ResetControls()
RefreshList()
Controls.LearningMode.String = "Off"
elseif mode == "Remove" then
RemoveTag(UID)
ResetControls()
RefreshList()
Controls.LearningMode.String = "Off"
end
end
function AddTag(UID, accessLevel, userName, backupPin)
-- Chech for existing tag
if UID then
local accessLevel = accessLevel or 0
local userName = userName or "TAG:" .. UID
local created = os.time()
local backupPin = backupPin or ""
local ExistingTagIndex = IndexOfKV(Database.Tags, "UID", UID)
if ExistingTagIndex ~= nil then
print("Tag already exists, updating")
Database.Tags[ExistingTagIndex].AccessLevel = accessLevel
Database.Tags[ExistingTagIndex].UserName = userName
Database.Tags[ExistingTagIndex].PIN = backupPin
else
print("Adding Tag")
table.insert(
Database.Tags,
{
["UID"] = UID,
["AccessLevel"] = accessLevel, -- 0=User, 1=Admin, 2=SuperAdmin
["UserName"] = userName,
["Created"] = created,
["PIN"] = backupPin,
["History"] = {}
}
)
end
rapidjson.dump(Database, DatabaseDir)
RefreshList()
else
print("Error, Cant add tag without UID")
end
end
function RemoveTag(UID)
for i, tag in ipairs(Database.Tags) do
if tag.UID == UID then
table.remove(Database.Tags, i)
rapidjson.dump(Database, DatabaseDir)
RefreshList()
return true
end
end
return false
end
function CheckTag(UID, location)
for i, tag in ipairs(Database.Tags) do
if tag.UID == tostring(UID) then
if location then -- add history when used
table.insert(tag.History, 1, {["Time"] = os.time(), ["Location"] = location})
while (#tag.History > Controls.HistoryLength.Value) do
table.remove(tag.History)
end
rapidjson.dump(Database, DatabaseDir)
end
return tag.AccessLevel, tag.UserName, tag
end
end
return -1, "Unknown Tag"
end
function PinCheck(pin, location)
for _, tag in ipairs(Database.Tags) do
if tonumber(tag.PIN) == tonumber(pin) then
if location then -- add history when used
table.insert(tag.History, 1, {["Time"] = os.time(), ["Location"] = location})
while (#tag.History > Controls.HistoryLength.Value) do
table.remove(tag.History)
end
rapidjson.dump(Database, DatabaseDir)
end
return tag.AccessLevel, tag.UserName
end
end
return -1, "Unknown Pin"
end
function Login(accessLevel, name, uci)
print("Login", accessLevel, name, uci)
local name = name or ""
if uci ~= nil then
if accessLevel >= 0 then --valid user
Uci.SetVariable(uci, "Admin", accessLevel >= 1)
Uci.SetVariable(uci, "Locked", false)
Uci.SetVariable(uci, "UserName", '"' .. name .. '"')
else
print("Invalid Login:", name)
end
end
end
function ClearList()
for i = 1, NumberOfLines do
Controls["ListName"][i].String = ""
Controls["ListUID"][i].String = ""
Controls["ListCreated"][i].String = ""
Controls["ListAccessLevel"][i].String = ""
end
end
function RefreshList()
ClearList()
if Database.Tags and #Database.Tags <= NumberOfLines then
Controls.ListScroll.IsInvisible = true
for i, user in ipairs(Database.Tags) do
print(user.UserName, user.UID, os.date("%e %b %Y %H:%M:%S%p", user.Created))
Controls["ListName"][i].String = user.UserName
Controls["ListUID"][i].String = user.UID
Controls["ListCreated"][i].String = os.date("%e %b %Y %H:%M:%S%p", user.Created)
Controls["ListAccessLevel"][i].String = AccessLevelChoices[user.AccessLevel + 1]
end
else
Controls.ListScroll.IsInvisible = false
local scroll = 100 - Controls.ListScroll.Value
local clicks = #Database.Tags - NumberOfLines + 1
local scrollSize = 100 / clicks
local start = scroll ~= 100 and math.floor(scroll / scrollSize) or clicks - 1
print("Scroll", scroll, clicks, scrollSize, start)
for i = 1, NumberOfLines do
local user = Database.Tags[start + i]
Controls["ListName"][i].String = user.UserName
Controls["ListUID"][i].String = user.UID
Controls["ListCreated"][i].String = os.date("%e %b %Y %H:%M:%S%p", user.Created)
Controls["ListAccessLevel"][i].String = AccessLevelChoices[user.AccessLevel + 1]
end
end
end
function ClearUserEdit()
Controls.UserEditName.String = ""
Controls.UserEditUID.String = ""
Controls.UserEditAccessLevel.String = ""
Controls.UserEditPin.String = ""
Controls.UserEditSave.IsDisabled = true
Controls.UserEditUniquePin.Boolean = false
end
function ResetControls()
-- Controls.Created.IsInvisible = Controls.LearningMode.String ~= "Read"
-- Controls.History.IsInvisible = Controls.LearningMode.String ~= "Read"
Controls.UserName.String = ""
Controls.UID.String = ""
Controls.BackupPin.String = ""
Controls.Created.String = ""
Controls.History.String = ""
Controls.ListScroll.Value = 100
for i = 1, NumberOfLines do
Controls["ListName"][i].String = ""
Controls["ListUID"][i].String = ""
Controls["ListCreated"][i].String = ""
Controls["ListAccessLevel"][i].String = ""
end
end
function Initialize()
Controls.EnableListDelete.Boolean = false
GetPanels()
GetSensors()
GetStatus()
EnableListDelete()
ResetControls()
ClearUserEdit()
LoadDatabase()
RefreshList()
SetupSensors()
SetupPinAccess()
end
function VerifyUniquePin(pin)
if tostring(pin):len() < 4 or tostring(pin):len() > 12 then
return false
end
for _, tag in ipairs(Database.Tags) do
if tag.PIN == tonumber(pin) then
return false
end
end
return true
end
function CheckUserEdit()
if
Controls.UserEditName.String ~= "" and Controls.UserEditUID.String ~= "" and
Controls.UserEditAccessLevel.String ~= "" and
(Controls.UserEditPin.String:len() >= 4 and Controls.UserEditPin.String:len() <= 12)
then
Controls.UserEditSave.IsDisabled = false
else
Controls.UserEditSave.IsDisabled = true
end
end
function EnableListDelete()
for i = 1, NumberOfLines do
Controls["ListDelete"][i].IsDisabled = not Controls.EnableListDelete.Boolean
end
end
Controls.EnableListDelete.EventHandler = EnableListDelete
for i, ctrl in ipairs(Controls.ListDelete) do
ctrl.EventHandler = function()
local UID = Controls["ListUID"][i].String
local name = Controls["ListName"][i].String
if RemoveTag(UID) then
print("Removed " .. name)
end
end
end
for i, ctrl in ipairs(Controls.ListEdit) do
ctrl.EventHandler = function()
ClearUserEdit()
local UID = Controls["ListUID"][i].String
local ExistingTagIndex = IndexOfKV(Database.Tags, "UID", UID)
if ExistingTagIndex ~= nil then
local tag = Database.Tags[ExistingTagIndex]
Controls.UserEditName.String = tag.UserName
Controls.UserEditUID.String = tag.UID
Controls.UserEditAccessLevel.String = AccessLevelChoices[tag.AccessLevel + 1]
Controls.UserEditPin.String = tag.PIN
Controls.UserEditSave.IsDisabled = false
end
end
end
for _, ctrl in ipairs(Controls.TSC_Name) do
ctrl.EventHandler = function()
SetupSensors()
SetupPinAccess()
end
end
for _, ctrl in ipairs(Controls.TSC_Sensor) do
ctrl.EventHandler = SetupSensors
end
for _, ctrl in ipairs(Controls.TSC_Status) do
ctrl.EventHandler = function()
SetupSensors()
SetupPinAccess()
end
end
for _, ctrl in ipairs(Controls.Uci_Name) do
ctrl.EventHandler = function()
SetupPinAccess()
end
end
for _, ctrl in ipairs(Controls.Uci_Status) do
ctrl.EventHandler = function()
SetupPinAccess()
end
end
Controls.RefreshList.EventHandler = RefreshList
Controls.ListScroll.EventHandler = RefreshList
Controls.BackupPin.EventHandler = function()
if Controls.LearningMode.String == "Add" then
Controls.UniquePin.Boolean = VerifyUniquePin(Controls.BackupPin.String)
else
Controls.UniquePin.Boolean = false
end
end
Controls.UserEditClear.EventHandler = ClearUserEdit
Controls.UserEditUidGen.EventHandler = function()
Controls.UserEditUID.String = string.format("%08X", math.random(0, 0xFFFFFFFF))
CheckUserEdit()
end
Controls.UserEditUID.EventHandler = function(ctrl)
CheckUserEdit()
end
Controls.UserEditName.EventHandler = CheckUserEdit
Controls.UserEditAccessLevel.EventHandler = CheckUserEdit
Controls.UserEditPin.EventHandler = function(ctrl)
Controls.UserEditUniquePin.Boolean = VerifyUniquePin(ctrl.String)
CheckUserEdit()
end
Controls.UserEditSave.EventHandler = function()
local UID = Controls.UserEditUID.String
local accessLevel = Controls.UserEditAccessLevel.String == "Admin" and 1 or 0
local userName = Controls.UserEditName.String
local backupPin = tonumber(Controls.UserEditPin.String)
AddTag(UID, accessLevel, userName, backupPin)
ClearUserEdit()
end
Initialize()