-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
116 lines (100 loc) · 2.77 KB
/
parser.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
package go_meter_parsing
import (
"encoding/xml"
"fmt"
"strings"
"github.com/sintemal/go_meter_parsing/formats"
"github.com/sintemal/go_meter_parsing/formats/alfen"
"github.com/sintemal/go_meter_parsing/formats/mennekes"
"github.com/sintemal/go_meter_parsing/formats/ocmf"
"github.com/sintemal/go_meter_parsing/formats/pcdf"
)
func Parse(data string) (formats.MeterFormat, error) {
// Check, which format we have
if strings.HasPrefix(data, "128.8.0") {
return pcdf.Parse(data)
} else if strings.HasPrefix(data, "OCMF|") {
return ocmf.Parse(data)
} else if strings.HasPrefix(data, "<ChargingProcess") {
return mennekes.Parse(data)
} else if strings.HasPrefix(data, "AP") {
return alfen.Parse(data)
} else {
return nil, fmt.Errorf("Unknown format")
}
}
func MeterValuekWh(m formats.MeterFormat) (float64, error) {
if m.MeterUnit() == "kWh" {
return m.MeterValue(), nil
} else if m.MeterUnit() == "Wh" {
return m.MeterValue() / 1000, nil
} else {
return 0, fmt.Errorf("Invalid unit")
}
}
type SignedData struct {
XMLName xml.Name `xml:"signedData"`
Format string `xml:"format,attr"`
Encoding string `xml:"encoding,attr"`
TransactionId string `xml:"transactionId,attr"`
Data string `xml:",innerxml"`
}
// Define struct for publicKey
type PublicKey struct {
XMLName xml.Name `xml:"publicKey"`
Key string `xml:",innerxml"`
}
// Define struct for value
type Value struct {
XMLName xml.Name `xml:"value"`
TransactionId string `xml:"transactionId,attr"`
Context string `xml:"context,attr"`
SignedData SignedData `xml:"signedData"`
PublicKey *PublicKey `xml:"publicKey,omitempty"`
}
// Define struct for values
type TransparenzsoftwareReadable struct {
XMLName xml.Name `xml:"values"`
Value []Value `xml:"value"`
}
func WrapTransparenzsoftware(m formats.MeterFormat, data string, publickey string) (string, error) {
if m.Name() == "Mennekes" { //mennekes can be parsed directly by transparenzsoftware
return data, nil
}
var pk PublicKey //sometimes the publickey is already included in the data
if publickey != "" {
pk = PublicKey{
Key: publickey,
}
}
signedData := SignedData{
Format: m.Name(),
TransactionId: "1",
Encoding: "plain",
Data: data,
}
var value Value
if publickey != "" {
value = Value{
TransactionId: "1",
Context: "Transaction.Begin",
SignedData: signedData,
PublicKey: &pk,
}
} else {
value = Value{
TransactionId: "1",
Context: "Transaction.Begin",
SignedData: signedData,
}
}
values := TransparenzsoftwareReadable{
Value: []Value{value},
}
x, err := xml.Marshal(values)
if err != nil {
return "", err
}
with_header := []byte(xml.Header + string(x))
return string(with_header), nil
}