lora_sync/main.py

148 lines
4.9 KiB
Python
Raw Permalink Normal View History

import time
import os
import meshtastic
import meshtastic.tcp_interface
from pubsub import pub
from send_to_irc import SendMsgBot
2024-06-13 14:09:22 -07:00
import json
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
MESSAGE_FILE = "message.txt"
def send_message_from_mesh():
2024-06-13 14:09:22 -07:00
print('send_message_from_mesh')
if os.path.exists(MESSAGE_FILE):
with open(MESSAGE_FILE, 'r') as file:
message = file.read().strip()
if message:
# If you dont have irc...then you can just print/hook into your service here.
xmpp = SendMsgBot('mesh_bot@website.com', 'totallyrealpassword', 'accounttosend@website.com', message)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
2024-06-13 14:09:22 -07:00
xmpp.process(forever=True)
# Clear the file after sending the message
open(MESSAGE_FILE, 'w').close()
2024-06-13 14:09:22 -07:00
print('done sending')
def onReceive(packet, interface):
# called when a packet arrives
2024-06-13 14:09:22 -07:00
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']
if 'TEXT_MESSAGE_APP' in packet['decoded']:
print('TEXT_MESSAGE_APP')
2024-06-09 11:36:02 -07:00
if 'portnum' in decoded:
app_name = decoded['portnum']
if app_name == 'TELEMETRY_APP':
with open(MESSAGE_FILE, 'w') as file:
2024-06-13 14:09:22 -07:00
file.write(message)
2024-06-09 11:36:02 -07:00
file.write(f"TELEMETRY_APP: ")
2024-06-13 14:09:22 -07:00
# file.write(str(packet))
print('TELEMETRY_APP found')
2024-06-09 11:36:02 -07:00
elif app_name == 'POSITION_APP':
with open(MESSAGE_FILE, 'w') as file:
2024-06-13 14:09:22 -07:00
file.write(message)
2024-06-09 11:36:02 -07:00
file.write(f"POSITION_APP: ")
2024-06-13 14:09:22 -07:00
# 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')
2024-06-09 11:36:02 -07:00
else:
2024-06-13 14:09:22 -07:00
print('Not a recognized portnum message')
2024-06-09 11:36:02 -07:00
print(packet)
if 'decoded' in packet and 'text' in packet['decoded']:
2024-06-13 14:09:22 -07:00
# print(packet)
txt_message = packet['decoded']['text']
# Write message to file
with open(MESSAGE_FILE, 'w') as file:
file.write(message)
2024-06-13 14:09:22 -07:00
file.write(txt_message)
# Notify send_message function
# pub.sendMessage('send_to_irc')
2024-06-13 14:09:22 -07:00
print(f"text message here: {txt_message}")
else:
print('Not the text message')
2024-06-13 14:09:22 -07:00
# print(packet)
2024-06-13 14:09:22 -07:00
# 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
# pub.subscribe(send_message, 'send_to_irc')
# This does not work ATM, so doing a quick workaround.
# Initialize the meshtastic interface
2024-06-13 14:09:22 -07:00
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")
2024-06-13 14:09:22 -07:00
# pub.subscribe(onReceive2, "meshtastic.receive")
# Keep the script running to process incoming messages
try:
while True:
time.sleep(1)
send_message_from_mesh()
except KeyboardInterrupt:
pass
finally:
interface.close()