approximate feature parity with rust attempt

This commit is contained in:
Finn 2024-10-20 18:38:35 -07:00
parent e00fdfa41a
commit e147f8a5ed
18 changed files with 656 additions and 22 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
matrix-meshtastic-bridge.json
matrix-meshtastic-bridge.db

42
bridge/bridge.go Normal file
View file

@ -0,0 +1,42 @@
package bridge
import (
"context"
"fmt"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/matrix"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/meshtastic/protobufs"
"github.com/sirupsen/logrus"
)
func RunBridge(ctx context.Context, fromRadioCh chan *protobufs.FromRadio) {
for {
fromRadio := <-fromRadioCh
switch payload := fromRadio.PayloadVariant.(type) {
case *protobufs.FromRadio_Channel:
logrus.WithField("type", "channel").Debugf("received %+v", payload)
case *protobufs.FromRadio_Packet:
if err := handlePacket(ctx, payload.Packet); err != nil {
logrus.WithError(err).Error("error handling meshtastic packet")
}
default:
logrus.Debugf("received unknown message type: %+v", payload)
}
}
}
func handlePacket(ctx context.Context, packet *protobufs.MeshPacket) error {
payload, ok := packet.PayloadVariant.(*protobufs.MeshPacket_Decoded)
if !ok {
return nil // ignore encrypted packets for now
}
switch payload.Decoded.Portnum {
case protobufs.PortNum_TEXT_MESSAGE_APP:
matrix.SendMessage(ctx, fmt.Sprintf("text from %x: %s (snr: %f, rssi: %d, hop limit: %d, hop start: %d)", packet.From, payload.Decoded.Payload, packet.RxSnr, packet.RxRssi, packet.HopLimit, packet.HopStart))
default:
logrus.WithField("type", protobufs.PortNum_name[int32(payload.Decoded.Portnum)]).Debug("ignoring unknown app payload")
}
return nil
}

View file

@ -7,13 +7,17 @@ import (
"github.com/sirupsen/logrus"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/bridge"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/config"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/db"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/matrix"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/meshtastic"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/meshtastic/protobufs"
)
func main() {
if err := run(); err != nil {
panic(err)
logrus.WithError(err).Fatal("error running bridge")
}
}
@ -23,10 +27,21 @@ func run() error {
return err
}
if err := db.Migrate(); err != nil {
return err
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
go meshtastic.Receive(ctx)
if err := matrix.Setup(ctx); err != nil {
return err
}
fromRadioCh := make(chan *protobufs.FromRadio)
go matrix.Run(ctx)
go meshtastic.Receive(ctx, fromRadioCh)
go bridge.RunBridge(ctx, fromRadioCh)
if err := meshtastic.SendConfigInit(ctx); err != nil {
logrus.WithError(err).Error("error sending init to meshtastic")
@ -35,6 +50,7 @@ func run() error {
<-ctx.Done()
meshtastic.ShutdownReceiver()
matrix.Shutdown()
return nil
}

View file

@ -6,10 +6,13 @@ import (
"time"
"github.com/sirupsen/logrus"
"maunium.net/go/mautrix/id"
)
type Config struct {
Meshtastic Meshtastic
Matrix Matrix
Database string
}
type Meshtastic struct {
@ -18,7 +21,14 @@ type Meshtastic struct {
PollingInterval time.Duration
}
type Matrix struct {
User id.UserID
Password string
Room id.RoomID
}
var C = Config{
Database: "matrix-meshtastic-bridge.db",
Meshtastic: Meshtastic{
RequestTimeout: time.Second * 5,
PollingInterval: time.Millisecond * 500,

31
db/db.go Normal file
View file

@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
package db
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

68
db/helper.go Normal file
View file

@ -0,0 +1,68 @@
package db
import (
"database/sql"
"embed"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/config"
_ "github.com/mattn/go-sqlite3"
goose "github.com/pressly/goose/v3"
"github.com/sirupsen/logrus"
)
//go:embed migrations
var migrations embed.FS
func Migrate() error {
logrus.Info("running database migrations")
_, dbConn, err := Get()
if err != nil {
return err
}
defer dbConn.Close()
goose.SetBaseFS(migrations)
if err := goose.SetDialect("sqlite3"); err != nil {
return err
}
if err := goose.Up(dbConn, "migrations"); err != nil {
return err
}
return nil
}
// Get the database and closable DB object
// example usage:
//
// queries, dbconn, err := db.Get()
// if err != nil {
// return err
// }
// defer dbconn.Close()
func Get() (*Queries, *sql.DB, error) {
db, err := sql.Open("sqlite3", config.C.Database)
if err != nil {
return nil, nil, err
}
_, err = db.Exec("PRAGMA foreign_keys = ON")
if err != nil {
return nil, nil, err
}
return New(db), db, nil
}
// NullableString accepts a string and returns an sql.NullString
// if the input string is zero-length, the output will be marked
// null
func NullableString(s string) sql.NullString {
return sql.NullString{
Valid: s != "",
String: s,
}
}

60
db/matrix_sync.sql.go Normal file
View file

@ -0,0 +1,60 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
// source: matrix_sync.sql
package db
import (
"context"
)
const matrixLoadFilterID = `-- name: MatrixLoadFilterID :one
SELECT filter_id FROM matrix_filters WHERE user_id = ?
`
func (q *Queries) MatrixLoadFilterID(ctx context.Context, userID string) (string, error) {
row := q.db.QueryRowContext(ctx, matrixLoadFilterID, userID)
var filter_id string
err := row.Scan(&filter_id)
return filter_id, err
}
const matrixLoadNextBatch = `-- name: MatrixLoadNextBatch :one
SELECT token FROM matrix_next_batch WHERE user_id = ?
`
func (q *Queries) MatrixLoadNextBatch(ctx context.Context, userID string) (string, error) {
row := q.db.QueryRowContext(ctx, matrixLoadNextBatch, userID)
var token string
err := row.Scan(&token)
return token, err
}
const matrixSaveFilterID = `-- name: MatrixSaveFilterID :exec
INSERT INTO matrix_filters (user_id, filter_id) VALUES (?, ?) ON CONFLICT (user_id) DO UPDATE SET filter_id = excluded.filter_id
`
type MatrixSaveFilterIDParams struct {
UserID string
FilterID string
}
func (q *Queries) MatrixSaveFilterID(ctx context.Context, arg MatrixSaveFilterIDParams) error {
_, err := q.db.ExecContext(ctx, matrixSaveFilterID, arg.UserID, arg.FilterID)
return err
}
const matrixSaveNextBatch = `-- name: MatrixSaveNextBatch :exec
INSERT INTO matrix_next_batch (user_id, token) VALUES (?, ?) ON CONFLICT (user_id) DO UPDATE SET token = excluded.token
`
type MatrixSaveNextBatchParams struct {
UserID string
Token string
}
func (q *Queries) MatrixSaveNextBatch(ctx context.Context, arg MatrixSaveNextBatchParams) error {
_, err := q.db.ExecContext(ctx, matrixSaveNextBatch, arg.UserID, arg.Token)
return err
}

View file

@ -0,0 +1,44 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
// source: meshtastic_nodes.sql
package db
import (
"context"
"database/sql"
)
const meshtasticNodeUpdate = `-- name: MeshtasticNodeUpdate :exec
INSERT INTO meshtastic_nodes (node_num, meshtastic_id, long_name, short_name, mac, hw_model, public_key) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (node_num) DO UPDATE SET
meshtastic_id = excluded.meshtastic_id,
long_name = excluded.long_name,
short_name = excluded.short_name,
mac = excluded.mac,
hw_model = excluded.hw_model,
public_key = excluded.public_key
`
type MeshtasticNodeUpdateParams struct {
NodeNum int64
MeshtasticID string
LongName sql.NullString
ShortName sql.NullString
Mac sql.NullString
HwModel sql.NullString
PublicKey []byte
}
func (q *Queries) MeshtasticNodeUpdate(ctx context.Context, arg MeshtasticNodeUpdateParams) error {
_, err := q.db.ExecContext(ctx, meshtasticNodeUpdate,
arg.NodeNum,
arg.MeshtasticID,
arg.LongName,
arg.ShortName,
arg.Mac,
arg.HwModel,
arg.PublicKey,
)
return err
}

View file

@ -0,0 +1,40 @@
-- +goose Up
-- +goose StatementBegin
PRAGMA foreign_keys = ON;
CREATE TABLE meshtastic_nodes (
id INTEGER PRIMARY KEY NOT NULL,
node_num INTEGER NOT NULL,
meshtastic_id TEXT NOT NULL,
long_name TEXT,
short_name TEXT,
mac TEXT,
hw_model TEXT,
public_key BLOB,
matrix_id TEXT
);
CREATE TABLE channels (
id INTEGER PRIMARY KEY NOT NULL,
meshtastic_index INTEGER NOT NULL,
meshtastic_psk TEXT UNIQUE NOT NULL,
name TEXT,
matrix_room TEXT
);
CREATE TABLE matrix_filters (
user_id TEXT UNIQUE NOT NULL,
filter_id TEXT NOT NULL
);
CREATE TABLE matrix_next_batch (
user_id TEXT UNIQUE NOT NULL,
token TEXT NOT NULL
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE channels;
DROP TABLE meshtastic_nodes;
-- +goose StatementEnd

39
db/models.go Normal file
View file

@ -0,0 +1,39 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
package db
import (
"database/sql"
)
type Channel struct {
ID int64
MeshtasticIndex int64
MeshtasticPsk string
Name sql.NullString
MatrixRoom sql.NullString
}
type MatrixFilter struct {
UserID string
FilterID string
}
type MatrixNextBatch struct {
UserID string
Token string
}
type MeshtasticNode struct {
ID int64
NodeNum int64
MeshtasticID string
LongName sql.NullString
ShortName sql.NullString
Mac sql.NullString
HwModel sql.NullString
PublicKey []byte
MatrixID sql.NullString
}

View file

@ -0,0 +1,11 @@
-- name: MatrixSaveFilterID :exec
INSERT INTO matrix_filters (user_id, filter_id) VALUES (?, ?) ON CONFLICT (user_id) DO UPDATE SET filter_id = excluded.filter_id;
-- name: MatrixLoadFilterID :one
SELECT filter_id FROM matrix_filters WHERE user_id = ?;
-- name: MatrixSaveNextBatch :exec
INSERT INTO matrix_next_batch (user_id, token) VALUES (?, ?) ON CONFLICT (user_id) DO UPDATE SET token = excluded.token;
-- name: MatrixLoadNextBatch :one
SELECT token FROM matrix_next_batch WHERE user_id = ?;

View file

@ -0,0 +1,8 @@
-- name: MeshtasticNodeUpdate :exec
INSERT INTO meshtastic_nodes (node_num, meshtastic_id, long_name, short_name, mac, hw_model, public_key) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (node_num) DO UPDATE SET
meshtastic_id = excluded.meshtastic_id,
long_name = excluded.long_name,
short_name = excluded.short_name,
mac = excluded.mac,
hw_model = excluded.hw_model,
public_key = excluded.public_key;

23
go.mod
View file

@ -3,8 +3,29 @@ module git.janky.solutions/finn/matrix-meshtastic-bridge-go
go 1.22.7
require (
github.com/mattn/go-sqlite3 v1.14.24
github.com/pressly/goose/v3 v3.22.1
github.com/rs/zerolog v1.33.0
github.com/sirupsen/logrus v1.9.3
google.golang.org/protobuf v1.35.1
maunium.net/go/mautrix v0.21.1
)
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
go.mau.fi/util v0.8.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
)

85
go.sum
View file

@ -1,21 +1,92 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.22.1 h1:2zICEfr1O3yTP9BRZMGPj7qFxQ+ik6yeo+z1LMuioLc=
github.com/pressly/goose/v3 v3.22.1/go.mod h1:xtMpbstWyCpyH+0cxLTMCENWBG+0CSxvTsXhW95d5eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
go.mau.fi/util v0.8.1 h1:Ga43cz6esQBYqcjZ/onRoVnYWoUwjWbsxVeJg2jOTSo=
go.mau.fi/util v0.8.1/go.mod h1:T1u/rD2rzidVrBLyaUdPpZiJdP/rsyi+aTzn0D+Q6wc=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mautrix v0.21.1 h1:Z+e448jtlY977iC1kokNJTH5kg2WmDpcQCqn+v9oZOA=
maunium.net/go/mautrix v0.21.1/go.mod h1:7F/S6XAdyc/6DW+Q7xyFXRSPb6IjfqMb1OMepQ8C8OE=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU=
modernc.org/sqlite v1.33.0 h1:WWkA/T2G17okiLGgKAj4/RMIvgyMT19yQ038160IeYk=
modernc.org/sqlite v1.33.0/go.mod h1:9uQ9hF/pCZoYZK73D/ud5Z7cIRIILSZI8NdIemVMTX8=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

75
matrix/matrix.go Normal file
View file

@ -0,0 +1,75 @@
package matrix
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/config"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"maunium.net/go/mautrix"
)
var client *mautrix.Client
func Setup(ctx context.Context) error {
clientWellKnown, err := mautrix.DiscoverClientAPI(ctx, config.C.Matrix.User.Homeserver())
if err != nil {
return err
}
homeserverURL, err := url.Parse(clientWellKnown.Homeserver.BaseURL)
if err != nil {
return fmt.Errorf("error parsing homeserver URL %s: %v", clientWellKnown.Homeserver.BaseURL, err)
}
client = &mautrix.Client{
HomeserverURL: homeserverURL,
UserAgent: "matrix-meshtastic-bridge-go/unversioned",
Client: &http.Client{Timeout: 180 * time.Second},
Syncer: mautrix.NewDefaultSyncer(),
Log: zerolog.New(logrus.New().Out),
Store: dbSyncStore{},
}
_, err = client.Login(ctx, &mautrix.ReqLogin{
Type: mautrix.AuthTypePassword,
Identifier: mautrix.UserIdentifier{
Type: mautrix.IdentifierTypeUser,
User: config.C.Matrix.User.Localpart(),
},
Password: config.C.Matrix.Password,
DeviceID: "matrix-meshtastic-bridge",
StoreCredentials: true,
StoreHomeserverURL: true,
})
if err != nil {
logrus.WithError(err).Fatal("failed to login to matrix")
}
return nil
}
func Run(ctx context.Context) {
for {
if err := client.Sync(); err != nil {
logrus.WithError(err).Error("error syncing with matrix")
}
}
}
func Shutdown() {
client.StopSync()
}
func SendMessage(ctx context.Context, text string) error {
_, err := client.SendText(ctx, config.C.Matrix.Room, text)
if err != nil {
return err
}
return nil
}

84
matrix/syncer.go Normal file
View file

@ -0,0 +1,84 @@
package matrix
import (
"context"
"database/sql"
"errors"
"git.janky.solutions/finn/matrix-meshtastic-bridge-go/db"
"maunium.net/go/mautrix/id"
)
type dbSyncStore struct{}
func (dbSyncStore) SaveFilterID(ctx context.Context, userID id.UserID, filterID string) error {
queries, dbconn, err := db.Get()
if err != nil {
return err
}
defer dbconn.Close()
err = queries.MatrixSaveFilterID(ctx, db.MatrixSaveFilterIDParams{
UserID: userID.String(),
FilterID: filterID,
})
if err != nil {
return err
}
return nil
}
func (dbSyncStore) LoadFilterID(ctx context.Context, userID id.UserID) (string, error) {
queries, dbconn, err := db.Get()
if err != nil {
return "", err
}
defer dbconn.Close()
filterID, err := queries.MatrixLoadFilterID(ctx, userID.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return "", err
}
return filterID, nil
}
func (dbSyncStore) SaveNextBatch(ctx context.Context, userID id.UserID, nextBatchToken string) error {
queries, dbconn, err := db.Get()
if err != nil {
return err
}
defer dbconn.Close()
err = queries.MatrixSaveNextBatch(ctx, db.MatrixSaveNextBatchParams{
UserID: userID.String(),
Token: nextBatchToken,
})
if err != nil {
return err
}
return nil
}
func (dbSyncStore) LoadNextBatch(ctx context.Context, userID id.UserID) (string, error) {
queries, dbconn, err := db.Get()
if err != nil {
return "", err
}
defer dbconn.Close()
token, err := queries.MatrixLoadNextBatch(ctx, userID.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return "", err
}
return token, nil
}

View file

@ -20,19 +20,23 @@ const (
pathToRadio = "/api/v1/toradio"
)
func Receive(ctx context.Context) {
for ctx.Err() == nil {
fromRadio, err := getFromRadio(ctx)
if err != nil {
logrus.WithError(err).Error("error communicating with radio")
func Receive(ctx context.Context, ch chan *protobufs.FromRadio) {
t := time.NewTicker(config.C.Meshtastic.PollingInterval)
for {
select {
case <-t.C:
fromRadio, err := getFromRadio(ctx)
if err != nil {
logrus.WithError(err).Error("error communicating with radio")
}
if fromRadio != nil && fromRadio.PayloadVariant != nil {
ch <- fromRadio
}
case <-ctx.Done():
return
}
if fromRadio != nil && fromRadio.PayloadVariant != nil {
logrus.Debugf("fromRadio: %+v", fromRadio)
}
time.Sleep(config.C.Meshtastic.PollingInterval)
}
}

9
sqlc.yaml Normal file
View file

@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "sqlite"
schema: "db/migrations"
queries: "db/queries"
gen:
go:
package: "db"
out: "db"