1
0
mirror of https://github.com/cmur2/miflorad.git synced 2024-11-15 04:56:16 +01:00
miflorad/common/ble/impl.go

85 lines
2.7 KiB
Go
Raw Normal View History

package ble
import (
"errors"
"miflorad/common"
"github.com/go-ble/ble"
)
func FindServiceByUUID(services []*ble.Service, u ble.UUID) *ble.Service {
for _, service := range services {
if service.UUID.Equal(u) {
return service
}
}
return nil
}
func FindCharacteristicByUUID(characteristics []*ble.Characteristic, u ble.UUID) *ble.Characteristic {
for _, characteristic := range characteristics {
if characteristic.UUID.Equal(u) {
return characteristic
}
}
return nil
}
func RequestVersionBattery(client ble.Client, profile *ble.Profile) (common.VersionBatteryResponse, error) {
mifloraService := FindServiceByUUID(profile.Services, ble.MustParse(common.MifloraServiceUUID))
if mifloraService == nil {
return common.VersionBatteryResponse{}, errors.New("Failed to get the miflora service")
}
mifloraVersionBatteryChar := FindCharacteristicByUUID(mifloraService.Characteristics, ble.MustParse(common.MifloraCharVersionBatteryUUID))
if mifloraVersionBatteryChar == nil {
return common.VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic")
}
bytes, err := client.ReadCharacteristic(mifloraVersionBatteryChar)
if err != nil {
return common.VersionBatteryResponse{}, err
}
return common.ParseVersionBattery(bytes), nil
}
func RequestModeChange(client ble.Client, profile *ble.Profile) error {
mifloraService := FindServiceByUUID(profile.Services, ble.MustParse(common.MifloraServiceUUID))
if mifloraService == nil {
return errors.New("Failed to get the miflora service")
}
mifloraModeChangeChar := FindCharacteristicByUUID(mifloraService.Characteristics, ble.MustParse(common.MifloraCharModeChangeUUID))
if mifloraModeChangeChar == nil {
return errors.New("Failed to discover the mode change characteristic")
}
err := client.WriteCharacteristic(mifloraModeChangeChar, common.MifloraGetModeChangeData(), false)
if err != nil {
return err
}
return nil
}
func RequestSensorData(client ble.Client, profile *ble.Profile) (common.SensorDataResponse, error) {
mifloraService := FindServiceByUUID(profile.Services, ble.MustParse(common.MifloraServiceUUID))
if mifloraService == nil {
return common.SensorDataResponse{}, errors.New("Failed to get the miflora service")
}
mifloraSensorDataChar := FindCharacteristicByUUID(mifloraService.Characteristics, ble.MustParse(common.MifloraCharReadSensorDataUUID))
if mifloraSensorDataChar == nil {
return common.SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic")
}
bytes, err := client.ReadCharacteristic(mifloraSensorDataChar)
if err != nil {
return common.SensorDataResponse{}, err
}
return common.ParseSensorData(bytes), nil
}