1
0
mirror of https://github.com/craigerl/aprsd.git synced 2025-04-10 13:39:33 -04:00

Send Message via admin Web interface

This patch adds the ability to send a message from the
admin interface's send-message.html page.
This commit is contained in:
Hemna 2021-05-03 10:28:31 -04:00
parent d243e577f0
commit 6d3258e833
4 changed files with 103 additions and 4 deletions

View File

@ -38,6 +38,11 @@ class Client:
if config:
self.config = config
def new(self):
obj = super().__new__(Client)
obj.config = self.config
return obj
@property
def client(self):
if not self.aprs_client:

View File

@ -4,14 +4,17 @@ import logging
from logging import NullHandler
from logging.handlers import RotatingFileHandler
import sys
import time
from aprslib.exceptions import LoginError
import flask
from flask import request
import flask_classful
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import check_password_hash, generate_password_hash
import aprsd
from aprsd import kissclient, messaging, packets, plugin, stats, utils
from aprsd import client, kissclient, messaging, packets, plugin, stats, utils
LOG = logging.getLogger("APRSD")
@ -115,6 +118,70 @@ class APRSDFlask(flask_classful.FlaskView):
return flask.render_template("messages.html", messages=json.dumps(msgs))
def setup_connection(self):
user = self.config["aprs"]["login"]
password = self.config["aprs"]["password"]
host = self.config["aprs"].get("host", "rotate.aprs.net")
port = self.config["aprs"].get("port", 14580)
connected = False
backoff = 1
while not connected:
try:
LOG.info("Creating aprslib client")
aprs_client = client.Aprsdis(
user,
passwd=password,
host=host,
port=port,
)
# Force the logging to be the same
aprs_client.logger = LOG
aprs_client.connect()
connected = True
backoff = 1
except LoginError as e:
LOG.error("Failed to login to APRS-IS Server '{}'".format(e))
connected = False
raise e
except Exception as e:
LOG.error("Unable to connect to APRS-IS server. '{}' ".format(e))
time.sleep(backoff)
backoff = backoff * 2
continue
LOG.debug("Logging in to APRS-IS with user '%s'" % user)
return aprs_client
def send_message(self):
if request.method == "POST":
from_call = request.form["from_call"]
to_call = request.form["to_call"]
message = request.form["message"]
LOG.info(
"From: '{}' To: '{}' Send '{}'".format(
from_call,
to_call,
message,
),
)
try:
aprsis_client = self.setup_connection()
except LoginError as e:
result = "Failed to setup Connection {}".format(e)
msg = messaging.TextMessage(from_call, to_call, message)
msg.send_direct(aprsis_client=aprsis_client)
result = "Message sent"
else:
from_call = self.config["aprs"]["login"]
result = ""
return flask.render_template(
"send-message.html",
from_call=from_call,
result=result,
)
@auth.login_required
def packets(self):
packet_list = packets.PacketList().get()
@ -224,6 +291,7 @@ def init_flask(config, loglevel, quiet):
flask_app.route("/stats", methods=["GET"])(server.stats)
flask_app.route("/messages", methods=["GET"])(server.messages)
flask_app.route("/packets", methods=["GET"])(server.packets)
flask_app.route("/send-message", methods=["GET", "POST"])(server.send_message)
flask_app.route("/save", methods=["GET"])(server.save)
flask_app.route("/plugins", methods=["GET"])(server.plugins)
return flask_app

View File

@ -299,7 +299,7 @@ class RawMessage(Message):
thread = SendMessageThread(message=self)
thread.start()
def send_direct(self):
def send_direct(self, aprsis_client=None):
"""Send a message without a separate thread."""
cl = self.get_transport()
log_message(
@ -379,7 +379,7 @@ class TextMessage(Message):
thread = SendMessageThread(message=self)
thread.start()
def send_direct(self):
def send_direct(self, aprsis_client=None):
"""Send a message without a separate thread."""
cl = self.get_transport()
log_message(
@ -498,7 +498,8 @@ class AckMessage(Message):
thread = SendAckThread(self)
thread.start()
def send_direct(self):
def send_direct(self, aprsis_client=None):
"""Send an ack message without a separate thread."""
cl = self.get_transport()
log_message(

View File

@ -0,0 +1,25 @@
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<form action="send-message" method="POST" name="send-message">
<p><label for="from_call">From Callsign:</label>
<input type="text" name="from_call" id="fcall" value="{{ from_call }}"></p>
<p><label for="to_call">To Callsign:</label>
<input type="text" name="to_call" id="tcall"></p>
<p><label for="message">Message:</label>
<input type="text" name="message" id="message"></p>
<input value="Submit" type="submit">
</form>
</body>
</html>