forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
53 lines (43 loc) · 1.2 KB
/
helper.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
package core
import (
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
)
var (
status = map[bool]string{false: "disable", true: "enable"}
presence = map[bool]string{false: "✗", true: "✓"}
// Voltage global value
Voltage float64
)
// bo returns an exponential backoff for reading meter power quickly
func bo() *backoff.ExponentialBackOff {
return backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(time.Second))
}
// powerToCurrent is a helper function to convert power to per-phase current
func powerToCurrent(power float64, phases int) float64 {
if Voltage == 0 {
panic("Voltage is not set")
}
return power / (float64(phases) * Voltage)
}
// currentToPower is a helper function to convert current to sum power
func currentToPower(current float64, phases int) float64 {
if Voltage == 0 {
panic("Voltage is not set")
}
return current * float64(phases) * Voltage
}
// printPtr returns a string representation of a pointer value
func printPtr[T any](format string, v *T) string {
if v == nil {
return "<nil>"
}
return fmt.Sprintf(format, *v)
}
func ptrValueEqual[T comparable](a, b *T) bool {
if (a == nil) != (b == nil) {
return false
}
return a == nil && b == nil || (*a) == (*b)
}