-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtypes.go
More file actions
251 lines (218 loc) Β· 5.78 KB
/
types.go
File metadata and controls
251 lines (218 loc) Β· 5.78 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package globalconfig
import (
"encoding/json"
"iter"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/ocpp"
"github.com/evcc-io/evcc/hems/shm"
"github.com/evcc-io/evcc/plugin/mqtt"
"github.com/evcc-io/evcc/server/eebus"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/modbus"
)
// ConfigStatus for publishing config, status and source to UI and external systems
type ConfigStatus struct {
Config any `json:"config,omitempty"`
Status any `json:"status,omitempty"`
YamlSource YamlSource `json:"yamlSource,omitempty"`
}
type YamlSource string
const (
YamlSourceFile YamlSource = "file"
YamlSourceDb YamlSource = "db"
YamlSourceNone YamlSource = ""
)
type All struct {
Network Network
Ocpp ocpp.Config
Log string
SponsorToken string
Plant string // telemetry plant id
Telemetry bool
Mcp bool
Metrics bool
Profile bool
Levels map[string]string
Interval time.Duration
Database DB
Mqtt Mqtt
ModbusProxy []ModbusProxy
OcppForwarder []OcppForwarder
Javascript []Javascript
Go []Go
Influx Influx
EEBus eebus.Config
HEMS Hems
SHM shm.Config
Messaging Messaging
MessagingEvents MessagingEvents
Meters []config.Named
Chargers []config.Named
Vehicles []config.Named
Tariffs Tariffs
Site map[string]any
Loadpoints []config.Named
Circuits []config.Named
}
// OcppForwarder is an alias for ocpp.ForwarderRule used in YAML/DB configuration.
type OcppForwarder = ocpp.ForwarderRule
type Javascript struct {
VM string
Script string
}
type Go struct {
VM string
Script string
}
type ModbusProxy struct {
Port int `json:"port"`
ReadOnly string `yaml:",omitempty" json:"readonly,omitempty"`
modbus.Settings `mapstructure:",squash" yaml:",inline,omitempty" json:"settings,omitempty"`
}
var _ api.Redactor = (*Hems)(nil)
type Hems config.Typed
func (c Hems) Redacted() any {
return struct {
Type string `json:"type,omitempty"`
}{
Type: c.Type,
}
}
var _ api.Redactor = (*Mqtt)(nil)
type Mqtt struct {
mqtt.Config `mapstructure:",squash"`
Topic string `json:"topic"`
}
// Redacted implements the redactor interface used by the tee publisher
func (m Mqtt) Redacted() any {
return Mqtt{
Config: mqtt.Config{
Broker: m.Broker,
User: m.User,
Password: util.Masked(m.Password),
ClientID: m.ClientID,
Insecure: m.Insecure,
CaCert: util.Masked(m.CaCert),
ClientCert: util.Masked(m.ClientCert),
ClientKey: util.Masked(m.ClientKey),
},
Topic: m.Topic,
}
}
// Influx is the influx db configuration
type Influx struct {
URL string `json:"url"`
Database string `json:"database"`
Token string `json:"token"`
Org string `json:"org"`
User string `json:"user"`
Password string `json:"password"`
Insecure bool `json:"insecure"`
}
// Redacted implements the redactor interface used by the tee publisher
func (c Influx) Redacted() any {
return Influx{
URL: c.URL,
Database: c.Database,
Token: util.Masked(c.Token),
Org: c.Org,
User: c.User,
Password: util.Masked(c.Password),
Insecure: c.Insecure,
}
}
type DB struct {
Type string
Dsn string
}
type Messaging struct {
Events MessagingEvents
Services []config.Typed
}
type MessagingEvents = map[string]MessagingEventTemplate
// MessagingEventTemplate is the push message configuration for an event
type MessagingEventTemplate struct {
Title string `json:"title"`
Msg string `json:"msg"`
Disabled bool `json:"disabled"`
}
func (c Messaging) IsConfigured() bool {
return len(c.Services) > 0 || len(c.Events) > 0
}
type Tariffs struct {
Currency string
Grid config.Typed
FeedIn config.Typed
Co2 config.Typed
Planner config.Typed
Solar []config.Typed
}
func (c Tariffs) IsConfigured() bool {
return c.Currency != "" || c.Grid.Type != "" || c.FeedIn.Type != "" || c.Co2.Type != "" || c.Planner.Type != "" || len(c.Solar) > 0
}
type TariffRefs struct {
Grid string `json:"grid"`
FeedIn string `json:"feedIn"`
Co2 string `json:"co2"`
Planner string `json:"planner"`
Solar []string `json:"solar"`
}
func (refs TariffRefs) IsConfigured() bool {
return refs.Grid != "" || refs.FeedIn != "" || refs.Co2 != "" || refs.Planner != "" || len(refs.Solar) > 0
}
func (refs TariffRefs) Used() iter.Seq[string] {
return func(yield func(string) bool) {
for _, ref := range append([]string{refs.Grid, refs.FeedIn, refs.Co2, refs.Planner}, refs.Solar...) {
if ref != "" {
if !yield(ref) {
return
}
}
}
}
}
type Network struct {
Schema_ string `json:"schema,omitempty" mapstructure:"schema"` // TODO deprecated
ExternalUrl string `json:"externalUrl"`
Host string `json:"host"`
Port int `json:"port"`
}
func (c Network) HostPort() string {
host := "localhost"
if h, err := os.Hostname(); err == nil {
host = h
}
if ips := util.LocalIPs(); len(ips) > 0 {
host = ips[0].IP.String()
}
if c.Port == 80 {
return host
}
return net.JoinHostPort(host, strconv.Itoa(c.Port))
}
func (c Network) InternalURL() string {
return "http://" + c.HostPort()
}
func (c Network) ExternalURL() string {
if c.ExternalUrl != "" {
return strings.TrimRight(c.ExternalUrl, "/")
}
return c.InternalURL()
}
// MarshalJSON includes the computed InternalUrl field in JSON output
func (c Network) MarshalJSON() ([]byte, error) {
type networkAlias Network
return json.Marshal(struct {
networkAlias
InternalUrl string `json:"internalUrl"`
}{
networkAlias: networkAlias(c),
InternalUrl: c.InternalURL(),
})
}