1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-10-18 05:14:19 +00:00

Add support for RawValue and array of float32

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-12-11 18:24:12 +01:00
parent 850214a205
commit 3e97c1b32a
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 78 additions and 0 deletions

View file

@ -46,6 +46,14 @@ func genericMarshal(in interface{}) ([]byte, error) {
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
continue
case reflect.TypeOf(RawValue{}):
v, err := valField.Interface().(RawValue).MarshalSII()
if err != nil {
return nil, errors.Wrap(err, "Unable to encode RawValue")
}
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
continue
}
switch typeField.Type.Kind() {
@ -71,6 +79,36 @@ func genericMarshal(in interface{}) ([]byte, error) {
v := strconv.FormatUint(valField.Uint(), 10)
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
case reflect.Array:
switch typeField.Type.Elem().Kind() {
case reflect.Float32:
var vals [][]byte
switch typeField.Type.Len() {
case 3:
for _, v := range valField.Interface().([3]float32) {
bv, err := float2sii(v)
if err != nil {
return nil, errors.Wrap(err, "Unable to encode float32")
}
vals = append(vals, bv)
}
default:
return nil, errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
}
buf.WriteString(fmt.Sprintf(" %s: (%s)\n", attributeName, bytes.Join(vals, []byte(", "))))
default:
return nil, errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
}
case reflect.Ptr:
switch typeField.Type.Elem().Kind() {

View file

@ -57,6 +57,15 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
valField.Set(reflect.ValueOf(v))
continue
case reflect.TypeOf(RawValue{}):
data := getSingleValue(in, attributeName)
v := RawValue{}
if err := v.UnmarshalSII(data); err != nil {
return errors.Wrapf(err, "Unable to parse RawValue for attribute %q", attributeName)
}
valField.Set(reflect.ValueOf(v))
continue
}
switch typeField.Type.Kind() {
@ -96,6 +105,37 @@ func genericUnmarshal(in []byte, out interface{}, unit *Unit) error {
}
valField.SetUint(v)
case reflect.Array:
switch typeField.Type.Elem().Kind() {
case reflect.Float32:
switch typeField.Type.Len() {
case 3:
grps := regexp.MustCompile(`^\(([0-9.]+|&[0-9a-f]+), ([0-9.]+|&[0-9a-f]+), ([0-9.]+|&[0-9a-f]+)\)$`).
FindSubmatch(getSingleValue(in, attributeName))
var v [3]float32
for i := range v {
val, err := sii2float(grps[i+1][:])
if err != nil {
return errors.Wrapf(err, "Unable to parse float32 for attribute %q", attributeName)
}
v[i] = val
}
valField.Set(reflect.ValueOf(v))
default:
return errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
}
default:
return errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
}
case reflect.Ptr:
switch typeField.Type.Elem().Kind() {