-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmesh.py
executable file
·202 lines (186 loc) · 6.61 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
"""Meshtastic Telegram Gateway"""
#
import argparse
import logging
import os
import sys
import time
#
import reverse_geocoder as rg
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
#
from mtg.bot.meshtastic import MeshtasticBot
from mtg.bot.openai import OpenAIBot
from mtg.bot.telegram import TelegramBot
from mtg.config import Config
from mtg.connection.aprs import APRSStreamer
from mtg.connection.meshtastic import FIFO, FIFO_CMD
from mtg.connection.mqtt import MQTT, MQTTHandler
from mtg.connection.rich import RichConnection
from mtg.connection.telegram import TelegramConnection
from mtg.database import sql_debug, MeshtasticDB
from mtg.filter import CallSignFilter, MeshtasticFilter, TelegramFilter
from mtg.log import setup_logger, LOGFORMAT
from mtg.utils import create_fifo, ExternalPlugins
from mtg.webapp import WebServer
#
from mtg.utils.rf.prefixes import ITUPrefix
def before_send(event, hint):
"""
Check Sentry event before sending it. Does nothing atm.
"""
print(event, hint)
return event
# pylint:disable=too-many-locals,too-many-statements
def main(args):
"""
Main function :)
:return:
"""
config = Config(config_path=args.config)
config.read()
level = logging.INFO
debug = False
if config.enforce_type(bool, config.DEFAULT.Debug):
debug = True
level = logging.DEBUG
sql_debug()
# setup APM
if config.enforce_type(bool, config.DEFAULT.SentryEnabled):
sentry_sdk.init(dsn=config.DEFAULT.SentryDSN,
debug=debug,
traces_sample_rate=1.0,
before_send=before_send,
integrations=[FlaskIntegration()]
)
# warm up reverse cache
rg.search((50.5, 30.5), verbose=debug)
# our logger
logger = setup_logger('mesh', level)
# meshtastic logger
logging.basicConfig(level=level,
format=LOGFORMAT)
basedir = os.path.abspath(os.path.dirname(__file__))
#
database = MeshtasticDB(os.path.join(basedir, config.Meshtastic.DatabaseFile), logger)
meshtastic_filter = MeshtasticFilter(database, config, logger)
#
telegram_connection = TelegramConnection(config.Telegram.Token, logger)
meshtastic_connection = RichConnection(config.Meshtastic.Device, logger, config, meshtastic_filter,
database, rg_fn=rg.search)
database.set_meshtastic(meshtastic_connection)
meshtastic_connection.connect()
#
mqtt_connection = MQTT(config.MQTT.Topic, config.MQTT.Host, config.MQTT.User, config.MQTT.Password,
logger, config.enforce_type(int, config.MQTT.Port))
mqtt_connection.set_config(config)
mqtt_handler = MQTTHandler(config.MQTT.Topic, logger)
mqtt_connection.set_handler(mqtt_handler.handler)
mqtt_handler.set_node_callback(meshtastic_connection.on_mqtt_node)
#
itu_prefix = ITUPrefix(logger)
#
aprs_streamer = APRSStreamer(config, itu_prefix)
call_sign_filter = CallSignFilter(database, config, logger)
aprs_streamer.set_db(database)
aprs_streamer.set_filter(call_sign_filter)
aprs_streamer.set_logger(logger)
aprs_streamer.set_meshtastic(meshtastic_connection)
aprs_streamer.set_telegram_connection(telegram_connection)
#
telegram_bot = TelegramBot(config, meshtastic_connection, telegram_connection)
telegram_filter = TelegramFilter(database, config, logger)
telegram_bot.set_aprs(aprs_streamer)
telegram_bot.set_filter(telegram_filter)
telegram_bot.set_logger(logger)
#
open_ai = OpenAIBot(logger)
meshtastic_bot = MeshtasticBot(database, config, meshtastic_connection, telegram_connection, open_ai)
# set filter for MQTT
mqtt_handler.set_filter(meshtastic_filter)
meshtastic_bot.set_filter(meshtastic_filter)
meshtastic_bot.set_logger(logger)
meshtastic_bot.set_aprs(aprs_streamer)
meshtastic_bot.subscribe()
#
template_folder = os.path.join(basedir, "web", "templates")
static_folder = os.path.join(basedir, "web", "static")
web_server = WebServer(database, config,
meshtastic_connection,
telegram_connection,
logger,
static_folder=static_folder,
template_folder=template_folder)
# external plugins
external_plugins = ExternalPlugins(database, config, meshtastic_connection, telegram_connection, logger)
# non-blocking
aprs_streamer.run()
# FIFO watcher
meshtastic_connection.run()
web_server.run()
telegram_bot.run()
mqtt_connection.run()
external_plugins.run()
# blocking
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
aprs_streamer.shutdown()
web_server.shutdown()
meshtastic_connection.shutdown()
mqtt_connection.shutdown()
telegram_bot.shutdown()
logger.info('Exit requested...')
sys.exit(0)
def post2mesh(args):
"""
post2mesh - send messages from console using Meshtastic networks. For alerts etc
:return:
"""
if args.message is None:
logging.error('Cannot send empty message...')
return
create_fifo(FIFO)
with open(FIFO, 'w', encoding='utf-8') as fifo:
fifo.write(args.message + '\n')
def post_cmd(args):
"""
post_cmd - send commands to Meshtastic connection
:param args:
:return:
"""
if args.command is None:
logging.error('Cannot send empty command...')
return
create_fifo(FIFO_CMD)
with open(FIFO_CMD, 'w', encoding='utf-8') as fifo:
fifo.write(args.command + '\n')
def cmd():
"""
cmd - Run argument parser and process command line parameters
:return:
"""
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(title="commands", help="commands")
post = subparser.add_parser("post2mesh", help="site command")
post.add_argument("-m", "--message", help="message to post to Meshtastic")
post.set_defaults(func=post2mesh)
#
run = subparser.add_parser("run", help="run")
run.add_argument("-c", "--config", help="path to config", default="./mesh.ini")
run.set_defaults(func=main)
#
reboot = subparser.add_parser("command", help="Send command")
reboot.add_argument("-c", "--command", help="Send command")
reboot.set_defaults(func=post_cmd)
#
argv = sys.argv[1:]
if len(argv) == 0:
argv = ['run']
args = parser.parse_args(argv)
logging.info(args.func(args))
if __name__ == '__main__':
cmd()