/
main.go
169 lines (134 loc) · 3.57 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/enbility/eebus-go/service"
"github.com/enbility/eebus-go/spine/model"
)
type evse struct {
myService *service.EEBUSService
}
func (h *evse) run() {
var err error
var certificate tls.Certificate
var remoteSki string
if len(os.Args) == 5 {
remoteSki = os.Args[2]
certificate, err = tls.LoadX509KeyPair(os.Args[3], os.Args[4])
if err != nil {
usage()
log.Fatal(err)
}
} else {
certificate, err = service.CreateCertificate("Demo", "Demo", "DE", "Demo-Unit-02")
if err != nil {
log.Fatal(err)
}
pemdata := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certificate.Certificate[0],
})
fmt.Println(string(pemdata))
b, err := x509.MarshalECPrivateKey(certificate.PrivateKey.(*ecdsa.PrivateKey))
if err != nil {
log.Fatal(err)
}
pemdata = pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: b})
fmt.Println(string(pemdata))
}
port, err := strconv.Atoi(os.Args[1])
if err != nil {
usage()
log.Fatal(err)
}
configuration, err := service.NewConfiguration(
"Demo", "Demo", "EVSE", "234567890",
model.DeviceTypeTypeChargingStation, port, certificate, 230)
if err != nil {
log.Fatal(err)
}
configuration.SetAlternateIdentifier("Demo-EVSE-234567890")
h.myService = service.NewEEBUSService(configuration, h)
h.myService.SetLogging(h)
if err = h.myService.Setup(); err != nil {
fmt.Println(err)
return
}
if len(remoteSki) == 0 {
os.Exit(0)
}
h.myService.Start()
// defer h.myService.Shutdown()
remoteService := service.NewServiceDetails(remoteSki)
h.myService.PairRemoteService(remoteService)
}
// EEBUSServiceHandler
func (h *evse) RemoteSKIConnected(service *service.EEBUSService, ski string) {}
func (h *evse) RemoteSKIDisconnected(service *service.EEBUSService, ski string) {}
func (h *evse) ReportServiceShipID(ski string, shipdID string) {}
// main app
func usage() {
fmt.Println("First Run:")
fmt.Println(" go run /cmd/evse/main.go <serverport>")
fmt.Println()
fmt.Println("General Usage:")
fmt.Println(" go run /cmd/evse/main.go <serverport> <hems-ski> <crtfile> <keyfile>")
}
func main() {
if len(os.Args) < 2 {
usage()
return
}
h := evse{}
h.run()
// Clean exit to make sure mdns shutdown is invoked
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
// User exit
}
// Logging interface
func (h *evse) Trace(args ...interface{}) {
h.print("TRACE", args...)
}
func (h *evse) Tracef(format string, args ...interface{}) {
h.printFormat("TRACE", format, args...)
}
func (h *evse) Debug(args ...interface{}) {
h.print("DEBUG", args...)
}
func (h *evse) Debugf(format string, args ...interface{}) {
h.printFormat("DEBUG", format, args...)
}
func (h *evse) Info(args ...interface{}) {
h.print("INFO ", args...)
}
func (h *evse) Infof(format string, args ...interface{}) {
h.printFormat("INFO ", format, args...)
}
func (h *evse) Error(args ...interface{}) {
h.print("ERROR", args...)
}
func (h *evse) Errorf(format string, args ...interface{}) {
h.printFormat("ERROR", format, args...)
}
func (h *evse) currentTimestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func (h *evse) print(msgType string, args ...interface{}) {
value := fmt.Sprintln(args...)
fmt.Printf("%s %s %s", h.currentTimestamp(), msgType, value)
}
func (h *evse) printFormat(msgType, format string, args ...interface{}) {
value := fmt.Sprintf(format, args...)
fmt.Println(h.currentTimestamp(), msgType, value)
}