Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure and flags #56

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions src/can2mqtt.go

This file was deleted.

4 changes: 1 addition & 3 deletions src/internal/canbus.go → src/canbus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package internal of c3re/can2mqtt contains some tools for bridging a CAN-Interface
// and a mqtt-network
package internal
package main

import (
"github.com/brutella/can"
Expand Down
File renamed without changes.
97 changes: 23 additions & 74 deletions src/internal/main.go → src/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package internal
package main

import (
"bufio" // Reader
"encoding/csv" // CSV Management
"fmt" // print :)
"github.com/brutella/can"
"github.com/c3re/can2mqtt/internal/convertfunctions"
"flag"
"github.com/c3re/can2mqtt/convertfunctions"
"io" // EOF const
"log" // error management
"log/slog"
Expand All @@ -14,86 +13,36 @@ import (
"sync"
)

type convertToCan func(input []byte) (can.Frame, error)
type convertToMqtt func(input can.Frame) ([]byte, error)

type ConvertMode interface {
convertToCan
convertToMqtt
}

// can2mqtt is a struct that represents the internal type of
// one line of the can2mqtt.csv file. It has
// the same three fields as the can2mqtt.csv file: CAN-ID,
// conversion method and MQTT-Topic.
type can2mqtt struct {
canId uint32
convMethod string
toCan convertToCan
toMqtt convertToMqtt
mqttTopic string
}

var pairFromID map[uint32]*can2mqtt // c2m pair (lookup from ID)
var pairFromTopic map[string]*can2mqtt // c2m pair (lookup from Topic)
var dbg = false // verbose on off [-v]
var ci = "can0" // the CAN-Interface [-c]
var cs = "tcp://localhost:1883" // mqtt-connect-string [-m]
var c2mf = "can2mqtt.csv" // path to the can2mqtt.csv [-f]
var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
var debugLog bool
var canInterface, mqttConnection, configFile string
var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
var wg sync.WaitGroup

// SetDbg decides whether there is really verbose output or
// just standard information output. Default is false.
func SetDbg(v bool) {
dbg = v
slog.SetLogLoggerLevel(slog.LevelDebug)
}

// SetCi sets the CAN-Interface to use for the CAN side
// of the bridge. Default is: can0.
func SetCi(c string) {
ci = c
}
func main() {
log.SetFlags(0)

// SetC2mf expects a string which is a path to a can2mqtt.csv file
// Default is: can2mqtt.csv
func SetC2mf(f string) {
c2mf = f
}
flag.BoolVar(&debugLog, "v", false, "show (very) verbose debug log")
flag.StringVar(&canInterface, "c", "can0", "which socket-can interface to use")
flag.StringVar(&mqttConnection, "m", "tcp://localhost:1883", "which mqtt-broker to use. Example: tcp://user:[email protected]:1883")
flag.StringVar(&configFile, "f", "can2mqtt.csv", "which config file to use")
flag.IntVar(&dirMode, "d", 0, "direction mode\n0: bidirectional (default)\n1: can2mqtt only\n2: mqtt2can only")
flag.Parse()

// SetCs sets the MQTT connect-string which contains: protocol,
// hostname and port. Default is: tcp://localhost:1883
func SetCs(s string) {
cs = s
}
if dirMode < 0 || dirMode > 2 {
slog.Error("got invalid value for -d. Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", "d", dirMode)
}

// SetConfDirMode sets the dirMode
func SetConfDirMode(s string) {
if s == "0" {
dirMode = 0
} else if s == "1" {
dirMode = 1
} else if s == "2" {
dirMode = 2
} else {
_ = fmt.Errorf("error: got invalid value for -d (%s). Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", s)
if debugLog {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
}

// Start is the function that should be called after debug-level
// connect-string, can interface and can2mqtt file have been set.
// Start takes care of everything that happens after that.
// It starts the CAN-Bus connection and the MQTT-Connection. It
// parses the can2mqtt.csv file and from there everything takes
// its course...
func Start() {
log.SetFlags(0)
slog.Info("Starting can2mqtt", "mqtt-config", cs, "can-interface", ci, "can2mqtt.csv", c2mf, "dir-mode", dirMode, "debug", dbg)
slog.Info("Starting can2mqtt", "mqtt-config", mqttConnection, "can-interface", canInterface, "can2mqtt.csv", configFile, "dir-mode", dirMode, "debug", debugLog)
wg.Add(1)
go canStart(ci) // epic parallel shit ;-)
mqttStart(cs)
readC2MPFromFile(c2mf)
go canStart(canInterface) // epic parallel shit ;-)
mqttStart(mqttConnection)
readC2MPFromFile(configFile)
wg.Wait()
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/mqtt.go → src/mqtt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package main

import (
MQTT "github.com/eclipse/paho.mqtt.golang"
Expand Down
2 changes: 1 addition & 1 deletion src/internal/receiving.go → src/receiving.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package main

import (
"github.com/brutella/can"
Expand Down
18 changes: 18 additions & 0 deletions src/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "github.com/brutella/can"

type convertToCan func(input []byte) (can.Frame, error)
type convertToMqtt func(input can.Frame) ([]byte, error)

// can2mqtt is a struct that represents the internal type of
// one line of the can2mqtt.csv file. It has
// the same three fields as the can2mqtt.csv file: CAN-ID,
// conversion method and MQTT-Topic.
type can2mqtt struct {
canId uint32
convMethod string
toCan convertToCan
toMqtt convertToMqtt
mqttTopic string
}
Loading