Get node packet info
This commit is contained in:
parent
b9d09f2209
commit
d2a6a14519
2 changed files with 94 additions and 11 deletions
79
main.py
79
main.py
|
@ -4,10 +4,15 @@ import meshtastic
|
|||
import meshtastic.tcp_interface
|
||||
from pubsub import pub
|
||||
from send_to_irc import SendMsgBot
|
||||
import json
|
||||
|
||||
from signal import signal, SIGPIPE, SIG_DFL
|
||||
signal(SIGPIPE,SIG_DFL)
|
||||
|
||||
MESSAGE_FILE = "message.txt"
|
||||
|
||||
def send_message_from_mesh():
|
||||
print('send_message_from_mesh')
|
||||
if os.path.exists(MESSAGE_FILE):
|
||||
with open(MESSAGE_FILE, 'r') as file:
|
||||
message = file.read().strip()
|
||||
|
@ -20,13 +25,39 @@ def send_message_from_mesh():
|
|||
|
||||
# Connect to the XMPP server and start processing XMPP stanzas.
|
||||
xmpp.connect()
|
||||
xmpp.process(forever=False)
|
||||
xmpp.process(forever=True)
|
||||
|
||||
# Clear the file after sending the message
|
||||
open(MESSAGE_FILE, 'w').close()
|
||||
print('done sending')
|
||||
|
||||
def onReceive(packet, interface):
|
||||
# called when a packet arrives
|
||||
print('******************************************')
|
||||
nodes = interface.nodesByNum
|
||||
# fromNode = nodes[packet['from']]
|
||||
# toNode = nodes[packet['to']]
|
||||
# TODO: REFACTOR, THIS IS UGLY AS F
|
||||
message = ''
|
||||
if packet['from'] in nodes:
|
||||
fromNode = nodes[packet['from']]
|
||||
message += f"From SN: {fromNode['user']['shortName']} LN: {fromNode['user']['longName']} \n"
|
||||
# print(f"From SN: {fromNode['user']['shortName']} LN: {fromNode['user']['longName']}")
|
||||
|
||||
if packet['to'] in nodes:
|
||||
toNode = nodes[packet['to']]
|
||||
message += f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']} \n"
|
||||
# print(f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']}")
|
||||
else:
|
||||
message += f"To SN: Unknown LN: Unknown \n"
|
||||
|
||||
if message:
|
||||
print(message)
|
||||
# print(f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']}")
|
||||
# fromNode = interface.getNode(nodeId=packet['from'])
|
||||
# print(fromNode.getLongName())
|
||||
# toNode = interface.getNode(nodeId=packet['to'])
|
||||
|
||||
if 'decoded' in packet:
|
||||
print('decoded')
|
||||
decoded = packet['decoded']
|
||||
|
@ -38,34 +69,50 @@ def onReceive(packet, interface):
|
|||
app_name = decoded['portnum']
|
||||
if app_name == 'TELEMETRY_APP':
|
||||
with open(MESSAGE_FILE, 'w') as file:
|
||||
file.write(message)
|
||||
file.write(f"TELEMETRY_APP: ")
|
||||
file.write(str(packet))
|
||||
print('Telemetry found')
|
||||
# file.write(str(packet))
|
||||
print('TELEMETRY_APP found')
|
||||
elif app_name == 'POSITION_APP':
|
||||
with open(MESSAGE_FILE, 'w') as file:
|
||||
file.write(message)
|
||||
file.write(f"POSITION_APP: ")
|
||||
file.write(str(packet))
|
||||
print('Position found')
|
||||
# file.write(str(packet))
|
||||
print('POSITION_APP found')
|
||||
elif app_name == 'NODEINFO_APP':
|
||||
with open(MESSAGE_FILE, 'w') as file:
|
||||
file.write(message)
|
||||
file.write(f"NODEINFO_APP: ")
|
||||
# file.write(str(packet))
|
||||
print('NODEINFO_APP found')
|
||||
else:
|
||||
print('Not a recodnized portnum message')
|
||||
print('Not a recognized portnum message')
|
||||
print(packet)
|
||||
|
||||
if 'decoded' in packet and 'text' in packet['decoded']:
|
||||
print(packet)
|
||||
message = packet['decoded']['text']
|
||||
# print(packet)
|
||||
txt_message = packet['decoded']['text']
|
||||
|
||||
# Write message to file
|
||||
with open(MESSAGE_FILE, 'w') as file:
|
||||
file.write(message)
|
||||
file.write(txt_message)
|
||||
|
||||
# Notify send_message function
|
||||
# pub.sendMessage('send_to_irc')
|
||||
|
||||
print(f"text message here: {message}")
|
||||
print(f"text message here: {txt_message}")
|
||||
else:
|
||||
print('Not the text message')
|
||||
print(packet)
|
||||
# print(packet)
|
||||
|
||||
# def onReceive2(packet, interface2):
|
||||
# onReceive(packet, interface2)
|
||||
|
||||
def onConnection(interface, topic=pub.AUTO_TOPIC):
|
||||
pass
|
||||
|
||||
pub.subscribe(onConnection, "meshtastic.connection.established")
|
||||
|
||||
|
||||
# Subscribe send_message function to the send_to_irc topic
|
||||
|
@ -73,12 +120,22 @@ def onReceive(packet, interface):
|
|||
# This does not work ATM, so doing a quick workaround.
|
||||
|
||||
# Initialize the meshtastic interface
|
||||
interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.102') # repeater
|
||||
interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.104') # mobile
|
||||
# interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.102') # repeater
|
||||
interface.showInfo()
|
||||
# # interface.getNode()
|
||||
# tables = interface.showNodes()
|
||||
# nodes = interface.nodesByNum
|
||||
# print(nodes)
|
||||
# interface.getNode()
|
||||
# interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.104') # mobile
|
||||
# send message from client ip to repeater.
|
||||
|
||||
# Subscribe onReceive function to the meshtastic.receive topic
|
||||
pub.subscribe(onReceive, "meshtastic.receive")
|
||||
|
||||
# pub.subscribe(onReceive2, "meshtastic.receive")
|
||||
|
||||
# Keep the script running to process incoming messages
|
||||
try:
|
||||
while True:
|
||||
|
|
26
run_script.sh
Executable file
26
run_script.sh
Executable file
|
@ -0,0 +1,26 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Set the maximum number of retries
|
||||
max_retries=5
|
||||
retries=0
|
||||
|
||||
while [ $retries -lt $max_retries ]; do
|
||||
# Run your Python script
|
||||
python main.py
|
||||
|
||||
# Check the exit code of the Python script
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error occurred. Retrying..."
|
||||
retries=$((retries + 1))
|
||||
else
|
||||
echo "Script executed successfully."
|
||||
retries=0
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $retries -eq $max_retries ]; then
|
||||
echo "Max retries reached. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
Loading…
Reference in a new issue