-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
215 lines (188 loc) · 4.57 KB
/
Copy pathapi.go
File metadata and controls
215 lines (188 loc) · 4.57 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package emu
import (
"fmt"
"io"
"os"
"time"
)
var (
ErrTimeOut = NewError("TIME_OUT")
ErrChannelClosed = NewError("CHAN_CLOSED")
ErrDeviceWrite = NewError("DEVICE_WRITE")
ErrDeviceRead = NewError("DEVICE_READ")
ErrDeviceIO = NewError("DEVICE_IO")
ErrMsgProc = NewError("MESSAGE_PROCESSING")
)
type LogLevel int
const (
LOG_ALL LogLevel = iota + 1
LOG_INFO
LOG_WARNING
LOG_ERROR
LOG_OFF
)
func StringToLogLevel(l string) (LogLevel, error) {
switch l {
case "LOG_ALL":
return LOG_ALL, nil
case "LOG_INFO":
return LOG_INFO, nil
case "LOG_WARNING":
return LOG_WARNING, nil
case " LOG_ERROR":
return LOG_ERROR, nil
case "LOG_OFF":
return LOG_OFF, nil
default:
return 0, fmt.Errorf("invalid log level: %s", l)
}
}
type EmuOptions struct {
BaudRate int
TimeOut time.Duration
LogWriter io.Writer
LogLevel LogLevel
}
type EmuOption func(*EmuOptions)
func WithBaudRate(baudRate int) EmuOption {
return func(o *EmuOptions) {
o.BaudRate = baudRate
}
}
func WithTimeOut(timeout time.Duration) EmuOption {
return func(o *EmuOptions) {
o.TimeOut = timeout
}
}
func WithLogWriter(w io.Writer) EmuOption {
return func(o *EmuOptions) {
o.LogWriter = w
}
}
func WithLoggingLevel(l LogLevel) EmuOption {
return func(o *EmuOptions) {
o.LogLevel = l
}
}
type Emu interface {
SendCommand(Command) error
GetResponse() (Message, error)
// Subscribe([]MessageName, *func(Message)) error
// Unsubscribe([]MessageName, *func(Message))
Subscribe(MessageName) (chan Message, error)
Unsubscribe(MessageName, <-chan Message)
Start()
Close()
// GetCumulativeEnergyConsumption() (*CumulativeEnergyConsumption, error)
// GetInstantaneousPowerConsumption() (*InstantaneousPowerDemand, error)
}
func NewEmu(dev string, opts ...EmuOption) (Emu, error) {
options := &EmuOptions{
BaudRate: 115200,
TimeOut: 15 * time.Second,
LogWriter: os.Stdout,
LogLevel: LOG_ERROR,
}
for _, opt := range opts {
opt(options)
}
return newEmuImpl(dev, options)
}
type CumulativeEnergyConsumption struct {
TimeStamp int64
Energy float64 //Unit is kWh
DeviceMacId string
MeterMacId string
}
func (m *CumulativeEnergyConsumption) GetName() string {
return string(CumulativeEnergy)
}
func (m *CumulativeEnergyConsumption) GetAttrib(at string) (any, bool) {
switch at {
case "TimeStamp":
return m.TimeStamp, true
case "Energy":
return m.Energy, true
case "DeviceMacId":
return m.DeviceMacId, true
case "MeterMacId":
return m.MeterMacId, true
default:
return nil, false
}
}
type InstantaneousPowerDemand struct {
TimeStamp int64
Power float64 //Unit is KW
DeviceMacId string
MeterMacId string
}
func (m *InstantaneousPowerDemand) GetName() string {
return string(InstantaneousPower)
}
func (m *InstantaneousPowerDemand) GetAttrib(at string) (any, bool) {
switch at {
case "TimeStamp":
return m.TimeStamp, true
case "Power":
return m.Power, true
case "DeviceMacId":
return m.DeviceMacId, true
case "MeterMacId":
return m.MeterMacId, true
default:
return nil, false
}
}
type MessageName string
const (
DeviceInfo MessageName = "DeviceInfo"
NetworkInfo MessageName = "NetworkInfo"
TimeCluster MessageName = "TimeCluster"
InstantaneousPower MessageName = "InstantaneousPower"
CumulativeEnergy MessageName = "CumulativeEnergy"
Ack MessageName = "Ack"
)
type Message interface {
GetName() string
// SetAttrib(string, any)
GetAttrib(string) (any, bool)
}
type CommandId int8
const (
RESTART CommandId = iota + 1 // restarts the emu-2 device
GET_DEVICE_INFO // gets the basic emu-2 device info HW/SW version, make/model etc.
GET_TIME // gets the time (local and UTC) on the emu-2 as sync with the smart energy meter
GET_CONN_STATUS // gets the current connection status with the smart energy meter
)
var CommandResponseMap = map[CommandId]MessageName{
RESTART: Ack,
GET_DEVICE_INFO: DeviceInfo,
GET_TIME: TimeCluster,
GET_CONN_STATUS: NetworkInfo,
}
func (c CommandId) String() string {
if str, ok := commandIdString[c]; ok {
return str
}
return "unknown"
}
func StrToCommandId(s string) (CommandId, error) {
if c, ok := stringCommandId[s]; ok {
return c, nil
}
return -1, fmt.Errorf("invalid string %s", s)
}
type Command interface {
CommandId() CommandId
SetAttrib(string, any)
}
func NewCommand(id CommandId) (Command, error) {
if name, ok := cmdIdcmdMap[id]; ok {
return &commandImpl{
Id: id,
Name: name,
}, nil
}
return nil, fmt.Errorf("invalid command id %+v", id)
}