2018-12-14 09:45:13 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
// captures response when reading meta data of miflora device
|
2018-12-14 09:45:13 +01:00
|
|
|
type VersionBatteryResponse struct {
|
|
|
|
BatteryLevel uint8 // in percent 0-100
|
2018-12-14 13:34:16 +01:00
|
|
|
FirmwareVersion string // as "x.y.z"
|
2018-12-14 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
// captures response when reading sensor data of miflora device
|
2018-12-14 09:45:13 +01:00
|
|
|
type SensorDataResponse struct {
|
|
|
|
Temperature float64 // in degree C
|
|
|
|
Brightness uint32 // in lux
|
|
|
|
Moisture uint8 // in percent 0-100
|
|
|
|
Conductivity uint16 // in µS/cm
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
// turns firmware version "2.3.4" into 20304
|
2018-12-14 09:45:13 +01:00
|
|
|
func (res VersionBatteryResponse) NumericFirmwareVersion() int {
|
|
|
|
version := 0
|
|
|
|
parts := strings.Split(res.FirmwareVersion, ".")
|
|
|
|
for i, part := range parts {
|
|
|
|
partNumber, err := strconv.Atoi(part)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
version += int(math.Pow10((len(parts)-(i+1))*2)) * partNumber
|
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|
2018-12-20 13:46:39 +01:00
|
|
|
|
|
|
|
// for the newer models a magic number must be written before we can read the current data
|
|
|
|
func (res VersionBatteryResponse) RequiresModeChangeBeforeRead() bool {
|
|
|
|
return res.FirmwareVersion >= "2.6.6"
|
|
|
|
}
|