Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mattcridland/laser whitelist #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 64 additions & 38 deletions tplink/laserboss.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
PORT_NAME = "/dev/ttyACM0"
READ_TIMEOUT = 2000 -- in ms
DB_FILE = "/root/laser.db"
COST_PER_MIN = 0.25
COST_PER_MIN = 0.5
USB_PATH = "/sys/bus/usb/devices/usb1/authorized"
UPDATE_INTERVAL = 3600
RETRY_INTERVAL = 300

BASE_RFID_URL = "https://www.acemonstertoys.org/export/"
LASER_EXTENSION = "laser/"
MEMBER_EXTENSION = "rfids/"
-- a file with LASER_KEY and WP_KEY API keys defined (not public)
dofile("/root/key.lua")

Expand All @@ -21,7 +25,8 @@ conn:commit()
rs232 = require("luars232")

-- socket library
http = require("socket.http")
http = requrie("socket.http")
https = require("ssl.https")
ltn12 = require("ltn12")


Expand All @@ -37,27 +42,34 @@ function try(f, catch_f)
if not status then catch_f(exception) end
end

function download_keys(extensionUrl)
local t = {}
local b, c = https.request{ url = BASE_RFID_URL .. extensionUrl,
method = "GET",
sink = ltn12.sink.table(t)
}

assert(c == 200, "Got " .. c .. " instead of 200")
assert(b == 1, "Got " .. b .. " instead of 1")
local json_str = table.concat(t)
assert(string.len(json_str) > 0, "Got empty string from rfids")

local outtbl = {}
local i = 0
for k in string.gmatch(string.sub(json_str, 5, -1), '(%w+)') do
outtbl[k] = true
-- also include short tags from new fob maker
outtbl[string.sub(k, 3, -1)] = true
i = i+1
end

assert(i > 0, "Got 0 keys")
return outtbl
end

function update_keys()
local t = {}
local b, c = http.request{ url = "https://acemonstertoys.wpengine.com/wp-json/amt/v1/rfids/active",
headers = {["X-Amt-Auth"] = WP_KEY},
method = "GET",
redirect = true,
sink = ltn12.sink.table(t)
}
assert(c == 200, "Got " .. c .. " instead of 200")
assert(b == 1, "Got " .. b .. " instead of 1")
local json_str = table.concat(t)
assert(string.sub(json_str, 3, 4) == 'OK', "Got " .. json_str)
local outtbl = {}
local i = 0
for k in string.gmatch(string.sub(json_str, 5, -1), '\"(%w+)\"') do
outtbl[k] = true
i = i+1
end
assert(i > 0, "Got 0 keys")
logger("Updated active key list, " .. i .. " entries")
active_keys = outtbl
active_keys = download_keys(MEMBER_EXTENSION)
laser_keys = download_keys(LASER_EXTENSION)
end

function submit_event(ts, evtype, userid, odo)
Expand Down Expand Up @@ -175,27 +187,31 @@ function display_active (odo_start, odo)
local minutes = math.floor((odo-odo_start)/60)
local seconds = (odo-odo_start) % 60
display(" Time: Cost:", string.format("%3.0f",minutes) .. ":" ..
string.format("%02.0f",seconds) .. " $" ..
string.format("%02.0f",seconds) .. " $" ..
string.format("%3.2f", COST_PER_MIN * (odo-odo_start)/60))
end

function is_valid_user(userid)
return (active_keys == nil or active_keys[userid] ~= nil)
return (laser_keys[userid] ~= nil)
end

function is_valid_member(userid)
return (active_keys[userid] ~= nil)
end

local isEnabled = false
local user, odo_start
local time_last_update = os.time()
local time_last_jrnl = 0
journal_dirty = true
try(function()
try(function()
update_keys()
end, function(e)
logger("Could not load key list")
print(e)
end)

try(function()
try(function()
open_port()
set_enabled(false)
end, function(e)
Expand All @@ -210,7 +226,7 @@ while true do
odo, scanned = get_status()
assert(odo ~= nil and scanned ~= nil, "Status is invalid")
if os.time() - time_last_update > UPDATE_INTERVAL then
try(function()
try(function()
update_keys()
time_last_update = os.time()
end, function(e)
Expand All @@ -219,8 +235,8 @@ while true do
time_last_update = time_last_update + RETRY_INTERVAL
end)
end
try(function()

try(function()
if (journal_dirty and os.time() - time_last_jrnl > RETRY_INTERVAL) then
upload_journal()
journal_dirty = false
Expand All @@ -231,9 +247,9 @@ while true do
logger("Failed to upload journal")
print("Failed to upload journal: ", e)
end)

set_enabled(isEnabled)

if isEnabled then
if scanned == "1" then
-- sign out
Expand All @@ -249,9 +265,14 @@ while true do
display("Welcome new user")
os.execute("sleep 1")
odo_start = odo
else
logger("Attempted login from inactive fob: " .. user2)
display("Fob not active")
else
if is_valid_member(user2) then
logger("Attempted login from non certified member: " .. user2)
display("Not certified")
else
logger("Attempted login from inactive fob: " .. user2)
display("Fob not active")
end
set_enabled(false)
isEnabled = false
os.execute("sleep 5")
Expand Down Expand Up @@ -283,8 +304,13 @@ while true do
display("Welcome!")
os.execute("sleep 2")
else
logger("Attempted login from inactive fob: " .. user)
display("Fob not active")
if is_valid_member(user) then
logger("Attempted login from non certified member: " .. user)
display("Not certified")
else
logger("Attempted login from inactive fob: " .. user)
display("Fob not active")
end
os.execute("sleep 5")
end
else
Expand All @@ -298,7 +324,7 @@ while true do
repeat
logger("Trying to reset port")
os.execute("sleep 1")
try(function()
try(function()
if p ~= nil then
p:close()
end
Expand Down