-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor; split up functionality into modules
- Loading branch information
Showing
5 changed files
with
489 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
-- | ||
-- This class keeps track of the devices in the local network | ||
-- | ||
|
||
local spin_util = require('spin_util') | ||
|
||
local _M = {} | ||
|
||
local device_manager = {} | ||
device_manager_mt = { __index = device_manager } | ||
|
||
function _M.create(profile_manager) | ||
local newinst = {} | ||
setmetatable(newinst, device_manager_mt) | ||
|
||
newinst.devices_seen = {} | ||
newinst.profile_manager = profile_manager | ||
newinst.last_update = spin_util.get_time_string() | ||
|
||
return newinst | ||
end | ||
|
||
function device_manager:get_device_seen(mac) | ||
return self.devices_seen[mac] | ||
end | ||
|
||
function device_manager:get_devices_seen() | ||
return self.devices_seen | ||
end | ||
|
||
-- Returns one of three options: | ||
-- true if the device has been seen and is marked as 'new' | ||
-- false if the device has been seen and is not marked as 'new' | ||
-- nil if the device has not been seen | ||
function device_manager:device_is_new(mac) | ||
if self.devices_seen[mac] == nil then return nil end | ||
return self.devices_seen[mac].new | ||
end | ||
|
||
function device_manager:set_device_is_new(mac, value) | ||
self.devices_seen_updated = spin_util.get_time_string() | ||
self.devices_seen[mac] = value | ||
end | ||
|
||
|
||
function device_manager:add_device_seen(mac, name, timestamp) | ||
local device_is_new = False | ||
if self.devices_seen[mac] ~= nil then | ||
self.devices_seen[mac]['lastSeen'] = timestamp | ||
self.devices_seen[mac]['name'] = name | ||
self.devices_seen[mac]['appliedProfiles'] = self.profile_manager:get_device_profiles(mac) | ||
else | ||
local device_data = {} | ||
device_data['lastSeen'] = timestamp | ||
device_data['name'] = name | ||
device_data['new'] = true | ||
device_data['mac'] = mac | ||
device_data['appliedProfiles'] = self.profile_manager:get_device_profiles(mac) | ||
device_data['enforcement'] = "" | ||
device_data['logging'] = "" | ||
|
||
self.devices_seen[mac] = device_data | ||
|
||
device_is_new = True | ||
end | ||
self.devices_seen_updated = spin_util.get_time_string() | ||
return device_is_new | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
-- | ||
-- Assorted utility functions for spin_webui | ||
-- | ||
|
||
local sys_stat = require "posix.sys.stat" | ||
local mt_io = require 'minittp_io' | ||
|
||
local _M = {} | ||
|
||
-- returns a straight list of all the whitespace-separated tokens | ||
-- in the given filename | ||
-- or nil, err on error | ||
function _M.file_tokenize(filename) | ||
local result = {} | ||
local fr, err = mt_io.file_reader(filename) | ||
if fr == nil then return nil, err end | ||
|
||
for line in fr:read_line_iterator(true) do | ||
for token in line:gmatch("%S+") do table.insert(result, token) end | ||
end | ||
|
||
return result | ||
end | ||
|
||
function _M.file_tokenize_iterator(filename) | ||
local data, err = file_tokenize(filename) | ||
if data == nil then return nil, err end | ||
|
||
result = {} | ||
result.index = 1 | ||
result.done = false | ||
result.data = data | ||
|
||
function result:nxt() | ||
if not self.done then | ||
local value = self.data[self.index] | ||
self.index = self.index + 1 | ||
if self.index > table.getn(self.data) then self.done = true end | ||
return value | ||
else | ||
return nil | ||
end | ||
end | ||
return result | ||
end | ||
|
||
function _M.strip_quotes(value) | ||
if value:len() == 1 then return value end | ||
if value:startswith("'") and value:endswith("'") then | ||
return value:sub(2, value:len()-1) | ||
elseif value:startswith('"') and value:endswith('"') then | ||
return value:sub(2, value:len()-1) | ||
else | ||
return value | ||
end | ||
end | ||
|
||
-- | ||
-- config file parser, reads config files | ||
-- in the 'luci-style' form of | ||
-- | ||
-- config <name> | ||
-- option <name> '<value>' | ||
-- option <name> '<value>' | ||
-- config <name> | ||
-- option <name> '<value>' | ||
-- | ||
-- very basic config parser; hardly any checking | ||
function _M.config_parse(filename) | ||
local config = {} | ||
local tokens, err = file_tokenize_iterator(filename) | ||
if tokens == nil then return nil, err end | ||
|
||
local cur_section = "main" | ||
|
||
while not tokens.done do | ||
local token = tokens:nxt() | ||
if token == "config" then | ||
cur_section = tokens:nxt() | ||
elseif token == "option" then | ||
local option_name = tokens:nxt() | ||
local option_val = strip_quotes(tokens:nxt()) | ||
if config[cur_section] == nil then config[cur_section] = {} end | ||
config[cur_section][option_name] = option_val | ||
print(config[cur_section][option_name]) | ||
end | ||
end | ||
return config | ||
end | ||
|
||
-- return the current time, or given timestamp in the format of RFC7232 section 2.2 | ||
function _M.get_time_string(timestamp) | ||
if timestamp ~= nil then | ||
return os.date("%a, %d %b %Y %X %z", timestamp) | ||
else | ||
return os.date("%a, %d %b %Y %X %z") | ||
end | ||
end | ||
|
||
-- return the file timestamp in the format of RFC7232 section 2.2 | ||
function _M.get_file_timestamp(file_path) | ||
local fstat, err = sys_stat.stat(file_path) | ||
if fstat == nil then | ||
return nil, err | ||
end | ||
|
||
return os.date("%a, %d %b %Y %X %z", fstat.st_mtime) | ||
end | ||
|
||
return _M |
Oops, something went wrong.