From c02603c61da4a478d6850527beefd5aa9e1dfe5f Mon Sep 17 00:00:00 2001 From: Malte Muench Date: Fri, 10 May 2024 20:37:52 +0200 Subject: [PATCH 1/3] works Signed-off-by: Malte Muench --- src/can2mqtt.go | 48 ----------------- src/internal/canbus.go | 4 +- src/internal/main.go | 106 ++++++++++++-------------------------- src/internal/mqtt.go | 2 +- src/internal/receiving.go | 2 +- src/internal/types.go | 18 +++++++ 6 files changed, 54 insertions(+), 126 deletions(-) delete mode 100644 src/can2mqtt.go create mode 100644 src/internal/types.go diff --git a/src/can2mqtt.go b/src/can2mqtt.go deleted file mode 100644 index c7709ee..0000000 --- a/src/can2mqtt.go +++ /dev/null @@ -1,48 +0,0 @@ -// minimal main package, only commandline argument parsing and a printHelp() function -package main - -import ( - "fmt" // print - C2M "github.com/c3re/can2mqtt/internal" - "os" // args -) - -// Parses commandline arguments -func main() { - conf := true - for i := 1; i < len(os.Args); i++ { - switch os.Args[i] { - case "-v": - C2M.SetDbg(true) - case "-c": - i++ - C2M.SetCi(os.Args[i]) - case "-m": - i++ - C2M.SetCs(os.Args[i]) - case "-f": - i++ - C2M.SetC2mf(os.Args[i]) - case "-d": - i++ - C2M.SetConfDirMode(os.Args[i]) - default: - i = len(os.Args) - conf = false - printHelp() - } - } - if conf { - C2M.Start() - } -} - -// help function (obvious...) -func printHelp() { - _, _ = fmt.Fprintf(os.Stderr, "welcome to the CAN2MQTT bridge!\n\n") - _, _ = fmt.Fprintf(os.Stderr, "Usage: can2mqtt [-f ] [-c ] [-m ] [-v] [-h] [-d ]\n") - _, _ = fmt.Fprintf(os.Stderr, ": a can2mqtt.csv file\n") - _, _ = fmt.Fprintf(os.Stderr, ": a CAN-Interface e.g. can0\n") - _, _ = fmt.Fprintf(os.Stderr, ": connectstring for MQTT. e.g.: tcp://[user:pass@]localhost:1883\n") - _, _ = fmt.Fprintf(os.Stderr, ": directional Mode 0 = bidirectional, 1 = can2mqtt only, 2 = mqtt2can only\n") -} diff --git a/src/internal/canbus.go b/src/internal/canbus.go index 6bc9bb2..75c0573 100644 --- a/src/internal/canbus.go +++ b/src/internal/canbus.go @@ -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" diff --git a/src/internal/main.go b/src/internal/main.go index 7b5a6f3..9b3f7ea 100644 --- a/src/internal/main.go +++ b/src/internal/main.go @@ -1,10 +1,10 @@ -package internal +package main import ( "bufio" // Reader "encoding/csv" // CSV Management - "fmt" // print :) - "github.com/brutella/can" + "flag" + "fmt" // print :) "github.com/c3re/can2mqtt/internal/convertfunctions" "io" // EOF const "log" // error management @@ -14,86 +14,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:password@broker.hivemq.com: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() } @@ -323,3 +273,13 @@ func isTopicInSlice(mqttTopic string) bool { func getTopicFromId(canId uint32) string { return pairFromID[canId].mqttTopic } + +// help function (obvious...) +func printHelp() { + _, _ = fmt.Fprintf(os.Stderr, "welcome to the CAN2MQTT bridge!\n\n") + _, _ = fmt.Fprintf(os.Stderr, "Usage: can2mqtt [-f ] [-c ] [-m ] [-v] [-h] [-d ]\n") + _, _ = fmt.Fprintf(os.Stderr, ": a can2mqtt.csv file\n") + _, _ = fmt.Fprintf(os.Stderr, ": a CAN-Interface e.g. can0\n") + _, _ = fmt.Fprintf(os.Stderr, ": connectstring for MQTT. e.g.: tcp://[user:pass@]localhost:1883\n") + _, _ = fmt.Fprintf(os.Stderr, ": directional Mode 0 = bidirectional, 1 = can2mqtt only, 2 = mqtt2can only\n") +} diff --git a/src/internal/mqtt.go b/src/internal/mqtt.go index 8cf9341..4a8a062 100644 --- a/src/internal/mqtt.go +++ b/src/internal/mqtt.go @@ -1,4 +1,4 @@ -package internal +package main import ( MQTT "github.com/eclipse/paho.mqtt.golang" diff --git a/src/internal/receiving.go b/src/internal/receiving.go index eee10e9..ae94b3e 100644 --- a/src/internal/receiving.go +++ b/src/internal/receiving.go @@ -1,4 +1,4 @@ -package internal +package main import ( "github.com/brutella/can" diff --git a/src/internal/types.go b/src/internal/types.go new file mode 100644 index 0000000..cb87f5f --- /dev/null +++ b/src/internal/types.go @@ -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 +} From 4dd69292517c2e32a43f45ae971dc3b2017286fa Mon Sep 17 00:00:00 2001 From: Malte Muench Date: Fri, 10 May 2024 20:43:08 +0200 Subject: [PATCH 2/3] removed one unused folder layer Signed-off-by: Malte Muench --- src/{internal => }/canbus.go | 0 src/{internal => }/convertfunctions/bytecolor2colorcode.go | 0 src/{internal => }/convertfunctions/int2ascii.go | 0 src/{internal => }/convertfunctions/none.go | 0 src/{internal => }/convertfunctions/none_test.go | 0 src/{internal => }/convertfunctions/pixelbin2ascii.go | 0 src/{internal => }/convertfunctions/sixteenbool2ascii.go | 0 src/{internal => }/convertfunctions/uint2ascii.go | 0 src/{internal => }/main.go | 2 +- src/{internal => }/mqtt.go | 0 src/{internal => }/receiving.go | 0 src/{internal => }/types.go | 0 12 files changed, 1 insertion(+), 1 deletion(-) rename src/{internal => }/canbus.go (100%) rename src/{internal => }/convertfunctions/bytecolor2colorcode.go (100%) rename src/{internal => }/convertfunctions/int2ascii.go (100%) rename src/{internal => }/convertfunctions/none.go (100%) rename src/{internal => }/convertfunctions/none_test.go (100%) rename src/{internal => }/convertfunctions/pixelbin2ascii.go (100%) rename src/{internal => }/convertfunctions/sixteenbool2ascii.go (100%) rename src/{internal => }/convertfunctions/uint2ascii.go (100%) rename src/{internal => }/main.go (99%) rename src/{internal => }/mqtt.go (100%) rename src/{internal => }/receiving.go (100%) rename src/{internal => }/types.go (100%) diff --git a/src/internal/canbus.go b/src/canbus.go similarity index 100% rename from src/internal/canbus.go rename to src/canbus.go diff --git a/src/internal/convertfunctions/bytecolor2colorcode.go b/src/convertfunctions/bytecolor2colorcode.go similarity index 100% rename from src/internal/convertfunctions/bytecolor2colorcode.go rename to src/convertfunctions/bytecolor2colorcode.go diff --git a/src/internal/convertfunctions/int2ascii.go b/src/convertfunctions/int2ascii.go similarity index 100% rename from src/internal/convertfunctions/int2ascii.go rename to src/convertfunctions/int2ascii.go diff --git a/src/internal/convertfunctions/none.go b/src/convertfunctions/none.go similarity index 100% rename from src/internal/convertfunctions/none.go rename to src/convertfunctions/none.go diff --git a/src/internal/convertfunctions/none_test.go b/src/convertfunctions/none_test.go similarity index 100% rename from src/internal/convertfunctions/none_test.go rename to src/convertfunctions/none_test.go diff --git a/src/internal/convertfunctions/pixelbin2ascii.go b/src/convertfunctions/pixelbin2ascii.go similarity index 100% rename from src/internal/convertfunctions/pixelbin2ascii.go rename to src/convertfunctions/pixelbin2ascii.go diff --git a/src/internal/convertfunctions/sixteenbool2ascii.go b/src/convertfunctions/sixteenbool2ascii.go similarity index 100% rename from src/internal/convertfunctions/sixteenbool2ascii.go rename to src/convertfunctions/sixteenbool2ascii.go diff --git a/src/internal/convertfunctions/uint2ascii.go b/src/convertfunctions/uint2ascii.go similarity index 100% rename from src/internal/convertfunctions/uint2ascii.go rename to src/convertfunctions/uint2ascii.go diff --git a/src/internal/main.go b/src/main.go similarity index 99% rename from src/internal/main.go rename to src/main.go index 9b3f7ea..47e4fa8 100644 --- a/src/internal/main.go +++ b/src/main.go @@ -5,7 +5,7 @@ import ( "encoding/csv" // CSV Management "flag" "fmt" // print :) - "github.com/c3re/can2mqtt/internal/convertfunctions" + "github.com/c3re/can2mqtt/convertfunctions" "io" // EOF const "log" // error management "log/slog" diff --git a/src/internal/mqtt.go b/src/mqtt.go similarity index 100% rename from src/internal/mqtt.go rename to src/mqtt.go diff --git a/src/internal/receiving.go b/src/receiving.go similarity index 100% rename from src/internal/receiving.go rename to src/receiving.go diff --git a/src/internal/types.go b/src/types.go similarity index 100% rename from src/internal/types.go rename to src/types.go From 0a9cc9c99fccc042c0f24c226c2373f6e5165793 Mon Sep 17 00:00:00 2001 From: Malte Muench Date: Fri, 10 May 2024 20:51:00 +0200 Subject: [PATCH 3/3] Removes unused help function Signed-off-by: Malte Muench --- src/main.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main.go b/src/main.go index 47e4fa8..f181185 100644 --- a/src/main.go +++ b/src/main.go @@ -4,7 +4,6 @@ import ( "bufio" // Reader "encoding/csv" // CSV Management "flag" - "fmt" // print :) "github.com/c3re/can2mqtt/convertfunctions" "io" // EOF const "log" // error management @@ -273,13 +272,3 @@ func isTopicInSlice(mqttTopic string) bool { func getTopicFromId(canId uint32) string { return pairFromID[canId].mqttTopic } - -// help function (obvious...) -func printHelp() { - _, _ = fmt.Fprintf(os.Stderr, "welcome to the CAN2MQTT bridge!\n\n") - _, _ = fmt.Fprintf(os.Stderr, "Usage: can2mqtt [-f ] [-c ] [-m ] [-v] [-h] [-d ]\n") - _, _ = fmt.Fprintf(os.Stderr, ": a can2mqtt.csv file\n") - _, _ = fmt.Fprintf(os.Stderr, ": a CAN-Interface e.g. can0\n") - _, _ = fmt.Fprintf(os.Stderr, ": connectstring for MQTT. e.g.: tcp://[user:pass@]localhost:1883\n") - _, _ = fmt.Fprintf(os.Stderr, ": directional Mode 0 = bidirectional, 1 = can2mqtt only, 2 = mqtt2can only\n") -}