From 73d523a883f22ac91248d36b4d5ff296a0c2405c Mon Sep 17 00:00:00 2001 From: spynappels Date: Thu, 3 May 2018 15:12:08 +0100 Subject: [PATCH] This is the complete minimum viable dash. (#4) It includes the Unlink and Link options. --- DASHBOARD | 8 +++- Makefile | 5 +-- dash/qng-dash.sh | 3 -- dash/qng-info.py | 100 +++++++++++++++++++++++++---------------------- dash/qngdash | 2 +- 5 files changed, 64 insertions(+), 54 deletions(-) delete mode 100755 dash/qng-dash.sh diff --git a/DASHBOARD b/DASHBOARD index b1a5a42..7a7d482 100644 --- a/DASHBOARD +++ b/DASHBOARD @@ -1,6 +1,6 @@ ##### DASH ##### -The first iteration of the dash is very rudimentary, but it provides the +This is the minimum viable dashboard to be useful. It provides the following information, refreshed every 30s: * Node Callsign @@ -8,6 +8,12 @@ following information, refreshed every 30s: * Node IP Address * Currently Linked Reflector +It also has a button to disconnect the currently connected reflector, and a text +field and button to connect to another reflector. Only the first 7 characters +entered here will be used, the rest will be discarded. If the reflector entered +is not valid (or not included in the gwys.txt file) the command will still run +but have no effect. + To access the dashboard, simply point a browser at the Hotspot's IP address or at http://.local/ (on the same subnet). diff --git a/Makefile b/Makefile index 4d42b5a..0652766 100644 --- a/Makefile +++ b/Makefile @@ -166,10 +166,9 @@ installmmdvm : 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 + /bin/sh /usr/bin/python3 $(BINDIR)/qng-info.py & uninstallmmdvm : systemctl stop mmdvm.service @@ -278,6 +277,6 @@ uninstalldtmf : /bin/rm -f $(BINDIR)/qndtmf uninstalldash : - /bin/rm -f $(SYSDIR)/qng-* + /bin/rm -f $(SYSDIR)/qng-info.py /bin/rm -f $(CRONDIR)/qngdash /usr/bin/pkill python3 diff --git a/dash/qng-dash.sh b/dash/qng-dash.sh deleted file mode 100755 index c631b69..0000000 --- a/dash/qng-dash.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/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 index 659fc75..e6ce226 100755 --- a/dash/qng-info.py +++ b/dash/qng-info.py @@ -1,49 +1,39 @@ #!/usr/bin/env python - -from http.server import BaseHTTPRequestHandler, HTTPServer -import datetime import socket +import datetime import csv import configparser import libconf +import json +import requests +import subprocess +from time import sleep # 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}

+

Callsign: {1}
+Frequency: {2}MHz
+IP Address: {3}
+External IP Address: {4}
+Reflector: {5}

+
+Note: Please enter a valid 7 character reflector code.
+Link Reflector:
+
+
+Unlink Reflector: +
""" - 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: @@ -65,17 +55,10 @@ def get_data(): 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(intip) + data.append(extip) data.append(reflector) return data @@ -86,14 +69,39 @@ def get_MMDVM(): 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() +intip = get_ip() +extip = requests.get('https://ipapi.co/json/').json()['ip'] +with open('/usr/local/etc/qn.cfg') as f: + config = libconf.load(f) +cs = config.ircddb.login +for key in config.module: + module = key + if config['module'][key]['type'] == 'mmdvm': + freq = get_MMDVM() + else: + freq = config['module'][key]['frequency'] +#Setup Socket WebServer +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', 80)) +s.listen(5) +while True: + conn, addr = s.accept() + request = conn.recv(1024) + request = str(request) + UNLINK = request.find('/?UNLINK=UL') + if UNLINK == 6: + unlink = "/usr/local/bin/qnremote " + module + " " + cs + " U" + subprocess.Popen(unlink.split()) + sleep(8) + LINK = request.find('/?LINK=') + if LINK == 6: + refl = request[13:20].upper() + link = "/usr/local/bin/qnremote " + module + " " + cs + " " + refl + "L" + subprocess.Popen(link.split()) + sleep(8) -run() + data = get_data() + response = html.format(data[2], data[0], data[1], data[2], data[3], data[4]) + conn.send(bytes(response, "UTF-8")) + conn.close() diff --git a/dash/qngdash b/dash/qngdash index cf6cb09..8dbda53 100755 --- a/dash/qngdash +++ b/dash/qngdash @@ -3,4 +3,4 @@ SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin -@reboot root /usr/local/bin/qng-dash.sh +@reboot root /bin/sleep 30 && /usr/bin/python3 /usr/local/bin/qng-info.py