Skip to content

Commit

Permalink
feat(plc4go): support for DATE_AND_LTIME
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 6, 2024
1 parent 90f43c9 commit 51434bd
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
|| (simpleField.name == "seconds")
|| (simpleField.name == "nanoseconds")
|| (simpleField.name == "nannosecondsOfSecond")
|| (simpleField.name == "nanosecondsSinceEpoch")
|| (simpleField.name == "dayOfWeek")
)
)
Expand Down Expand Up @@ -258,6 +259,14 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
return values.NewPlcDATA_AND_TIMEFromSecondsSinceEpoch(secondsSinceEpoch), nil
</#if>
<#break>
<#case "DATE_AND_LTIME">
readBuffer.CloseContext("${type.name}")
<#if helper.hasFieldsWithNames(case.fields, "year", "month", "day", "hour", "minutes", "seconds", "nanoseconds")>
return values.NewPlcDATA_AND_LTIMEFromSegments(uint32(year), uint32(month), uint32(day), uint32(hour), uint32(minutes), uint32(seconds), uint32(nanoseconds)), nil
<#elseif helper.hasFieldsWithNames(case.fields, "nanosecondsSinceEpoch")>
return values.NewPlcDATA_AND_LTIMEFromNanosecondsSinceEpoch(nanosecondsSinceEpoch), nil
</#if>
<#break>
<#case "LDATE_AND_TIME">
readBuffer.CloseContext("${type.name}")
<#if helper.hasFieldsWithNames(case.fields, "nanosecondsSinceEpoch")>
Expand Down
3 changes: 2 additions & 1 deletion plc4go/pkg/api/values/plc_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ const (
TIME_OF_DAY PlcValueType = 0x55
LTIME_OF_DAY PlcValueType = 0x56
DATE_AND_TIME PlcValueType = 0x57
LDATE_AND_TIME PlcValueType = 0x58
DATE_AND_LTIME PlcValueType = 0x58
LDATE_AND_TIME PlcValueType = 0x59
Struct PlcValueType = 0x61
List PlcValueType = 0x62
RAW_BYTE_ARRAY PlcValueType = 0x71
Expand Down
5 changes: 2 additions & 3 deletions plc4go/protocols/s7/readwrite/model/DataItem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 132 additions & 0 deletions plc4go/spi/values/PlcDATE_AND_LTIME.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package values

import (
"context"
"encoding/binary"
"fmt"
"time"

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

type PlcDATE_AND_LTIME struct {
PlcValueAdapter
value time.Time
}

func NewPlcDATE_AND_LTIME(value time.Time) PlcDATE_AND_LTIME {
return PlcDATE_AND_LTIME{
value: value,
}
}

func NewPlcDATA_AND_LTIMEFromSegments(year, month, day, hour, minutes, seconds, nanoseconds uint32) PlcDATE_AND_LTIME {
return NewPlcDATE_AND_LTIME(time.Date(int(year), time.Month(month), int(day), int(hour), int(minutes), int(seconds), int(nanoseconds), time.Local))
}

func NewPlcDATA_AND_LTIMEFromNanosecondsSinceEpoch(nanosecondsSinceEpoch uint64) PlcDATE_AND_LTIME {
timeStamp := time.Time{}
timeStamp.Add(time.Duration(nanosecondsSinceEpoch) * time.Nanosecond)
return NewPlcDATE_AND_LTIME(timeStamp)
}

func (m PlcDATE_AND_LTIME) IsRaw() bool {
return true
}

func (m PlcDATE_AND_LTIME) GetRaw() []byte {
theBytes, _ := m.Serialize()
return theBytes
}

func (m PlcDATE_AND_LTIME) GetSecondsSinceEpoch() uint32 {
return uint32(m.value.Unix())
}

func (m PlcDATE_AND_LTIME) GetYear() uint16 {
return uint16(m.value.Year())
}

func (m PlcDATE_AND_LTIME) GetMonth() uint8 {
return uint8(m.value.Month())
}

func (m PlcDATE_AND_LTIME) GetDay() uint8 {
return uint8(m.value.Day())
}

func (m PlcDATE_AND_LTIME) GetDayOfWeek() uint8 {
return uint8(m.value.Weekday())
}

func (m PlcDATE_AND_LTIME) GetHour() uint8 {
return uint8(m.value.Hour())
}

func (m PlcDATE_AND_LTIME) GetMinutes() uint8 {
return uint8(m.value.Minute())
}

func (m PlcDATE_AND_LTIME) GetSeconds() uint8 {
return uint8(m.value.Second())
}

func (m PlcDATE_AND_LTIME) GetNanoseconds() uint32 {
return uint32(m.value.Nanosecond())
}

func (m PlcDATE_AND_LTIME) GetMillisecondsOfSecond() uint64 {
return uint64(time.Duration(m.GetNanoseconds()).Milliseconds())
}

func (m PlcDATE_AND_LTIME) IsDateTime() bool {
return true
}

func (m PlcDATE_AND_LTIME) GetDateTime() time.Time {
return m.value
}

func (m PlcDATE_AND_LTIME) GetString() string {
return fmt.Sprintf("%v", m.GetDateTime())
}

func (m PlcDATE_AND_LTIME) GetPlcValueType() apiValues.PlcValueType {
return apiValues.DATE_AND_LTIME
}

func (m PlcDATE_AND_LTIME) Serialize() ([]byte, error) {
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))
if err := m.SerializeWithWriteBuffer(context.Background(), wb); err != nil {
return nil, err
}
return wb.GetBytes(), nil
}

func (m PlcDATE_AND_LTIME) SerializeWithWriteBuffer(ctx context.Context, writeBuffer utils.WriteBuffer) error {
return writeBuffer.WriteString("PlcDATE_AND_LTIME", uint32(len([]rune(m.GetString()))*8), "UTF-8", m.GetString())
}

func (m PlcDATE_AND_LTIME) String() string {
return fmt.Sprintf("%s(%dbit):%v", m.GetPlcValueType(), uint32(len([]rune(m.GetString()))*8), m.value)
}

0 comments on commit 51434bd

Please sign in to comment.