Skip to content

Commit

Permalink
Merge pull request #96 from geoffwhittington/initial-trunk
Browse files Browse the repository at this point in the history
trunk.io changes
  • Loading branch information
jeremiah-k authored Nov 14, 2024
2 parents 8f7937c + 793246b commit 8d0bb55
Show file tree
Hide file tree
Showing 29 changed files with 518 additions and 306 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Build Windows Executable

permissions:
contents: write
issues: read

on:
release:
types: [published]
Expand All @@ -9,12 +13,12 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: "3.x"
python-version: 3.x

- name: Install dependencies
run: |
Expand Down
9 changes: 9 additions & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
tmp
2 changes: 2 additions & 0 deletions .trunk/configs/.isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
2 changes: 2 additions & 0 deletions .trunk/configs/.markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Prettier friendly markdownlint config (all formatting rules disabled)
extends: markdownlint/style/prettier
7 changes: 7 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
5 changes: 5 additions & 0 deletions .trunk/configs/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generic, formatter-friendly config.
select = ["B", "D3", "E", "F"]

# Never enforce `E501` (line length violations). This should be handled by formatters.
ignore = ["E501"]
38 changes: 38 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.22.8
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.6.4
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
actions:
disabled:
- trunk-announce
- trunk-check-pre-push
- trunk-fmt-pre-commit
enabled:
- trunk-upgrade-available
101 changes: 52 additions & 49 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,52 @@
# Development

You can run the relay using Python 3.9 on Linux, MacOS, and Windows. We would enjoy pull requests to fix or enhance the relay
The relay is compatible with Python 3.9 and newer on Linux, macOS, and Windows. We encourage contributions to fix bugs or add enhancements.

## Installation

Clone the repository:

```
```bash
git clone https://github.com/geoffwhittington/meshtastic-matrix-relay.git
```

### Setup

Create a Python virtual environment in the project directory:

```
```bash
python3 -m venv .pyenv
```

Activate the virtual environment and install dependencies:

```
```bash
source .pyenv/bin/activate
pip install -r requirements.txt
```


### Configuration

Create a `config.yaml` in the project directory with the appropriate values. A sample configuration is provided below:

```yaml
matrix:
homeserver: "https://example.matrix.org"
access_token: "reaalllllyloooooongsecretttttcodeeeeeeforrrrbot" # See: https://t2bot.io/docs/access_tokens/
bot_user_id: "@botuser:example.matrix.org"

matrix_rooms: # Needs at least 1 room & channel, but supports all Meshtastic channels
- id: "#someroomalias:example.matrix.org" # Matrix room aliases & IDs supported
meshtastic_channel: 0
- id: "!someroomid:example.matrix.org"
meshtastic_channel: 2

meshtastic:
connection_type: serial # Choose either "network" or "serial"
serial_port: /dev/ttyUSB0 # Only used when connection is "serial"
host: "meshtastic.local" # Only used when connection is "network"
meshnet_name: "Your Meshnet Name" # This is displayed in full on Matrix, but is truncated when sent to a Meshnet
broadcast_enabled: true
detection_sensor: true

logging:
level: "info"

plugins: # Optional plugins
health:
active: true
map:
active: true
```
To configure the relay, create a `config.yaml` file in the project directory. You can refer to the provided `sample_config.yaml` for an example configuration.

## Usage

Activate the virtual environment:
```

```bash
source .pyenv/bin/activate
```

Run the `main.py` script:
```

```bash
python main.py
```

Example output:
```

$ python main.py
```bash
python main.py
INFO:meshtastic.matrix.relay:Starting Meshtastic <==> Matrix Relay...
INFO:meshtastic.matrix.relay:Connecting to radio at meshtastic.local ...
INFO:meshtastic.matrix.relay:Connected to radio at meshtastic.local.
Expand All @@ -86,13 +60,12 @@ INFO:meshtastic.matrix.relay:Sent inbound radio message to matrix room: #someroo
```

## Persistence
If you'd like the bridge to run automatically (and persistently) on startup in Linux, you can set up a systemd service.
In this example, it is assumed that you have the project a (non-root) user's home directory, and set up the venv according to the above.

Create the file ```~/.config/systemd/user/mmrelay.service```:
```
To run the bridge automatically on startup in Linux, set up a systemd service:

```systemd
[Unit]
Description=A Meshtastic to [matrix] bridge
Description=A Meshtastic to Matrix bridge
After=default.target
[Service]
Expand All @@ -104,8 +77,38 @@ Restart=on-failure
[Install]
WantedBy=default.target
```
The service is enabled and started by
```
$ systemctl --user enable mmrelay.service
$ systemctl --user start mmrelay.service

Enable and start the service:

```bash
systemctl --user enable mmrelay.service
systemctl --user start mmrelay.service
```

### Contributing & Code Quality Checks

We use **Trunk** for automated code quality checks and formatting. Contributors are expected to run these checks before submitting a pull request.

#### Installing Trunk

Follow these steps to set up Trunk:

1. Install Trunk via the official installation script:

```bash
curl -fsSL https://get.trunk.io | bash
```

2. Initialize Trunk in your local environment:

```bash
trunk init
```

3. To check your code and automatically fix issues, run:

```bash
trunk check --all --fix
```

Refer to the [Trunk documentation](https://trunk.io/docs) for more details on using Trunk effectively.
54 changes: 24 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,71 @@
# M<>M Relay

### (Meshtastic <=> Matrix Relay)
## (Meshtastic <=> Matrix Relay)

A powerful and easy-to-use relay between Meshtastic devices and Matrix chat rooms, allowing seamless communication across platforms. This opens the door for bridging Meshtastic devices to [many other platforms](https://matrix.org/bridges/).

## Features
### Features

- Bidirectional message relay between Meshtastic devices and Matrix chat rooms, capable of supporting multiple meshnets
- Supports serial, network, and ***BLE (now too!)*** connections for Meshtastic devices
- Supports serial, network, and **_BLE (now too!)_** connections for Meshtastic devices
- Custom keys are embedded in Matrix messages which are used when relaying messages between two or more meshnets.
- Truncates long messages to fit within Meshtastic's payload size
- SQLite database to store node information for improved functionality
- Customizable logging level for easy debugging
- Configurable through a simple YAML file
- Supports mapping multiple rooms and channels 1:1
- Relays messages to/from a MQTT broker, if configured in the Meshtastic firmware (*Note: Messages relayed via MQTT currently share the relay's `meshnet_name`*)
- Relays messages to/from an MQTT broker, if configured in the Meshtastic firmware


*We would love to support [Matrix E2EE rooms](https://github.com/geoffwhittington/meshtastic-matrix-relay/issues/33), but this is currently not implemented. If you are familiar with [matrix-nio](https://github.com/poljar/matrix-nio/), we would gladly accept a PR for this feature!*


_We would love to support [Matrix E2EE rooms](https://github.com/geoffwhittington/meshtastic-matrix-relay/issues/33), but this is currently not implemented._

### Windows Installer

<img src="https://user-images.githubusercontent.com/1770544/235249050-8c79107a-50cc-4803-b989-39e58100342d.png" width="500"/>
![Windows Installer Screenshot](https://user-images.githubusercontent.com/1770544/235249050-8c79107a-50cc-4803-b989-39e58100342d.png)

The latest installer is available [here](https://github.com/geoffwhittington/meshtastic-matrix-relay/releases)

### Plugins
M<>M Relay supports plugins for extending its functionality, enabling customization and enhancement of the relay to suit specific needs. Plugins can add new features, integrate with other services, or modify the behavior of the relay without changing the core code.

M<>M Relay supports plugins for extending its functionality, enabling customization and enhancement of the relay to suit specific needs.

## Core Plugins
Generate a map of your nodes

<img src="https://user-images.githubusercontent.com/1770544/235247915-47750b4f-d505-4792-a458-54a5f24c1523.png" width="500"/>
Generate a map of your nodes:

Produce high-level details about your mesh
![Map Plugin Screenshot](https://user-images.githubusercontent.com/1770544/235247915-47750b4f-d505-4792-a458-54a5f24c1523.png)

<img src="https://user-images.githubusercontent.com/1770544/235245873-1ddc773b-a4cd-4c67-b0a5-b55a29504b73.png" width="500"/>
Produce high-level details about your mesh:

![Mesh Details Screenshot](https://user-images.githubusercontent.com/1770544/235245873-1ddc773b-a4cd-4c67-b0a5-b55a29504b73.png)

## Custom plugins
It is possible to create custom plugins to add new features or modify the relay's behavior. Check more info in [example_plugins/README.md](https://github.com/geoffwhittington/meshtastic-matrix-relay/tree/main/example_plugins)

## Install a community plugin
To install plugins, simply modify the config.yaml file and add the user's repository under the community-plugins section.
It is possible to create custom plugins. Check more info in [example_plugins/README.md](https://github.com/geoffwhittington/meshtastic-matrix-relay/tree/main/example_plugins).

```
### Install a community plugin

Add the repository under the `community-plugins` section in `config.yaml`:

```yaml
community-plugins:
weather_plugin:
active: true
repository: https://github.com/anotheruser/weather_plugin.git
tag: master
```

**Note:** If the plugin requires additional dependencies, they will be installed automatically if a requirements.txt file is present in the plugin's directory.

## Getting Started with Matrix
### Getting Started with Matrix
See our Wiki page [Getting Started With Matrix & MM Relay](https://github.com/geoffwhittington/meshtastic-matrix-relay/wiki/Getting-Started-With-Matrix-&-MM-Relay).
## Already on Matrix?
### Already on Matrix?
Join us!
- In our project's room:
[#mmrelay:meshnet.club](https://matrix.to/#/#mmrelay:meshnet.club)

- Which is a part of the Meshtastic Community Matrix space *(an unofficial group of enthusiasts)*:
[#meshtastic-community:meshnet.club](https://matrix.to/#/#meshtastic-community:meshnet.club)
- Our project's room: [#mmrelay:meshnet.club](https://matrix.to/#/#mmrelay:meshnet.club)
- Part of the Meshtastic Community Matrix space: [#meshtastic-community:meshnet.club](https://matrix.to/#/#meshtastic-community:meshnet.club)
## Supported Platforms
### Supported Platforms
The relay is compatible with the following operating systems:
Expand Down
9 changes: 6 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import os
import sys

import yaml
from yaml.loader import SafeLoader
import sys
import os


def get_app_path():
"""
Returns the base directory of the application, whether running from source or as an executable.
"""
if getattr(sys, 'frozen', False):
if getattr(sys, "frozen", False):
# Running in a bundle (PyInstaller)
return os.path.dirname(sys.executable)
else:
# Running in a normal Python environment
return os.path.dirname(os.path.abspath(__file__))


relay_config = {}
config_path = os.path.join(get_app_path(), "config.yaml")

Expand Down
Loading

0 comments on commit 8d0bb55

Please sign in to comment.