Initial commit
This commit is contained in:
18
hardware/hardware.go
Normal file
18
hardware/hardware.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package hardware
|
||||
|
||||
type (
|
||||
Hardware interface {
|
||||
Read() (uint16, error)
|
||||
}
|
||||
Client struct {
|
||||
hardware Hardware
|
||||
}
|
||||
)
|
||||
|
||||
func NewClient(h Hardware) Client {
|
||||
return Client{hardware: h}
|
||||
}
|
||||
|
||||
func (c Client) Read() (uint16, error) {
|
||||
return c.hardware.Read()
|
||||
}
|
||||
22
hardware/mock.go
Normal file
22
hardware/mock.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package hardware
|
||||
|
||||
type Mock struct {
|
||||
currValue int16
|
||||
delta int16
|
||||
}
|
||||
|
||||
func NewMock() *Mock {
|
||||
return &Mock{currValue: 0, delta: 1}
|
||||
}
|
||||
|
||||
func (m *Mock) Read() (uint16, error) {
|
||||
m.currValue += m.delta
|
||||
|
||||
if m.currValue >= 100 {
|
||||
m.delta = -1
|
||||
} else if m.currValue <= 0 {
|
||||
m.delta = 1
|
||||
}
|
||||
|
||||
return uint16(m.currValue), nil
|
||||
}
|
||||
26
hardware/siemens.go
Normal file
26
hardware/siemens.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
snap7 "github.com/danclive/snap7-go"
|
||||
)
|
||||
|
||||
type Siemens struct {
|
||||
client snap7.Snap7Client
|
||||
}
|
||||
|
||||
func NewSiemens(client snap7.Snap7Client) Siemens {
|
||||
return Siemens{client: client}
|
||||
}
|
||||
|
||||
func (s Siemens) Read() (uint16, error) {
|
||||
db, err := s.client.DBRead(1, 0, 8)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
value := binary.BigEndian.Uint16(db[2:4])
|
||||
|
||||
return value, nil
|
||||
}
|
||||
Reference in New Issue
Block a user