This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
snips.go
108 lines (91 loc) · 2.34 KB
/
snips.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
package sdm630
import (
"encoding/json"
"fmt"
"math"
"sync"
"time"
. "github.com/gonium/gosdm630/internal/meters"
)
// QuerySnip represents modbus query operations
type QuerySnip struct {
DeviceId uint8
Operation `json:"-"`
Value float64
ReadTimestamp time.Time
}
func NewQuerySnip(deviceId uint8, operation Operation) QuerySnip {
snip := QuerySnip{
DeviceId: deviceId,
Operation: operation,
Value: math.NaN(),
}
return snip
}
// String representation
func (q *QuerySnip) String() string {
return fmt.Sprintf("DevID: %d, FunCode: %d, Opcode: %x, IEC: %s, Value: %.3f",
q.DeviceId, q.FuncCode, q.OpCode, q.IEC61850, q.Value)
}
// MarshalJSON converts QuerySnip to json, replacing ReadTimestamp with unix time representation
func (q *QuerySnip) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
DeviceId uint8
Value float64
IEC61850 string
Description string
Timestamp int64
}{
DeviceId: q.DeviceId,
Value: q.Value,
IEC61850: q.IEC61850,
Description: GetIecDescription(q.IEC61850),
Timestamp: q.ReadTimestamp.UnixNano() / 1e6,
})
}
type QuerySnipChannel chan QuerySnip
// QuerySnipBroadcaster acts as hub for broadcating QuerySnips
// to multiple recipients
type QuerySnipBroadcaster struct {
in QuerySnipChannel
recipients []QuerySnipChannel
mux sync.Mutex // guard recipients
}
// NewQuerySnipBroadcaster creates QuerySnipBroadcaster
func NewQuerySnipBroadcaster(in QuerySnipChannel) *QuerySnipBroadcaster {
return &QuerySnipBroadcaster{
in: in,
recipients: make([]QuerySnipChannel, 0),
}
}
// Run executes the broadcaster
func (b *QuerySnipBroadcaster) Run() {
for {
s := <-b.in
b.mux.Lock()
for _, recipient := range b.recipients {
recipient <- s
}
b.mux.Unlock()
}
}
// Attach creates and attaches a QuerySnipChannel to the broadcaster
func (b *QuerySnipBroadcaster) Attach() QuerySnipChannel {
channel := make(QuerySnipChannel)
b.mux.Lock()
b.recipients = append(b.recipients, channel)
b.mux.Unlock()
return channel
}
// ControlSnip wraps control information like query success or failure.
type ControlSnip struct {
Type ControlSnipType
Message string
DeviceId uint8
}
type ControlSnipType uint8
const (
CONTROLSNIP_OK ControlSnipType = iota
CONTROLSNIP_ERROR
)
type ControlSnipChannel chan ControlSnip