-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
type_conversions.go
67 lines (62 loc) · 1.44 KB
/
type_conversions.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
package s7comm
import (
"encoding/binary"
"math"
"github.com/robinson/gos7"
)
var helper = &gos7.Helper{}
func determineConversion(dtype string) converterFunc {
switch dtype {
case "X":
return func(buf []byte) interface{} {
return buf[0] != 0
}
case "B":
return func(buf []byte) interface{} {
return buf[0]
}
case "C":
return func(buf []byte) interface{} {
return string(buf[0])
}
case "S":
return func(buf []byte) interface{} {
if len(buf) <= 2 {
return ""
}
// Get the length of the encoded string
length := int(buf[0])
// Clip the string if we do not fill the whole buffer
if length < len(buf)-2 {
return string(buf[2 : 2+length])
}
return string(buf[2:])
}
case "W":
return func(buf []byte) interface{} {
return binary.BigEndian.Uint16(buf)
}
case "I":
return func(buf []byte) interface{} {
return int16(binary.BigEndian.Uint16(buf))
}
case "DW":
return func(buf []byte) interface{} {
return binary.BigEndian.Uint32(buf)
}
case "DI":
return func(buf []byte) interface{} {
return int32(binary.BigEndian.Uint32(buf))
}
case "R":
return func(buf []byte) interface{} {
x := binary.BigEndian.Uint32(buf)
return math.Float32frombits(x)
}
case "DT":
return func(buf []byte) interface{} {
return helper.GetDateTimeAt(buf, 0).UnixNano()
}
}
panic("Unknown type! Please file an issue on https://github.com/influxdata/telegraf including your config.")
}