Skip to content

Commit fdff89c

Browse files
mxmxchereMalte Muench
andauthored
jRestructure and flags (#56)
* works Signed-off-by: Malte Muench <mamu@stablerock.de> * removed one unused folder layer Signed-off-by: Malte Muench <mamu@stablerock.de> * Removes unused help function Signed-off-by: Malte Muench <mamu@stablerock.de> --------- Signed-off-by: Malte Muench <mamu@stablerock.de> Co-authored-by: Malte Muench <mamu@stablerock.de>
1 parent 3c920dd commit fdff89c

File tree

13 files changed

+44
-127
lines changed

13 files changed

+44
-127
lines changed

src/can2mqtt.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/internal/canbus.go renamed to src/canbus.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Package internal of c3re/can2mqtt contains some tools for bridging a CAN-Interface
2-
// and a mqtt-network
3-
package internal
1+
package main
42

53
import (
64
"github.com/brutella/can"
File renamed without changes.

src/internal/main.go renamed to src/main.go

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package internal
1+
package main
22

33
import (
44
"bufio" // Reader
55
"encoding/csv" // CSV Management
6-
"fmt" // print :)
7-
"github.com/brutella/can"
8-
"github.com/c3re/can2mqtt/internal/convertfunctions"
6+
"flag"
7+
"github.com/c3re/can2mqtt/convertfunctions"
98
"io" // EOF const
109
"log" // error management
1110
"log/slog"
@@ -14,86 +13,36 @@ import (
1413
"sync"
1514
)
1615

17-
type convertToCan func(input []byte) (can.Frame, error)
18-
type convertToMqtt func(input can.Frame) ([]byte, error)
19-
20-
type ConvertMode interface {
21-
convertToCan
22-
convertToMqtt
23-
}
24-
25-
// can2mqtt is a struct that represents the internal type of
26-
// one line of the can2mqtt.csv file. It has
27-
// the same three fields as the can2mqtt.csv file: CAN-ID,
28-
// conversion method and MQTT-Topic.
29-
type can2mqtt struct {
30-
canId uint32
31-
convMethod string
32-
toCan convertToCan
33-
toMqtt convertToMqtt
34-
mqttTopic string
35-
}
36-
3716
var pairFromID map[uint32]*can2mqtt // c2m pair (lookup from ID)
3817
var pairFromTopic map[string]*can2mqtt // c2m pair (lookup from Topic)
39-
var dbg = false // verbose on off [-v]
40-
var ci = "can0" // the CAN-Interface [-c]
41-
var cs = "tcp://localhost:1883" // mqtt-connect-string [-m]
42-
var c2mf = "can2mqtt.csv" // path to the can2mqtt.csv [-f]
43-
var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
18+
var debugLog bool
19+
var canInterface, mqttConnection, configFile string
20+
var dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
4421
var wg sync.WaitGroup
4522

46-
// SetDbg decides whether there is really verbose output or
47-
// just standard information output. Default is false.
48-
func SetDbg(v bool) {
49-
dbg = v
50-
slog.SetLogLoggerLevel(slog.LevelDebug)
51-
}
52-
53-
// SetCi sets the CAN-Interface to use for the CAN side
54-
// of the bridge. Default is: can0.
55-
func SetCi(c string) {
56-
ci = c
57-
}
23+
func main() {
24+
log.SetFlags(0)
5825

59-
// SetC2mf expects a string which is a path to a can2mqtt.csv file
60-
// Default is: can2mqtt.csv
61-
func SetC2mf(f string) {
62-
c2mf = f
63-
}
26+
flag.BoolVar(&debugLog, "v", false, "show (very) verbose debug log")
27+
flag.StringVar(&canInterface, "c", "can0", "which socket-can interface to use")
28+
flag.StringVar(&mqttConnection, "m", "tcp://localhost:1883", "which mqtt-broker to use. Example: tcp://user:password@broker.hivemq.com:1883")
29+
flag.StringVar(&configFile, "f", "can2mqtt.csv", "which config file to use")
30+
flag.IntVar(&dirMode, "d", 0, "direction mode\n0: bidirectional (default)\n1: can2mqtt only\n2: mqtt2can only")
31+
flag.Parse()
6432

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

71-
// SetConfDirMode sets the dirMode
72-
func SetConfDirMode(s string) {
73-
if s == "0" {
74-
dirMode = 0
75-
} else if s == "1" {
76-
dirMode = 1
77-
} else if s == "2" {
78-
dirMode = 2
79-
} else {
80-
_ = fmt.Errorf("error: got invalid value for -d (%s). Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", s)
37+
if debugLog {
38+
slog.SetLogLoggerLevel(slog.LevelDebug)
8139
}
82-
}
8340

84-
// Start is the function that should be called after debug-level
85-
// connect-string, can interface and can2mqtt file have been set.
86-
// Start takes care of everything that happens after that.
87-
// It starts the CAN-Bus connection and the MQTT-Connection. It
88-
// parses the can2mqtt.csv file and from there everything takes
89-
// its course...
90-
func Start() {
91-
log.SetFlags(0)
92-
slog.Info("Starting can2mqtt", "mqtt-config", cs, "can-interface", ci, "can2mqtt.csv", c2mf, "dir-mode", dirMode, "debug", dbg)
41+
slog.Info("Starting can2mqtt", "mqtt-config", mqttConnection, "can-interface", canInterface, "can2mqtt.csv", configFile, "dir-mode", dirMode, "debug", debugLog)
9342
wg.Add(1)
94-
go canStart(ci) // epic parallel shit ;-)
95-
mqttStart(cs)
96-
readC2MPFromFile(c2mf)
43+
go canStart(canInterface) // epic parallel shit ;-)
44+
mqttStart(mqttConnection)
45+
readC2MPFromFile(configFile)
9746
wg.Wait()
9847
}
9948

src/internal/mqtt.go renamed to src/mqtt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package main
22

33
import (
44
MQTT "github.com/eclipse/paho.mqtt.golang"

src/internal/receiving.go renamed to src/receiving.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package main
22

33
import (
44
"github.com/brutella/can"

src/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "github.com/brutella/can"
4+
5+
type convertToCan func(input []byte) (can.Frame, error)
6+
type convertToMqtt func(input can.Frame) ([]byte, error)
7+
8+
// can2mqtt is a struct that represents the internal type of
9+
// one line of the can2mqtt.csv file. It has
10+
// the same three fields as the can2mqtt.csv file: CAN-ID,
11+
// conversion method and MQTT-Topic.
12+
type can2mqtt struct {
13+
canId uint32
14+
convMethod string
15+
toCan convertToCan
16+
toMqtt convertToMqtt
17+
mqttTopic string
18+
}

0 commit comments

Comments
 (0)