56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package hottisGy21
|
|
|
|
import (
|
|
//"log"
|
|
"fmt"
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"udi/database"
|
|
)
|
|
|
|
type hottisGy21Values struct {
|
|
Connected uint8
|
|
Status uint8
|
|
RawHumidity uint16
|
|
RawTemperature uint16
|
|
}
|
|
|
|
|
|
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error {
|
|
if fPort != 2 {
|
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
}
|
|
|
|
b, err := base64.StdEncoding.DecodeString(frmPayload)
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to base64-decode payload: %v", err)
|
|
}
|
|
|
|
var values hottisGy21Values
|
|
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to cast into struct: %v", err)
|
|
}
|
|
var temperature float32 = -46.85 + 175.72 * (float32(values.RawTemperature) / 65536.0)
|
|
var humidity float32 = -6 + 125 * (float32(values.RawHumidity) / 65536.0);
|
|
|
|
// log.Printf("CO2: %f, Temp: %f, Hum: %f, Status: %d", co2concentration, temperature, humidity, values.Status)
|
|
|
|
(*variables)["Humidity"] = database.VariableType {
|
|
Label: "Humidity",
|
|
Variable: "Humidity",
|
|
Unit: "%",
|
|
Value: humidity,
|
|
}
|
|
(*variables)["Temperature"] = database.VariableType {
|
|
Label: "Temperature",
|
|
Variable: "Temperature",
|
|
Unit: "°C",
|
|
Value: temperature,
|
|
}
|
|
|
|
(*attributes)["Status"] = values.Status
|
|
return nil
|
|
}
|