-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsccommands.go
127 lines (110 loc) · 3.51 KB
/
tsccommands.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
package main
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"log"
"strconv"
"time"
_ "time/tzdata"
)
type CarConfigurationEntry struct {
ChargeMode int `json:"chargeMode"`
MinimumSoC int `json:"minimumSoC"`
LatestTimeToReachSoC string `json:"latestTimeToReachSoC"`
IgnoreLatestTimeToReachSocDate bool `json:"ignoreLatestTimeToReachSocDate"`
MaximumAmpere int `json:"maximumAmpere"`
MinimumAmpere int `json:"minimumAmpere"`
UsableEnergy int `json:"usableEnergy"`
ShouldBeManaged bool `json:"shouldBeManaged"`
ShouldSetChargeStartTimes bool `json:"shouldSetChargeStartTimes"`
ChargingPriority int `json:"chargingPriority"`
}
func scheduleCharge(carid int, chargelimit int) bool {
var successful bool
var PostData CarConfigurationEntry
successful = false
location, err := time.LoadLocation(Cfg.TimeZone)
if err != nil {
panic(err)
}
currentTime := time.Now().UTC().In(location)
TimeToReachLayout := "2006-01-02T15:04:05"
if Cfg.Debug {
logger.Printf("futureTime: %s\n", currentTime.Format(TimeToReachLayout))
}
fSeconds := currentTime.Second()
fAdd := 59 - fSeconds
futureTime := currentTime.Add(10 * time.Minute).Add(time.Duration(fAdd) * time.Second)
PostData = PreviousCarSettings
PostData.MinimumSoC = chargelimit
PostData.LatestTimeToReachSoC = futureTime.Format(TimeToReachLayout)
if Cfg.Debug {
m, _ := json.MarshalIndent(PostData, "", " ")
fmt.Printf("scheduleCharge, PostData: %s\n", m)
}
if Cfg.DryRun == false {
client := resty.New()
resp, err := client.R().
SetHeader("Content-Type", "application/json-patch+json").
SetBody(PostData).
Put(Cfg.Tscapi + "/api/Config/UpdateCarConfiguration?carId=" + strconv.Itoa(carid))
if err != nil {
log.Printf("Error during scheduleCharge: %+v\n", err)
successful = false
}
//fmt.Printf("Status Code: %d\n", resp.StatusCode())
if resp.StatusCode() == 200 {
successful = true
}
} else {
logger.Printf("Dry run!\n")
}
return successful
}
func stopCharge(carid int, chargelimit int) bool {
var successful bool
var PostData CarConfigurationEntry
successful = false
currentTime := time.Now()
//TimeToReachLayout := "2006-01-02T15:04:05"
TimeToReachLayout := time.RFC3339
if !isSpotCharge(carid) {
return false
}
if Cfg.Debug {
logger.Printf("stopCharge, currentTime: %s\n", currentTime.Format(TimeToReachLayout))
}
pSeconds := currentTime.Second()
pAdd := 59 - pSeconds
pastTime := currentTime.Add(-10 * time.Minute).Add(time.Duration(pAdd) * time.Second)
PostData = PreviousCarSettings
if PreviousCarSettings.MinimumSoC == chargelimit {
PostData.MinimumSoC = Cfg.FallbackSocLimit
} else {
PostData.MinimumSoC = PreviousCarSettings.MinimumSoC
}
PostData.LatestTimeToReachSoC = pastTime.Format(TimeToReachLayout)
if Cfg.Debug {
m, _ := json.MarshalIndent(PostData, "", " ")
fmt.Printf("Debug, stopCharge, PostData: %s\n", m)
}
if Cfg.DryRun == false {
client := resty.New()
resp, err := client.R().
SetHeader("Content-Type", "application/json-patch+json").
SetBody(PostData).
Put(Cfg.Tscapi + "/api/Config/UpdateCarConfiguration?carId=" + strconv.Itoa(carid))
if err != nil {
log.Printf("Error during scheduleCharge: %+v\n", err)
successful = false
}
//fmt.Printf("Status Code: %d\n", resp.StatusCode())
if resp.StatusCode() == 200 {
successful = true
} else {
logger.Printf("Dry run!\n")
}
}
return successful
}