-
Notifications
You must be signed in to change notification settings - Fork 16
/
emitter.go
153 lines (139 loc) · 3.97 KB
/
emitter.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
// SPDX-License-Identifier: Apache-2.0
package mqtt
import (
"context"
"encoding/json"
"fmt"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
"github.com/thoughtworks/maeve-csms/manager/transport"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"net/url"
"sync"
"time"
)
// Emitter is an implementation of transport.Emitter that uses MQTT
// as the transport.
//
// Messages are published on a topic that is composed of a number of
// elements: <prefix>/out/<ocpp-version>/<cs-id>. The prefix is
// configured, the ocpp-version and cs-id are provided to the Emit
// function. If not configured the default prefix is `cs`.
//
// The Emitter defaults to connecting to a broker on 127.0.0.1:1883.
type Emitter struct {
sync.Mutex
connectionDetails
tracer trace.Tracer
conn *autopaho.ConnectionManager
}
func NewEmitter(opts ...Opt[Emitter]) transport.Emitter {
e := new(Emitter)
for _, opt := range opts {
opt(e)
}
ensureEmitterDefaults(e)
return e
}
func (e *Emitter) Emit(ctx context.Context, ocppVersion transport.OcppVersion, chargeStationId string, message *transport.Message) error {
topic := fmt.Sprintf("%s/out/%s/%s", e.mqttPrefix, ocppVersion, chargeStationId)
payload, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("marshalling response of type %s: %v", message.Action, err)
}
newCtx, span := e.tracer.Start(ctx,
fmt.Sprintf("%s/out/%s/# publish", e.mqttPrefix, ocppVersion),
trace.WithSpanKind(trace.SpanKindProducer),
trace.WithAttributes(
semconv.MessagingSystem("mqtt"),
semconv.MessagingMessagePayloadSizeBytes(len(payload)),
semconv.MessagingOperationKey.String("publish"),
semconv.MessagingMessageConversationID(message.MessageId),
attribute.String("csId", chargeStationId),
attribute.String(getActionName(message), message.Action),
))
defer span.End()
correlationMap := make(map[string]string)
otel.GetTextMapPropagator().Inject(newCtx, propagation.MapCarrier(correlationMap))
correlationData, err := json.Marshal(correlationMap)
if err != nil {
return fmt.Errorf("marshalling correlation map: %v", err)
}
err = e.ensureConnection(ctx)
if err != nil {
return fmt.Errorf("connecting to MQTT: %v", err)
}
_, err = e.conn.Publish(newCtx, &paho.Publish{
Topic: topic,
Payload: payload,
Properties: &paho.PublishProperties{
CorrelationData: correlationData,
},
})
if err != nil {
return fmt.Errorf("publishing to %s: %v", topic, err)
}
return nil
}
func getActionName(msg *transport.Message) string {
switch msg.MessageType {
case transport.MessageTypeCall:
return "call.action"
case transport.MessageTypeCallResult:
return "call_result.action"
default:
return "call_error.action"
}
}
func ensureEmitterDefaults(e *Emitter) {
if e.mqttBrokerUrls == nil {
u, err := url.Parse("mqtt://127.0.0.1:1883/")
if err != nil {
panic(err)
}
e.mqttBrokerUrls = []*url.URL{u}
}
if e.mqttPrefix == "" {
e.mqttPrefix = "cs"
}
if e.mqttConnectTimeout == 0 {
e.mqttConnectTimeout = 10 * time.Second
}
if e.mqttConnectRetryDelay == 0 {
e.mqttConnectRetryDelay = 1 * time.Second
}
if e.mqttKeepAliveInterval == 0 {
e.mqttKeepAliveInterval = 10
}
if e.tracer == nil {
e.tracer = noop.NewTracerProvider().Tracer("")
}
}
func (e *Emitter) ensureConnection(ctx context.Context) error {
e.Lock()
defer e.Unlock()
if e.conn == nil {
conn, err := autopaho.NewConnection(context.Background(), autopaho.ClientConfig{
BrokerUrls: e.mqttBrokerUrls,
KeepAlive: e.mqttKeepAliveInterval,
ConnectRetryDelay: e.mqttConnectRetryDelay,
ClientConfig: paho.ClientConfig{
ClientID: fmt.Sprintf("%s-%s", "manager-emit", randSeq(5)),
},
})
if err != nil {
return err
}
e.conn = conn
err = conn.AwaitConnection(ctx)
if err != nil {
return err
}
}
return nil
}