Skip to content
This repository was archived by the owner on Mar 12, 2026. It is now read-only.

Commit beebb85

Browse files
committed
add new field struct
1 parent d562c4c commit beebb85

5 files changed

Lines changed: 304 additions & 206 deletions

File tree

frame.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

message.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package mapache
2+
3+
import "fmt"
4+
5+
type Message struct {
6+
ID int
7+
Data []byte
8+
Fields []Field
9+
}
10+
11+
type Field struct {
12+
// Name of the field. Will be mapped to a signal name unless otherwise specified by ExportSignalFunc.
13+
Name string
14+
// Bytes, Size, Sign, and Endian are used to properly decode and encode the signal.
15+
Bytes []byte
16+
Size int
17+
Sign SignMode
18+
Endian Endian
19+
// Value is the integer value of the field.
20+
Value int
21+
// ExportSignalFunc is the function that is used to export the field as an array of signals.
22+
ExportSignalFunc ExportSignalFunc
23+
}
24+
25+
// ExportSignalFunc is a function that indicates how a field should be exported as an array of signals.
26+
// Any required scaling will be applied here. If ExportSignalFunc is not set, the field will be directly
27+
// exported as a single signal without scaling.
28+
type ExportSignalFunc func(Field) []Signal
29+
30+
// NewField creates a new Field object with the given name, bytes, size, sign, endian, and export function.
31+
// If no export function is provided, DefaultSignalExportFunc will be used.
32+
func NewField(name string, bytes []byte, size int, sign SignMode, endian Endian, exportSignalFunc ExportSignalFunc) Field {
33+
return Field{
34+
Name: name,
35+
Bytes: bytes,
36+
Size: size,
37+
Sign: sign,
38+
Endian: endian,
39+
ExportSignalFunc: exportSignalFunc,
40+
}
41+
}
42+
43+
// Decode takes a Field object, decodes the bytes into an integer value, and returns the decoded Field object.
44+
func (f Field) Decode() Field {
45+
if f.Sign == Signed && f.Endian == BigEndian {
46+
f.Value = BigEndianBytesToSignedInt(f.Bytes)
47+
} else if f.Sign == Signed && f.Endian == LittleEndian {
48+
f.Value = LittleEndianBytesToSignedInt(f.Bytes)
49+
} else if f.Sign == Unsigned && f.Endian == BigEndian {
50+
f.Value = BigEndianBytesToUnsignedInt(f.Bytes)
51+
} else if f.Sign == Unsigned && f.Endian == LittleEndian {
52+
f.Value = LittleEndianBytesToUnsignedInt(f.Bytes)
53+
}
54+
return f
55+
}
56+
57+
// Encode takes a Field object, encodes the integer value into bytes, and returns the encoded Field object.
58+
func (f Field) Encode() (Field, error) {
59+
var err error
60+
if f.Sign == Signed && f.Endian == BigEndian {
61+
f.Bytes, err = BigEndianSignedIntToBinary(f.Value, f.Size)
62+
} else if f.Sign == Signed && f.Endian == LittleEndian {
63+
f.Bytes, err = LittleEndianSignedIntToBinary(f.Value, f.Size)
64+
} else if f.Sign == Unsigned && f.Endian == BigEndian {
65+
f.Bytes, err = BigEndianUnsignedIntToBinary(f.Value, f.Size)
66+
} else if f.Sign == Unsigned && f.Endian == LittleEndian {
67+
f.Bytes, err = LittleEndianUnsignedIntToBinary(f.Value, f.Size)
68+
} else {
69+
return f, fmt.Errorf("invalid sign or endian")
70+
}
71+
return f, err
72+
}
73+
74+
// CheckBit takes a Field object and a bit position, and returns true if the bit at the given position is set to 1, otherwise false.
75+
func (f Field) CheckBit(bit int) bool {
76+
return (f.Bytes[0] & (1 << bit)) != 0
77+
}
78+
79+
// ExportSignals takes a Field object and exports it as an array of signals.
80+
func (f Field) ExportSignals() []Signal {
81+
if f.ExportSignalFunc == nil {
82+
return DefaultSignalExportFunc(f)
83+
}
84+
return f.ExportSignalFunc(f)
85+
}
86+
87+
// DefaultSignalExportFunc is the default export function for a field. It exports the field as a single signal with no scaling.
88+
func DefaultSignalExportFunc(f Field) []Signal {
89+
return []Signal{Signal{
90+
Name: f.Name,
91+
Value: float64(f.Value),
92+
RawValue: f.Value,
93+
}}
94+
}

message_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package mapache
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
"unsafe"
8+
)
9+
10+
func TestTimestamp(t *testing.T) {
11+
now := time.Now().UnixMicro()
12+
fmt.Println(now)
13+
fmt.Printf("Size of timestamp: %d bytes\n", unsafe.Sizeof(now))
14+
}

signal.go

Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mapache
22

33
import (
4-
"fmt"
54
"time"
65
)
76

@@ -38,84 +37,76 @@ type Signal struct {
3837
ProducedAt time.Time `json:"produced_at"`
3938
// CreatedAt is the time at which the signal was actually stored in the database.
4039
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;precision:6"`
41-
42-
// Bytes, Size, Sign, and Endian are used to properly decode and encode the signal.
43-
Bytes []byte `json:"-" gorm:"-"`
44-
Size int `json:"-" gorm:"-"`
45-
Sign SignMode `json:"-" gorm:"-"`
46-
Endian Endian `json:"-" gorm:"-"`
47-
// ScalingFunc is the function that is used to scale the signal.
48-
ScalingFunc ScalingFunc `json:"-" gorm:"-"`
4940
}
5041

5142
func (Signal) TableName() string {
5243
return "signal"
5344
}
5445

55-
// ScalingFunc is a function that indicated how a value should be scaled.
56-
type ScalingFunc func(float64) float64
46+
// // ScalingFunc is a function that indicated how a value should be scaled.
47+
// type ScalingFunc func(float64) float64
5748

58-
// Scale scales the value of the signal by the given function.
59-
func (s Signal) Scale() Signal {
60-
if s.ScalingFunc == nil {
61-
return s
62-
}
63-
s.Value = s.ScalingFunc(float64(s.RawValue))
64-
return s
65-
}
49+
// // Scale scales the value of the signal by the given function.
50+
// func (s Signal) Scale() Signal {
51+
// if s.ScalingFunc == nil {
52+
// return s
53+
// }
54+
// s.Value = s.ScalingFunc(float64(s.RawValue))
55+
// return s
56+
// }
6657

67-
// Decode decodes the bytes stored in a Signal object and returns a signal object with the decoded raw value.
68-
func (s Signal) Decode() Signal {
69-
if s.Sign == Signed && s.Endian == BigEndian {
70-
s.RawValue = BigEndianBytesToSignedInt(s.Bytes)
71-
} else if s.Sign == Signed && s.Endian == LittleEndian {
72-
s.RawValue = LittleEndianBytesToSignedInt(s.Bytes)
73-
} else if s.Sign == Unsigned && s.Endian == BigEndian {
74-
s.RawValue = BigEndianBytesToUnsignedInt(s.Bytes)
75-
} else if s.Sign == Unsigned && s.Endian == LittleEndian {
76-
s.RawValue = LittleEndianBytesToUnsignedInt(s.Bytes)
77-
}
78-
return s
79-
}
58+
// // Decode decodes the bytes stored in a Signal object and returns a signal object with the decoded raw value.
59+
// func (s Signal) Decode() Signal {
60+
// if s.Sign == Signed && s.Endian == BigEndian {
61+
// s.RawValue = BigEndianBytesToSignedInt(s.Bytes)
62+
// } else if s.Sign == Signed && s.Endian == LittleEndian {
63+
// s.RawValue = LittleEndianBytesToSignedInt(s.Bytes)
64+
// } else if s.Sign == Unsigned && s.Endian == BigEndian {
65+
// s.RawValue = BigEndianBytesToUnsignedInt(s.Bytes)
66+
// } else if s.Sign == Unsigned && s.Endian == LittleEndian {
67+
// s.RawValue = LittleEndianBytesToUnsignedInt(s.Bytes)
68+
// }
69+
// return s
70+
// }
8071

81-
// Encode encodes the integer value stored in a Field object and returns a signal object with the encoded bytes.
82-
func (s Signal) Encode() (Signal, error) {
83-
var err error
84-
if s.Sign == Signed && s.Endian == BigEndian {
85-
s.Bytes, err = BigEndianSignedIntToBinary(s.RawValue, s.Size)
86-
} else if s.Sign == Signed && s.Endian == LittleEndian {
87-
s.Bytes, err = LittleEndianSignedIntToBinary(s.RawValue, s.Size)
88-
} else if s.Sign == Unsigned && s.Endian == BigEndian {
89-
s.Bytes, err = BigEndianUnsignedIntToBinary(s.RawValue, s.Size)
90-
} else if s.Sign == Unsigned && s.Endian == LittleEndian {
91-
s.Bytes, err = LittleEndianUnsignedIntToBinary(s.RawValue, s.Size)
92-
} else {
93-
return s, fmt.Errorf("invalid sign or endian")
94-
}
95-
return s, err
96-
}
72+
// // Encode encodes the integer value stored in a Field object and returns a signal object with the encoded bytes.
73+
// func (s Signal) Encode() (Signal, error) {
74+
// var err error
75+
// if s.Sign == Signed && s.Endian == BigEndian {
76+
// s.Bytes, err = BigEndianSignedIntToBinary(s.RawValue, s.Size)
77+
// } else if s.Sign == Signed && s.Endian == LittleEndian {
78+
// s.Bytes, err = LittleEndianSignedIntToBinary(s.RawValue, s.Size)
79+
// } else if s.Sign == Unsigned && s.Endian == BigEndian {
80+
// s.Bytes, err = BigEndianUnsignedIntToBinary(s.RawValue, s.Size)
81+
// } else if s.Sign == Unsigned && s.Endian == LittleEndian {
82+
// s.Bytes, err = LittleEndianUnsignedIntToBinary(s.RawValue, s.Size)
83+
// } else {
84+
// return s, fmt.Errorf("invalid sign or endian")
85+
// }
86+
// return s, err
87+
// }
9788

98-
// CheckBit returns a signal object with the raw value set to 1 if the bit at the given position is set, otherwise 0.
99-
// Useful if the signal is a byte that contains multiple boolean flags.
100-
func (s Signal) CheckBit(bit int) Signal {
101-
v := (s.Bytes[0] & (1 << bit)) != 0
102-
if v {
103-
s.RawValue = 1
104-
} else {
105-
s.RawValue = 0
106-
}
107-
return s
108-
}
89+
// // CheckBit returns a signal object with the raw value set to 1 if the bit at the given position is set, otherwise 0.
90+
// // Useful if the signal is a byte that contains multiple boolean flags.
91+
// func (s Signal) CheckBit(bit int) Signal {
92+
// v := (s.Bytes[0] & (1 << bit)) != 0
93+
// if v {
94+
// s.RawValue = 1
95+
// } else {
96+
// s.RawValue = 0
97+
// }
98+
// return s
99+
// }
109100

110-
// NewSignal creates a new Signal object with the provided vehicle ID, name, size, sign, endian, and scaling function.
111-
// If the scaling function is nil, the signal will not be scaled (default to just x1).
112-
func NewSignal(vehicleID string, name string, size int, sign SignMode, endian Endian, scalingFunc ScalingFunc) Signal {
113-
return Signal{
114-
VehicleID: vehicleID,
115-
Name: name,
116-
Size: size,
117-
Sign: sign,
118-
Endian: endian,
119-
ScalingFunc: scalingFunc,
120-
}
121-
}
101+
// // NewSignal creates a new Signal object with the provided vehicle ID, name, size, sign, endian, and scaling function.
102+
// // If the scaling function is nil, the signal will not be scaled (default to just x1).
103+
// func NewSignal(vehicleID string, name string, size int, sign SignMode, endian Endian, scalingFunc ScalingFunc) Signal {
104+
// return Signal{
105+
// VehicleID: vehicleID,
106+
// Name: name,
107+
// Size: size,
108+
// Sign: sign,
109+
// Endian: endian,
110+
// ScalingFunc: scalingFunc,
111+
// }
112+
// }

0 commit comments

Comments
 (0)