Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/thoda-dev/du-simple-access-control. a script to control access with doors of force fields. Work with user name or ID or Organisation name or id
···11-# du simple access control
22- a script to control access with doors of force fields
11+# Du-Simple-Access-Control
22+33+A script to control access with doors of force fields.
44+55+- It can control several doors and force fields at the same time.
66+- You can manage access with user name, user id, organisation name or organisation id.
77+88+## How to install
99+1010+1. copy the script on the board
1111+2. link the doors you want to control to the board
1212+3. link the force fields you want to control to the board
1313+4. link the screens where you want to diplay the access is granted or not to the board
1414+5. link the zone detection to the board (with a relay if you want to use several). The link must be done from the detector to the board (blue link, not green).
1515+1616+## Mounting Scheme
1717+1818+![Mounting Scheme]()
1919+2020+## How to configure
2121+2222+In the Lua Parameters of the board (right click on the board > advanced > Lua parameters), you can change the following parameters:
2323+- AllowedUsersId: the list of the users id allowed to access, comma separated.
2424+- AllowedUsersName: the list of the users name allowed to access, comma separated, case sensitive.
2525+- AllowedUserOrgsId: the list of the organisations id allowed to access, comma separated.
2626+- AllowedUserOrgsName: the list of the organisations name allowed to access, comma separated, case sensitive.
2727+2828+![Lua Parameters]()
+1
config.json
···11+{"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"--version 1.0.0\n\n--LUA PARAMETERS\nlocal AllowedUsersId = \"\" --export: the list of all IDs, comma separated\nlocal AllowedUsersName = \"Jericho\" --export: the list of all names, comma separated (Case sensitive)\nlocal AllowedUserOrgsId = \"18261\" --export: the list of all organisation IDs, comma separated\nlocal AllowedUserOrgsName = \"Federatis\" --export: the list of all organisation Names, comma separated (Case sensitive)\n\n--Libraries\n--split is a function to split a string in a table with a delimiter written by Jericho\nfunction split(s,d)local t={}s:gsub(\"[^\"..d..\"]+\",function(w)table.insert(t,w)end)return t end\n\n--hide unit widget\nunit.hideWidget()\n\n--auto detecting the door, force fields and the screens\nDoors = {}\nFields = {}\nScreens = {}\nfor slotName,slot in pairs(unit) do\n if\n type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n local ElementClass = slot.getClass():lower()\n if ElementClass:find(\"doorunit\") then\n table.insert(Doors, slot)\n elseif ElementClass:find(\"forcefieldunit\") then\n table.insert(Fields, slot)\n slot.activate()\n elseif ElementClass:find(\"screenunit\") then\n table.insert(Screens, slot)\n slot.activate()\n else\n system.print(ElementClass)\n end\n end\nend\n\nfunction getRenderScript(access)\n local color = \"red\"\n local message = \"Access Denied\"\n if access then\n color = \"green\"\n message = \"Access Granted\"\n end\n return [[\n setBackgroundColor(15/255,24/255,29/255)\n local rx,ry = getResolution()\n local cx, cy = getCursor()\n if vmode then\n ry,rx = getResolution()\n cy, cx = getCursor()\n cx = rx - cx\n if vmode_side == \"right\" then\n cy = ry - cy\n cx = rx - cx\nend\nend\n\n local smallBold=loadFont('Play-Bold',40)\n local bigBold=loadFont('Play-Bold',64)\n\n local front=createLayer()\n local back=createLayer()\n setDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\n setDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n setDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\n setDefaultFillColor( front,Shape_Text,0,0,0,1)\n setDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\n setDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\n local red = {177/255,42/255,42/255}\n local green = {34/255,177/255,76/255}\n\n function renderHeader(title)\n local h = 55\n addLine(back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,smallBold,title,44,h-12)\nend\n\n setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1)\n setNextTextAlign(front,AlignH_Center,AlignV_Middle)\n addText(front,smallBold,\"]] .. message .. [[\", rx/2, ry/4)\n\n\n setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1)\n setNextTextAlign(front,AlignH_Center,AlignV_Middle)\n addText(front,bigBold,\"]] .. player.getName() .. [[\", rx/2, ry/2)\n\n renderHeader(\"Access Control System\")\n requestAnimationFrame(10)\n ]]\nend\n\nlocal AccessPermitted = false\n\nfunction setRenderScriptOnScreens(access)\n for _,screen in pairs(Screens) do\n screen.setRenderScript(getRenderScript(access))\n end\nend\n\nfunction grantAccess()\n AccessPermitted = true\n for _,Door in pairs(Doors) do\n Door.open() --open the door\n end\n for _,Field in pairs(Fields) do\n Field.deactivate() --open the field\n end\n setRenderScriptOnScreens(true)\nend\n\nfunction closeAll()\n AccessPermitted = false\n for _,Door in pairs(Doors) do\n Door.close() --close the door\n end\n for _,Field in pairs(Fields) do\n Field.activate() --close the field\n end\nend\n\nfunction rejectAccess()\n closeAll()\n setRenderScriptOnScreens(false)\n unit.exit()\nend\n\n--converting to tables\nlocal tPermittedUsersId = split(AllowedUsersId,',') or {}\nlocal tPermittedUsersName = split(AllowedUsersName,',') or {}\nlocal tPermittedOrgsId = split(AllowedUserOrgsId,',') or {}\nlocal tPermittedOrgsName = split(AllowedUserOrgsName,',') or {}\n\nlocal player_id = player.getId()\n\nfor _,id in pairs(tPermittedUsersId) do\n if id == player_id then\n grantAccess()\n AccessPermitted = true\n break\n end\nend\n\nif not AccessPermitted then\n local player_name = player.getName()\n for _,name in pairs(tPermittedUsersName) do\n if name == player_name then\n grantAccess()\n break\n end\n end\nend\n\nif not AccessPermitted then\n local player_orgs_id = player.getOrgIds()\n for _,oid in pairs(player_orgs_id) do\n for _,id in pairs(tPermittedOrgsId) do\n if tonumber(id) == oid then\n grantAccess()\n break\n end\n end\n --if access is granted, end the loop\n if AccessPermitted then break end\n --if not a valid ID, veifying the org name\n local o = system.getOrganization(oid)\n if o.name then\n for _,name in pairs(tPermittedOrgsName) do\n if name == o.name then\n grantAccess()\n break\n end\n end\n end\n --if access is granted, end the loop\n if AccessPermitted then break end\n end\nend\n\nif not AccessPermitted then\n rejectAccess()\nend","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"},{"code":"closeAll()\n","filter":{"args":[],"signature":"onStop()","slotKey":"-1"},"key":"1"}],"methods":[],"events":[]}
images/lua_parameters.png
This is a binary file and will not be displayed.
images/mounting_scheme.png
This is a binary file and will not be displayed.
images/screen_access_deniedpng.png
This is a binary file and will not be displayed.
images/screen_access_granted.png
This is a binary file and will not be displayed.
+187
source/unit/onStart.lua
···11+--version 1.0.0
22+33+--LUA PARAMETERS
44+local AllowedUsersId = "" --export: the list of all IDs, comma separated
55+local AllowedUsersName = "Jericho" --export: the list of all names, comma separated (Case sensitive)
66+local AllowedUserOrgsId = "18261" --export: the list of all organisation IDs, comma separated
77+local AllowedUserOrgsName = "Federatis" --export: the list of all organisation Names, comma separated (Case sensitive)
88+99+--Libraries
1010+--split is a function to split a string in a table with a delimiter written by Jericho
1111+function split(s,d)local t={}s:gsub("[^"..d.."]+",function(w)table.insert(t,w)end)return t end
1212+1313+--hide unit widget
1414+unit.hideWidget()
1515+1616+--auto detecting the door, force fields and the screens
1717+Doors = {}
1818+Fields = {}
1919+Screens = {}
2020+for slotName,slot in pairs(unit) do
2121+ if
2222+ type(slot) == "table"
2323+ and type(slot.export) == "table"
2424+ and slot.getClass
2525+ then
2626+ local ElementClass = slot.getClass():lower()
2727+ if ElementClass:find("doorunit") then
2828+ table.insert(Doors, slot)
2929+ elseif ElementClass:find("forcefieldunit") then
3030+ table.insert(Fields, slot)
3131+ slot.activate()
3232+ elseif ElementClass:find("screenunit") then
3333+ table.insert(Screens, slot)
3434+ slot.activate()
3535+ else
3636+ system.print(ElementClass)
3737+ end
3838+ end
3939+end
4040+4141+function getRenderScript(access)
4242+ local color = "red"
4343+ local message = "Access Denied"
4444+ if access then
4545+ color = "green"
4646+ message = "Access Granted"
4747+ end
4848+ return [[
4949+ setBackgroundColor(15/255,24/255,29/255)
5050+ local rx,ry = getResolution()
5151+ local cx, cy = getCursor()
5252+ if vmode then
5353+ ry,rx = getResolution()
5454+ cy, cx = getCursor()
5555+ cx = rx - cx
5656+ if vmode_side == "right" then
5757+ cy = ry - cy
5858+ cx = rx - cx
5959+end
6060+end
6161+6262+ local smallBold=loadFont('Play-Bold',40)
6363+ local bigBold=loadFont('Play-Bold',64)
6464+6565+ local front=createLayer()
6666+ local back=createLayer()
6767+ setDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)
6868+ setDefaultShadow( back,Shape_Line,6,0,0,0,0.5)
6969+ setDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)
7070+ setDefaultFillColor( front,Shape_Text,0,0,0,1)
7171+ setDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)
7272+ setDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)
7373+7474+ local red = {177/255,42/255,42/255}
7575+ local green = {34/255,177/255,76/255}
7676+7777+ function renderHeader(title)
7878+ local h = 55
7979+ addLine(back,0,h+12,rx,h+12)
8080+ addBox(front,0,12,rx,h)
8181+ addText(front,smallBold,title,44,h-12)
8282+end
8383+8484+ setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1)
8585+ setNextTextAlign(front,AlignH_Center,AlignV_Middle)
8686+ addText(front,smallBold,"]] .. message .. [[", rx/2, ry/4)
8787+8888+8989+ setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1)
9090+ setNextTextAlign(front,AlignH_Center,AlignV_Middle)
9191+ addText(front,bigBold,"]] .. player.getName() .. [[", rx/2, ry/2)
9292+9393+ renderHeader("Access Control System")
9494+ requestAnimationFrame(10)
9595+ ]]
9696+end
9797+9898+local AccessPermitted = false
9999+100100+function setRenderScriptOnScreens(access)
101101+ for _,screen in pairs(Screens) do
102102+ screen.setRenderScript(getRenderScript(access))
103103+ end
104104+end
105105+106106+function grantAccess()
107107+ AccessPermitted = true
108108+ for _,Door in pairs(Doors) do
109109+ Door.open() --open the door
110110+ end
111111+ for _,Field in pairs(Fields) do
112112+ Field.deactivate() --open the field
113113+ end
114114+ setRenderScriptOnScreens(true)
115115+end
116116+117117+function closeAll()
118118+ AccessPermitted = false
119119+ for _,Door in pairs(Doors) do
120120+ Door.close() --close the door
121121+ end
122122+ for _,Field in pairs(Fields) do
123123+ Field.activate() --close the field
124124+ end
125125+end
126126+127127+function rejectAccess()
128128+ closeAll()
129129+ setRenderScriptOnScreens(false)
130130+ unit.exit()
131131+end
132132+133133+--converting to tables
134134+local tPermittedUsersId = split(AllowedUsersId,',') or {}
135135+local tPermittedUsersName = split(AllowedUsersName,',') or {}
136136+local tPermittedOrgsId = split(AllowedUserOrgsId,',') or {}
137137+local tPermittedOrgsName = split(AllowedUserOrgsName,',') or {}
138138+139139+local player_id = player.getId()
140140+141141+for _,id in pairs(tPermittedUsersId) do
142142+ if id == player_id then
143143+ grantAccess()
144144+ AccessPermitted = true
145145+ break
146146+ end
147147+end
148148+149149+if not AccessPermitted then
150150+ local player_name = player.getName()
151151+ for _,name in pairs(tPermittedUsersName) do
152152+ if name == player_name then
153153+ grantAccess()
154154+ break
155155+ end
156156+ end
157157+end
158158+159159+if not AccessPermitted then
160160+ local player_orgs_id = player.getOrgIds()
161161+ for _,oid in pairs(player_orgs_id) do
162162+ for _,id in pairs(tPermittedOrgsId) do
163163+ if tonumber(id) == oid then
164164+ grantAccess()
165165+ break
166166+ end
167167+ end
168168+ --if access is granted, end the loop
169169+ if AccessPermitted then break end
170170+ --if not a valid ID, veifying the org name
171171+ local o = system.getOrganization(oid)
172172+ if o.name then
173173+ for _,name in pairs(tPermittedOrgsName) do
174174+ if name == o.name then
175175+ grantAccess()
176176+ break
177177+ end
178178+ end
179179+ end
180180+ --if access is granted, end the loop
181181+ if AccessPermitted then break end
182182+ end
183183+end
184184+185185+if not AccessPermitted then
186186+ rejectAccess()
187187+end