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
/
mockclient.go
84 lines (67 loc) · 2.12 KB
/
mockclient.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
package sdm630
import (
"errors"
"math/rand"
"time"
)
type MockClient struct {
errorRate int32
responseTime time.Duration
}
func NewMockClient(errorRate int32) *MockClient {
return &MockClient{
errorRate: errorRate,
responseTime: 10 * time.Millisecond,
}
}
func (c *MockClient) fail() bool {
if c.errorRate > 0 {
return rand.Int31n(100) < c.errorRate
}
return false
}
func (c *MockClient) random(quantity uint16) (results []byte, err error) {
bytes := make([]byte, quantity*2)
rand.Read(bytes)
return bytes, nil
}
func (c *MockClient) read(quantity uint16) (results []byte, err error) {
time.Sleep(c.responseTime)
if c.fail() {
return nil, errors.New("Failed")
}
return c.random(quantity)
}
func (c *MockClient) ReadInputRegisters(address, quantity uint16) (results []byte, err error) {
return c.read(quantity)
}
func (c *MockClient) ReadHoldingRegisters(address, quantity uint16) (results []byte, err error) {
return c.read(quantity)
}
func (c *MockClient) ReadCoils(address, quantity uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) ReadDiscreteInputs(address, quantity uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) MaskWriteRegister(address, andMask, orMask uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) ReadFIFOQueue(address uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) WriteSingleCoil(address, value uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) WriteMultipleCoils(address, quantity uint16, value []byte) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) WriteSingleRegister(address, value uint16) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error) {
panic("Not implemented")
}
func (c *MockClient) ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error) {
panic("Not implemented")
}