mirror of
https://github.com/geoffwhittington/meshtastic-bridge.git
synced 2026-07-13 13:10:56 +02:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 159b3b097d | |||
| dd83f29806 | |||
| 26d540fead | |||
| b72102c402 | |||
| 0930aa917e | |||
| 7ad584830a | |||
| fe5e119059 | |||
| 23c1250ccb | |||
| c5134fd5b4 | |||
| c3ff3b3a4f | |||
| 0f1e5a908d | |||
| 74114f1acc |
@@ -4,21 +4,16 @@ on:
|
|||||||
- "*"
|
- "*"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
docker-buildx:
|
||||||
name: Build, push
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout main
|
- name: Checkout main
|
||||||
uses: actions/checkout@main
|
uses: actions/checkout@main
|
||||||
|
|
||||||
- name: Build container
|
- name: Build and publish image
|
||||||
run: docker build --tag gwhittington/meshtastic-bridge:latest .
|
uses: zmingxie/docker_buildx@master
|
||||||
|
with:
|
||||||
- name: Log in to Container Registry with short-lived credentials
|
publish: true
|
||||||
run: docker login --username=gwhittington --password "${{secrets.DOCKER_HUB}}"
|
imageName: gwhittington/meshtastic-bridge
|
||||||
|
dockerHubUser: gwhittington
|
||||||
- name: Push image to Container Registry
|
dockerHubPassword: ${{secrets.DOCKER_HUB}}
|
||||||
run: docker push gwhittington/meshtastic-bridge:latest
|
|
||||||
|
|
||||||
- name: Logout from Container Registry
|
|
||||||
run: docker logout
|
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ WORKDIR /code
|
|||||||
# copy the dependencies file to the working directory
|
# copy the dependencies file to the working directory
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y cargo
|
||||||
|
|
||||||
# install dependencies
|
# install dependencies
|
||||||
|
RUN pip install -U pip
|
||||||
|
RUN pip install setuptools_rust wheel
|
||||||
RUN pip install -r requirements.txt
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
# copy the content of the local src directory to the working directory
|
# copy the content of the local src directory to the working directory
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ mqtt_servers:
|
|||||||
mqtt-to-radio:
|
mqtt-to-radio:
|
||||||
- decrypt_filter:
|
- decrypt_filter:
|
||||||
key: '/home/user/keys/key.pem'
|
key: '/home/user/keys/key.pem'
|
||||||
- send_plugin:
|
- radio_message_plugin:
|
||||||
device: remote
|
device: remote
|
||||||
pipelines:
|
pipelines:
|
||||||
radio-to-mqtt:
|
radio-to-mqtt:
|
||||||
@@ -105,12 +105,12 @@ The following plugins can be used in the `pipelines` section of `config.yaml`:
|
|||||||
| ----------------- | -------------------------------------------------------------------- |
|
| ----------------- | -------------------------------------------------------------------- |
|
||||||
| `debugger` | Log the packet to the system console |
|
| `debugger` | Log the packet to the system console |
|
||||||
| `message_filter` | Filters out packets from the bridge that match a specific criteria |
|
| `message_filter` | Filters out packets from the bridge that match a specific criteria |
|
||||||
| `distance_filter` | Filters out packets that originate too far from a specified `device` |
|
| `location_filter` | Filters out packets that originate too far from a specified `device` |
|
||||||
| `webhook` | Send HTTP requests with custom payloads using packet information |
|
| `webhook` | Send HTTP requests with custom payloads using packet information |
|
||||||
| `mqtt_plugin` | Send packets to a MQTT server |
|
| `mqtt_plugin` | Send packets to a MQTT server |
|
||||||
| `encrypt_filter` | Encrypt a packet for a desired MQTT recipient |
|
| `encrypt_filter` | Encrypt a packet for a desired MQTT recipient |
|
||||||
| `decrypt_filter` | Decrypt a packet originating from MQTT |
|
| `decrypt_filter` | Decrypt a packet originating from MQTT |
|
||||||
| `send_plugin` | Send a packet to a specified `device` |
|
| `radio_message_plugin` | Send a packet to a specified `device` |
|
||||||
|
|
||||||
### debugger - Output the contents of a packet
|
### debugger - Output the contents of a packet
|
||||||
|
|
||||||
@@ -131,28 +131,37 @@ Useful for troubleshooting.
|
|||||||
- **app** Name of meshtastic application to allow or disallow
|
- **app** Name of meshtastic application to allow or disallow
|
||||||
- **from** The packet `fromId` values to allow or disallow
|
- **from** The packet `fromId` values to allow or disallow
|
||||||
- **to** The packet `toId` values to allow or disallow
|
- **to** The packet `toId` values to allow or disallow
|
||||||
|
- **message** The packet `message` values to allow or disallow. Supports Regex.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```
|
```
|
||||||
message_filter:
|
message_filter:
|
||||||
app:
|
from:
|
||||||
allow:
|
allow:
|
||||||
- !bd5ba0ec
|
- !bd5ba0ec
|
||||||
- !f85bc0bc
|
- !f85bc0bc
|
||||||
disallow:
|
disallow:
|
||||||
- !c15ba2ec
|
- !c15ba2ec
|
||||||
|
message:
|
||||||
|
disallow:
|
||||||
|
- Good night
|
||||||
```
|
```
|
||||||
|
|
||||||
### distance_filter - Allow or block packets based on distance from origin to radio
|
### location_filter - Filter packets by location from current node (default) or specific location
|
||||||
|
|
||||||
- **log_level** `debug` or `info`. Default `info`
|
- **log_level** `debug` or `info`. Default `info`
|
||||||
- **max_distance_km** Number of kilometers
|
- **max_distance_km** Filter packets more than a certain distance
|
||||||
|
- **comparison** `within` or `outside`. Default `within`
|
||||||
|
- **compare_latitude** latitude to compare against
|
||||||
|
- **compare_longitude** longitude to compare against
|
||||||
|
- **latitude** Set the latitude
|
||||||
|
- **longitude** Set the longitude
|
||||||
|
|
||||||
For example:
|
For example
|
||||||
|
|
||||||
```
|
```
|
||||||
distance_filter:
|
location_filter:
|
||||||
max_distance_km: 1000
|
max_distance_km: 1000
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -225,7 +234,7 @@ decrypt_filter:
|
|||||||
key: '/home/user/keys/key.pem'
|
key: '/home/user/keys/key.pem'
|
||||||
```
|
```
|
||||||
|
|
||||||
### send_plugin - Send a packet to a radio
|
### radio_message_plugin - Send a packet to a radio
|
||||||
|
|
||||||
- **log_level** `debug` or `info`. Default `info`
|
- **log_level** `debug` or `info`. Default `info`
|
||||||
- **active** Plugin is active. Values: `true` or `false`. Default `true`.
|
- **active** Plugin is active. Values: `true` or `false`. Default `true`.
|
||||||
@@ -242,7 +251,7 @@ For example:
|
|||||||
Broadcasts all packets to the "remote" radio network that are destined to the node `12354345`.
|
Broadcasts all packets to the "remote" radio network that are destined to the node `12354345`.
|
||||||
|
|
||||||
```
|
```
|
||||||
send_plugin:
|
radio_message_plugin:
|
||||||
device: remote
|
device: remote
|
||||||
node_mapping:
|
node_mapping:
|
||||||
12354345: ^all
|
12354345: ^all
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
from re import I
|
||||||
import meshtastic
|
import meshtastic
|
||||||
import meshtastic.serial_interface
|
import meshtastic.serial_interface
|
||||||
import meshtastic.tcp_interface
|
import meshtastic.tcp_interface
|
||||||
@@ -20,7 +21,15 @@ logger = logging.getLogger(name="meshtastic.bridge")
|
|||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomTCPInterface(meshtastic.tcp_interface.TCPInterface):
|
||||||
|
def __init__(self, hostname, device_name):
|
||||||
|
self.device_name = device_name
|
||||||
|
self.hostname = hostname
|
||||||
|
super(CustomTCPInterface, self).__init__(hostname)
|
||||||
|
|
||||||
|
|
||||||
def onReceive(packet, interface): # called when a packet arrives
|
def onReceive(packet, interface): # called when a packet arrives
|
||||||
|
nodeInfo = interface.getMyNodeInfo()
|
||||||
|
|
||||||
for pipeline, pipeline_plugins in bridge_config["pipelines"].items():
|
for pipeline, pipeline_plugins in bridge_config["pipelines"].items():
|
||||||
logger.debug(f"Pipeline {pipeline} initiated")
|
logger.debug(f"Pipeline {pipeline} initiated")
|
||||||
@@ -29,6 +38,9 @@ def onReceive(packet, interface): # called when a packet arrives
|
|||||||
pipeline_packet = p.do_action(packet)
|
pipeline_packet = p.do_action(packet)
|
||||||
|
|
||||||
for plugin in pipeline_plugins:
|
for plugin in pipeline_plugins:
|
||||||
|
if not pipeline_packet:
|
||||||
|
continue
|
||||||
|
|
||||||
for plugin_key, plugin_config in plugin.items():
|
for plugin_key, plugin_config in plugin.items():
|
||||||
|
|
||||||
logger.debug(f"Processing plugin: {pipeline}/{plugin_key}")
|
logger.debug(f"Processing plugin: {pipeline}/{plugin_key}")
|
||||||
@@ -57,8 +69,17 @@ def onConnection(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def onLost(interface):
|
||||||
|
logger.debug(f"Connecting to {interface.hostname} ...")
|
||||||
|
devices[interface.device_name] = CustomTCPInterface(
|
||||||
|
hostname=interface.hostname, device_name=interface.device_name
|
||||||
|
)
|
||||||
|
logger.debug(f"Connected to {interface.hostname}")
|
||||||
|
|
||||||
|
|
||||||
pub.subscribe(onReceive, "meshtastic.receive")
|
pub.subscribe(onReceive, "meshtastic.receive")
|
||||||
pub.subscribe(onConnection, "meshtastic.connection.established")
|
pub.subscribe(onConnection, "meshtastic.connection.established")
|
||||||
|
pub.subscribe(onLost, "meshtastic.connection.lost")
|
||||||
|
|
||||||
with open("config.yaml") as f:
|
with open("config.yaml") as f:
|
||||||
bridge_config = yaml.load(f, Loader=SafeLoader)
|
bridge_config = yaml.load(f, Loader=SafeLoader)
|
||||||
@@ -75,99 +96,121 @@ for device in bridge_config["devices"]:
|
|||||||
devPath=device["serial"]
|
devPath=device["serial"]
|
||||||
)
|
)
|
||||||
elif "tcp" in device:
|
elif "tcp" in device:
|
||||||
devices[device["name"]] = meshtastic.tcp_interface.TCPInterface(
|
logger.debug(f"Connecting to {device['tcp']} ...")
|
||||||
hostname=device["tcp"]
|
devices[device["name"]] = CustomTCPInterface(
|
||||||
|
hostname=device["tcp"], device_name=device["name"]
|
||||||
)
|
)
|
||||||
|
logger.debug(f"Connected to {device['tcp']}")
|
||||||
else:
|
else:
|
||||||
devices[device["name"]] = meshtastic.serial_interface.SerialInterface()
|
devices[device["name"]] = meshtastic.serial_interface.SerialInterface()
|
||||||
|
|
||||||
for config in bridge_config["mqtt_servers"]:
|
if "mqtt_servers" in bridge_config:
|
||||||
required_options = [
|
for config in bridge_config["mqtt_servers"]:
|
||||||
"name",
|
required_options = [
|
||||||
"server",
|
"name",
|
||||||
"port",
|
"server",
|
||||||
]
|
"port",
|
||||||
|
]
|
||||||
|
|
||||||
for option in required_options:
|
for option in required_options:
|
||||||
if option not in config:
|
if option not in config:
|
||||||
logger.warning("Missing config: {option}")
|
logger.warning("Missing config: {option}")
|
||||||
|
|
||||||
client_id = config["client_id"] if "client_id" in config else None
|
client_id = config["client_id"] if "client_id" in config else None
|
||||||
username = config["username"] if "username" in config else None
|
username = config["username"] if "username" in config else None
|
||||||
password = config["password"] if "password" in config else None
|
password = config["password"] if "password" in config else None
|
||||||
|
|
||||||
if client_id:
|
logger.info(f"Connected to MQTT {config['name']}")
|
||||||
mqttc = mqtt.Client(client_id)
|
|
||||||
else:
|
|
||||||
mqttc = mqtt.Client()
|
|
||||||
|
|
||||||
if username and password:
|
if client_id:
|
||||||
mqttc.username_pw_set(username, password)
|
mqttc = mqtt.Client(client_id)
|
||||||
|
else:
|
||||||
|
mqttc = mqtt.Client()
|
||||||
|
|
||||||
mqtt_servers[config["name"]] = mqttc
|
if username and password:
|
||||||
|
mqttc.username_pw_set(username, password)
|
||||||
|
|
||||||
def on_connect(mqttc, obj, flags, rc):
|
def on_connect(mqttc, obj, flags, rc):
|
||||||
logger.debug(f"Connected to MQTT {config['name']}")
|
logger.debug(f"Connected to MQTT {config['name']}")
|
||||||
|
|
||||||
def on_message(mqttc, obj, msg):
|
def on_message(mqttc, obj, msg):
|
||||||
packet = msg.payload.decode()
|
orig_packet = msg.payload.decode()
|
||||||
|
|
||||||
logger.debug(f"MQTT {config['name']}: on_message")
|
logger.debug(f"MQTT {config['name']}: on_message")
|
||||||
|
logger.debug(f"MQTT {config['name']}: {orig_packet}")
|
||||||
|
|
||||||
if "pipelines" not in config:
|
if "pipelines" not in config:
|
||||||
logger.warning(f"MQTT {config['name']}: no pipeline")
|
logger.warning(f"MQTT {config['name']}: no pipeline")
|
||||||
return
|
return
|
||||||
|
|
||||||
for pipeline, pipeline_plugins in config["pipelines"].items():
|
p = plugins["packet_filter"]
|
||||||
|
pipeline_packet = p.do_action(orig_packet)
|
||||||
|
|
||||||
logger.debug(f"MQTT {config['name']} pipeline {pipeline} started")
|
for pipeline, pipeline_plugins in config["pipelines"].items():
|
||||||
if not packet:
|
|
||||||
continue
|
|
||||||
|
|
||||||
for plugin in pipeline_plugins:
|
packet = pipeline_packet
|
||||||
for plugin_key, plugin_config in plugin.items():
|
|
||||||
if plugin_key not in plugins:
|
logger.debug(f"MQTT {config['name']} pipeline {pipeline} initiated")
|
||||||
logger.error(f"No such plugin: {plugin_key}. Skipping")
|
if not packet:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for plugin in pipeline_plugins:
|
||||||
|
if not packet:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
p = plugins[plugin_key]
|
for plugin_key, plugin_config in plugin.items():
|
||||||
p.configure(devices, mqtt_servers, plugin_config)
|
if plugin_key not in plugins:
|
||||||
|
logger.error(f"No such plugin: {plugin_key}. Skipping")
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
p = plugins[plugin_key]
|
||||||
packet = p.do_action(packet)
|
p.configure(devices, mqtt_servers, plugin_config)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Hit an error: {e}", exc_info=True)
|
|
||||||
logger.debug(f"MQTT {config['name']} pipeline {pipeline} finished")
|
|
||||||
|
|
||||||
def on_publish(mqttc, obj, mid):
|
try:
|
||||||
logger.debug(f"MQTT {config['name']}: on_publish: {mid}")
|
packet = p.do_action(packet)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Hit an error: {e}", exc_info=True)
|
||||||
|
logger.debug(f"MQTT {config['name']} pipeline {pipeline} finished")
|
||||||
|
|
||||||
def on_subscribe(mqttc, obj, mid, granted_qos):
|
def on_publish(mqttc, obj, mid):
|
||||||
logger.debug(f"MQTT {config['name']}: on_subscribe: {mid}")
|
logger.debug(f"MQTT {config['name']}: on_publish: {mid}")
|
||||||
|
|
||||||
mqttc.on_message = on_message
|
def on_subscribe(mqttc, obj, mid, granted_qos):
|
||||||
mqttc.on_connect = on_connect
|
logger.debug(f"MQTT {config['name']}: on_subscribe: {mid}")
|
||||||
mqttc.on_publish = on_publish
|
|
||||||
mqttc.on_subscribe = on_subscribe
|
|
||||||
|
|
||||||
import ssl
|
mqttc.on_message = on_message
|
||||||
|
mqttc.on_connect = on_connect
|
||||||
|
mqttc.on_publish = on_publish
|
||||||
|
mqttc.on_subscribe = on_subscribe
|
||||||
|
|
||||||
if "insecure" in config and config["insecure"]:
|
mqtt_servers[config["name"]] = mqttc
|
||||||
mqttc.tls_set(cert_reqs=ssl.CERT_NONE)
|
|
||||||
mqttc.tls_insecure_set(True)
|
|
||||||
|
|
||||||
mqttc.connect(config["server"], config["port"], 60)
|
import ssl
|
||||||
|
|
||||||
if "topic" in config:
|
if "insecure" in config and config["insecure"]:
|
||||||
mqttc.subscribe(config["topic"], 0)
|
mqttc.tls_set(cert_reqs=ssl.CERT_NONE)
|
||||||
|
mqttc.tls_insecure_set(True)
|
||||||
|
|
||||||
mqttc.loop_start()
|
try:
|
||||||
|
logger.debug(f"Connecting to MQTT {config['server']}")
|
||||||
|
|
||||||
|
mqttc.connect(config["server"], config["port"], 60)
|
||||||
|
|
||||||
|
if "topic" in config:
|
||||||
|
mqttc.subscribe(config["topic"], 0)
|
||||||
|
|
||||||
|
mqttc.loop_start()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"MQTT {config['name']} could not start: {e}")
|
||||||
|
pass
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1000)
|
time.sleep(1000)
|
||||||
|
|
||||||
for device, instance in devices.items():
|
if devices:
|
||||||
instance.close()
|
for device, instance in devices.items():
|
||||||
|
instance.close()
|
||||||
|
|
||||||
for server, instance in mqtt_servers.items():
|
if mqtt_servers:
|
||||||
instance.disconnect()
|
for server, instance in mqtt_servers.items():
|
||||||
|
instance.disconnect()
|
||||||
|
|||||||
+142
-78
@@ -1,16 +1,20 @@
|
|||||||
from haversine import haversine
|
from haversine import haversine
|
||||||
from meshtastic import mesh_pb2
|
from meshtastic import mesh_pb2
|
||||||
from meshtastic.__init__ import BROADCAST_ADDR
|
from random import randrange
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
plugins = {}
|
plugins = {}
|
||||||
|
|
||||||
|
|
||||||
class Plugin:
|
class Plugin(object):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
def configure(self, devices, mqtt_servers, config):
|
def configure(self, devices, mqtt_servers, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.devices = devices
|
self.devices = devices
|
||||||
@@ -29,25 +33,42 @@ class Plugin:
|
|||||||
class PacketFilter(Plugin):
|
class PacketFilter(Plugin):
|
||||||
logger = logging.getLogger(name="meshtastic.bridge.filter.packet")
|
logger = logging.getLogger(name="meshtastic.bridge.filter.packet")
|
||||||
|
|
||||||
def strip_raw(self, dict_obj):
|
def strip_raw(self, data):
|
||||||
|
if type(data) is not dict:
|
||||||
|
return data
|
||||||
|
|
||||||
|
if "raw" in data:
|
||||||
|
del data["raw"]
|
||||||
|
|
||||||
|
for k, v in data.items():
|
||||||
|
data[k] = self.strip_raw(v)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
def normalize(self, dict_obj):
|
||||||
|
"""
|
||||||
|
Packets are either a dict, string dict or string
|
||||||
|
"""
|
||||||
if type(dict_obj) is not dict:
|
if type(dict_obj) is not dict:
|
||||||
return dict_obj
|
try:
|
||||||
|
dict_obj = json.loads(dict_obj)
|
||||||
|
except:
|
||||||
|
dict_obj = {"decoded": {"text": dict_obj}}
|
||||||
|
|
||||||
if "raw" in dict_obj:
|
return self.strip_raw(dict_obj)
|
||||||
del dict_obj["raw"]
|
|
||||||
|
|
||||||
for k, v in dict_obj.items():
|
|
||||||
dict_obj[k] = self.strip_raw(v)
|
|
||||||
|
|
||||||
return dict_obj
|
|
||||||
|
|
||||||
def do_action(self, packet):
|
def do_action(self, packet):
|
||||||
packet = self.strip_raw(packet)
|
self.logger.debug(f"Before normalization: {packet}")
|
||||||
|
packet = self.normalize(packet)
|
||||||
|
|
||||||
if "decoded" in packet and "payload" in packet["decoded"]:
|
if "decoded" in packet and "payload" in packet["decoded"]:
|
||||||
packet["decoded"]["payload"] = base64.b64encode(
|
if type(packet["decoded"]["payload"]) is bytes:
|
||||||
packet["decoded"]["payload"]
|
text = packet["decoded"]["payload"]
|
||||||
).decode("utf-8")
|
packet["decoded"]["payload"] = base64.b64encode(
|
||||||
|
packet["decoded"]["payload"]
|
||||||
|
).decode("utf-8")
|
||||||
|
|
||||||
|
self.logger.debug(f"After normalization: {packet}")
|
||||||
|
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
@@ -74,6 +95,33 @@ class MessageFilter(Plugin):
|
|||||||
self.logger.error("Missing packet")
|
self.logger.error("Missing packet")
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
|
text = packet["decoded"]["text"] if "text" in packet["decoded"] else None
|
||||||
|
|
||||||
|
if text and "message" in self.config:
|
||||||
|
if "allow" in self.config["message"]:
|
||||||
|
matches = False
|
||||||
|
for allow_regex in self.config["message"]["allow"]:
|
||||||
|
if not matches and re.search(allow_regex, text):
|
||||||
|
matches = True
|
||||||
|
|
||||||
|
if not matches:
|
||||||
|
self.logger.debug(
|
||||||
|
f"Dropped because it doesn't match message allow filter"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
if "disallow" in self.config["message"]:
|
||||||
|
matches = False
|
||||||
|
for disallow_regex in self.config["message"]["disallow"]:
|
||||||
|
if not matches and re.search(disallow_regex, text):
|
||||||
|
matches = True
|
||||||
|
|
||||||
|
if matches:
|
||||||
|
self.logger.debug(
|
||||||
|
f"Dropped because it matches message disallow filter"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
filters = {
|
filters = {
|
||||||
"app": packet["decoded"]["portnum"],
|
"app": packet["decoded"]["portnum"],
|
||||||
"from": packet["fromId"],
|
"from": packet["fromId"],
|
||||||
@@ -83,12 +131,15 @@ class MessageFilter(Plugin):
|
|||||||
for filter_key, value in filters.items():
|
for filter_key, value in filters.items():
|
||||||
if filter_key in self.config:
|
if filter_key in self.config:
|
||||||
filter_val = self.config[filter_key]
|
filter_val = self.config[filter_key]
|
||||||
|
|
||||||
if (
|
if (
|
||||||
"allow" in filter_val
|
"allow" in filter_val
|
||||||
and filter_val["allow"]
|
and filter_val["allow"]
|
||||||
and value not in filter_val["allow"]
|
and value not in filter_val["allow"]
|
||||||
):
|
):
|
||||||
self.logger.debug(f"Dropped because it doesn't match allow filter")
|
self.logger.debug(
|
||||||
|
f"Dropped because {value} doesn't match {filter_key} allow filter"
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -96,7 +147,9 @@ class MessageFilter(Plugin):
|
|||||||
and filter_val["disallow"]
|
and filter_val["disallow"]
|
||||||
and value in filter_val["disallow"]
|
and value in filter_val["disallow"]
|
||||||
):
|
):
|
||||||
self.logger.debug(f"Dropped because it matches disallow filter")
|
self.logger.debug(
|
||||||
|
f"Dropped because {value} matches {filter_key} disallow filter"
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.logger.debug(f"Accepted")
|
self.logger.debug(f"Accepted")
|
||||||
@@ -106,21 +159,24 @@ class MessageFilter(Plugin):
|
|||||||
plugins["message_filter"] = MessageFilter()
|
plugins["message_filter"] = MessageFilter()
|
||||||
|
|
||||||
|
|
||||||
class DistanceFilter(Plugin):
|
class LocationFilter(Plugin):
|
||||||
logger = logging.getLogger(name="meshtastic.bridge.filter.distance")
|
logger = logging.getLogger(name="meshtastic.bridge.filter.distance")
|
||||||
|
|
||||||
def do_action(self, packet):
|
def do_action(self, packet):
|
||||||
if "device" not in self.config:
|
|
||||||
return packet
|
|
||||||
|
|
||||||
if "position" not in packet["decoded"]:
|
|
||||||
return packet
|
|
||||||
|
|
||||||
message_source_position = None
|
message_source_position = None
|
||||||
current_local_position = None
|
current_local_position = None
|
||||||
|
|
||||||
|
if "device" in self.config and self.config["device"] in self.devices:
|
||||||
|
nodeInfo = self.devices[self.config["device"]].getMyNodeInfo()
|
||||||
|
current_local_position = (
|
||||||
|
nodeInfo["position"]["latitude"],
|
||||||
|
nodeInfo["position"]["longitude"],
|
||||||
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
"latitude" in packet["decoded"]["position"]
|
"decoded" in packet
|
||||||
|
and "position" in packet["decoded"]
|
||||||
|
and "latitude" in packet["decoded"]["position"]
|
||||||
and "longitude" in packet["decoded"]["position"]
|
and "longitude" in packet["decoded"]["position"]
|
||||||
):
|
):
|
||||||
message_source_position = (
|
message_source_position = (
|
||||||
@@ -128,44 +184,49 @@ class DistanceFilter(Plugin):
|
|||||||
packet["decoded"]["position"]["longitude"],
|
packet["decoded"]["position"]["longitude"],
|
||||||
)
|
)
|
||||||
|
|
||||||
nodeInfo = self.devices[self.config["device"]].getMyNodeInfo()
|
if "compare_latitude" in self.config and "compare_longitude" in self.config:
|
||||||
current_local_position = (
|
current_local_position = (
|
||||||
nodeInfo["position"]["latitude"],
|
self.config["compare_latitude"],
|
||||||
nodeInfo["position"]["longitude"],
|
self.config["compare_longitude"],
|
||||||
)
|
)
|
||||||
|
|
||||||
if message_source_position and current_local_position:
|
if message_source_position and current_local_position:
|
||||||
|
|
||||||
distance_km = haversine(message_source_position, current_local_position)
|
distance_km = haversine(message_source_position, current_local_position)
|
||||||
|
|
||||||
|
comparison = (
|
||||||
|
self.config["comparison"] if "comparison" in self.config else "within"
|
||||||
|
)
|
||||||
|
|
||||||
# message originates from too far a distance
|
# message originates from too far a distance
|
||||||
if (
|
if "max_distance_km" in self.config and self.config["max_distance_km"] > 0:
|
||||||
"max_distance_km" in self.config
|
acceptable_distance = self.config["max_distance_km"]
|
||||||
and self.config["max_distance_km"] > 0
|
|
||||||
and distance_km > self.config["max_distance_km"]
|
if comparison == "within" and distance_km > acceptable_distance:
|
||||||
):
|
self.logger.debug(
|
||||||
logger.debug(
|
f"Packet from too far: {distance_km} > {acceptable_distance}"
|
||||||
f"Packet from too far: {distance_km} > {SUPPORTED_BRIDGE_DISTANCE_KM}"
|
)
|
||||||
)
|
return None
|
||||||
return None
|
elif comparison == "outside" and distance_km < acceptable_distance:
|
||||||
|
self.logger.debug(
|
||||||
|
f"Packet too close: {distance_km} < {acceptable_distance}"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
if "latitude" in self.config:
|
||||||
|
packet["decoded"]["position"]["latitude"] = self.config["latitude"]
|
||||||
|
if "longitude" in self.config:
|
||||||
|
packet["decoded"]["position"]["longitude"] = self.config["longitude"]
|
||||||
|
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
|
|
||||||
plugins["distance_filter"] = DistanceFilter()
|
plugins["location_filter"] = LocationFilter()
|
||||||
|
|
||||||
|
|
||||||
class WebhookPlugin(Plugin):
|
class WebhookPlugin(Plugin):
|
||||||
logger = logging.getLogger(name="meshtastic.bridge.plugin.webhook")
|
logger = logging.getLogger(name="meshtastic.bridge.plugin.webhook")
|
||||||
|
|
||||||
def do_action(self, packet):
|
def do_action(self, packet):
|
||||||
if type(packet) is not dict:
|
|
||||||
try:
|
|
||||||
packet = json.loads(packet)
|
|
||||||
except:
|
|
||||||
self.logger.warning("Packet is not dict")
|
|
||||||
return packet
|
|
||||||
|
|
||||||
if "active" in self.config and not self.config["active"]:
|
if "active" in self.config and not self.config["active"]:
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
@@ -235,9 +296,16 @@ class MQTTPlugin(Plugin):
|
|||||||
|
|
||||||
mqtt_server = self.mqtt_servers[self.config["name"]]
|
mqtt_server = self.mqtt_servers[self.config["name"]]
|
||||||
|
|
||||||
packet_payload = packet if type(packet) is str else json.dumps(packet)
|
if not mqtt_server.is_connected():
|
||||||
|
self.logger.error("Not sent, not connected")
|
||||||
|
return
|
||||||
|
|
||||||
message = self.config["message"] if "message" in self.config else packet_payload
|
packet_message = json.dumps(packet)
|
||||||
|
|
||||||
|
if "message" in self.config:
|
||||||
|
message = self.config["message"].replace("{MSG}", packet["decoded"]["text"])
|
||||||
|
else:
|
||||||
|
message = packet_message
|
||||||
|
|
||||||
info = mqtt_server.publish(self.config["topic"], message)
|
info = mqtt_server.publish(self.config["topic"], message)
|
||||||
info.wait_for_publish()
|
info.wait_for_publish()
|
||||||
@@ -311,52 +379,40 @@ class DecryptFilter(Plugin):
|
|||||||
plugins["decrypt_filter"] = DecryptFilter()
|
plugins["decrypt_filter"] = DecryptFilter()
|
||||||
|
|
||||||
|
|
||||||
class SendPlugin(Plugin):
|
class RadioMessagePlugin(Plugin):
|
||||||
logger = logging.getLogger(name="meshtastic.bridge.plugin.send")
|
logger = logging.getLogger(name="meshtastic.bridge.plugin.send")
|
||||||
|
|
||||||
def do_action(self, packet):
|
def do_action(self, packet):
|
||||||
|
|
||||||
if type(packet) is not dict:
|
|
||||||
try:
|
|
||||||
packet = json.loads(packet)
|
|
||||||
except:
|
|
||||||
self.logger.error("Packet is not a dict")
|
|
||||||
return packet
|
|
||||||
|
|
||||||
if self.config["device"] not in self.devices:
|
if self.config["device"] not in self.devices:
|
||||||
self.logger.error(f"Missing interface for device {self.config['device']}")
|
self.logger.error(f"Missing interface for device {self.config['device']}")
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
if "to" not in packet and "toId" not in packet:
|
destinationId = None
|
||||||
self.logger.debug("Not a message")
|
|
||||||
return packet
|
|
||||||
|
|
||||||
# Broadcast messages or specific
|
|
||||||
if (
|
|
||||||
"node_mapping" in self.config
|
|
||||||
and packet["to"] in self.config["node_mapping"]
|
|
||||||
):
|
|
||||||
destinationId = self.config["node_mapping"][packet["to"]]
|
|
||||||
else:
|
|
||||||
destinationId = packet["to"] if "to" in packet else packet["toId"]
|
|
||||||
|
|
||||||
if "to" in self.config:
|
if "to" in self.config:
|
||||||
destinationId = self.config["to"]
|
destinationId = self.config["to"]
|
||||||
elif "toId" in self.config:
|
elif "toId" in self.config:
|
||||||
destinationId = self.config["toId"]
|
destinationId = self.config["toId"]
|
||||||
|
elif "node_mapping" in self.config and "to" in packet:
|
||||||
|
destinationId = self.config["node_mapping"][packet["to"]]
|
||||||
|
elif "to" in packet:
|
||||||
|
destinationId = packet["to"]
|
||||||
|
elif "toId" in packet:
|
||||||
|
destinationId = packet["toId"]
|
||||||
|
|
||||||
|
if not destinationId:
|
||||||
|
self.logger.error("Missing 'to' property in config or packet")
|
||||||
|
return packet
|
||||||
|
|
||||||
device_name = self.config["device"]
|
device_name = self.config["device"]
|
||||||
|
|
||||||
if device_name not in self.devices:
|
|
||||||
self.logger.warning(f"No such radio device: {device_name}")
|
|
||||||
return packet
|
|
||||||
|
|
||||||
device = self.devices[device_name]
|
device = self.devices[device_name]
|
||||||
|
|
||||||
self.logger.debug(f"Sending packet to Radio {device_name}")
|
# Not a radio packet
|
||||||
|
if "decoded" in packet and "text" in packet["decoded"] and "from" not in packet:
|
||||||
|
self.logger.debug(f"Sending text to Radio {device_name}")
|
||||||
|
device.sendText(text=packet["decoded"]["text"], destinationId=destinationId)
|
||||||
|
|
||||||
if "message" in self.config and self.config["message"]:
|
|
||||||
device.sendText(text=self.config["message"], destinationId=destinationId)
|
|
||||||
elif (
|
elif (
|
||||||
"lat" in self.config
|
"lat" in self.config
|
||||||
and self.config["lat"] > 0
|
and self.config["lat"] > 0
|
||||||
@@ -367,13 +423,19 @@ class SendPlugin(Plugin):
|
|||||||
lng = self.config["lng"]
|
lng = self.config["lng"]
|
||||||
altitude = self.config["alt"] if "alt" in self.config else 0
|
altitude = self.config["alt"] if "alt" in self.config else 0
|
||||||
|
|
||||||
|
self.logger.debug(f"Sending position to Radio {device_name}")
|
||||||
|
|
||||||
device.sendPosition(
|
device.sendPosition(
|
||||||
latitude=lat,
|
latitude=lat,
|
||||||
longitude=lng,
|
longitude=lng,
|
||||||
altitude=altitude,
|
altitude=altitude,
|
||||||
destinationId=destinationId,
|
destinationId=destinationId,
|
||||||
)
|
)
|
||||||
else:
|
elif (
|
||||||
|
"decoded" in packet
|
||||||
|
and "payload" in packet["decoded"]
|
||||||
|
and "portnum" in packet["decoded"]
|
||||||
|
):
|
||||||
meshPacket = mesh_pb2.MeshPacket()
|
meshPacket = mesh_pb2.MeshPacket()
|
||||||
meshPacket.channel = 0
|
meshPacket.channel = 0
|
||||||
meshPacket.decoded.payload = base64.b64decode(packet["decoded"]["payload"])
|
meshPacket.decoded.payload = base64.b64decode(packet["decoded"]["payload"])
|
||||||
@@ -381,9 +443,11 @@ class SendPlugin(Plugin):
|
|||||||
meshPacket.decoded.want_response = False
|
meshPacket.decoded.want_response = False
|
||||||
meshPacket.id = device._generatePacketId()
|
meshPacket.id = device._generatePacketId()
|
||||||
|
|
||||||
|
self.logger.debug(f"Sending packet to Radio {device_name}")
|
||||||
|
|
||||||
device._sendPacket(meshPacket=meshPacket, destinationId=destinationId)
|
device._sendPacket(meshPacket=meshPacket, destinationId=destinationId)
|
||||||
|
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
|
|
||||||
plugins["send_plugin"] = SendPlugin()
|
plugins["radio_message_plugin"] = RadioMessagePlugin()
|
||||||
|
|||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
devices:
|
devices:
|
||||||
- name: local
|
- name: local
|
||||||
pipelines:
|
pipelines:
|
||||||
- debugger:
|
- debugger:
|
||||||
log_level: info
|
log_level: info
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
devices:
|
||||||
|
- name: radio1
|
||||||
|
tcp: 192.168.86.27
|
||||||
|
mqtt_servers:
|
||||||
|
- name: external
|
||||||
|
server: broker.hivemq.com
|
||||||
|
port: 1883
|
||||||
|
topic: meshtastic/radio-network1
|
||||||
|
pipelines:
|
||||||
|
mqtt-to-radio:
|
||||||
|
- radio_message_plugin:
|
||||||
|
device: radio1
|
||||||
|
to: "^all"
|
||||||
|
pipelines:
|
||||||
|
pipeline1:
|
||||||
|
- debugger:
|
||||||
|
log_level: debug
|
||||||
|
radio-to-mqtt:
|
||||||
|
- message_filter:
|
||||||
|
app:
|
||||||
|
allow:
|
||||||
|
- "TEXT_MESSAGE_APP"
|
||||||
|
- mqtt_plugin:
|
||||||
|
name: external
|
||||||
|
topic: meshtastic/radio-network1
|
||||||
Reference in New Issue
Block a user