Compare commits
2 Commits
preparing-
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
185182b92f | ||
|
|
976d2453a2 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
logs/
|
||||
meshtastic/
|
||||
.vscode/
|
||||
__pycache__/
|
||||
__pycache__/
|
||||
MeshtasticLogger.service
|
||||
18
CommandHandler.py
Normal file
18
CommandHandler.py
Normal file
@@ -0,0 +1,18 @@
|
||||
class CommandHandler:
|
||||
def __init__(self, print_method):
|
||||
self.commands: dict = {}
|
||||
self.print = print_method
|
||||
|
||||
def register_callback(self, key: str, callback):
|
||||
self.commands[key.lower()] = callback
|
||||
|
||||
def parse_command(self, command : str):
|
||||
args :list[str] = command.split(" ")
|
||||
if args[0].lower() in self.commands:
|
||||
return_message = self.commands[args[0].lower()](args[1:])
|
||||
if not (return_message is None):
|
||||
self.print(return_message)
|
||||
else:
|
||||
self.print("Command not recognized. Type 'help' for a list of commands.")
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
[Unit]
|
||||
Description=A simple chat interface and autoresponder for meshtastic
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
WorkingDirectory=/home/quinn/Projects/Meshtastic-Webserver/
|
||||
ExecStart=sudo /home/quinn/Projects/Meshtastic-Webserver/meshtastic/bin/python -m flask --app /home/quinn/Projects/Meshtastic-Webserver/app.py run --host=0.0.0.0 --port=80
|
||||
Restart=always
|
||||
User=quinn
|
||||
StandardOutput=append:/home/quinn/Projects/Meshtastic-Webserver/logs/meshtastic_system.log
|
||||
StandardError=append:/home/quinn/Projects/Meshtastic-Webserver/logs/meshtastic_system_error.log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -24,4 +24,9 @@ And you can stop the service by running:
|
||||
`sudo systemctl stop MeshtasticLogger`
|
||||
|
||||
To have the service start on boot run:
|
||||
`systemctl enable MeshtasticLogger`
|
||||
`systemctl enable MeshtasticLogger`
|
||||
|
||||
# Automatic Deployment
|
||||
Make sure the bash script is executable by running: `chmod u+x install.sh`
|
||||
|
||||
Make certain you are actually in the Meshtastic-Webserver Directory or this will fail. Run the script with: `sudo ./install.sh`
|
||||
95
app.py
95
app.py
@@ -1,8 +1,12 @@
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
import meshtastic
|
||||
from MeshtasticLogger import MeshtasticLogger
|
||||
from CommandHandler import CommandHandler
|
||||
import os
|
||||
|
||||
# -------------------------------
|
||||
# Initialize data and objects
|
||||
# -------------------------------
|
||||
messages = []
|
||||
app = Flask(__name__)
|
||||
interface = meshtastic.serial_interface.SerialInterface()
|
||||
@@ -22,54 +26,57 @@ def web_print(message: str):
|
||||
print(message)
|
||||
messages.append(message)
|
||||
get_messages()
|
||||
|
||||
def callback(message: str):
|
||||
web_print(message)
|
||||
|
||||
def help_response():
|
||||
help_message: str = "Commands:\nhelp - show this message\clear - clear the text log\nsend <text> - sends text to all channels\nchannel <number> - sets the current channel (0-7)\n"
|
||||
web_print(help_message)
|
||||
command_handler: CommandHandler = CommandHandler(web_print)
|
||||
logger = MeshtasticLogger(interface, "logs/mesh_logs.log", channel=1, message_received_callback=web_print)
|
||||
|
||||
logger = MeshtasticLogger(interface, "logs/mesh_logs.log", channel=1, message_received_callback=callback)
|
||||
# -------------------------
|
||||
# Command Callback Handlers
|
||||
# -------------------------
|
||||
def help_response(args: list[str]):
|
||||
return "Commands:\nhelp - show this message\clear - clear the text log\nsend <text> - sends text to all channels\nchannel <number> - sets the current channel (0-7)\n"
|
||||
|
||||
def channel_response(args: list[str]):
|
||||
if(len(args) < 2):
|
||||
web_print(f"Current channel number: {logger.channel}")
|
||||
return
|
||||
channel: int = int(args[1])
|
||||
if channel > 7 or channel < 0:
|
||||
web_print("Channel must be between 0 and 7")
|
||||
else:
|
||||
try:
|
||||
logger.channel = channel
|
||||
except Exception as e:
|
||||
web_print(e)
|
||||
else:
|
||||
web_print(f"Channel set to {channel}")
|
||||
|
||||
def send_response(args: list[str], command: str):
|
||||
if len(args) < 2:
|
||||
web_print("Please provide text to send")
|
||||
else:
|
||||
logger.send(command[5:])
|
||||
web_print(f"Sent: {command[5:]}")
|
||||
|
||||
def parse_command(command):
|
||||
args = command.split(" ")
|
||||
if len(args) < 1:
|
||||
return True
|
||||
|
||||
if args[0] == "clear":
|
||||
messages.clear()
|
||||
elif args[0] == "help":
|
||||
help_response()
|
||||
elif args[0] == "channel":
|
||||
channel_response(args)
|
||||
elif args[0] == "send":
|
||||
send_response(args, command)
|
||||
else:
|
||||
web_print("Command not recognized. Type \'help\' for a list of commands.")
|
||||
return f"Current channel number: {logger.channel}"
|
||||
channel: int = int(args[0])
|
||||
if channel > 7 or channel < 0:
|
||||
return "Channel must be between 0 and 7"
|
||||
|
||||
try:
|
||||
logger.channel = channel
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
return f"Channel set to {channel}"
|
||||
|
||||
def send_response(args: list[str]):
|
||||
if len(args) == 0:
|
||||
return "Please provide text to send"
|
||||
|
||||
command = ""
|
||||
for arg in args:
|
||||
command += arg + " "
|
||||
|
||||
command = command[:-1] # remove the trailing space
|
||||
|
||||
logger.send(command)
|
||||
return_message = f"Sent: {command}"
|
||||
return return_message
|
||||
|
||||
def clear_response(args):
|
||||
messages.clear()
|
||||
return None
|
||||
|
||||
# register callbacks
|
||||
command_handler.register_callback("clear", clear_response)
|
||||
command_handler.register_callback("help", help_response)
|
||||
command_handler.register_callback("channel", channel_response)
|
||||
command_handler.register_callback("send", send_response)
|
||||
|
||||
# -----------------------
|
||||
# Flask Callback Handlers
|
||||
# -----------------------
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
@@ -78,12 +85,10 @@ def index():
|
||||
def send():
|
||||
data = request.json
|
||||
message = data.get("message", "")
|
||||
parse_command(message)
|
||||
command_handler.parse_command(message)
|
||||
return jsonify({"messages": messages})
|
||||
|
||||
@app.route('/messages', methods=['GET'])
|
||||
def get_messages():
|
||||
return jsonify({"messages": messages})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host="0.0.0.0", port=80, debug=True)
|
||||
33
install.sh
Executable file
33
install.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
CURRENT_DIRECTORY=$(pwd)
|
||||
echo "Current directory: $CURRENT_DIRECTORY"
|
||||
|
||||
cd $CURRENT_DIRECTORY
|
||||
|
||||
if test -f "meshtastic"; then
|
||||
echo "Virtual environment already exists. Skipping venv creation."
|
||||
else
|
||||
echo "Making virtual environment..."
|
||||
python3 -m venv meshtastic
|
||||
fi
|
||||
|
||||
echo "Installing venv requirements"
|
||||
meshtastic/bin/python -m pip install -r requirements.txt
|
||||
|
||||
echo "Generating service file from template"
|
||||
|
||||
read -p "Do you want to continue? (y/n): " choice
|
||||
|
||||
# Check if the user typed 'n' or 'N'
|
||||
if [[ "$choice" == "n" || "$choice" == "N" ]]; then
|
||||
echo "Exiting the script..."
|
||||
exit 0
|
||||
fi
|
||||
echo "Continuing...\n Creating Service file..."
|
||||
# Replace all instances of ${WORKING_DIRECTORY} with the actual working directory and write to the new file
|
||||
sed "s|\${WORKING_DIRECTORY}|$CURRENT_DIRECTORY|g" "service-template/MeshtasticLogger-template.service" > "MeshtasticLogger.service"
|
||||
|
||||
echo "Deploying service"
|
||||
sudo cp "$CURRENT_DIRECTORY/MeshtasticLogger.service" /etc/systemd/system/ && sudo systemctl daemon-reload && sudo systemctl start MeshtasticLogger
|
||||
|
||||
|
||||
14
service-template/MeshtasticLogger-template.service
Normal file
14
service-template/MeshtasticLogger-template.service
Normal file
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=A simple chat interface and autoresponder for meshtastic
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
WorkingDirectory=${WORKING_DIRECTORY}/
|
||||
ExecStart=sudo ${WORKING_DIRECTORY}/meshtastic/bin/python -m flask --app ${WORKING_DIRECTORY}/app.py run --host=0.0.0.0 --port=80
|
||||
Restart=always
|
||||
User=quinn
|
||||
StandardOutput=append:${WORKING_DIRECTORY}/logs/meshtastic_system.log
|
||||
StandardError=append:${WORKING_DIRECTORY}/logs/meshtastic_system_error.log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user