-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcan_dump.go
107 lines (91 loc) · 3.12 KB
/
can_dump.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
package main
import (
"context"
"flag"
"os"
"strconv"
"time"
"github.com/DIMO-Network/edge-network/commands"
"github.com/DIMO-Network/edge-network/config"
"github.com/DIMO-Network/edge-network/internal/loggers"
"github.com/google/subcommands"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
// - To scan can bus and save local copy on autopi:
// ./edge-network candump -cycles <cycle_count> -save
// - To scan can bus and send chunked dump to mqtt:
// ./edge-network candump -cycles <cycle_count> [-send <chunk_size>]
// - To scan can bus and send chunked dump to mqtt AND save local copies on autopi:
// ./edge-network candump -cycles <cycle_count> [-send <chunk_size>] -save
type canDumpCmd struct {
unitID uuid.UUID
save bool
cycleCount int
chunkSize int
}
func (*canDumpCmd) Name() string { return "candump" }
func (*canDumpCmd) Synopsis() string {
return "Performs can dump to local file and/or remote file via MQTT"
}
func (*canDumpCmd) Usage() string {
return `candump [-cycles <int>] [-send <chunkSize_int>] [-save]`
}
func (p *canDumpCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.save, "save", false, "save result to local file")
f.IntVar(&p.cycleCount, "cycles", 100, "the qty of cycles to record in can dump, default=100")
f.IntVar(&p.chunkSize, "send", 0, "send result over mqtt to the s3 bucket using <chunk_size>")
}
func (p *canDumpCmd) Execute(_ context.Context, _ *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
log := zerolog.New(os.Stdout).With().
Timestamp().
Str("app", "edge-network").
Str("version", Version).
Logger().
Output(zerolog.ConsoleWriter{Out: os.Stdout})
println("cycleCount: " + strconv.Itoa(p.cycleCount))
println("chunkSize: " + strconv.Itoa(p.chunkSize))
currentTime, _ := time.Now().MarshalJSON()
currentTime = currentTime[1 : len(currentTime)-1]
ethAddr, ethErr := commands.GetEthereumAddress(unitID)
if ethErr != nil {
log.Err(ethErr).Send()
return subcommands.ExitFailure
}
if p.chunkSize > p.cycleCount {
log.Error().Msg("chunkSize cannot be greater than cycleCount")
return subcommands.ExitFailure
}
canDumperInstance := new(loggers.PassiveCanDumper)
canErr := canDumperInstance.ReadCanBus(p.cycleCount, 500000)
if canErr != nil {
log.Fatal().Err(canErr).Msg("canDumperInstance error")
return subcommands.ExitFailure
}
// read config file
conf, ok := args[0].(config.Config)
if !ok {
log.Error().Msg("unable to read config file")
return subcommands.ExitFailure
}
if p.chunkSize > 0 && p.save {
mqttErr := canDumperInstance.WriteToMQTT(log, unitID, *ethAddr, p.chunkSize, string(currentTime), true, conf)
if mqttErr != nil {
log.Err(mqttErr).Send()
return subcommands.ExitFailure
}
} else if p.chunkSize > 0 {
mqttErr := canDumperInstance.WriteToMQTT(log, unitID, *ethAddr, p.chunkSize, string(currentTime), true, conf)
if mqttErr != nil {
log.Err(mqttErr).Send()
return subcommands.ExitFailure
}
} else if p.save {
fileErr := canDumperInstance.WriteToFile("can_dump_" + string(currentTime))
if fileErr != nil {
log.Err(fileErr).Send()
return subcommands.ExitFailure
}
}
return subcommands.ExitSuccess
}