Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timestamp util #30

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions opcua/enc/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
}
serviceType, ok := messageBody.TypeId.Identifier.(uint16)
if !ok {
return errors.New("know type service")
return errors.New("unknown type service")

Check warning on line 168 in opcua/enc/decoder.go

View check run for this annotation

Codecov / codecov/patch

opcua/enc/decoder.go#L168

Added line #L168 was not covered by tests
}

switch uamsg.ServiceTypeEnum(serviceType) {
Expand Down Expand Up @@ -198,7 +198,7 @@
}
messageBody.Service = service
default:
return errors.New("know type service")
return errors.New("unknown type service")

Check warning on line 201 in opcua/enc/decoder.go

View check run for this annotation

Codecov / codecov/patch

opcua/enc/decoder.go#L201

Added line #L201 was not covered by tests
}
msg.MessageBody = messageBody
default:
Expand Down
8 changes: 4 additions & 4 deletions opcua/enc/special_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
)

func StringEncoder(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
value, ok := v.(string)
if !ok {
return nil, fmt.Errorf("error type of string, %v", reflect.TypeOf(v).Name())
}
buff := bytes.NewBuffer(nil)
length := int32(len(value))
if length == 0 {
length = -1
Expand All @@ -29,11 +29,11 @@
}

func ByteStringEncoder(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
value, ok := v.([]byte)
if !ok {
return nil, fmt.Errorf("error type of bytestring, %v", reflect.TypeOf(v).Name())
}
buff := bytes.NewBuffer(nil)
// ByteString's empty and null encodings are inconsistent
length := int32(len(value))
if length == 0 && value == nil {
Expand All @@ -48,7 +48,6 @@
}

func GuidEncoder(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
var guid uamsg.Guid
temp, ok := v.(*uamsg.Guid)
if !ok {
Expand All @@ -59,6 +58,7 @@
temp = &guid
}
guid = *temp
buff := bytes.NewBuffer(nil)

Check warning on line 61 in opcua/enc/special_encoder.go

View check run for this annotation

Codecov / codecov/patch

opcua/enc/special_encoder.go#L61

Added line #L61 was not covered by tests
err := binary.Write(buff, binary.LittleEndian, guid.Data1)
if err != nil {
return nil, err
Expand All @@ -79,7 +79,6 @@
}

func QualifiedNameEncoder(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
var qualifiedName uamsg.QualifiedName
temp, ok := v.(*uamsg.QualifiedName)
if !ok {
Expand All @@ -90,6 +89,7 @@
temp = &qualifiedName
}
qualifiedName = *temp
buff := bytes.NewBuffer(nil)

Check warning on line 92 in opcua/enc/special_encoder.go

View check run for this annotation

Codecov / codecov/patch

opcua/enc/special_encoder.go#L92

Added line #L92 was not covered by tests
err := binary.Write(buff, binary.LittleEndian, qualifiedName.NamespaceIndex)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions opcua/secure_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package opcua

type SecureChannel struct {
}
7 changes: 7 additions & 0 deletions opcua/uamsg/generic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package uamsg

const (
MessageSecurityModeInvalid MessageSecurityModeEnum = 0
MessageSecurityModeNone MessageSecurityModeEnum = 1
MessageSecurityModeSign MessageSecurityModeEnum = 2
MessageSecurityModeSignAndEncrypt MessageSecurityModeEnum = 3
)

type (
SecurityTokenRequestType uint32
MessageSecurityModeEnum uint32
Expand Down
17 changes: 17 additions & 0 deletions opcua/util/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import "time"

const (
// OpcuaDateTimeTicksToUnixEpoch ((1970 - 1601) * 365 + 3 * 24 + 17) * 86400 * 10 * 1000 * 1000
OpcuaDateTimeTicksToUnixEpoch int64 = 116444736000000000
)

func GetCurrentUaTimestamp() uint64 {
ticks := time.Now().UTC().UnixNano()/100 + OpcuaDateTimeTicksToUnixEpoch
if ticks < 0 {
ticks = 0

Check warning on line 13 in opcua/util/timestamp.go

View check run for this annotation

Codecov / codecov/patch

opcua/util/timestamp.go#L10-L13

Added lines #L10 - L13 were not covered by tests
}

return uint64(ticks)

Check warning on line 16 in opcua/util/timestamp.go

View check run for this annotation

Codecov / codecov/patch

opcua/util/timestamp.go#L16

Added line #L16 was not covered by tests
}