Skip to content

Commit

Permalink
parse datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Jun 20, 2020
1 parent 41f5510 commit 93b9735
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 36 deletions.
27 changes: 14 additions & 13 deletions encoding/appdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,19 @@ func (e *Encoder) time(t bactype.Time) {
// Stored as 1/100 of a second
e.write(uint8(t.Millisecond / 10))
}
func (d *Decoder) time(t *bactype.Time) {
var hour, min, sec, centisec uint8
d.decode(&hour)
d.decode(&min)
d.decode(&sec)
// Yeah, they report centisecs instead of milliseconds.
d.decode(&centisec)

t.Hour = int(hour)
t.Minute = int(min)
t.Second = int(sec)
t.Millisecond = int(centisec) * 10
func (d *Decoder) time(t *bactype.Time, length int) {
if length <= 0 {
return
}
data := make([]byte, length)
if _, d.err = d.Read(data); d.err != nil {
return
}

t.Hour = int(data[0])
t.Minute = int(data[1])
t.Second = int(data[2])
t.Millisecond = int(data[3]) * 10

}

Expand Down Expand Up @@ -292,7 +293,7 @@ func (d *Decoder) AppDataOfTag(tag uint8, len int) (interface{}, error) {
return date, d.Error()
case tagTime:
var t bactype.Time
d.time(&t)
d.time(&t, len)
return t, d.Error()
case tagObjectID:
objType, objInstance := d.objectId()
Expand Down
16 changes: 7 additions & 9 deletions encoding/context_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ func tagTypeInContext(property types.PropertyType, tagNumber uint8) uint8 {
case types.PROP_REQUESTED_SHED_LEVEL:
case types.PROP_EXPECTED_SHED_LEVEL:
switch tagNumber {
case 0:
case 1:
case 0, 1:
tag = tagUint
break
case 2:
Expand All @@ -23,20 +22,16 @@ func tagTypeInContext(property types.PropertyType, tagNumber uint8) uint8 {
break
case types.PROP_ACTION:
switch tagNumber {
case 0:
case 1:
case 0, 1:
tag = tagObjectID
break
case 2:
tag = tagEnumerated
break
case 3:
case 5:
case 6:
case 3, 5, 6:
tag = tagUint
break
case 7:
case 8:
case 7, 8:
tag = tagBool
break
case 4: /* propertyValue: abstract syntax */
Expand Down Expand Up @@ -71,6 +66,7 @@ func tagTypeInContext(property types.PropertyType, tagNumber uint8) uint8 {
case types.PROP_LOG_DEVICE_OBJECT_PROPERTY:
switch tagNumber {
case 0: /* Object ID */
fallthrough
case 3: /* Device ID */
tag = tagObjectID
break
Expand All @@ -88,6 +84,7 @@ func tagTypeInContext(property types.PropertyType, tagNumber uint8) uint8 {
/* BACnetARRAY[N] of BACnetDeviceObjectReference */
switch tagNumber {
case 0: /* Optional Device ID */
fallthrough
case 1: /* Object ID */
tag = tagObjectID
break
Expand Down Expand Up @@ -134,6 +131,7 @@ func tagTypeInContext(property types.PropertyType, tagNumber uint8) uint8 {
break
}
break

default:
break
}
Expand Down
5 changes: 5 additions & 0 deletions encoding/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (d *Decoder) Read(data []byte) (int, error) {
return d.buff.Read(data)
}

func (d *Decoder) Skip(n uint32) error {
_, d.err = d.buff.Read(make([]byte, n))
return d.err
}

func (d *Decoder) UnreadByte() error {
return d.buff.UnreadByte()
}
Expand Down
38 changes: 24 additions & 14 deletions encoding/readmultipleack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package encoding

import (
"fmt"

bactype "github.com/alexbeltran/gobacnet/types"
)

Expand Down Expand Up @@ -134,7 +133,9 @@ func (d *Decoder) objectsWithData(objects *[]bactype.Object) error {

// Tag 3 - (Optional) Array Length
tag, meta = d.tagNumber()
if tag == 3 {
if tag == 2 {
continue
} else if tag == 3 {
if !meta.isContextSpecific() {
return &ErrorWrongTagType{ContextTag}
}
Expand All @@ -157,6 +158,23 @@ func (d *Decoder) objectsWithData(objects *[]bactype.Object) error {
if meta.isContextSpecific() {
if meta.isClosing() {
_ = d.UnreadByte()
} else if meta.isOpening() {
if /*prop.Type == bactype.PROP_EVENT_TIME_STAMPS &&*/ tag == bactype.TimeStampDatetime {
dt := &bactype.DataTime{}
for {
tag, meta, length = d.tagNumberAndValue()
if meta.isClosing() && tag == bactype.TimeStampDatetime {
break
} else if tag == tagDate {
d.date(&dt.Date, int(length))
} else if tag == tagTime {
d.time(&dt.Time, int(length))
} else if length > 0 {
d.Skip(length)
}
}
array = append(array, dt)
}
} else {
// TODO how to parse it in Context???
*objects = append(*objects, obj)
Expand Down Expand Up @@ -187,12 +205,12 @@ func (d *Decoder) objectsWithData(objects *[]bactype.Object) error {
array = append(array, data)
}
tag, meta = d.tagNumber()
if meta.isClosing() { //tag 4
if meta.isClosing() && tag == 4 { //tag 4
//
break
} else {
} /*else {
_ = d.UnreadByte()
}
}*/
}
if len(array) == 1 {
prop.Data = array[0]
Expand All @@ -201,7 +219,7 @@ func (d *Decoder) objectsWithData(objects *[]bactype.Object) error {
}
obj.Properties = append(obj.Properties, prop)

tag, meta = d.tagNumber()
tag, meta, length = d.tagNumberAndValue()
} else if tag == 5 && meta.isOpening() {
//Tag 5 error
var class, code uint32
Expand All @@ -219,14 +237,6 @@ func (d *Decoder) objectsWithData(objects *[]bactype.Object) error {
}
}

// Tag 1 - Closing Tag
tag, meta = d.tagNumber()
if tag != 1 {
return &ErrorIncorrectTag{Expected: 1, Given: tag}
} else if !meta.isClosing() {
return &ErrorWrongTagType{OpeningTag}
}

*objects = append(*objects, obj)
}
return d.Error()
Expand Down
11 changes: 11 additions & 0 deletions types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ type Time struct {
Millisecond int
}

type DataTime struct {
Date
Time
}

// UnspecifiedTime means that this time is triggered through out a period. An
// example of this is 02:FF:FF:FF will trigger all through out 2 am
const UnspecifiedTime = 0xFF

const (
TimeStampTime = 0
TimeStampSequence = 1
TimeStampDatetime = 2
)

0 comments on commit 93b9735

Please sign in to comment.