-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
146 lines (118 loc) · 3.26 KB
/
builder.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
package ocmf_go
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type Builder struct {
payload PayloadSection
signature Signature
privateKey *ecdsa.PrivateKey
}
func NewBuilder(privateKey *ecdsa.PrivateKey, opts ...BuilderOption) *Builder {
builder := &Builder{
payload: PayloadSection{
FormatVersion: OcmfVersion,
},
// Set default signature parameters
signature: *NewDefaultSignature(),
privateKey: privateKey,
}
// Apply builder options
for _, option := range opts {
option(builder)
}
return builder
}
func (b *Builder) WithGatewayID(gatewayID string) *Builder {
b.payload.GatewayID = gatewayID
return b
}
func (b *Builder) WithGatewaySerial(gatewaySerial string) *Builder {
b.payload.GatewaySerial = gatewaySerial
return b
}
func (b *Builder) WithGatewayVersion(gatewayVersion string) *Builder {
b.payload.GatewayVersion = gatewayVersion
return b
}
func (b *Builder) WithPagination(pagination string) *Builder {
b.payload.Pagination = pagination
return b
}
func (b *Builder) AddReading(reading Reading) *Builder {
b.payload.Readings = append(b.payload.Readings, reading)
return b
}
func (b *Builder) AddIdentificationFlag(flag string) *Builder {
b.payload.IdentificationFlags = append(b.payload.IdentificationFlags, flag)
return b
}
func (b *Builder) WithMeterSerial(serial string) *Builder {
b.payload.MeterSerial = serial
return b
}
func (b *Builder) WithIdentificationStatus(status bool) *Builder {
b.payload.IdentificationStatus = status
return b
}
func (b *Builder) WithIdentificationLevel(level string) *Builder {
b.payload.IdentificationLevel = level
return b
}
func (b *Builder) WithIdentificationType(idType string) *Builder {
b.payload.IdentificationType = idType
return b
}
func (b *Builder) WithIdentificationData(data string) *Builder {
b.payload.IdentificationData = data
return b
}
func (b *Builder) WithTariffText(text string) *Builder {
b.payload.TariffText = text
return b
}
func (b *Builder) WithChargeControllerVersion(version string) *Builder {
b.payload.ChargeControllerVersion = version
return b
}
func (b *Builder) WithChargePointIdentificationType(serial string) *Builder {
b.payload.ChargePointIdentificationType = serial
return b
}
func (b *Builder) WithChargePointIdentification(serial string) *Builder {
b.payload.ChargePointIdentification = serial
return b
}
func (b *Builder) AddLossCompensation(lossCompensation LossCompensation) *Builder {
b.payload.LossCompensation = lossCompensation
return b
}
func (b *Builder) ClearPayloadSection() {
b.payload = PayloadSection{
FormatVersion: OcmfVersion,
}
}
func (b *Builder) Build() (*string, error) {
// Validate payload
err := b.payload.Validate()
if err != nil {
return nil, errors.Wrap(err, "payload validation failed")
}
// Sign payload with private key
err = b.signature.Sign(b.payload, b.privateKey)
if err != nil {
return nil, errors.Wrap(err, "failed to sign message")
}
payload, err := json.Marshal(b.payload)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal payload")
}
signature, err := json.Marshal(b.signature)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal signature")
}
return lo.ToPtr(fmt.Sprintf("OCMF|%s|%s", payload, signature)), nil
}