-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
http_session_handler.go
137 lines (113 loc) Β· 3.32 KB
/
http_session_handler.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
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/server/db"
"github.com/evcc-io/evcc/util/locale"
"github.com/gorilla/mux"
"golang.org/x/text/language"
)
func csvResult(ctx context.Context, w http.ResponseWriter, res any, filename string) {
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", `attachment; filename="`+filename+`.csv"`)
if ww, ok := res.(api.CsvWriter); ok {
_ = ww.WriteCsv(ctx, w)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
// sessionHandler returns the list of charging sessions
func sessionHandler(w http.ResponseWriter, r *http.Request) {
if db.Instance == nil {
jsonError(w, http.StatusBadRequest, errors.New("database offline"))
return
}
var (
res session.Sessions
cond []string
args []any
)
push := func(field, val string) {
cond = append(cond, field)
args = append(args, val)
}
filename := "session"
if year := r.URL.Query().Get("year"); year != "" {
filename += "-" + year
push("STRFTIME('%Y', created) LIKE ?", year)
if month := fmt.Sprintf("%02s", r.URL.Query().Get("month")); month != "00" {
filename += "-" + month
push("STRFTIME('%m', created) LIKE ?", month)
}
}
// TODO support other databases than Sqlite
query := strings.Join(append([]string{"charged_kwh>=0.05"}, cond...), " AND ")
if txn := db.Instance.Where(query, args...).Order("created DESC").Find(&res); txn.Error != nil {
jsonError(w, http.StatusInternalServerError, txn.Error)
return
}
// prepare data
for i, s := range res {
if s.Odometer != nil {
odo := math.Round(*s.Odometer*10) / 10
res[i].Odometer = &odo
}
}
if r.URL.Query().Get("format") == "csv" {
lang := r.URL.Query().Get("lang")
if lang == "" {
// get request language
lang = r.Header.Get("Accept-Language")
if tags, _, err := language.ParseAcceptLanguage(lang); err == nil && len(tags) > 0 {
lang = tags[0].String()
}
}
ctx := context.WithValue(context.Background(), locale.Locale, lang)
csvResult(ctx, w, &res, filename)
return
}
jsonResult(w, res)
}
// deleteSessionHandler removes session in sessions table with given id
func deleteSessionHandler(w http.ResponseWriter, r *http.Request) {
if db.Instance == nil {
jsonError(w, http.StatusBadRequest, errors.New("database offline"))
return
}
var res session.Sessions
vars := mux.Vars(r)
id := vars["id"]
if txn := db.Instance.Table("sessions").Delete(&res, id); txn.Error != nil {
jsonError(w, http.StatusBadRequest, txn.Error)
return
}
jsonResult(w, res)
}
// updateSessionHandler updates the data of an existing session
func updateSessionHandler(w http.ResponseWriter, r *http.Request) {
if db.Instance == nil {
jsonError(w, http.StatusBadRequest, errors.New("database offline"))
return
}
vars := mux.Vars(r)
id := vars["id"]
var session struct {
Vehicle string
}
if err := json.NewDecoder(r.Body).Decode(&session); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
// https://github.com/evcc-io/evcc/issues/13738#issuecomment-2094070362
if txn := db.Instance.Table("sessions").Where("id = ?", id).Select("vehicle").Updates(&session); txn.Error != nil {
jsonError(w, http.StatusBadRequest, txn.Error)
return
}
}