-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter.go
92 lines (82 loc) · 1.77 KB
/
meter.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
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/davecgh/go-spew/spew"
"net"
"time"
)
const (
PowerReg = 1000
RegNum = 27
)
type EatonValue struct {
V []float64 `json:"v"`
T time.Time `json:"t"`
M string `json:"m"`
}
func (this EatonValue) Values() []float64 {
return this.V
}
func (this EatonValue) Power() float64 {
n := this.V[0]
if n >= 0 {
return n
}
return -n
}
func (this EatonValue) Time() time.Time {
return this.T
}
func (this EatonValue) Print() {
spew.Dump(this)
}
type Eaton struct {
Name string `json:"name"`
Addr string `json:"addr"`
Avg float64
Max float64
Min float64
Stddev float64
}
func (this *Eaton) Read() (value EatonValue, err error) {
conn, err := net.Dial("tcp", this.Addr)
if err != nil {
return
}
defer conn.Close()
// send req
buf := new(bytes.Buffer)
buf.WriteByte(0x1) // addr
buf.WriteByte(0x3) // func
binary.Write(buf, binary.BigEndian, uint16(PowerReg-1))
binary.Write(buf, binary.BigEndian, uint16(RegNum*2))
binary.Write(buf, binary.LittleEndian, crc16(buf.Bytes()))
buf.WriteTo(conn)
conn.SetWriteDeadline(time.Now())
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// read resp
var b byte
binary.Read(conn, binary.BigEndian, &b) //addr
binary.Read(conn, binary.BigEndian, &b) //func
if b != 0x3 {
err = errors.New(fmt.Sprintf("[%v] expected %v but got %v", this.Addr, 0x3, b))
return
}
binary.Read(conn, binary.BigEndian, &b) //length
if b != RegNum*4 {
err = errors.New(fmt.Sprintf("[%v] expected %v but got %v", this.Addr, RegNum*4, b))
return
}
value.M = this.Name
value.T = time.Now()
for i := 0; i < RegNum; i++ {
var f float32
binary.Read(conn, binary.BigEndian, &f)
value.V = append(value.V, float64(f))
}
// ignore crc16
return
}