-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.go
146 lines (120 loc) · 3.29 KB
/
values.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package bthome
import (
"encoding/binary"
"strconv"
)
// GetDataValue returns a new DataValuer for the given data type and data.
func GetDataValue(typ DataType, data []byte) DataValuer {
switch typ.TypeID() {
case TypeFloat32:
return Float32Value{DataValue{DataType: typ, Value: data}}
case TypeInt8:
return Int8Value{DataValue{DataType: typ, Value: data}}
case TypeInt16:
return Int16Value{DataValue{DataType: typ, Value: data}}
case TypeBool:
return BoolValue{DataValue{DataType: typ, Value: data}}
}
return nil
}
// NewDataValue returns a new DataValuer for the given data type.
func NewDataValue(typ DataType) DataValuer {
switch typ.TypeID() {
case TypeFloat32:
return Float32Value{DataValue{DataType: typ, Value: make([]byte, typ.Size())}}
case TypeInt8:
return Int8Value{DataValue{DataType: typ, Value: make([]byte, typ.Size())}}
case TypeInt16:
return Int16Value{DataValue{DataType: typ, Value: make([]byte, typ.Size())}}
case TypeBool:
return BoolValue{DataValue{DataType: typ, Value: make([]byte, typ.Size())}}
}
return nil
}
// DataValuer is an interface for data values in the service data payload.
type DataValuer interface {
Type() DataType
Data() []byte
Get() any
Set(any)
String() string
}
// DataValue represents an individual data value in the service data payload.
type DataValue struct {
DataType DataType
Value []byte
}
// Type returns the data type of the data value.
func (d DataValue) Type() DataType {
return d.DataType
}
// Data returns the raw data of the data value.
func (d DataValue) Data() []byte {
return d.Value
}
// Float32Value represents a float32 data value in the service data payload.
type Float32Value struct {
DataValue
}
// Get returns the float32 value of the data value.
func (d Float32Value) Get() any {
return float32(float32(binary.LittleEndian.Uint16(d.Value)) * d.DataType.Factor())
}
// Set sets the float32 value of the data value.
func (d Float32Value) Set(v any) {
var val float32
switch v := v.(type) {
case float32:
val = v / float32(d.DataType.Factor())
case float64:
val = float32(v) / float32(d.DataType.Factor())
}
binary.LittleEndian.PutUint16(d.Value, uint16(val))
}
func (d Float32Value) String() string {
return d.Type().Name() + ": " + strconv.FormatFloat(float64(d.Get().(float32)), 'f', -1, 32)
}
type Int8Value struct {
DataValue
}
func (d Int8Value) Get() any {
return int(d.Value[0]) / int(d.DataType.Factor())
}
func (d Int8Value) Set(v any) {
val := v.(int) * int(d.DataType.Factor())
d.Value[0] = byte(val)
}
func (d Int8Value) String() string {
return d.Type().Name() + ": " + strconv.Itoa(d.Get().(int))
}
type Int16Value struct {
DataValue
}
func (d Int16Value) Get() any {
val := binary.LittleEndian.Uint16(d.Value)
return int(float32(val) / d.DataType.Factor())
}
func (d Int16Value) Set(v any) {
val := v.(int) * int(d.DataType.Factor())
binary.LittleEndian.PutUint16(d.Value, uint16(val))
}
func (d Int16Value) String() string {
return d.Type().Name() + ": " + strconv.Itoa(d.Get().(int))
}
type BoolValue struct {
DataValue
}
func (d BoolValue) Get() any {
return d.Value[0] != 0
}
func (d BoolValue) Set(v any) {
val := v.(bool)
if val {
d.Value[0] = 1
} else {
d.Value[0] = 0
}
}
func (d BoolValue) String() string {
return d.Type().Name() + ": " + strconv.FormatBool(d.Get().(bool))
}