-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
139 lines (125 loc) · 4.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio" // Reader
"encoding/csv" // CSV Management
"flag"
"github.com/c3re/can2mqtt/convertmode"
"io" // EOF const
"log" // error management
"log/slog"
"os" // open files
"strconv" // parse strings
"sync"
)
var (
pairFromID map[uint32]*can2mqtt // c2m pair (lookup from ID)
pairFromTopic map[string]*can2mqtt // c2m pair (lookup from Topic)
convertModeFromString map[string]ConvertMode
debugLog bool
canInterface, mqttConnection, configFile string
version = "dev"
dirMode = BIDIRECTIONAL // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
wg sync.WaitGroup
)
func main() {
log.SetFlags(0)
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()
if dirMode < BIDIRECTIONAL || dirMode > MQTT2CAN_ONLY {
slog.Error("got invalid value for -d. Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", "d", dirMode)
}
if debugLog {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
slog.Info("Starting can2mqtt", "version", version, "mqtt-config", mqttConnection, "can-interface", canInterface, "can2mqtt.csv", configFile, "dir-mode", dirMode, "debug", debugLog)
wg.Add(1)
go canStart(canInterface) // epic parallel shit ;-)
mqttStart(mqttConnection)
readC2MPFromFile(configFile)
wg.Wait()
}
// this functions opens, parses and extracts information out
// of the can2mqtt.csv
func readC2MPFromFile(filename string) {
file, err := os.Open(filename)
if err != nil {
slog.Error("can2mqtt.csv could not be opened", "filename", filename, "error", err)
os.Exit(1)
}
r := csv.NewReader(bufio.NewReader(file))
r.FieldsPerRecord = 3
pairFromID = make(map[uint32]*can2mqtt)
pairFromTopic = make(map[string]*can2mqtt)
convertModeFromString = make(map[string]ConvertMode)
// initialize all convertModes
convertModeFromString[convertmode.None{}.String()] = convertmode.None{}
convertModeFromString[convertmode.SixteenBool2Ascii{}.String()] = convertmode.SixteenBool2Ascii{}
convertModeFromString[convertmode.PixelBin2Ascii{}.String()] = convertmode.PixelBin2Ascii{}
convertModeFromString[convertmode.ByteColor2ColorCode{}.String()] = convertmode.ByteColor2ColorCode{}
convertModeFromString[convertmode.MyMode{}.String()] = convertmode.MyMode{}
// Dynamically create int and uint convertmodes
for _, bits := range []uint{8, 16, 32, 64} {
for _, instances := range []uint{1, 2, 4, 8} {
if bits*instances <= 64 {
// int
cmi, _ := convertmode.NewInt2Ascii(instances, bits)
convertModeFromString[cmi.String()] = cmi
// uint
cmu, _ := convertmode.NewUint2Ascii(instances, bits)
convertModeFromString[cmu.String()] = cmu
}
}
}
if debugLog {
for _, cm := range convertModeFromString {
slog.Debug("convertmode initialized", "convertmode", cm)
}
}
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
if err != nil {
slog.Warn("skipping line", "filename", filename, "error", err)
continue
}
line, _ := r.FieldPos(0)
tmp, err := strconv.ParseUint(record[0], 10, 32)
if err != nil {
slog.Warn("skipping line, malformed can-ID", "error", err, "line", line)
continue
}
canID := uint32(tmp)
convMode := record[1]
topic := record[2]
if pairFromID[canID] != nil {
slog.Warn("skipping line, duplicate ID", "id", canID, "line", line)
continue
}
if pairFromTopic[topic] != nil {
slog.Warn("skipping line duplicate topic", "topic", topic, "line", line)
continue
}
if convertModeFromString[convMode] == nil {
slog.Warn("skipping line, unsupported convertMode ", "convertMode", convMode, "line", line)
continue
}
pairFromID[canID] = &can2mqtt{
canId: canID,
convertMode: convertModeFromString[convMode],
mqttTopic: topic,
}
pairFromTopic[topic] = pairFromID[canID]
mqttSubscribe(topic) // TODO move to append function
canSubscribe(canID) // TODO move to append function
}
for _, c2mp := range pairFromID {
slog.Debug("extracted pair", "id", c2mp.canId, "convertmode", c2mp.convertMode, "topic", c2mp.mqttTopic)
}
}