Some LUA Code Clean Up, API (#327)

* Refactor API

* work in progress

* lua clean up
This commit is contained in:
Raymond Suelzer 2019-01-21 13:41:31 -08:00 committed by dman776
parent 13a6afb815
commit 664f2ee629
6 changed files with 368 additions and 55 deletions

View File

@ -35,24 +35,51 @@
--]]
require("uci")
require("aredn.uci")
local aredn_uci = require("aredn.uci")
require("aredn.utils")
-- require("aredn.http")
local lip=require("luci.ip")
require("nixio")
require("ubus")
function getNodeName()
css=getUciConfType("system", "system")
-------------------------------------
-- Public API is attached to table
-------------------------------------
local model = {}
-------------------------------------
-- Returns WAN Address
-------------------------------------
local function getWAN()
local cubus = ubus.connect()
niws=cubus:call("network.interface.wan","status",{})
if niws['ipv4-address'] == nil or niws['ipv4-address'][1] == nil then
return ""
end
return niws['ipv4-address'][1]['address']
end
-------------------------------------
-- Returns name of the node
-------------------------------------
function model.getNodeName()
css=aredn_uci.getUciConfType("system", "system")
return css[0]['hostname']
end
function getNodeDescription()
css=getUciConfType("system", "system")
-------------------------------------
-- Returns description of the node
-------------------------------------
function model.getNodeDescription()
css=aredn_uci.getUciConfType("system", "system")
return css[0]['description']
end
function getLatLon()
-------------------------------------
-- Returns array [Latitude, Longitude]
-------------------------------------
function model.getLatLon()
local llfname="/etc/latlon"
local lat=""
local lon=""
@ -67,7 +94,10 @@ function getLatLon()
return lat,lon
end
function getGridSquare()
-------------------------------------
-- Returns Grid Square of Node
-------------------------------------
function model.getGridSquare()
local gsfname="/etc/gridsquare"
local grid=""
if file_exists(gsfname) then
@ -80,7 +110,10 @@ function getGridSquare()
return grid
end
function getFirmwareVersion()
-------------------------------------
-- Returns Current Firmware Version
-------------------------------------
function model.getFirmwareVersion()
local relfile=io.open("/etc/mesh-release","r")
local fv=""
if relfile~=nil then
@ -90,15 +123,22 @@ function getFirmwareVersion()
return fv
end
function getModel()
-------------------------------------
-- Retuns Model / Device name
-------------------------------------
function model.getModel()
m=os.capture("/usr/local/bin/get_model")
return m:chomp()
end
function getSSID()
-------------------------------------
-- Returns current SSID
-------------------------------------
function model.getSSID()
-- SSID
local myssid=""
local wif=getUciConfType("wireless", "wifi-iface")
local wif=aredn_uci.getUciConfType("wireless", "wifi-iface")
for pos, t in pairs(wif) do
if wif[pos]['network']=="wifi" then
myssid=wif[pos]['ssid']
@ -107,10 +147,14 @@ function getSSID()
return myssid
end
function getMeshRadioDevice()
-------------------------------------
-- Determine Radio Device for Mesh
-------------------------------------
function model.getMeshRadioDevice()
--Determine radio device for mesh
local radio=""
local wifiinterfaces=getUciConfType("wireless","wifi-iface")
local wifiinterfaces=aredn_uci.getUciConfType("wireless","wifi-iface")
for pos,i in pairs(wifiinterfaces) do
if wifiinterfaces[pos]['mode']=="adhoc" then
radio=wifiinterfaces[pos]['device']
@ -119,15 +163,25 @@ function getMeshRadioDevice()
return radio
end
function getBand(radio)
-------------------------------------
-- TODO: Return Band
-------------------------------------
function model.getBand(radio)
return ""
end
function getFrequency(radio)
-------------------------------------
-- TODO: Return Frequency
-------------------------------------
function model.getFrequency(radio)
return ""
end
function getChannel(radio)
-------------------------------------
-- Return Channel for Radio
-- @param radio Radio Device.
-------------------------------------
function model.getChannel(radio)
--Wifi Channel Number
local ctx = uci.cursor()
if not ctx then
@ -142,7 +196,12 @@ function getChannel(radio)
return tostring(chan)
end
function getChannelBW(radio)
-------------------------------------
-- Return Channel BW for Radio
-- @param radio Radio Device.
-------------------------------------
function model.getChannelBW(radio)
--Wifi Bandwidth
ctx = uci.cursor()
if not ctx then
@ -153,21 +212,42 @@ function getChannelBW(radio)
return chanbw
end
function getUptime()
-------------------------------------
-- Current System Uptime
-------------------------------------
function model.getUptime()
local mynix=nixio.sysinfo()
local upsecs=mynix['uptime']
return secondsToClock(upsecs)
return upsecs
end
function getDate()
-------------------------------------
-- System Date Formatted
-------------------------------------
function model.getDate()
return os.date("%a %b %d %Y")
end
function getTime()
-------------------------------------
-- System Time Formatted
-------------------------------------
function model.getTime()
return os.date("%H:%M:%S %Z")
end
function getLoads()
-------------------------------------
-- Returns current epoch time
-------------------------------------
function getEpoch()
return os.time()
end
-------------------------------------
-- Returns last three average loads
-------------------------------------
function model.getLoads()
local loads={}
local mynix=nixio.sysinfo()
loads=mynix['loads']
@ -177,7 +257,10 @@ function getLoads()
return loads
end
function getFreeMemory()
-------------------------------------
-- Returns memory details
-------------------------------------
function model.getFreeMemory()
local mem={}
local mynix=nixio.sysinfo()
mem['freeram']=mynix['freeram']/1024
@ -186,7 +269,10 @@ function getFreeMemory()
return mem
end
function getFSFree()
-------------------------------------
-- Returns FS Usage details
-------------------------------------
function model.getFSFree()
local fsf={}
local mynix=nixio.fs.statvfs("/")
fsf['rootfree']=mynix['bfree']*4
@ -196,7 +282,10 @@ function getFSFree()
return fsf
end
function getOLSRInfo()
-------------------------------------
-- Returns OLSR info
-------------------------------------
function model.getOLSRInfo()
local info={}
tot=os.capture('/sbin/ip route list table 30|wc -l')
info['entries']=tot:chomp()
@ -205,11 +294,23 @@ function getOLSRInfo()
return info
end
function getInterfaceIPAddress(interface)
return getUciConfSectionOption("network",interface,"ipaddr")
-------------------------------------
-- Returns Interface IP Address
-- @param interface name of interface 'wifi' | 'lan' | 'wan'
-------------------------------------
function model.getInterfaceIPAddress(interface)
-- special case
if interface == "wan" then
return getWAN()
end
function getDefaultGW()
return aredn_uci.getUciConfSectionOption("network",interface,"ipaddr")
end
-------------------------------------
-- Returns Default Gateway
-------------------------------------
function model.getDefaultGW()
local gw=""
local rt=lip.route("8.8.8.8")
if rt ~= "" then
@ -218,8 +319,6 @@ function getDefaultGW()
return gw
end
function getWAN()
local cubus = ubus.connect()
niws=cubus:call("network.interface.wan","status",{})
return niws['ipv4-address'][1]['address']
end
return model

View File

@ -36,14 +36,18 @@
require("uci")
function getUciConfType(conf,type)
local api = {}
function api.getUciConfType(conf,type)
local curs=uci.cursor()
local ifce={}
curs:foreach(conf,type,function(s) ifce[s[".index"]]=s end)
return ifce
end
function getUciConfSectionOption(conf,sect,option)
function api.getUciConfSectionOption(conf,sect,option)
local curs=uci.cursor()
return curs:get(conf,sect,option)
end
return api

View File

@ -35,7 +35,7 @@
--]]
require("aredn.utils")
require("aredn.info")
local aredn_info=require("aredn.info")
require("uci")
require("aredn.uci")
local nxo=require("nixio")
@ -333,8 +333,8 @@ end
)
-- get the current bandwidth setting
local radio=getMeshRadioDevice()
bandwidth=getChannelBW(radio)
local radio=aredn_info.getMeshRadioDevice()
bandwidth=aredn_info.getChannelBW(radio)
-- iterate over all the stations
for mstation in pairs(stations) do

212
files/www/cgi-bin/api Normal file
View File

@ -0,0 +1,212 @@
#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2018 Darryl Quinn
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("uci")
require("aredn.uci")
require("aredn.utils")
require("aredn.http")
local aredn_info = require("aredn.info")
require("nixio")
local json = require("luci.jsonc")
-- Function extensions
os.capture = capture
function getSysinfo()
local info={}
info['node']=aredn_info.getNodeName()
info['description']=aredn_info.getNodeDescription()
info['firmware_version']=aredn_info.getFirmwareVersion()
info['model']=aredn_info.getModel()
--
info['date']=aredn_info.getDate()
info['time']=aredn_info.getTime()
--
info['uptime']=aredn_info.getUptime()
info['loads']=aredn_info.getLoads()
return info
end
function getStatusMeshRF()
local info={}
info['ssid']=aredn_info.getSSID()
info['device']=aredn_info.getMeshRadioDevice()
info['channel']=aredn_info.getChannel(info['device'])
info['chanbw']=aredn_info.getChannelBW(info['device'])
info['band']=aredn_info.getBand(info['device'])
info['frequency']=aredn_info.getFrequency(info['device'])
return info
end
function getStatusIp()
local info={}
info['gateway']=aredn_info.getDefaultGW()
info['wifi']=aredn_info.getInterfaceIPAddress('wifi')
info['lan']=aredn_info.getInterfaceIPAddress('lan')
info['wan']=aredn_info.getInterfaceIPAddress('wan')
return info
end
function getLocationInfo()
local info={}
local lat, lon= aredn_info.getLatLon()
local gs= aredn_info.getGridSquare()
info['lat']=lat
info['lon']=lon
info['gridsquare']=gs
return info
end
function getCurrentNeighbors()
local info={}
local links=getOLSRLinks()
for k,v in pairs(links) do
local host
local remip=v['remoteIP']
local remhost=nslookup(remip)
info[remip]={}
info[remip]['olsrInterface']=v['olsrInterface']
info[remip]['linkType']= getInterfaceType(v['olsrInterface'])
info[remip]['linkQuality']=v['linkQuality']*100
info[remip]['neighborLinkQuality']=v['neighborLinkQuality']*100
host = string.gsub(remhost,"dtdlink%.", "")
host = string.gsub(host,"mid%d.", "")
info[remip]['hostname']=host
-- services
info[remip]['services']={}
-- get TxMBPS
info[remip]['rate']="0"
end
return info
end
function getServicesByNode()
local nodes=getAllHosts()
return nodes
end
function getFreeMemory()
local info = aredn_info.getFreeMemory()
return info
end
-- ==== MAIN =====
ctx = uci.cursor()
if not ctx then
error("Failed to get uci cursor")
end
info={}
-- get/process query string
local qsset={}
if (arg[1]==nil and arg[2]==nil) then
qs=nixio.getenv("QUERY_STRING")
if qs~="" then
qsset=parseQueryString(qs)
else
-- maybe default to a help page
qsset["api"]="help"
end
else
qsset[arg[1]]=arg[2]
end
info['pages']={}
for page, comps in pairs(qsset) do
-- ---------------- /mesh page
if not setContains(info['pages'],page) then
info['pages'][page]={}
end
if page=="api" then
info['pages'][page]=nil
info['api_help']="AREDN API. This API's primary function is to drive the Web UI."
elseif page=="status" then
for i,comp in pairs(comps:split(',')) do
if comp=="meshrf" then
info['pages'][page][comp]=getStatusMeshRF()
elseif comp=="ip" then
info['pages'][page][comp]=getStatusIp()
elseif comp=="sysinfo" then
info['pages'][page][comp]=getSysinfo()
elseif comp=="memory" then
info['pages'][page][comp]=getFreeMemory()
elseif comp=="storage" then
info['pages'][page][comp]=aredn_info.getFSFree()
elseif comp=="olsr" then
info['pages'][page][comp]=aredn_info.getOLSRInfo()
elseif comp=="location" then
info['pages'][page][comp]=getLocationInfo()
end
end
elseif page=="mesh" then
for i,comp in pairs(comps:split(',')) do
if comp=="sysinfo" then
info['pages'][page][comp]=getSysinfo()
elseif comp=="localhosts" then
info['pages'][page][comp]={}
elseif comp=="remotenodes" then
info['pages'][page][comp]={}
elseif comp=="currentneighbors" then
info['pages'][page][comp]=getCurrentNeighbors()
elseif comp=="previousneighbors" then
info['pages'][page][comp]={}
end
end
elseif page=="services" then
for i,comp in pairs(comps:split(',')) do
if comp=="sysinfo" then
info['pages'][page][comp]=getSysinfo()
elseif comp=="bynode" then
info['pages'][page][comp]=getServicesByNode()
elseif comp=="byservice" then
info['pages'][page][comp]=getServicesByService()
end
end
end
end
-- Output the HTTP header for JSON
-- json_header()
json_header()
-- Output the info table as json
print(json.stringify(info,true))

View File

@ -37,7 +37,7 @@
require("uci")
require("aredn.uci")
require("aredn.utils")
require("aredn.info")
local aredn_info = require("aredn.info")
require("aredn.http")
require("nixio")
require("iwinfo")
@ -74,8 +74,8 @@ end
local wifiiface=get_ifname("wifi")
-- get bandwidth
local radio=getMeshRadioDevice()
local bandwidth=getChannelBW(radio)
local radio=aredn_info.getMeshRadioDevice()
local bandwidth=aredn_info.getChannelBW(radio)
if parms.realtime then
-- REALTIME

View File

@ -35,9 +35,8 @@
--]]
require("uci")
require("aredn.uci")
require("aredn.utils")
require("aredn.info")
local aredn_info = require("aredn.info")
require("aredn.http")
require("nixio")
local ipc = require("luci.ip")
@ -56,19 +55,18 @@ end
info={}
-- API version
info['api_version']="1.5"
info['api_version']="1.6"
-- NODE name
css=getUciConfType("system", "system")
info['node']=css[0]['hostname']
info['node']=aredn_info.getNodeName()
info['node_details']={}
-- MODEL
info['node_details']['model']=getModel()
info['node_details']['model']=aredn_info.getModel()
-- DESCRIPTION
info['node_details']['description']=getNodeDescription()
info['node_details']['description']=aredn_info.getNodeDescription()
-- BOARD ID
info['node_details']['board_id']=hardware_boardid()
@ -81,16 +79,16 @@ end
info['node_details']['firmware_mfg']=fw_mfg
-- Firmware version
info['node_details']['firmware_version']=getFirmwareVersion()
info['node_details']['firmware_version']=aredn_info.getFirmwareVersion()
-- Mesh RF info
info['meshrf']={}
local radio=getMeshRadioDevice()
local radio=aredn_info.getMeshRadioDevice()
if ( radio ~= nill and radio ~= "" ) then
info['meshrf']['status']="on"
info['meshrf']['ssid']=getSSID()
info['meshrf']['channel']=getChannel(radio)
info['meshrf']['chanbw']=getChannelBW(radio)
info['meshrf']['ssid']=aredn_info.getSSID()
info['meshrf']['channel']=aredn_info.getChannel(radio)
info['meshrf']['chanbw']=aredn_info.getChannelBW(radio)
else
info['meshrf']['status']="off"
end
@ -110,12 +108,12 @@ info['tunnels']['active_tunnel_count']=atc:chomp()
-- Location info
-- LAT/LON
local lat, lon = getLatLon()
local lat, lon = aredn_info.getLatLon()
info['lat']=lat
info['lon']=lon
-- GRID SQUARE
info['grid_square']=getGridSquare()
info["grid_square"]=aredn_info.getGridSquare()
-- UPTIME AND LOADAVGS
mynix=nixio.sysinfo()