|
1 | 1 | package mapache |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "time" |
6 | 5 | ) |
7 | 6 |
|
@@ -38,84 +37,76 @@ type Signal struct { |
38 | 37 | ProducedAt time.Time `json:"produced_at"` |
39 | 38 | // CreatedAt is the time at which the signal was actually stored in the database. |
40 | 39 | 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:"-"` |
49 | 40 | } |
50 | 41 |
|
51 | 42 | func (Signal) TableName() string { |
52 | 43 | return "signal" |
53 | 44 | } |
54 | 45 |
|
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 |
57 | 48 |
|
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 | +// } |
66 | 57 |
|
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 | +// } |
80 | 71 |
|
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 | +// } |
97 | 88 |
|
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 | +// } |
109 | 100 |
|
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