-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature refactor processor and parser (#6)
- Loading branch information
Showing
18 changed files
with
547 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# examples | ||
|
||
All examples allow you to run with the supplied `ibt` file or with your own. | ||
|
||
For example: | ||
|
||
|
||
```shell | ||
go run examples/[Example Folder]/main.go | ||
|
||
# Or | ||
|
||
go run examples/[Example Folder]/main.go /path/to/telem/files/*.ibt | ||
``` | ||
|
||
Available examples: | ||
|
||
* [track temperature](./track_temp/README.md) - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package examples | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/teamjorge/ibt" | ||
) | ||
|
||
// Use the default testing file if no ibt files were provided | ||
// Provided files can use wildcards, for example ./telemetry/*.ibt | ||
func getExampleFilePattern() string { | ||
flag.Parse() | ||
|
||
var filePattern string | ||
if flag.Arg(0) == "" { | ||
filePattern = ".testing/valid_test_file.ibt" | ||
} else { | ||
filePattern = flag.Arg(0) | ||
} | ||
|
||
return filePattern | ||
} | ||
|
||
func ParseExampleStubs() (ibt.StubGroup, error) { | ||
filePattern := getExampleFilePattern() | ||
|
||
// Find all files for parsing in case it had included a wildcard | ||
files, err := filepath.Glob(filePattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not glob the given input files: %v", err) | ||
} | ||
|
||
// Parse the files into stubs | ||
stubs, err := ibt.ParseStubs(files...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse stubs for %v. error - %v", files, err) | ||
} | ||
|
||
return stubs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# track_temp | ||
|
||
The `track_temp` example shows a simple processor summarising the track temperature on each lap of the provided `ibt` files. | ||
|
||
## Running | ||
|
||
From the root of the repository: | ||
|
||
```shell | ||
go run examples/track_temp/main.go | ||
|
||
# Or with your own files | ||
|
||
go run examples/track_temp/main.go /path/to/telem/files/*.ibt | ||
``` | ||
|
||
Using the included `ibt` file will yield only a single lap and it's value. However, if you have telemetry consisting of a few laps and/or files from a longer session, you should have a nicely summarised per-lap output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"context" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
|
||
"github.com/teamjorge/ibt" | ||
"github.com/teamjorge/ibt/headers" | ||
"github.com/teamjorge/ibt/utilities" | ||
"golang.org/x/exp/maps" | ||
"github.com/teamjorge/ibt/examples" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
// Use the default testing file if no ibt files were provided | ||
// Provided files can use wildcards, for example ./telemetry/*.ibt | ||
var filePattern string | ||
if flag.Arg(0) == "" { | ||
filePattern = ".testing/valid_test_file.ibt" | ||
} else { | ||
filePattern = flag.Arg(0) | ||
} | ||
|
||
// Find all files for parsing in case it had included a wildcard | ||
files, err := filepath.Glob(filePattern) | ||
if err != nil { | ||
log.Fatalf("could not glob the given input files: %v", err) | ||
} | ||
|
||
// Parse the files into stubs | ||
stubs, err := ibt.ParseStubs(files...) | ||
stubs, err := examples.ParseExampleStubs() | ||
if err != nil { | ||
log.Fatalf("failed to parse stubs for %v. error - %v", files, err) | ||
log.Fatal(err) | ||
} | ||
|
||
// Group the stubs into iRacing sessions | ||
stubGroups := stubs.Group() | ||
|
||
for _, stubGroup := range stubGroups { | ||
for _, stub := range stubGroup { | ||
stubFile, err := os.Open(stub.Filename()) | ||
if err != nil { | ||
log.Fatalf("failed to open stub file %s for reading: %v", stub.Filename(), err) | ||
} | ||
// We group the stubs by iRacing session. This allows us to summarise results for | ||
// an entire session, instead of just a single ibt file. | ||
groups := stubs.Group() | ||
|
||
// Create the instance(s) of your processor(s) | ||
processor := NewTrackTempProcessor() | ||
for groupIdx, group := range groups { | ||
// Create the instance(s) of your processor(s) for this group | ||
processor := newTrackTempProcessor() | ||
|
||
// Process the available telemetry for the ibt file. This is currently only utilising the Track Temp processor, | ||
// but can include as many as you want. | ||
if err := ibt.Process(stubFile, *stub.Headers(), processor); err != nil { | ||
log.Fatalf("failed to process telemetry for stub %s: %v", stub.Filename(), err) | ||
} | ||
|
||
// Print the summarised track temperature | ||
processor.Print() | ||
// Process the available telemetry for the ibt file. This is currently only utilising the Track Temp processor, | ||
// but can include as many as you want. | ||
if err := ibt.Process(context.Background(), group, processor); err != nil { | ||
log.Fatalf("failed to process telemetry for group %d: %v", groupIdx, err) | ||
} | ||
} | ||
|
||
} | ||
|
||
type TrackTempProcessor struct { | ||
tempMap map[int]float32 | ||
} | ||
|
||
func NewTrackTempProcessor() *TrackTempProcessor { | ||
t := new(TrackTempProcessor) | ||
|
||
t.tempMap = make(map[int]float32) | ||
|
||
return t | ||
} | ||
|
||
// Display name of the processor | ||
func (t *TrackTempProcessor) Name() string { return "Track Temp" } | ||
|
||
// Method used for processing every tick of telemetry | ||
func (t *TrackTempProcessor) Process(input map[string]headers.VarHeader, hasNext bool, session *headers.Session) error { | ||
TrackTempProcessor := input["TrackTempCrew"].Value.(float32) | ||
lap := input["Lap"].Value.(int) | ||
|
||
t.tempMap[lap] = TrackTempProcessor | ||
|
||
return nil | ||
} | ||
|
||
// Utility function for create a result that can be joined with other processors. | ||
// | ||
// This will convert the results to map[int]interface{}, where the keys will refer to laps. | ||
// Result is not yet required by any interfaces, but is useful when using multiple processors | ||
// that summarise telemetry based by lap. | ||
func (t *TrackTempProcessor) Result() map[int]interface{} { | ||
return utilities.CreateGenericMap(t.tempMap) | ||
} | ||
|
||
// Columns required for the processor | ||
func (t *TrackTempProcessor) Whitelist() []string { return []string{"Lap", "TrackTempCrew"} } | ||
|
||
// Print the summarised Track Temperature | ||
func (t *TrackTempProcessor) Print() { | ||
fmt.Println("Track Temp:") | ||
laps := maps.Keys(t.tempMap) | ||
sort.Ints(laps) | ||
|
||
for _, lap := range laps { | ||
fmt.Printf("%03d - %.3f\n", lap, t.tempMap[lap]) | ||
// Print the summarised track temperature | ||
processor.Print() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/teamjorge/ibt" | ||
"github.com/teamjorge/ibt/headers" | ||
"github.com/teamjorge/ibt/utilities" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// TrackTempProcessors tracks the track temperature for each lap of the ibt file | ||
type trackTempProcessor struct { | ||
tempMap map[int]float32 | ||
} | ||
|
||
// NewTrackTempProcessor creates and initialises a new trackTempProcessor | ||
func newTrackTempProcessor() *trackTempProcessor { | ||
t := new(trackTempProcessor) | ||
|
||
// tempMap will store a temperature value against a lap number | ||
t.tempMap = make(map[int]float32) | ||
|
||
return t | ||
} | ||
|
||
// Display name of the processor | ||
func (t *trackTempProcessor) Name() string { return "Track Temp" } | ||
|
||
// Method used for processing every tick of telemetry | ||
func (t *trackTempProcessor) Process(input ibt.Tick, hasNext bool, session *headers.Session) error { | ||
trackTemp, err := ibt.GetVariableValue[float32](input, "TrackTempCrew") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
lap, err := ibt.GetVariableValue[int](input, "Lap") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.tempMap[lap] = trackTemp | ||
|
||
return nil | ||
} | ||
|
||
// Utility function for create a result that can be joined with other processors. | ||
// | ||
// This will convert the results to map[int]interface{}, where the keys will refer to laps. | ||
// Result is not yet required by any interfaces, but is useful when using multiple processors | ||
// that summarise telemetry based by lap. | ||
func (t *trackTempProcessor) Result() map[int]interface{} { | ||
return utilities.CreateGenericMap(t.tempMap) | ||
} | ||
|
||
// Columns required for the processor | ||
func (t *trackTempProcessor) Whitelist() []string { return []string{"Lap", "TrackTempCrew"} } | ||
|
||
// Print the summarised Track Temperature | ||
func (t *trackTempProcessor) Print() { | ||
fmt.Println("Track Temp:") | ||
laps := maps.Keys(t.tempMap) | ||
sort.Ints(laps) | ||
|
||
for _, lap := range laps { | ||
fmt.Printf("%03d - %.3f\n", lap, t.tempMap[lap]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.