aredn/files/www/cgi-bin/signal.json

194 lines
4.7 KiB
JSON
Raw Normal View History

#!/usr/bin/lua
--[[
2015-07-26 22:09:34 -06:00
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2016 Darryl Quinn
See Contributors file for additional contributors
2015-07-26 22:09:34 -06:00
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.
2015-07-26 22:09:34 -06:00
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")
require("nixio")
require("iwinfo")
local ipc = require("luci.ip")
local json = require ("luci.jsonc")
-- Function extensions
os.capture = capture
-- ==== MAIN =====
ctx = uci.cursor()
if not ctx then
error("Failed to get uci cursor")
end
local defnoise=-95
local ol={}
local dpa={} -- datapoint table/array
local dirname="/tmp/snrlog"
local values={}
local counter=0
local datepattern="(%d+)/(%d+)/(%d+)%s(%d+):(%d+):(%d+)"
local parms={}
parms=parsecgi(nixio.getenv("QUERY_STRING") or "")
-- make sure snrlog dir is created
if dir_exists(dirname) then
nixio.fs.mkdir(dirname)
end
-- get wifi ifname
local wifiiface=get_ifname("wifi")
-- get bandwidth
local bandwidth=get_bandwidth()
if parms.realtime then
-- REALTIME
local d=os.date("%H:%M:%S")
local n -- noise
local m -- margin
local s -- signal
local dp={} -- datapoint
local yinfo={}
if (parms.device=="strongest" or parms.device=="" or (not parms.device)) then
-- REALTIME/STRONGEST SIGNAL
-- get radio noise floor
n=iwinfo["nl80211"].noise(wifiiface)
if (n < -101 or n > -50) then
n=defnoise
end
-- get strongest signal
s=iwinfo["nl80211"].signal(wifiiface)
m=tonumber(n)-tonumber(s)
if s==0 then
s="null"
end
tx_rate="N/A"
rx_rate="N/A"
tx_mcs="N/A"
rx_mcs="N/A"
dp.tx_rate=tx_rate
dp.rx_rate=rx_rate
dp.tx_mcs=tx_mcs
dp.rx_mcs=rx_mcs
else
-- REALTIME/SPECIFIC SIGNAL
-- split out device to mac-host
local mac,host=string.match(parms.device,"([^-]*)-(.*)")
local macinfo=iwinfo["nl80211"].assoclist(wifiiface)[mac:upper()]
n=macinfo.noise
s=macinfo.signal
n=tonumber(n)
s=tonumber(s)
m=s-n
tx_rate=macinfo.tx_rate/1000
tx_rate=adjust_rate(tx_rate,bandwidth)
rx_rate=macinfo.rx_rate/1000
rx_rate=adjust_rate(rx_rate,bandwidth)
tx_mcs=macinfo.tx_mcs
rx_mcs=macinfo.rx_mcs
m=tonumber(s)-tonumber(n)
end
if s==0 then
s="null"
end
table.insert(yinfo,s)
table.insert(yinfo,n)
dp.label=d
dp.y=yinfo
dp.m=m
dp.tx_rate=tx_rate
dp.rx_rate=rx_rate
dp.tx_mcs=tx_mcs
dp.rx_mcs=rx_mcs
table.insert(dpa,dp)
table.insert(ol,dpa)
else
-- ARCHIVE
local filename
if not parms.device or parms.device=="" then
-- get the first file from dirlist
for maclist in nixio.fs.glob(dirname.."/".."*") do
filename=maclist
parms.device=maclist
break
end
else
filename=dirname.."/"..parms.device
end
if filename then
for line in io.lines(filename) do
local yinfo={}
local dp={}
local dt,s,n,tm,tr,rm,rr=line:match("(.*)%,(.*)%,(.*),(.*),(.*),(.*),(.*)")
local dtm,dtd,dty,dth,dtmin,dts=dt:match(datepattern)
local dtseconds=os.time({month=dtm,day=dtd,year=dty,hour=dth,min=dtmin,sec=dts})
dtseconds=dtseconds*1000
dtseconds=math.floor(dtseconds)
n=tonumber(n)
if s~="null" then
s=tonumber(s)
m=s-n
else
m="N/A"
end
table.insert(yinfo,s)
table.insert(yinfo,n)
dp.label=dt
dp.x=dtseconds
dp.y=yinfo
dp.m=m
dp.tx_rate=tr
dp.rx_rate=rr
dp.tx_mcs=tm
dp.rx_mcs=rm
table.insert(dpa,dp)
end
table.insert(ol,dpa)
end
end
-- Output the HTTP header for JSON
json_header()
-- Output the info table as json
print(json.stringify(ol,false))
-- END MAIN