Skip to content

Commit

Permalink
refactor(plc4go/cbus): added cleanups and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Sep 27, 2022
1 parent 01a2ece commit 04ff774
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 2 additions & 3 deletions plc4go/internal/cbus/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ func (c *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
log.Trace().Msg("Connecting")
ch := make(chan plc4go.PlcConnectionConnectResult)
go func() {
err := c.messageCodec.Connect()
if err != nil {
if err := c.messageCodec.Connect(); err != nil {
c.fireConnectionError(errors.Wrap(err, "Error connecting codec"), ch)
return
}
Expand Down Expand Up @@ -312,7 +311,7 @@ func (c *Connection) sendReset(ctx context.Context, ch chan plc4go.PlcConnection
if sendOutErrorNotification {
c.fireConnectionError(errors.Errorf("Timeout after %v", timeout.Sub(startTime)), ch)
} else {
log.Trace().Msg("timeout")
log.Trace().Msgf("Timeout after %v", timeout.Sub(startTime))
}
return false
}
Expand Down
10 changes: 7 additions & 3 deletions plc4go/internal/cbus/MessageCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ func (m *MessageCodec) Send(message spi.Message) error {
}

func (m *MessageCodec) Receive() (spi.Message, error) {
log.Trace().Msg("Receive")
ti := m.GetTransportInstance()
confirmation := false
// Fill the buffer
{
if err := ti.FillBuffer(func(_ uint, currentByte byte, reader *bufio.Reader) bool {
log.Trace().Uint8("byte", currentByte).Msg("current byte")
switch currentByte {
case
readWriteModel.ResponseTermination_CR,
Expand All @@ -111,6 +113,7 @@ func (m *MessageCodec) Receive() (spi.Message, error) {
return nil, errors.Wrap(err, "error filling buffer")
}
}
log.Trace().Msg("Buffer filled")

// Check how many readable bytes we have
var readableBytes uint32
Expand Down Expand Up @@ -165,11 +168,13 @@ lookingForTheEnd:
break lookingForTheEnd
}
}
log.Trace().Msgf("indexOfCR %d,indexOfLF %d,indexOfConfirmation %d", indexOfCR, indexOfLF, indexOfConfirmation)
if indexOfCR < 0 && indexOfLF >= 0 {
// This means that the package is garbage as a lf is always prefixed with a cr
log.Debug().Err(err).Msg("Error reading")
// TODO: Possibly clean up ...
return nil, nil
garbage, err := ti.Read(readableBytes)
log.Warn().Bytes("garbage", garbage).Msg("Garbage bytes")
return nil, err
}
if indexOfCR+1 == indexOfLF {
// This means a <cr> is directly followed by a <lf> which means that we know for sure this is a response
Expand Down Expand Up @@ -287,7 +292,6 @@ lookingForTheEnd:
}

log.Warn().Err(err).Msg("error parsing")
// TODO: Possibly clean up ...
return nil, nil
}
return cBusMessage, nil
Expand Down

0 comments on commit 04ff774

Please sign in to comment.