2023-05-20 19:53:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2024-02-19 04:41:22 +00:00
|
|
|
"log/slog"
|
|
|
|
|
2024-02-12 20:38:01 +00:00
|
|
|
"github.com/kschamplin/gotelem/internal/logparsers"
|
2023-05-20 19:53:56 +00:00
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// this command can be used to decode candump logs and dump json output.
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "skylabify"
|
|
|
|
app.Usage = "decode skylab packets"
|
|
|
|
app.ArgsUsage = "<input file>"
|
|
|
|
app.Commands = nil
|
|
|
|
app.Description = `skylabify can read in candump logs and output newline-delimited JSON.
|
|
|
|
It is designed to make reading candumps fast and easy.
|
|
|
|
|
|
|
|
skylabify can be combined with jq and candump to allow for advanced queries.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
skylabify candump.txt
|
|
|
|
|
|
|
|
candump -L can0 | skylabify -
|
|
|
|
|
|
|
|
skylabify previous_candump.txt | jq <some json query>
|
|
|
|
|
|
|
|
I highly suggest reading the manpages for candump and jq. The -L option is
|
|
|
|
required for piping candump into skylabify. Likewise, data should be stored with
|
|
|
|
-l.
|
|
|
|
|
|
|
|
`
|
2024-02-12 20:38:01 +00:00
|
|
|
parsersString := func() string {
|
|
|
|
// create a string like "'telem', 'candump', 'anotherparser'"
|
|
|
|
keys := make([]string, len(logparsers.ParsersMap))
|
|
|
|
i := 0
|
|
|
|
for k := range logparsers.ParsersMap {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
s := strings.Join(keys, "', '")
|
|
|
|
return "'" + s + "'"
|
|
|
|
}()
|
2023-05-20 19:53:56 +00:00
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
},
|
2024-02-12 15:45:23 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Aliases: []string{"f"},
|
2024-02-12 20:38:01 +00:00
|
|
|
Usage: "the format of the incoming data. One of " + parsersString,
|
2024-02-12 15:45:23 +00:00
|
|
|
},
|
2023-05-20 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Action = run
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(ctx *cli.Context) (err error) {
|
|
|
|
path := ctx.Args().Get(0)
|
|
|
|
if path == "" {
|
2023-07-07 19:03:53 +00:00
|
|
|
fmt.Println("missing input file")
|
2023-05-20 19:53:56 +00:00
|
|
|
cli.ShowAppHelpAndExit(ctx, int(syscall.EINVAL))
|
|
|
|
}
|
|
|
|
|
|
|
|
var istream *os.File
|
|
|
|
if path == "-" {
|
|
|
|
istream = os.Stdin
|
|
|
|
} else {
|
|
|
|
istream, err = os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
fileReader := bufio.NewReader(istream)
|
|
|
|
|
2024-03-07 19:30:15 +00:00
|
|
|
var pfun logparsers.BusEventParser
|
2024-02-12 15:45:23 +00:00
|
|
|
|
2024-02-12 20:38:01 +00:00
|
|
|
pfun, ok := logparsers.ParsersMap[ctx.String("format")]
|
2024-02-12 15:45:23 +00:00
|
|
|
if !ok {
|
|
|
|
fmt.Println("invalid format!")
|
|
|
|
cli.ShowAppHelpAndExit(ctx, int(syscall.EINVAL))
|
|
|
|
}
|
|
|
|
|
|
|
|
n_err := 0
|
2024-02-19 04:41:22 +00:00
|
|
|
unknown_packets := 0
|
2023-05-20 19:53:56 +00:00
|
|
|
|
|
|
|
for {
|
2024-02-12 15:45:23 +00:00
|
|
|
line, err := fileReader.ReadString('\n')
|
2023-05-20 19:53:56 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-12 15:45:23 +00:00
|
|
|
return err // i/o failures are fatal
|
2023-05-20 19:53:56 +00:00
|
|
|
}
|
2024-02-12 15:45:23 +00:00
|
|
|
f, err := pfun(line)
|
2023-06-28 01:39:57 +00:00
|
|
|
var idErr *skylab.UnknownIdError
|
|
|
|
if errors.As(err, &idErr) {
|
|
|
|
// unknown id
|
|
|
|
slog.Info("unknown id", "err", err)
|
2024-02-12 15:45:23 +00:00
|
|
|
unknown_packets++
|
2023-07-01 03:08:06 +00:00
|
|
|
continue
|
2023-06-28 01:39:57 +00:00
|
|
|
} else if err != nil {
|
2024-02-12 15:45:23 +00:00
|
|
|
// TODO: we should consider absorbing all errors.
|
2024-02-14 08:15:21 +00:00
|
|
|
slog.Error("got an error", "err", err)
|
2024-02-12 15:45:23 +00:00
|
|
|
n_err++
|
|
|
|
continue
|
2023-05-20 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 13:50:07 +00:00
|
|
|
// format and print out the JSON.
|
2024-02-12 15:45:23 +00:00
|
|
|
out, _ := json.Marshal(&f)
|
2023-05-22 13:50:07 +00:00
|
|
|
fmt.Println(string(out))
|
2023-05-20 19:53:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|