-
Notifications
You must be signed in to change notification settings - Fork 7
/
reader.go
67 lines (60 loc) · 1.83 KB
/
reader.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 teleinfo
import (
"bufio"
"fmt"
"io"
)
// Reader defines an interface to read raw Teleinfo frames.
type Reader interface {
// ReadFrame reads a raw Teleinfo frame.
ReadFrame() (Frame, error)
}
type reader struct {
buffer *bufio.Reader
mode *string
}
// NewReader create a Teleinfo frame reader from a simple Reader.
// r is usually the struct returned by OpenPort().
func NewReader(r io.Reader, mode *string) Reader {
return &reader{
buffer: bufio.NewReader(r),
mode: mode,
}
}
// NOTE: as per https://golang.org/pkg/bufio/#pkg-constants the bufio.Reader will never consider more than
// 64kB of data to find the given delimiter. This saves us from running into a loop where invalid data gets
// read from the serial port, but no valid frame start/end are encountered.
func readRawFrame(buffer *bufio.Reader) ([]byte, error) {
const (
FrameStart byte = 0x2
FrameEnd byte = 0x3
)
var frame []byte
var err error
// TODO: check for interrupted frame marker
if _, err = buffer.ReadSlice(FrameStart); err != nil {
incrementErrorCounter(frameReadErrorCounter, "no_frame_start_marker")
return nil, fmt.Errorf("error looking for start of frame marker: %w", err)
}
if frame, err = buffer.ReadBytes(FrameEnd); err != nil {
incrementErrorCounter(frameReadErrorCounter, "no_frame_end_marker")
return nil, fmt.Errorf("error looking for end of frame marker: %w", err)
}
if len(frame) == 0 {
incrementErrorCounter(frameReadErrorCounter, "empty_frame")
return frame, fmt.Errorf("read empty frame")
}
frame = frame[0 : len(frame)-1]
frameReadCounter.Inc()
return frame, nil
}
func (t *reader) ReadFrame() (Frame, error) {
rawFrame, err := readRawFrame(t.buffer)
if err != nil {
return nil, err
}
if t.mode != nil && *t.mode == "standard" {
return decodeStandardFrame(rawFrame)
}
return decodeHistoricFrame(rawFrame)
}