-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttyReader.go
129 lines (116 loc) · 4.12 KB
/
ttyReader.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
package main
import (
"bufio"
"flag"
"net"
"os"
"time"
"github.com/ian-kent/go-log/appenders"
"github.com/ian-kent/go-log/layout"
"github.com/ian-kent/go-log/log"
"github.com/tarm/serial"
)
func main() {
devicePtr := flag.String("device", "/dev/ttyUSB0", "the smartmeter device")
hostPtr := flag.String("host", "10.0.1.2:9999", "the host that will receive the data")
protocolPtr := flag.String("protocol", "udp", "the protocol for the host connection (tcp, udp and IP networks)")
readTimeoutPtr := time.Duration(*flag.Int("readTimeout", 5, "the smartmeter device read timeout in minutes")) * time.Minute
flag.Parse()
initLogger()
read(*devicePtr, *hostPtr, *protocolPtr, readTimeoutPtr)
}
func initLogger() {
logger := log.Logger()
logger.SetAppender(appenders.RollingFile("smartmeter.log", true))
appender := logger.Appender()
appender.SetLayout(layout.Pattern("%d %p - %m%n"))
}
func read(device string, host string, protocol string, readTimeout time.Duration) {
log.Info("Opening smartmeter device '%s' ...", device)
c := &serial.Config{Name: device, Baud: 300, Size: 7, Parity: 'E', ReadTimeout: readTimeout}
s, err := serial.OpenPort(c)
if err != nil {
log.Error("Couldn't open connection to smartmeter device '%s'. Abort.", device)
log.Fatal(err)
os.Exit(-1000)
}
log.Debug("Waiting a maximum of %s minutes for smartmeter data.", readTimeout)
log.Info("Configure smartmeter Td3511 for read out ...")
_, err = s.Write([]byte("1:0:9a7:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0"))
if err != nil {
log.Fatal(err)
os.Exit(-1000)
}
_, err = s.Write([]byte("\x2F\x3F\x21\x0D\x0A"))
if err != nil {
log.Fatal(err)
os.Exit(-1000)
}
_, err = s.Write([]byte("\x06\x30\x30\x30\x0D\x0A"))
if err != nil {
log.Fatal(err)
os.Exit(-1000)
}
log.Info("Waiting for data ...")
reader := bufio.NewReader(s)
readData, err := reader.ReadString('\x21')
if err != nil {
log.Error("Couldn't read any data from smartmeter within timeout. Abort.")
log.Fatal(err)
os.Exit(-2000)
}
matchedData := matchData(readData)
matchedDataSize := len(matchedData)
log.Info("Received %d data records.", matchedDataSize)
if matchedDataSize > 0 {
log.Info("Opening %s connection to %s ...", protocol, host)
conn, err := net.Dial(protocol, host)
if err != nil {
log.Error("Couldn't open %s connection to %s.", protocol, host)
log.Fatal(err)
os.Exit(-3000)
}
for key, value := range matchedData {
if value["omis"] == "1.7.0" {
data := value["data"]
conn.Write([]byte("1.7.0:" + data))
log.Info("1.7.0/%d: Aktueller Verbrauch (%s): %s", key, value["unit"], data)
} else if value["omis"] == "1.8.0" {
data := value["data"]
conn.Write([]byte("1.8.0:" + data))
log.Info("1.8.0/%d: Verbrauch Gesamt (%s): %s", key, value["unit"], data)
} else if value["omis"] == "1.8.1" {
data := value["data"]
conn.Write([]byte("1.8.1:" + data))
log.Info("1.8.1/%d: Verbrauch Tarif 1 (%s): %s", key, value["unit"], data)
} else if value["omis"] == "1.8.2" {
data := value["data"]
conn.Write([]byte("1.8.2:" + data))
log.Info("1.8.2/%d: Verbrauch Tarif 2 (%s): %s", key, value["unit"], data)
} else if value["omis"] == "2.7.0" {
data := value["data"]
conn.Write([]byte("2.7.0:" + data))
log.Info("2.7.0/%d: Aktuelle Lieferung (%s): %s", key, value["unit"], data)
} else if value["omis"] == "2.8.0" {
data := value["data"]
conn.Write([]byte("2.8.0:" + data))
log.Info("2.8.0/%d: Lieferung Gesamt (%s): %s", key, value["unit"], data)
} else if value["omis"] == "2.8.1" {
data := value["data"]
conn.Write([]byte("2.8.1:" + data))
log.Info("2.8.1/%d: Lieferung Tarif 1 (%s): %s", key, value["unit"], data)
} else if value["omis"] == "2.8.2" {
data := value["data"]
conn.Write([]byte("2.8.2:" + data))
log.Info("2.8.2/%d: Lieferung Tarif 2 (%s): %s", key, value["unit"], data)
}
}
log.Info("Sucessfully processed all %d data records, closing connection ...", matchedDataSize)
conn.Close()
} else {
log.Warn("Couldn't match any data records.")
}
log.Info("Closing TD3511 smartmeter, bye bye.")
s.Close()
os.Exit(0)
}