2020-12-29 18:34:17 -05:00
|
|
|
#!/usr/bin/env python3
|
2020-12-29 05:55:32 -05:00
|
|
|
import telnetlib
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import rrdtool
|
|
|
|
import os.path
|
|
|
|
import config as cfg
|
|
|
|
from airium import Airium
|
|
|
|
|
2020-12-29 18:57:22 -05:00
|
|
|
# Read config parameters from config.py
|
|
|
|
# and store them into variables
|
2020-12-29 05:55:32 -05:00
|
|
|
host = cfg.host
|
|
|
|
port = cfg.port
|
|
|
|
timeout = cfg.timeout
|
|
|
|
db_file = cfg.db_file
|
|
|
|
web_path = cfg.web_path
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Create a new database if not already done before
|
2020-12-29 05:55:32 -05:00
|
|
|
def init_db():
|
|
|
|
if not os.path.isfile(db_file):
|
|
|
|
print ("creating DB file " + db_file)
|
|
|
|
rrdtool.create(
|
|
|
|
db_file,
|
|
|
|
"--start", "now",
|
|
|
|
"--step", "5",
|
|
|
|
"RRA:AVERAGE:0.5:12:86400",
|
|
|
|
"DS:dbm:GAUGE:15:-80:50")
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Connect to the rig via Telnet to rigctld
|
2020-12-29 05:55:32 -05:00
|
|
|
def connect_rig():
|
|
|
|
try:
|
2020-12-29 18:57:22 -05:00
|
|
|
print("Connecting to " + host + ":" + str(port) + "...")
|
2020-12-29 05:55:32 -05:00
|
|
|
session = telnetlib.Telnet(host, port, timeout)
|
2020-12-29 18:57:22 -05:00
|
|
|
print("Connection successful.")
|
2020-12-29 05:55:32 -05:00
|
|
|
return session
|
|
|
|
except Exception as e:
|
2020-12-29 18:57:22 -05:00
|
|
|
print("Connection failed:")
|
2020-12-29 05:55:32 -05:00
|
|
|
print(e)
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Query the rig for signal strength (l STRENGTH)
|
|
|
|
# and write the output into the database
|
2020-12-29 05:55:32 -05:00
|
|
|
def query_rig(session):
|
|
|
|
global db_file
|
2020-12-29 18:57:22 -05:00
|
|
|
# read signal strength from TRX
|
2020-12-29 05:55:32 -05:00
|
|
|
session.write(b"l STRENGTH\n")
|
2020-12-29 18:57:22 -05:00
|
|
|
# wait a moment
|
2020-12-29 05:55:32 -05:00
|
|
|
time.sleep(0.1)
|
2020-12-29 18:57:22 -05:00
|
|
|
# read the answer
|
2020-12-29 05:55:32 -05:00
|
|
|
strength = session.read_very_eager().decode("utf-8")
|
|
|
|
x = strength.replace("\n", "")
|
2020-12-29 18:57:22 -05:00
|
|
|
# write the value returned from rigctld to the DB
|
2020-12-29 05:55:32 -05:00
|
|
|
if x:
|
|
|
|
rrdtool.update(db_file, 'N:%s' % x)
|
|
|
|
else:
|
|
|
|
print("yep, that went wrong.")
|
|
|
|
rrdtool.update(db_file, 'N:U')
|
|
|
|
print(x)
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Generate a PNG file with a graph for a certain time range
|
2020-12-29 05:55:32 -05:00
|
|
|
def print_graph(filename,title,time_window):
|
|
|
|
global db_file
|
|
|
|
global web_path
|
|
|
|
|
2020-12-29 18:57:22 -05:00
|
|
|
# Definition of the graph and the output file
|
2020-12-29 05:55:32 -05:00
|
|
|
graphv_args = [
|
|
|
|
web_path+"/"+filename,
|
|
|
|
'--title', title,
|
|
|
|
'--start', time_window,
|
|
|
|
'--lower-limit=0',
|
|
|
|
'--interlaced',
|
|
|
|
'--imgformat', 'PNG',
|
|
|
|
'--width=800',
|
|
|
|
'--vertical-label', 'dbm',
|
|
|
|
'DEF:noiselevel='+db_file+':dbm:AVERAGE',
|
|
|
|
'LINE1:noiselevel#ff0000:"This is a red line"'
|
|
|
|
]
|
2020-12-29 18:57:22 -05:00
|
|
|
# generate the output file
|
2020-12-29 05:55:32 -05:00
|
|
|
rrdtool.graphv(*graphv_args)
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Generate an index.html file containing some graphs
|
2020-12-29 05:55:32 -05:00
|
|
|
def gen_html():
|
|
|
|
global web_path
|
|
|
|
a = Airium()
|
|
|
|
|
2020-12-29 18:57:22 -05:00
|
|
|
# Generate the HTML code
|
2020-12-29 05:55:32 -05:00
|
|
|
a('<!DOCTYPE html>')
|
|
|
|
with a.html(lang="en"):
|
|
|
|
with a.head():
|
|
|
|
a.meta(charset="utf-8")
|
|
|
|
a.title(_t="Noise Floor")
|
|
|
|
with a.body():
|
|
|
|
with a.h3():
|
|
|
|
a("Last 1 hour")
|
|
|
|
with a.div():
|
|
|
|
a.img(src='1.png', alt='alt text')
|
|
|
|
with a.h3():
|
|
|
|
a("Last 4 hours")
|
|
|
|
with a.div():
|
|
|
|
a.img(src='2.png', alt='alt text')
|
|
|
|
with a.h3():
|
|
|
|
a("Last 12 hours")
|
|
|
|
with a.div():
|
|
|
|
a.img(src='3.png', alt='alt text')
|
|
|
|
with a.h3():
|
|
|
|
a("Last 24 hours")
|
|
|
|
with a.div():
|
|
|
|
a.img(src='4.png', alt='alt text')
|
|
|
|
|
2020-12-29 18:57:22 -05:00
|
|
|
html = str(a)
|
|
|
|
# write the html code to disk as index.html
|
2020-12-29 05:55:32 -05:00
|
|
|
with open(web_path + "/index.html", "w") as html_file:
|
|
|
|
print(f"{html}", file=html_file)
|
|
|
|
|
2020-12-29 18:34:17 -05:00
|
|
|
# Main functionality
|
2020-12-29 18:57:22 -05:00
|
|
|
init_db() # If no DB exists, create one
|
|
|
|
s = connect_rig() # Connect via Telnet to rigctld
|
2020-12-29 05:55:32 -05:00
|
|
|
while s:
|
2020-12-29 18:57:22 -05:00
|
|
|
query_rig(s) # Query the signal strength and write it to the DB
|
|
|
|
print_graph("1.png","Noise Floor","-1h") # create a graph as PNG file
|
|
|
|
print_graph("2.png","Noise Floor","-4h") # ^
|
|
|
|
print_graph("3.png","Noise Floor","-12h") # ^
|
|
|
|
print_graph("4.png","Noise Floor","-24h") # ^
|
|
|
|
gen_html() # create and write a html file
|
|
|
|
time.sleep(5) # wait 5 seconds
|