Skip to content

Commit

Permalink
feat(plc4go/bacnet): basic mapping to reader/writer
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jan 16, 2023
1 parent eb59f7c commit 55f7913
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
59 changes: 54 additions & 5 deletions plc4go/internal/bacnetip/MessageCodec.go
Expand Up @@ -128,14 +128,63 @@ func (m *ApplicationLayerMessageCodec) Send(message spi.Message) error {

func (m *ApplicationLayerMessageCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
// TODO: implement me
return nil
panic("not yet implemented")
}

func (m *ApplicationLayerMessageCodec) SendRequest(ctx context.Context, message spi.Message, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {

// TODO: implement me
m.Send(message)

address, err := NewAddress(m.remoteAddress)
if err != nil {
return err
}
iocb, err := NewIOCB(NewPDU(message, WithPDUDestination(address)), address)
if err != nil {
return errors.Wrap(err, "error creating IOCB")
}
go func() {
go m.bipSimpleApplication.RequestIO(iocb)
iocb.Wait()
if err := iocb.ioError; err != nil {
if err := handleError(err); err != nil {
log.Debug().Err(err).Msg("error handling error")
return
}
} else if response := iocb.ioResponse; response != nil {
// TODO: we wrap it into a BVLC for now. Once we change the Readers etc. to accept apdus we can remove that
tempBVLC := model.NewBVLCOriginalUnicastNPDU(
model.NewNPDU(
0,
model.NewNPDUControl(
false,
false,
false,
false,
model.NPDUNetworkPriority_NORMAL_MESSAGE,
),
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
response.GetMessage().(model.APDU),
0,
),
0,
)
if acceptsMessage(tempBVLC) {
if err := handleMessage(
tempBVLC,
); err != nil {
log.Debug().Err(err).Msg("error handling message")
return
}
}
} else {
// TODO: what now?
}
}()
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions plc4go/internal/bacnetip/Reader.go
Expand Up @@ -272,22 +272,22 @@ func (m *Reader) ToPlc4xReadResponse(apdu readWriteModel.APDU, readRequest apiMo
return spiModel.NewDefaultPlcReadResponse(readRequest, responseCodes, plcValues), nil
}

switch complexAck := complexAck.(type) {
switch serviceAck := complexAck.GetServiceAck().(type) {
case readWriteModel.BACnetServiceAckReadPropertyExactly:
// TODO: super lazy implementation for now
responseCodes[readRequest.GetTagNames()[0]] = apiModel.PlcResponseCode_OK
plcValues[readRequest.GetTagNames()[0]] = spiValues.NewPlcSTRING(complexAck.GetValues().(fmt.Stringer).String())
plcValues[readRequest.GetTagNames()[0]] = spiValues.NewPlcSTRING(serviceAck.GetValues().(fmt.Stringer).String())
case readWriteModel.BACnetServiceAckReadPropertyMultipleExactly:

// way to know how to interpret the responses is by aligning them with the
// items from the request as this information is not returned by the PLC.
if len(readRequest.GetTagNames()) != len(complexAck.GetData()) {
if len(readRequest.GetTagNames()) != len(serviceAck.GetData()) {
return nil, errors.New("The number of requested items doesn't match the number of returned items")
}
for i, tagName := range readRequest.GetTagNames() {
// TODO: super lazy implementation for now
responseCodes[tagName] = apiModel.PlcResponseCode_OK
plcValues[tagName] = spiValues.NewPlcSTRING(complexAck.GetData()[i].GetListOfResults().(fmt.Stringer).String())
plcValues[tagName] = spiValues.NewPlcSTRING(serviceAck.GetData()[i].GetListOfResults().(fmt.Stringer).String())
}
}

Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/bacnetip/UDPCommunicationsModule.go
Expand Up @@ -170,7 +170,7 @@ func NewUDPDirector(address AddressTuple[string, uint16], timeout *int, reuse *b
// create the request queue
d.request = make(chan _PDU)
go func() {
for {
for d.running {
pdu := <-d.request
serialize, err := pdu.GetMessage().Serialize()
if err != nil {
Expand Down

0 comments on commit 55f7913

Please sign in to comment.