Skip to content

Commit

Permalink
feat(plc4go): added GetPLCValueType() PLCValueType to plc_value.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 19, 2022
1 parent 2a860ce commit 1f14f81
Show file tree
Hide file tree
Showing 34 changed files with 277 additions and 76 deletions.
39 changes: 39 additions & 0 deletions plc4go/pkg/api/values/plc_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ type PlcValue interface {
GetStruct() map[string]PlcValue
//
///

GetPLCValueType() PLCValueType
}

// RawPlcValue This type is used in cases where the driver doesn't have access to type information and therefore can't decode
Expand All @@ -144,3 +146,40 @@ type RawPlcValue interface {
// RawReset Reset the internal read-buffer (For the case that a raw plc-value has to be parsed multiple times)
RawReset()
}

type PLCValueType uint8

const (
BINT PLCValueType = iota
BIT_STRING
BOOL
BREAL
BYTE
BYTE_ARRAY
CHAR
DATE
DATE_AND_TIME
DINT
DWORD
INT
LINT
LIST
LREAL
LTIME
LWORD
NULL
RAW_PLC_VALUE
REAL
STRUCT
SINT
STRING
TIME
TIME_OF_DAY
UDINT
UINT
USINT
ULINT
WCHAR
WORD
WSTRING
)
5 changes: 5 additions & 0 deletions plc4go/spi/values/BINT.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
"math/big"
Expand Down Expand Up @@ -126,6 +127,10 @@ func (m PlcBINT) GetString() string {
return strconv.Itoa(int(m.GetInt64()))
}

func (m PlcBINT) GetPLCValueType() apiValues.PLCValueType {
return apiValues.BINT
}

func (m PlcBINT) isZero() bool {
return m.value.Cmp(big.NewInt(0)) == 0
}
Expand Down
9 changes: 8 additions & 1 deletion plc4go/spi/values/BOOL.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package values

import "github.com/apache/plc4x/plc4go/spi/utils"
import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

type PlcBOOL struct {
value bool
Expand Down Expand Up @@ -74,6 +77,10 @@ func (m PlcBOOL) GetString() string {
}
}

func (m PlcBOOL) GetPLCValueType() apiValues.PLCValueType {
return apiValues.BOOL
}

func (m PlcBOOL) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteBit("PlcBOOL", m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/BREAL.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package values

import (
"fmt"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
"math/big"
Expand Down Expand Up @@ -156,6 +157,10 @@ func (m PlcBREAL) GetString() string {
return fmt.Sprintf("%g", m.GetFloat64())
}

func (m PlcBREAL) GetPLCValueType() apiValues.PLCValueType {
return apiValues.BREAL
}

func (m PlcBREAL) isZero() bool {
return m.value.Cmp(big.NewFloat(0.0)) == 0.0
}
Expand Down
5 changes: 5 additions & 0 deletions plc4go/spi/values/BYTE.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

Expand Down Expand Up @@ -98,6 +99,10 @@ func (m PlcBYTE) GetString() string {
return strVal
}

func (m PlcBYTE) GetPLCValueType() apiValues.PLCValueType {
return apiValues.BYTE
}

func (m PlcBYTE) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteByte("PlcBYTE", m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/CHAR.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

Expand Down Expand Up @@ -47,6 +48,10 @@ func (m PlcCHAR) GetString() string {
return string(m.value)
}

func (m PlcCHAR) GetPLCValueType() apiValues.PLCValueType {
return apiValues.CHAR
}

func (m PlcCHAR) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteString("PlcBYTE", 16, "UTF-8", string(m.value))
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/DATE.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"time"
)
Expand Down Expand Up @@ -66,6 +67,10 @@ func (m PlcDATE) GetString() string {
return m.GetDate().Format("2006-01-02")
}

func (m PlcDATE) GetPLCValueType() apiValues.PLCValueType {
return apiValues.DATE
}

func (m PlcDATE) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteString("PlcDATE", uint32(len([]rune(m.GetString()))*8), "UTF-8", m.GetString())
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/DATE_AND_TIME.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package values

import (
"fmt"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"time"
)
Expand Down Expand Up @@ -53,6 +54,10 @@ func (m PlcDATE_AND_TIME) GetString() string {
return fmt.Sprintf("%v", m.GetDateTime())
}

func (m PlcDATE_AND_TIME) GetPLCValueType() apiValues.PLCValueType {
return apiValues.DATE_AND_TIME
}

func (m PlcDATE_AND_TIME) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteString("PlcDATE_AND_TIME", uint32(len([]rune(m.GetString()))*8), "UTF-8", m.GetString())
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/DINT.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
"strconv"
Expand Down Expand Up @@ -137,6 +138,10 @@ func (m PlcDINT) GetString() string {
return strconv.Itoa(int(m.GetInt64()))
}

func (m PlcDINT) GetPLCValueType() apiValues.PLCValueType {
return apiValues.DINT
}

func (m PlcDINT) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteInt32("PlcDINT", 32, m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/DWORD.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

Expand Down Expand Up @@ -97,6 +98,10 @@ func (m PlcDWORD) GetString() string {
return strVal
}

func (m PlcDWORD) GetPLCValueType() apiValues.PLCValueType {
return apiValues.DWORD
}

func (m PlcDWORD) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteUint32("PlcDINT", 32, m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/INT.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
"strconv"
Expand Down Expand Up @@ -130,6 +131,10 @@ func (m PlcINT) GetString() string {
return strconv.Itoa(int(m.GetInt64()))
}

func (m PlcINT) GetPLCValueType() apiValues.PLCValueType {
return apiValues.INT
}

func (m PlcINT) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteInt16("PlcINT", 16, m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/LINT.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
"strconv"
Expand Down Expand Up @@ -144,6 +145,10 @@ func (m PlcLINT) GetString() string {
return strconv.Itoa(int(m.GetInt64()))
}

func (m PlcLINT) GetPLCValueType() apiValues.PLCValueType {
return apiValues.LINT
}

func (m PlcLINT) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteInt64("PlcLINT", 64, m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/LREAL.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package values

import (
"fmt"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"math"
)
Expand Down Expand Up @@ -160,6 +161,10 @@ func (m PlcLREAL) GetString() string {
return fmt.Sprintf("%g", m.GetFloat64())
}

func (m PlcLREAL) GetPLCValueType() apiValues.PLCValueType {
return apiValues.LREAL
}

func (m PlcLREAL) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteFloat64("PlcLREAL", 64, m.value)
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/LTIME.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package values

import (
"fmt"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
"time"
)
Expand Down Expand Up @@ -58,6 +59,10 @@ func (m PlcLTIME) GetString() string {
return fmt.Sprintf("PT%0.fS", m.GetDuration().Seconds())
}

func (m PlcLTIME) GetPLCValueType() apiValues.PLCValueType {
return apiValues.LTIME
}

func (m PlcLTIME) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteString("PlcLTIME", uint32(len([]rune(m.GetString()))*8), "UTF-8", m.GetString())
}
5 changes: 5 additions & 0 deletions plc4go/spi/values/LWORD.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package values

import (
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

Expand Down Expand Up @@ -113,6 +114,10 @@ func (m PlcLWORD) GetString() string {
return strVal
}

func (m PlcLWORD) GetPLCValueType() apiValues.PLCValueType {
return apiValues.LWORD
}

func (m PlcLWORD) Serialize(writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteUint64("PlcLWORD", 64, m.value)
}
6 changes: 6 additions & 0 deletions plc4go/spi/values/NULL.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package values

import apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"

type PlcNULL struct {
PlcValueAdapter
}
Expand All @@ -30,3 +32,7 @@ func NewPlcNULL() PlcNULL {
func (m PlcNULL) GetRaw() []byte {
return []byte{}
}

func (m PlcNULL) GetPLCValueType() apiValues.PLCValueType {
return apiValues.NULL
}
20 changes: 12 additions & 8 deletions plc4go/spi/values/PlcBitString.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package values

import (
api "github.com/apache/plc4x/plc4go/pkg/api/values"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
)

Expand All @@ -29,25 +29,25 @@ type PlcBitString struct {
}

func NewPlcBitString(value interface{}) PlcBitString {
var bools []api.PlcValue
var bools []apiValues.PlcValue
switch value.(type) {
case uint8:
bools = make([]api.PlcValue, 8)
bools = make([]apiValues.PlcValue, 8)
for i := 0; i < 8; i++ {
bools[i] = NewPlcBOOL(((value.(uint8) >> uint8((8-1)-i)) & 0x01) == 0x01)
}
case uint16:
bools = make([]api.PlcValue, 16)
bools = make([]apiValues.PlcValue, 16)
for i := 0; i < 16; i++ {
bools[i] = NewPlcBOOL(((value.(uint16) >> uint8((16-1)-i)) & 0x01) == 0x01)
}
case uint32:
bools = make([]api.PlcValue, 32)
bools = make([]apiValues.PlcValue, 32)
for i := 0; i < 32; i++ {
bools[i] = NewPlcBOOL(((value.(uint32) >> uint8((32-1)-i)) & 0x01) == 0x01)
}
case uint64:
bools = make([]api.PlcValue, 64)
bools = make([]apiValues.PlcValue, 64)
for i := 0; i < 64; i++ {
bools[i] = NewPlcBOOL(((value.(uint64) >> uint8((64-1)-i)) & 0x01) == 0x01)
}
Expand All @@ -72,10 +72,14 @@ func (m PlcBitString) GetLength() uint32 {
return uint32(len(m.Values))
}

func (m PlcBitString) GetIndex(i uint32) api.PlcValue {
func (m PlcBitString) GetIndex(i uint32) apiValues.PlcValue {
return m.Values[i]
}

func (m PlcBitString) GetList() []api.PlcValue {
func (m PlcBitString) GetList() []apiValues.PlcValue {
return m.Values
}

func (m PlcBitString) GetPLCValueType() apiValues.PLCValueType {
return apiValues.BIT_STRING
}
Loading

0 comments on commit 1f14f81

Please sign in to comment.