Initial commit

This commit is contained in:
Gašper Dobrovoljc
2023-03-13 14:50:49 +01:00
commit 0c0d82db42
9 changed files with 298 additions and 0 deletions

18
hardware/hardware.go Normal file
View 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
View 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
View 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
}