102 lines
3.0 KiB
Go
Raw Normal View History

2024-07-12 11:16:37 +02:00
package hottisThreeWayThermometer
import (
"log"
"fmt"
"bytes"
2024-07-31 16:14:29 +02:00
"strconv"
2024-07-12 11:16:37 +02:00
"encoding/base64"
"encoding/binary"
2024-07-31 16:10:52 +02:00
"encoding/json"
2024-07-12 11:16:37 +02:00
"udi/database"
)
type hottisThreeWayThermometerValues struct {
Status uint8
2024-07-31 15:29:54 +02:00
Battery uint16
2024-07-12 11:16:37 +02:00
SensorAddress1 uint64
Value1 int32
SensorAddress2 uint64
Value2 int32
SensorAddress3 uint64
Value3 int32
}
2024-07-31 16:14:29 +02:00
func getSensorName(sensorsMap *map[string]string, sensorAddress uint64) string {
2024-07-31 16:10:52 +02:00
key := strconv.FormatUint(sensorAddress, 10)
if sensorName, exists := (*sensorsMap)[key]; exists {
return sensorName
}
return "Sensor" + key
}
2024-07-12 11:16:37 +02:00
2024-07-31 16:22:39 +02:00
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
2024-07-31 16:10:52 +02:00
sensorsMap := make(map[string]string)
2024-07-31 16:22:39 +02:00
sensorsJSON, ok := (*device).Attributes["Sensors"].(string)
2024-07-31 16:10:52 +02:00
if !ok {
2024-07-31 16:26:51 +02:00
return fmt.Errorf("Unable to load sensor map from attributes: %s", (*device).Attributes)
2024-07-31 16:10:52 +02:00
}
errJ := json.Unmarshal([]byte(sensorsJSON), &sensorsMap)
if errJ != nil {
return fmt.Errorf("Unable to parse sensor map: %v", errJ)
}
2024-07-12 11:16:37 +02:00
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 hottisThreeWayThermometerValues
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
if err != nil {
return fmt.Errorf("Unable to cast into struct: %v", err)
}
2024-07-31 15:29:54 +02:00
var battery float32 = float32(values.Battery) / 1000.0
2024-07-12 11:16:37 +02:00
var value1 float32 = float32(values.Value1) / 128.0
var value2 float32 = float32(values.Value2) / 128.0
var value3 float32 = float32(values.Value3) / 128.0
//log.Printf("Status: %d, Battery: %d", values.Status, values.Battery);
log.Printf("Status: %d", values.Status);
2024-07-31 15:29:54 +02:00
log.Printf("Battery: %d, %f", values.Battery, battery);
2024-07-12 11:16:37 +02:00
log.Printf("Sensor1: Addr: %d, Value: %f", values.SensorAddress1, value1);
log.Printf("Sensor2: Addr: %d, Value: %f", values.SensorAddress2, value2);
log.Printf("Sensor3: Addr: %d, Value: %f", values.SensorAddress3, value3);
2024-07-31 15:29:54 +02:00
(*variables)["Battery"] = database.VariableType {
Label: "Battery",
Variable: "Voltage",
Unit: "V",
Value: battery,
}
2024-07-12 11:16:37 +02:00
(*variables)["Temperature1"] = database.VariableType {
2024-07-31 16:10:52 +02:00
Label: getSensorName(&sensorsMap, values.SensorAddress1),
2024-07-12 11:16:37 +02:00
Variable: "Temperature",
Unit: "°C",
Value: value1,
}
(*variables)["Temperature2"] = database.VariableType {
2024-07-31 16:10:52 +02:00
Label: getSensorName(&sensorsMap, values.SensorAddress2),
2024-07-12 11:16:37 +02:00
Variable: "Temperature",
Unit: "°C",
Value: value2,
}
(*variables)["Temperature3"] = database.VariableType {
2024-07-31 16:10:52 +02:00
Label: getSensorName(&sensorsMap, values.SensorAddress3),
2024-07-12 11:16:37 +02:00
Variable: "Temperature",
Unit: "°C",
Value: value3,
}
(*attributes)["Status"] = values.Status
(*attributes)["SensorAddress1"] = values.SensorAddress1
(*attributes)["SensorAddress2"] = values.SensorAddress2
(*attributes)["SensorAddress3"] = values.SensorAddress3
return nil
}