-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
146 lines (124 loc) · 11.4 KB
/
main.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
//go:generate go-bindata -pkg ui -o internal/ui/ui.go -prefix "internal/ui/" -nometadata internal/ui/templates/
//go:generate go-bindata -pkg static -o internal/static/static.go -nomemcopy -fs -prefix "internal/static/files/" internal/static/files/...
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/shreddedbacon/twcmanager/internal/controller"
"github.com/shreddedbacon/twcmanager/internal/static"
"github.com/tarm/serial"
"gopkg.in/robfig/cron.v2"
"gopkg.in/yaml.v2"
)
func main() {
configPath := "./config.yml"
file, err := os.Open(configPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
d := yaml.NewDecoder(file)
twcConfig := &controller.TWCPrimary{}
// Start YAML decoding from file
if err := d.Decode(&twcConfig); err != nil {
log.Fatal(err)
}
// Set the ID of the primary controller
twcConfig.ID = []byte{0x77, 0x77}
// Create the serial port configuration
serialConfig := &serial.Config{
Name: twcConfig.SerialConfig.DevicePath,
Baud: twcConfig.SerialConfig.BaudRate,
ReadTimeout: 750 * time.Millisecond,
Size: 8,
}
twcConfig.ConfigPath = configPath
sPort, err := newSerialPort(serialConfig)
if err != nil {
log.Fatal(err)
}
// Create the primary TWC controller
p, err := controller.NewPrimary(
*twcConfig,
sPort.port,
)
if err != nil {
log.Fatal(err)
}
// pre start the controller
p.PreStart()
// then actually run it
go p.Run()
go p.LEDLoop()
c := cron.New()
// Add the cron runner, every second
c.AddFunc("* * * * * *", func() {
p.RunCron()
})
// start crons.
c.Start()
// start the webservice
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(static.AssetFile())))
r.HandleFunc("/favicon.ico", faviconHandler)
// API
// Handle setting the maximum available amperate
r.HandleFunc("/api/v1/maxamps/{availableAmps}", p.APISetMaxAmpsHandler).Methods("GET")
r.HandleFunc("/api/v1/maxamps", p.APISetMaxAmpsHandler).Methods("POST")
// Handle setting the maximum available amperage using available watts
r.HandleFunc("/api/v1/maxwatts/{availableWatts}", p.APISetMaxWattsHandler).Methods("GET")
r.HandleFunc("/api/v1/maxwatts", p.APISetMaxWattsHandler).Methods("POST")
// Handle actions against specific TWCs
// r.HandleFunc("/api/v1/disable/all", p.APIDisableTWC).Methods("POST")
r.HandleFunc("/api/v1/disable", p.APIStopCharging).Methods("POST")
r.HandleFunc("/api/v1/disable/{twcid}", p.APIStopCharging).Methods("GET")
// r.HandleFunc("/api/v1/enable/all", p.APIEnableTWC).Methods("POST")
r.HandleFunc("/api/v1/enable", p.APIStartCharging).Methods("POST")
r.HandleFunc("/api/v1/enable/{twcid}", p.APIStartCharging).Methods("GET")
// Tesla API functions
r.HandleFunc("/api/v1/teslapi/auth", p.TeslaAPIAuth).Methods("POST")
// Handle general API functions
r.HandleFunc("/api/v1/debug/{debugLevel}", p.APISetDebugLevel).Methods("POST")
r.HandleFunc("/api/v1/stats", p.APIGetStats).Methods("GET")
r.HandleFunc("/api/v1/settings", p.APISettings).Methods("GET", "POST")
r.HandleFunc("/api/v1/powerwallsettings", p.APIPowerwallSettings).Methods("GET", "POST")
r.HandleFunc("/api/vi/send/{msg}", p.CustomMessage)
r.HandleFunc("/api/v1/pollvin", p.APIPollVIN).Methods("POST")
r.HandleFunc("/api/v1/pollplugstate", p.APIPollPlugState).Methods("POST")
r.HandleFunc("/api/v1/pollfirmware", p.APIPollFirmwareInfo).Methods("POST")
// Template serving pages
r.HandleFunc("/", p.GetWallConnectors)
r.HandleFunc("/info/{twcid}", p.GetWallConnectorInfo)
r.HandleFunc("/settings", p.GetPrimarySettings)
r.HandleFunc("/powerwall", p.GetPowerwallSettings)
r.HandleFunc("/accounts", p.GetTeslaAPIUsers)
r.HandleFunc("/vehicleslist", p.ListTeslaAPIVehicles)
r.HandleFunc("/powerwall/stats", p.GetPowerwallSiteUsage)
http.Handle("/", r)
err = http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println(err)
}
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("Cache-Control", "public, max-age=7776000")
fmt.Fprintf(w, "data:image/x-icon;base64,AAABAAIAEBAAAAEAIABoBAAAJgAAACAgAAABACAAqBAAAI4EAAAoAAAAEAAAACAAAAABACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMAAAAzBQAAMwiAADMAAAAzAAAAMwAAADMAAAAzAAAAMwATk7bAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMAAAAzAAAAMxAAADMTgAAzAAAAMwAAADMAAAAzAAAAMwAAADMAE5O2wAAAMwAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMbgAAzHwAAMwAAADMAAAAzAAAAMwAAADMAAAAzABOTtsAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMAAAAzJwAAMyoAADMAAAAzAAAAMwAAADMAAAAzAAAAMwATk7bAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMAAAAzAAAAMzIAADM1gAAzAAAAMwAAADMAAAAzAAAAMwAAADMAE5O2wAAAMwAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwEAADM9AAAzPoAAMwIAADMAAAAzAAAAMwAAADMAAAAzABOTtsAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwAAADMJgAAzP8AAMz/AADMMAAAzAAAAMwAAADMAAA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lgAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzP8AAMz2AADMpgAAzDKdnesCAADMXgAAzMIAAMxcAADMGAAAzFgAAMymAADM6AAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzNIAAMwOAADMAAAAzAAAAMwGAADMwAAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzO4AAMyuAADMYAAAzBoAAMxSAADMtm9v4ngAAMzSAADM/wAAzP8AAMzyAADMqgAAzF4AAMwcAADMKgAAzF4AAMyKAADMrgAAzMoAAMzSAADMIgAAzAAAAMwAAADMAAAAzAAAAMwWAADMygAAzMwAAMyyAADMjgAAzGIAAMwuAADMGAAAzFYAAMygAADM7gAAzP8AAMz/CQnN6AAAzBYAAMxuAADMxAAAzPwAAMz/AADM/wAAzP8AAMzaAADMpgAAzHgAAMxQAADMMgAAzBgAAMwKAADMBAAAzAIAAMwAAADMBAAAzAoAAMwWAADMLgAAzE4AAMx0AADMoAAAzNQAAMz/AADM/wAAzP8AAMz/AADMzAAAzHaPj+gcAADMAAAAzAAAAMwCAADMGgAAzGIAAMymAADM5AAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzPwAAMz2AADM9AAAzPQAAMz2AADM+gAAzP8AAMz/AADM/wAAzP8AAMz/AADM/wAAzOgAAMysAADMagAAzCAAAMwCAADMAJ2d6wAAAMwAAADMAAAAzAAAAMwAAADMAAAAzAAAAMwCAADMHgAAzE4AAMx6AADMngAAzL4AAMzWAADM6gAAzPYAAMz/AADM/wAAzPgAAMzqAADM2AAAzMAAAMyiAADMfgAAzFIAAMwiAADMAgAAzAAAAMwAAADMAAAAzAAAAMwAnZ3rAP////////////9////+f////n////5////+f////n////4////8P////D////w////8P////D////gf///4H///+B////gf///4H///+A////AP///wD///8A//8/APz+HwD4fAAIADwAGAA7gDwB0Hh+HgwH/+A/gAAB//wAP/\n")
}
// SerialPort .
type SerialPort struct {
port *serial.Port
}
func newSerialPort(config *serial.Config) (*SerialPort, error) {
port, err := serial.OpenPort(config)
if err != nil {
log.Fatal(err)
}
return &SerialPort{
port: port,
}, nil
}