-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
198 lines (157 loc) · 4.46 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/enbility/eebus-go/api"
"github.com/enbility/eebus-go/service"
shipapi "github.com/enbility/ship-go/api"
"github.com/enbility/ship-go/cert"
"github.com/enbility/spine-go/model"
)
var remoteSki string
type hems struct {
myService *service.Service
}
func (h *hems) run() {
var err error
var certificate tls.Certificate
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 = cert.CreateCertificate("Demo", "Demo", "DE", "Demo-Unit-01")
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 := api.NewConfiguration(
"Demo", "Demo", "HEMS", "123456789",
model.DeviceTypeTypeEnergyManagementSystem,
[]model.EntityTypeType{model.EntityTypeTypeCEM},
port, certificate, 230, time.Second*4)
if err != nil {
log.Fatal(err)
}
configuration.SetAlternateIdentifier("Demo-HEMS-123456789")
h.myService = service.NewService(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.RegisterRemoteSKI(remoteSki)
h.myService.Start()
// defer h.myService.Shutdown()
}
// EEBUSServiceHandler
func (h *hems) RemoteSKIConnected(service api.ServiceInterface, ski string) {}
func (h *hems) RemoteSKIDisconnected(service api.ServiceInterface, ski string) {}
func (h *hems) VisibleRemoteServicesUpdated(service api.ServiceInterface, entries []shipapi.RemoteService) {
}
func (h *hems) ServiceShipIDUpdate(ski string, shipdID string) {}
func (h *hems) ServicePairingDetailUpdate(ski string, detail *shipapi.ConnectionStateDetail) {
if ski == remoteSki && detail.State() == shipapi.ConnectionStateRemoteDeniedTrust {
fmt.Println("The remote service denied trust. Exiting.")
h.myService.CancelPairingWithSKI(ski)
h.myService.UnregisterRemoteSKI(ski)
h.myService.Shutdown()
os.Exit(0)
}
}
func (h *hems) AllowWaitingForTrust(ski string) bool {
return ski == remoteSki
}
// UCEvseCommisioningConfigurationCemDelegate
// handle device state updates from the remote EVSE device
func (h *hems) HandleEVSEDeviceState(ski string, failure bool, errorCode string) {
fmt.Println("EVSE Error State:", failure, errorCode)
}
// main app
func usage() {
fmt.Println("First Run:")
fmt.Println(" go run /cmd/hems/main.go <serverport>")
fmt.Println()
fmt.Println("General Usage:")
fmt.Println(" go run /cmd/hems/main.go <serverport> <evse-ski> <crtfile> <keyfile>")
}
func main() {
if len(os.Args) < 2 {
usage()
return
}
h := hems{}
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 *hems) Trace(args ...interface{}) {
h.print("TRACE", args...)
}
func (h *hems) Tracef(format string, args ...interface{}) {
h.printFormat("TRACE", format, args...)
}
func (h *hems) Debug(args ...interface{}) {
h.print("DEBUG", args...)
}
func (h *hems) Debugf(format string, args ...interface{}) {
h.printFormat("DEBUG", format, args...)
}
func (h *hems) Info(args ...interface{}) {
h.print("INFO ", args...)
}
func (h *hems) Infof(format string, args ...interface{}) {
h.printFormat("INFO ", format, args...)
}
func (h *hems) Error(args ...interface{}) {
h.print("ERROR", args...)
}
func (h *hems) Errorf(format string, args ...interface{}) {
h.printFormat("ERROR", format, args...)
}
func (h *hems) currentTimestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func (h *hems) print(msgType string, args ...interface{}) {
value := fmt.Sprintln(args...)
fmt.Printf("%s %s %s", h.currentTimestamp(), msgType, value)
}
func (h *hems) printFormat(msgType, format string, args ...interface{}) {
value := fmt.Sprintf(format, args...)
fmt.Println(h.currentTimestamp(), msgType, value)
}