Skip to content

Commit

Permalink
extract Client interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Jul 14, 2020
1 parent 6cf1112 commit a3f0b6e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
25 changes: 18 additions & 7 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ const mtuHeaderLength = 4
const defaultStateSize = 20
const forwardHeaderLength = 10

type Client struct {
type Client interface {
io.Closer
Run()
WhoIs(low, high int) ([]bactype.Device, error)
Objects(dev bactype.Device) (bactype.Device, error)
ReadProperty(dest bactype.Device, rp bactype.PropertyData) (bactype.PropertyData, error)
ReadMultiProperty(dev bactype.Device, rp bactype.MultiplePropertyData) (bactype.MultiplePropertyData, error)
WriteProperty(dest bactype.Device, wp bactype.PropertyData) error
WriteMultiProperty(dev bactype.Device, wp bactype.MultiplePropertyData) error
}

type client struct {
dataLink datalink.DataLink
tsm *tsm.TSM
utsm *utsm.Manager
Expand All @@ -59,14 +70,14 @@ type Client struct {

// NewClient creates a new client with the given interface and
// port.
func NewClient(dataLink datalink.DataLink, maxPDU uint16) *Client {
func NewClient(dataLink datalink.DataLink, maxPDU uint16) Client {
if maxPDU == 0 {
maxPDU = bactype.MaxAPDU
}
log := logrus.New()
log.Formatter = &logrus.TextFormatter{}
log.SetLevel(logrus.InfoLevel)
return &Client{
return &client{
dataLink: dataLink,
tsm: tsm.New(defaultStateSize),
utsm: utsm.NewManager(
Expand All @@ -80,7 +91,7 @@ func NewClient(dataLink datalink.DataLink, maxPDU uint16) *Client {
}
}

func (c *Client) Run() {
func (c *client) Run() {
var err error = nil
for err == nil {
b := c.readBufferPool.Get().([]byte)
Expand All @@ -94,7 +105,7 @@ func (c *Client) Run() {
}
}

func (c *Client) handleMsg(src *bactype.Address, b []byte) {
func (c *client) handleMsg(src *bactype.Address, b []byte) {
var header bactype.BVLC
var npdu bactype.NPDU
var apdu bactype.APDU
Expand Down Expand Up @@ -192,7 +203,7 @@ func (c *Client) handleMsg(src *bactype.Address, b []byte) {
}

// Send transfers the raw apdu byte slice to the destination address.
func (c *Client) Send(dest bactype.Address, npdu *bactype.NPDU, data []byte) (int, error) {
func (c *client) Send(dest bactype.Address, npdu *bactype.NPDU, data []byte) (int, error) {
var header bactype.BVLC

// Set packet type
Expand All @@ -218,7 +229,7 @@ func (c *Client) Send(dest bactype.Address, npdu *bactype.NPDU, data []byte) (in
}

// Close free resources for the client. Always call this function when using NewClient
func (c *Client) Close() error {
func (c *client) Close() error {
if c.dataLink != nil {
c.dataLink.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
bactype "github.com/alexbeltran/gobacnet/types"
)

func (c *Client) iAm(dest bactype.Address) error {
func (c *client) iAm(dest bactype.Address) error {
npdu := &bactype.NPDU{
Version: bactype.ProtocolVersion,
Destination: &dest,
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestServices(t *testing.T) {

}

func testReadPropertyService(c *Client, t *testing.T) {
func testReadPropertyService(c Client, t *testing.T) {
dev, err := c.WhoIs(testServer, testServer)
read := types.PropertyData{
Object: types.Object{
Expand All @@ -117,7 +117,7 @@ func testReadPropertyService(c *Client, t *testing.T) {
t.Logf("Response: %v", resp.Object.Properties[0].Data)
}

func testWhoIs(c *Client, t *testing.T) {
func testWhoIs(c Client, t *testing.T) {
dev, err := c.WhoIs(testServer-1, testServer+1)
if err != nil {
t.Fatal(err)
Expand All @@ -130,7 +130,7 @@ func testWhoIs(c *Client, t *testing.T) {
// This test will first cconver the name of an analogue sensor to a different
// value, read the property to make sure the name was changed, revert back, and
// ensure that the revert was successful
func testWritePropertyService(c *Client, t *testing.T) {
func testWritePropertyService(c Client, t *testing.T) {
const targetName = "Hotdog"
dev, err := c.WhoIs(testServer, testServer)
wp := types.PropertyData{
Expand Down
12 changes: 6 additions & 6 deletions objectlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
bactype "github.com/alexbeltran/gobacnet/types"
)

func (c *Client) objectListLen(dev bactype.Device) (int, error) {
func (c *client) objectListLen(dev bactype.Device) (int, error) {
rp := bactype.PropertyData{
Object: bactype.Object{
ID: dev.ID,
Expand Down Expand Up @@ -35,7 +35,7 @@ func (c *Client) objectListLen(dev bactype.Device) (int, error) {
return int(data), nil
}

func (c *Client) objectsRange(dev bactype.Device, start, end int) ([]bactype.Object, error) {
func (c *client) objectsRange(dev bactype.Device, start, end int) ([]bactype.Object, error) {
rpm := bactype.MultiplePropertyData{
Objects: []bactype.Object{
bactype.Object{
Expand Down Expand Up @@ -83,7 +83,7 @@ func objectCopy(dest bactype.ObjectMap, src []bactype.Object) {

}

func (c *Client) objectList(dev *bactype.Device) error {
func (c *client) objectList(dev *bactype.Device) error {
dev.Objects = make(bactype.ObjectMap)

l, err := c.objectListLen(*dev)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *Client) objectList(dev *bactype.Device) error {
return nil
}

func (c *Client) objectInformation(dev *bactype.Device, objs []bactype.Object) error {
func (c *client) objectInformation(dev *bactype.Device, objs []bactype.Object) error {
// Often times the map will re arrange the order it spits out
// so we need to keep track since the response will be in the
// same order we issue the commands.
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *Client) objectInformation(dev *bactype.Device, objs []bactype.Object) e
return nil
}

func (c *Client) allObjectInformation(dev *bactype.Device) error {
func (c *client) allObjectInformation(dev *bactype.Device) error {
objs := dev.ObjectSlice()
incrSize := 5

Expand All @@ -191,7 +191,7 @@ func (c *Client) allObjectInformation(dev *bactype.Device) error {
// gather additional information from the object such as the name and
// description of the objects. The device returned contains all of the name and
// description fields for all objects
func (c *Client) Objects(dev bactype.Device) (bactype.Device, error) {
func (c *client) Objects(dev bactype.Device) (bactype.Device, error) {
err := c.objectList(&dev)
if err != nil {
return dev, fmt.Errorf("unable to get object list: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions readmulti.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const maxReattempt = 2
// good feature to read all present values of every object in the device. This
// is a batch operation compared to a ReadProperty and should be used in place
// when reading more than two objects/properties.
func (c *Client) ReadMultiProperty(dev bactype.Device, rp bactype.MultiplePropertyData) (bactype.MultiplePropertyData, error) {
func (c *client) ReadMultiProperty(dev bactype.Device, rp bactype.MultiplePropertyData) (bactype.MultiplePropertyData, error) {
var out bactype.MultiplePropertyData

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *Client) ReadMultiProperty(dev bactype.Device, rp bactype.MultipleProper
return out, fmt.Errorf("failed %d tries: %v", maxReattempt, err)
}

func (c *Client) sendReadMultipleProperty(id int, dev bactype.Device, npdu *bactype.NPDU, request []byte) (bactype.MultiplePropertyData, error) {
func (c *client) sendReadMultipleProperty(id int, dev bactype.Device, npdu *bactype.NPDU, request []byte) (bactype.MultiplePropertyData, error) {
var out bactype.MultiplePropertyData
_, err := c.Send(dev.Addr, npdu, request)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion readprop.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

// ReadProperty reads a single property from a single object in the given device.
func (c *Client) ReadProperty(dest bactype.Device, rp bactype.PropertyData) (bactype.PropertyData, error) {
func (c *client) ReadProperty(dest bactype.Device, rp bactype.PropertyData) (bactype.PropertyData, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
id, err := c.tsm.ID(ctx)
Expand Down
2 changes: 1 addition & 1 deletion whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
// Use constant ArrayAll for both fields to scan the entire network at once.
// Using ArrayAll is highly discouraged for most networks since it can lead
// to a high congested network.
func (c *Client) WhoIs(low, high int) ([]types.Device, error) {
func (c *client) WhoIs(low, high int) ([]types.Device, error) {
dest := *c.dataLink.GetBroadcastAddress()

enc := encoding.NewEncoder()
Expand Down
4 changes: 2 additions & 2 deletions writemulti.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
bactype "github.com/alexbeltran/gobacnet/types"
)

func (c *Client) WriteMultiProperty(dev bactype.Device, wp bactype.MultiplePropertyData) error {
func (c *client) WriteMultiProperty(dev bactype.Device, wp bactype.MultiplePropertyData) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
id, err := c.tsm.ID(ctx)
Expand Down Expand Up @@ -52,7 +52,7 @@ func (c *Client) WriteMultiProperty(dev bactype.Device, wp bactype.MultiplePrope
return fmt.Errorf("failed %d tries: %v", maxReattempt, err)
}

func (c *Client) sendWriteMultipleProperty(id int, dev bactype.Device, npdu *bactype.NPDU, request []byte) error {
func (c *client) sendWriteMultipleProperty(id int, dev bactype.Device, npdu *bactype.NPDU, request []byte) error {
_, err := c.Send(dev.Addr, npdu, request)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion writeprop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
bactype "github.com/alexbeltran/gobacnet/types"
)

func (c *Client) WriteProperty(dest bactype.Device, wp bactype.PropertyData) error {
func (c *client) WriteProperty(dest bactype.Device, wp bactype.PropertyData) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
id, err := c.tsm.ID(ctx)
Expand Down

0 comments on commit a3f0b6e

Please sign in to comment.