1
- package internal
1
+ package main
2
2
3
3
import (
4
4
"bufio" // Reader
5
5
"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"
9
8
"io" // EOF const
10
9
"log" // error management
11
10
"log/slog"
@@ -14,86 +13,36 @@ import (
14
13
"sync"
15
14
)
16
15
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
-
37
16
var pairFromID map [uint32 ]* can2mqtt // c2m pair (lookup from ID)
38
17
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]
44
21
var wg sync.WaitGroup
45
22
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 )
58
25
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\n 0: bidirectional (default)\n 1: can2mqtt only\n 2: mqtt2can only" )
31
+ flag .Parse ()
64
32
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
+ }
70
36
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 )
81
39
}
82
- }
83
40
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 )
93
42
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 )
97
46
wg .Wait ()
98
47
}
99
48
0 commit comments