-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
121 lines (96 loc) · 2.69 KB
/
client.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
package ocpp
import (
"encoding/base64"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
)
var client *Client
type ClientTimeoutConfig struct {
// ocpp response timeout in seconds
OcppWait time.Duration
// time allowed to write a message to the peer
WriteWait time.Duration
// pong wait in seconds
PongWait time.Duration
// ping period in seconds
PingPeriod time.Duration
}
type Client struct {
Id string
// register implemented action handler functions
actionHandlers map[string]func(*ChargePoint, Payload) Payload
// register after-action habdler functions
afterHandlers map[string]func(*ChargePoint, Payload)
// timeout configuration
ocppWait time.Duration
writeWait time.Duration
pongWait time.Duration
pingPeriod time.Duration
header http.Header
returnError func(error)
callQuequeSize int
}
// create new Client instance
func NewClient() *Client {
client = &Client{
actionHandlers: make(map[string]func(*ChargePoint, Payload) Payload),
afterHandlers: make(map[string]func(*ChargePoint, Payload)),
ocppWait: ocppWait,
writeWait: writeWait,
pongWait: pongWait,
pingPeriod: pingPeriod,
header: http.Header{},
}
return client
}
func (c *Client) SetCallQueueSize(size int) {
c.callQuequeSize = size
}
func (c *Client) SetTimeoutConfig(config ClientTimeoutConfig) {
c.ocppWait = config.OcppWait
c.writeWait = config.WriteWait
c.pongWait = config.PongWait
c.pingPeriod = config.PingPeriod
}
// register action handler function
func (c *Client) On(action string, f func(*ChargePoint, Payload) Payload) *Client {
c.actionHandlers[action] = f
return c
}
// register after-action handler function
func (c *Client) After(action string, f func(*ChargePoint, Payload)) *Client {
c.afterHandlers[action] = f
return c
}
func (c *Client) getHandler(action string) func(*ChargePoint, Payload) Payload {
return c.actionHandlers[action]
}
func (c *Client) getAfterHandler(action string) func(*ChargePoint, Payload) {
return c.afterHandlers[action]
}
func (c *Client) AddSubProtocol(protocol string) {
c.header.Add("Sec-WebSocket-Protocol", protocol)
}
func (c *Client) SetBasicAuth(username string, password string) {
auth := username + ":" + password
enc := base64.StdEncoding.EncodeToString([]byte(auth))
c.header.Set("Authorization", "Basic "+enc)
}
func (c *Client) Start(addr string, path string) (cp *ChargePoint, err error) {
urlStr, err := url.JoinPath(addr, path, c.Id)
if err != nil {
c.returnError(err)
return
}
conn, _, err := websocket.DefaultDialer.Dial(urlStr, c.header)
if err != nil {
return
}
cp = NewChargePoint(conn, c.Id, conn.Subprotocol(), false)
return
}
func (c *Client) SetID(id string) {
c.Id = id
}