forked from mlnoga/rct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
175 lines (132 loc) · 5.02 KB
/
write.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
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
package rct
import (
"encoding/binary"
"fmt"
"math"
)
/*
* See https://github.com/do-gooder/rctpower_writesupport?tab=readme-ov-file#usage
*/
// SetSocStrategy sets the SOC strategy (power_mng.soc_strategy) with the given ENUM value
func (c *Connection) SetSocStrategy(strategy uint8) error {
if strategy > SOCTargetSchedule {
return fmt.Errorf("invalid SOC strategy value: %d", strategy)
}
if err := c.Write(PowerMngSocStrategy, []byte{strategy}); err != nil {
return fmt.Errorf("failed to set SOC strategy: %w", err)
}
return nil
}
// SetSocTarget sets the SOC target (power_mng.soc_target_set) with the given value
func (c *Connection) SetSocTarget(target float32) error {
if target < 0.00 || target > 1.00 {
return fmt.Errorf("invalid SOC target value: %.2f, valid range is 0.00 to 1.00", target)
}
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(target))
if err := c.Write(PowerMngSocTargetSet, data); err != nil {
return fmt.Errorf("failed to set SOC target: %w", err)
}
return nil
}
// SetBatteryPowerExtern sets the external battery power (power_mng.battery_power_extern) with the given float32 value in W
func (c *Connection) SetBatteryPowerExtern(power float32) error {
if power < -6000 || power > 6000 {
return fmt.Errorf("invalid battery power value: %.2f, valid range is -6000 to 6000", power)
}
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(power))
if err := c.Write(PowerMngBatteryPowerExternW, data); err != nil {
return fmt.Errorf("failed to set battery power extern: %w", err)
}
return nil
}
// SetSocMin sets the minimum SOC target (power_mng.soc_min) with the given value
func (c *Connection) SetSocMin(min float32) error {
if min < 0.00 || min > 1.00 {
return fmt.Errorf("invalid SOC min value: %.2f, valid range is 0.00 to 1.00", min)
}
// Round to 2 decimal places
min = float32(math.Round(float64(min)*100) / 100)
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(min))
if err := c.Write(BatterySoCTargetMin, data); err != nil {
return fmt.Errorf("failed to set SOC min: %w", err)
}
return nil
}
// SetSocMinIsland sets the minimum SOC target (power_mng.soc_min_island) with the given value
func (c *Connection) SetSocMinIsland(min float32) error {
if min < 0.00 || min > 1.00 {
return fmt.Errorf("invalid SOC min value: %.2f, valid range is 0.00 to 1.00", min)
}
// Round to 2 decimal places
min = float32(math.Round(float64(min)*100) / 100)
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(min))
if err := c.Write(BatterySoCTargetMinIsland, data); err != nil {
return fmt.Errorf("failed to set SOC min island: %w", err)
}
return nil
}
// SetSocMax sets the maximum SOC target (power_mng.soc_max) with the given value
func (c *Connection) SetSocMax(max float32) error {
if max < 0.00 || max > 1.00 {
return fmt.Errorf("invalid SOC max value: %.2f, valid range is 0.00 to 1.00", max)
}
// Round to 2 decimal places
max = float32(math.Round(float64(max)*100) / 100)
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(max))
if err := c.Write(PowerMngSocMax, data); err != nil {
return fmt.Errorf("failed to set SOC max: %w", err)
}
return nil
}
// SetSocChargePower sets the charging power to reach SOC target (power_mng.soc_charge_power)
func (c *Connection) SetSocChargePower(power uint16) error {
// Valid range is not defined, assume it’s a valid unsigned integer
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, power)
if err := c.Write(PowerMngSocChargePowerW, data); err != nil {
return fmt.Errorf("failed to set SOC charge power: %w", err)
}
return nil
}
// SetSocCharge sets the trigger for charging to SOC_min (power_mng.soc_charge)
func (c *Connection) SetSocCharge(charge float32) error {
if charge < 0.00 || charge > 1.00 {
return fmt.Errorf("invalid SOC charge value: %.2f, valid range is 0.00 to 1.00", charge)
}
// Round to 2 decimal places
charge = float32(math.Round(float64(charge)*100) / 100)
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(charge))
if err := c.Write(PowerMngSocCharge, data); err != nil {
return fmt.Errorf("failed to set SOC charge: %w", err)
}
return nil
}
// SetGridPowerLimit sets the maximum battery-to-grid power (p_rec_lim[1])
func (c *Connection) SetGridPowerLimit(power uint16) error {
if power > 6000 {
return fmt.Errorf("invalid grid power limit value: %d, valid range is 0 to 6000", power)
}
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, power)
if err := c.Write(PowerMngGridPowerLimitW, data); err != nil {
return fmt.Errorf("failed to set grid power limit: %w", err)
}
return nil
}
// SetUseGridPower sets the enable/disable flag for grid power usage (power_mng.use_grid_power_enable)
func (c *Connection) SetUseGridPower(enable bool) error {
var data byte
if enable {
data = 1
}
if err := c.Write(PowerMngUseGridPowerEnable, []byte{data}); err != nil {
return fmt.Errorf("failed to set grid power usage: %w", err)
}
return nil
}