From 7afa5fef9972267b69b77896192625090986818d Mon Sep 17 00:00:00 2001 From: spynappels Date: Sat, 28 Apr 2018 13:07:56 +0100 Subject: [PATCH] Basic dash (#3) * First version of the basic dash. * Dashboard doc v0.1 * Updated to support DVAP and DVRPTR too by parsing qn.cfg as appropriate. * Corrected typo in get_MMDVM() function * Updated doc to include info on pip3 and libconf module installation. --- DASHBOARD | 27 +++++++++++++ Makefile | 14 +++++++ dash/qng-dash.sh | 3 ++ dash/qng-info.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ dash/qngdash | 6 +++ 5 files changed, 149 insertions(+) create mode 100644 DASHBOARD create mode 100755 dash/qng-dash.sh create mode 100755 dash/qng-info.py create mode 100755 dash/qngdash diff --git a/DASHBOARD b/DASHBOARD new file mode 100644 index 0000000..b1a5a42 --- /dev/null +++ b/DASHBOARD @@ -0,0 +1,27 @@ + ##### DASH ##### + +The first iteration of the dash is very rudimentary, but it provides the +following information, refreshed every 30s: + +* Node Callsign +* Node TX Frequency +* Node IP Address +* Currently Linked Reflector + +To access the dashboard, simply point a browser at the Hotspot's IP address or +at http://.local/ (on the same subnet). + + +To install, run the following command after the main installation: + + sudo make installdash + +Note that this will install the PIP package for Python3 if not installed, and +it will also install the libconf Python module. These are not uninstalled by the +uninstall script, but as these are idempotent commands, there is no issue running +the install if they are already present. + + +To uninstall, run the following: + + sudo make uninstalldash diff --git a/Makefile b/Makefile index 04ebe27..4d42b5a 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ CFGDIR=/usr/local/etc MMPATH=../MMDVMHost SYSDIR=/lib/systemd/system IRC=ircddb +CRONDIR=/etc/cron.d # use this if you want debugging help in the case of a crash #CPPFLAGS=-g -ggdb -W -Wall -std=c++11 -Iircddb -DCFG_DIR=\"$(CFGDIR)\" @@ -162,6 +163,14 @@ installmmdvm : systemctl daemon-reload systemctl start mmdvm.service +installdash : + /usr/bin/apt-get -y install python3-pip + /usr/bin/pip3 install libconf + /bin/cp -f dash/qng-dash.sh $(BINDIR) + /bin/cp -f dash/qng-info.py $(BINDIR) + /bin/cp -f dash/qngdash $(CRONDIR) + /bin/sh $(BINDIR)/qng-dash.sh + uninstallmmdvm : systemctl stop mmdvm.service systemctl disable mmdvm.timer @@ -267,3 +276,8 @@ uninstalldtmf : /bin/rm -f $(SYSDIR)/qndtmf.service systemctl daemon-reload /bin/rm -f $(BINDIR)/qndtmf + +uninstalldash : + /bin/rm -f $(SYSDIR)/qng-* + /bin/rm -f $(CRONDIR)/qngdash + /usr/bin/pkill python3 diff --git a/dash/qng-dash.sh b/dash/qng-dash.sh new file mode 100755 index 0000000..c631b69 --- /dev/null +++ b/dash/qng-dash.sh @@ -0,0 +1,3 @@ +#!/bin/sh +script='/usr/local/bin/qng-info.py' +nohup /usr/bin/python3 $script & diff --git a/dash/qng-info.py b/dash/qng-info.py new file mode 100755 index 0000000..659fc75 --- /dev/null +++ b/dash/qng-info.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +from http.server import BaseHTTPRequestHandler, HTTPServer +import datetime +import socket +import csv +import configparser +import libconf + +# HTML to send to browser +html = """ + + +QnetGateway D-Star Hotspot + + +

QnetGateway D-Star Hotspot

+ +

This status page shows the Callsign, Frequency, IP address and Connected Reflector for the QnetGateway D-Star Hotspot.

+

Callsign: {0}
+Frequency: {1}MHz
+IP Address: {2}
+Reflector: {3}

+ +""" + +data = [] +# HTTPRequestHandler class +class HS_HTTPServer_RequestHandler(BaseHTTPRequestHandler): + + # GET + def do_GET(self): + # Send response status code + self.send_response(200) + + # Send headers + self.send_header('Content-type','text/html') + self.end_headers() + + # Send message back to client + data = get_data() + message = html.format(data[0], data[1], data[2], data[3]) + # Write content as utf-8 data + self.wfile.write(bytes(message, "utf8")) + return + +def get_ip(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + # doesn't even have to be reachable + s.connect(('10.255.255.255', 1)) + IP = s.getsockname()[0] + except: + IP = '127.0.0.1' + finally: + s.close() + return IP + + +def get_data(): + global data + data = [] + reflector = "Unlinked" + with open('/usr/local/etc/RPTR_STATUS.txt') as csvfile: + readCSV = csv.reader(csvfile, delimiter=',') + for row in readCSV: + reflector = row[1] + row[2] + with open('/usr/local/etc/qn.cfg') as f: + config = libconf.load(f) + cs = config.ircddb.login + for key in config.module: + if config['module'][key]['type'] == 'mmdvm': + freq = get_MMDVM() + else: + freq = config['module'][key]['frequency'] + data.append(cs) + data.append(freq) + data.append(str(get_ip())) + data.append(reflector) + return data + +def get_MMDVM(): + MMDVM_config = configparser.ConfigParser() + MMDVM_config.read('/usr/local/etc/MMDVM.qn') + rawfreq = MMDVM_config['Info']['txfrequency'] + freq = float(rawfreq)/1000000 + return freq + +def run(): + print('starting server...') + + # Server settings + server_address = ('', 80) + httpd = HTTPServer(server_address, HS_HTTPServer_RequestHandler) + print('running server...') + httpd.serve_forever() + + +run() diff --git a/dash/qngdash b/dash/qngdash new file mode 100755 index 0000000..cf6cb09 --- /dev/null +++ b/dash/qngdash @@ -0,0 +1,6 @@ +# Cronjob to kick off dash + +SHELL=/bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +@reboot root /usr/local/bin/qng-dash.sh