Skip to content

Commit

Permalink
feat(plc4go/cbus): prepare bridge support
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Mar 24, 2023
1 parent 874e2b9 commit 74976da
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 211 deletions.
4 changes: 2 additions & 2 deletions plc4go/internal/cbus/Browser.go
Expand Up @@ -116,7 +116,7 @@ func (m Browser) BrowseQuery(ctx context.Context, browseRequest apiModel.PlcBrow
}
readTagName := fmt.Sprintf("%s/%d/%s", queryName, unitAddress, attribute)
readRequest, _ := m.connection.ReadRequestBuilder().
AddTag(readTagName, NewCALIdentifyTag(unit, attribute, 1)).
AddTag(readTagName, NewCALIdentifyTag(unit, nil /*TODO: add bridge support*/, attribute, 1)).
Build()
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, time.Second*2)
requestResult := <-readRequest.ExecuteWithContext(timeoutCtx)
Expand All @@ -133,7 +133,7 @@ func (m Browser) BrowseQuery(ctx context.Context, browseRequest apiModel.PlcBrow
continue unitLoop
}
queryResult := &model.DefaultPlcBrowseItem{
Tag: NewCALIdentifyTag(unit, attribute, 1),
Tag: NewCALIdentifyTag(unit, nil /*TODO: add bridge support*/, attribute, 1),
Name: queryName,
Readable: true,
Writable: false,
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/cbus/CBusMessageMapper.go
Expand Up @@ -74,7 +74,7 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera

cBusMessage, supportsRead = readWriteModel.NewCBusMessageToServer(request, requestContext, cbusOptions), true
return
case *calGetstatusTag:
case *calGetStatusTag:
calData := readWriteModel.NewCALDataGetStatus(tagType.parameter, tagType.count, readWriteModel.CALCommandTypeContainer_CALCommandGetStatus, nil, requestContext)
//TODO: we need support for bridged commands
command := readWriteModel.NewCBusPointToPointCommandDirect(tagType.unitAddress, 0x0000, calData, cbusOptions)
Expand Down
116 changes: 116 additions & 0 deletions plc4go/internal/cbus/CBusMessageMapper_test.go
@@ -0,0 +1,116 @@
package cbus

import (
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/cbus/readwrite/model"
"github.com/apache/plc4x/plc4go/spi"
"reflect"
"testing"
)

func TestMapEncodedReply(t *testing.T) {
type args struct {
transaction *spi.RequestTransaction
encodedReply readWriteModel.EncodedReply
tagName string
addResponseCode func(name string, responseCode apiModel.PlcResponseCode)
addPlcValue func(name string, plcValue apiValues.PlcValue)
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty input",
args: args{
transaction: func() *spi.RequestTransaction {
transactionManager := spi.NewRequestTransactionManager(1)
transaction := transactionManager.StartTransaction()
transaction.Submit(func() {
// NO-OP
})
return transaction
}(),
encodedReply: nil,
tagName: "",
addResponseCode: nil,
addPlcValue: nil,
},
},
{
name: "CALDataStatus",
args: args{
transaction: func() *spi.RequestTransaction {
transactionManager := spi.NewRequestTransactionManager(1)
transaction := transactionManager.StartTransaction()
transaction.Submit(func() {
// NO-OP
})
return transaction
}(),
encodedReply: func() readWriteModel.EncodedReplyCALReply {
calDataStatus := readWriteModel.NewCALDataStatus(readWriteModel.ApplicationIdContainer_LIGHTING_3A, 0, nil, readWriteModel.CALCommandTypeContainer_CALCommandStatus_0Bytes, nil, nil)
calReplyShort := readWriteModel.NewCALReplyShort(0, calDataStatus, nil, nil)
return readWriteModel.NewEncodedReplyCALReply(calReplyShort, 0, nil, nil)
}(),
tagName: "someTag",
addResponseCode: func(name string, responseCode apiModel.PlcResponseCode) {
// TODO: add assertions
},
addPlcValue: func(name string, plcValue apiValues.PlcValue) {
// TODO: add assertions
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := MapEncodedReply(tt.args.transaction, tt.args.encodedReply, tt.args.tagName, tt.args.addResponseCode, tt.args.addPlcValue); (err != nil) != tt.wantErr {
t.Errorf("MapEncodedReply() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestTagToCBusMessage(t *testing.T) {
type args struct {
tag apiModel.PlcTag
value apiValues.PlcValue
alphaGenerator *AlphaGenerator
messageCodec *MessageCodec
}
tests := []struct {
name string
args args
wantCBusMessage readWriteModel.CBusMessage
wantSupportsRead bool
wantSupportsWrite bool
wantSupportsSubscribe bool
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCBusMessage, gotSupportsRead, gotSupportsWrite, gotSupportsSubscribe, err := TagToCBusMessage(tt.args.tag, tt.args.value, tt.args.alphaGenerator, tt.args.messageCodec)
if (err != nil) != tt.wantErr {
t.Errorf("TagToCBusMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotCBusMessage, tt.wantCBusMessage) {
t.Errorf("TagToCBusMessage() gotCBusMessage = %v, want %v", gotCBusMessage, tt.wantCBusMessage)
}
if gotSupportsRead != tt.wantSupportsRead {
t.Errorf("TagToCBusMessage() gotSupportsRead = %v, want %v", gotSupportsRead, tt.wantSupportsRead)
}
if gotSupportsWrite != tt.wantSupportsWrite {
t.Errorf("TagToCBusMessage() gotSupportsWrite = %v, want %v", gotSupportsWrite, tt.wantSupportsWrite)
}
if gotSupportsSubscribe != tt.wantSupportsSubscribe {
t.Errorf("TagToCBusMessage() gotSupportsSubscribe = %v, want %v", gotSupportsSubscribe, tt.wantSupportsSubscribe)
}
})
}
}
171 changes: 0 additions & 171 deletions plc4go/internal/cbus/Reader_test.go

This file was deleted.

0 comments on commit 74976da

Please sign in to comment.