aboutsummaryrefslogtreecommitdiffstats
path: root/compressor/compressor.go
diff options
context:
space:
mode:
Diffstat (limited to 'compressor/compressor.go')
-rw-r--r--compressor/compressor.go116
1 files changed, 70 insertions, 46 deletions
diff --git a/compressor/compressor.go b/compressor/compressor.go
index fc12f26..4a30c19 100644
--- a/compressor/compressor.go
+++ b/compressor/compressor.go
@@ -1,12 +1,17 @@
package compressor
import (
+ "github.com/BurntSushi/toml"
+ "io/fs"
+ fp "path/filepath"
"time"
"github.com/sam-anthony/volute/mass"
"github.com/sam-anthony/volute/util"
)
+const root = "compressor/res/"
+
type Compressor struct {
Name string
FileName string
@@ -27,60 +32,79 @@ type Compressor struct {
MaxPressureRatio float32
}
+// [manufacturer][series][model]
var compressors = make(map[string]map[string]map[string]Compressor)
func init() {
- compressors["Garrett"] = make(map[string]map[string]Compressor)
- compressors["Garrett"]["G"] = make(map[string]Compressor)
- compressors["Garrett"]["G"]["25-660"] = garrettG25660()
- compressors["BorgWarner"] = make(map[string]map[string]Compressor)
- compressors["BorgWarner"]["K"] = make(map[string]Compressor)
- compressors["BorgWarner"]["K"]["03"] = borgwarnerK03()
- compressors["BorgWarner"]["K"]["04"] = borgwarnerK04()
- compressors["BorgWarner"]["EFR"] = make(map[string]Compressor)
- compressors["BorgWarner"]["EFR"]["6258"] = borgwarnerEFR6258()
-}
+ // Walk root, looking for .toml files describing a compressor.
+ // Parse these toml files, create a Compressor and add it to compressors.
+ err := fp.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
-func Compressors() map[string]map[string]map[string]Compressor {
- return compressors
-}
+ if fp.Ext(path) != ".toml" {
+ return nil
+ }
-func garrettG25660() Compressor {
- maxFlow, err := mass.NewFlowRate(
- mass.Mass{70, mass.Pound},
- time.Minute,
- mass.PoundsPerMinute,
- )
+ path = path[len(root):]
+
+ // manufacturer/series, model
+ manSer, mod := fp.Split(path)
+ manSer = fp.Clean(manSer) // Clean trailing slash
+ mod = mod[:len(mod)-len(".toml")] // Trim .toml extension
+ // manufacturer, series
+ man, ser := fp.Split(manSer)
+ man = fp.Clean(man) // Clean trailing slash
+
+ var exists bool
+ _, exists = compressors[man]
+ if !exists {
+ compressors[man] = make(map[string]map[string]Compressor)
+ }
+ _, exists = compressors[man][ser]
+ if !exists {
+ compressors[man][ser] = make(map[string]Compressor)
+ }
+
+ tomlFile := fp.Join(root, path)
+
+ var c Compressor
+ _, err = toml.DecodeFile(tomlFile, &c)
+ if err != nil {
+ return err
+ }
+
+ // Replace .toml with .jpg
+ imageFile := tomlFile[:len(tomlFile)-len(".toml")] + ".jpg"
+ c.FileName = imageFile
+
+ // Must parse MaxFlow seperately because the MassFlowRateUnit
+ // is stored as a string and must be converted with
+ // FlowRateUnitFromString().
+ flow := struct {
+ FlowVal float32
+ FlowUnit string
+ }{}
+ _, err = toml.DecodeFile(tomlFile, &flow)
+ if err != nil {
+ return err
+ }
+ u, err := mass.FlowRateUnitFromString(flow.FlowUnit)
+ if err != nil {
+ return err
+ }
+ c.MaxFlow = mass.FlowRate{flow.FlowVal, u}
+
+ compressors[man][ser][mod] = c
+
+ return nil
+ })
util.Check(err)
- return Compressor{
- "Garrett G25-660",
- "compressor/res/garrett/g/25-660.jpg",
- 204,
- 1885,
- 1665,
- 25,
- maxFlow,
- 4.0,
- }
}
-func borgwarnerEFR6258() Compressor {
- maxFlow, err := mass.NewFlowRate(
- mass.Mass{0.50, mass.Kilogram},
- time.Second,
- mass.KilogramsPerSecond,
- )
- util.Check(err)
- return Compressor{
- "BorgWarner EFR6258",
- "compressor/res/borgwarner/efr/6258.jpg",
- 47,
- 455,
- 773,
- 6,
- maxFlow,
- 3.8,
- }
+func Compressors() map[string]map[string]map[string]Compressor {
+ return compressors
}
func borgwarnerK04() Compressor {