Skip to content

Commit

Permalink
fix(plc4go): port over s7 changes to golang
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 6, 2024
1 parent 570cf1e commit faba6fe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions plc4go/protocols/s7/readwrite/model/StaticHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"time"
)

Expand Down Expand Up @@ -188,3 +189,24 @@ func S7msecToInt(ctx context.Context, readBuffer utils.ReadBuffer) (any, error)
func IntToS7msec(ctx context.Context, writeBuffer utils.WriteBuffer, value uint16) error {
return nil
}

func ParseSiemensYear(_ context.Context, readBuffer utils.ReadBuffer) (uint16, error) {
year, err := readBuffer.ReadUint16("year", 8, utils.WithEncoding("BCD"))
if err != nil {
return 0, errors.Wrap(err, "Error parsing year")
}
if year < 90 {
return 2000 + year, nil
} else {
return 1900 + year, nil
}
}

func SerializeSiemensYear(ctx context.Context, writeBuffer utils.WriteBuffer, dateTime values.PlcValue) error {
year := dateTime.GetDateTime().Year()
if year > 2000 {
return writeBuffer.WriteUint16("year", 8, uint16(year-2000), utils.WithEncoding("BCD"))
} else {
return writeBuffer.WriteUint16("year", 8, uint16(year-1900), utils.WithEncoding("BCD"))
}
}
10 changes: 10 additions & 0 deletions plc4go/spi/utils/Buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func WithRenderAsList(renderAsList bool) WithReaderWriterArgs {
return withRenderAsList{readerWriterArg: readerWriterArg{WithReaderArgs: readerArg{}, WithWriterArgs: writerArg{}}, renderAsList: renderAsList}
}

// WithEncoding specifies an encoding
func WithEncoding(encoding string) WithReaderWriterArgs {
return withEncoding{readerWriterArg: readerWriterArg{WithReaderArgs: readerArg{}, WithWriterArgs: writerArg{}}, encoding: encoding}
}

///////////////////////////////////////
///////////////////////////////////////
//
Expand Down Expand Up @@ -64,6 +69,11 @@ type withRenderAsList struct {
renderAsList bool
}

type withEncoding struct {
readerWriterArg
encoding string
}

//
// Internal section
//
Expand Down
22 changes: 22 additions & 0 deletions plc4go/spi/utils/bufferCommons.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ func (b BufferCommons) ExtractAdditionalStringRepresentation(readerWriterArgs ..
return ""
}

func (b BufferCommons) ExtractEncoding(readerWriterArgs ...WithReaderWriterArgs) string {
for _, arg := range readerWriterArgs {
if !arg.isWriterArgs() && !arg.isReaderArgs() {
panic("not a reader or writer arg")
}
switch rwArg := arg.(type) {
case withEncoding:
return rwArg.encoding
case readerWriterArg:
switch rArg := rwArg.WithReaderArgs.(type) {
case withEncoding:
return rArg.encoding
}
switch wArg := rwArg.WithWriterArgs.(type) {
case withEncoding:
return wArg.encoding
}
}
}
return ""
}

type Stack struct {
list.List
}
Expand Down

0 comments on commit faba6fe

Please sign in to comment.