-
Notifications
You must be signed in to change notification settings - Fork 36
/
sensor.go
70 lines (59 loc) · 1.65 KB
/
sensor.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
package main
import (
"fmt"
"io"
"strings"
"text/template"
)
type SensorComponent struct {
Azimuth float64 `yaml:"azimuth"`
Dip float64 `yaml:"dip"`
}
type SensorModel struct {
Type string `yaml:"type"`
Description string `yaml:"description"`
Manufacturer string `yaml:"manufacturer"`
Vendor string `yaml:"vendor"`
Components []SensorComponent `yaml:"components"`
}
func (s SensorModel) Desc(name string) string {
return fmt.Sprintf("%s %s %s", s.Manufacturer, strings.Split(strings.Fields(name)[0], "/")[0], s.Type)
}
func (s SensorModel) Make() string {
switch parts := strings.Fields(s.Manufacturer); {
case len(parts) > 0:
label := strings.Join(parts, " ")
for _, s := range []string{"/", ".", "+", " "} {
label = strings.ReplaceAll(label, s, "-")
}
return strings.TrimRight(label, "-")
default:
return "Unknown"
}
}
var sensorModelTemplate = `
var SensorModels map[string]SensorModel = map[string]SensorModel{
{{ range $k, $v := . }} "{{ $k}}": SensorModel{
Name: "{{$k}}",
Type: "{{$v.Type}}",
Description: "{{$v.Desc $k}}",
Manufacturer: "{{$v.Manufacturer}}",
Vendor: "{{$v.Vendor}}",
Components: []SensorComponent{{"{"}}{{ range $z := $v.Components}}SensorComponent{Azimuth: {{ $z.Azimuth }}, Dip: {{ $z.Dip }}{{"}"}},{{end}}{{"}"}},
},
{{end}}{{"}"}}
`
func sensormodel(w io.Writer, dl map[string]SensorModel) error {
t, err := template.New("sensormodels").Funcs(
template.FuncMap{
"escape": func(s string) string { return strings.Join(strings.Fields(s), " ") },
},
).Parse(sensorModelTemplate)
if err != nil {
return err
}
if err := t.Execute(w, dl); err != nil {
return err
}
return nil
}