2018-12-14 09:45:13 +01:00
|
|
|
package ble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"miflorad/common"
|
|
|
|
|
|
|
|
"github.com/go-ble/ble"
|
2018-12-14 16:22:27 +01:00
|
|
|
"github.com/pkg/errors"
|
2018-12-14 09:45:13 +01:00
|
|
|
)
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func FindServiceByUUID(services []*ble.Service, uuid string) *ble.Service {
|
|
|
|
u := ble.MustParse(uuid)
|
2018-12-14 09:45:13 +01:00
|
|
|
for _, service := range services {
|
|
|
|
if service.UUID.Equal(u) {
|
|
|
|
return service
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func FindCharacteristicByUUID(characteristics []*ble.Characteristic, uuid string) *ble.Characteristic {
|
|
|
|
u := ble.MustParse(uuid)
|
2018-12-14 09:45:13 +01:00
|
|
|
for _, characteristic := range characteristics {
|
|
|
|
if characteristic.UUID.Equal(u) {
|
|
|
|
return characteristic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func RequestVersionBattery(client ble.Client) (common.VersionBatteryResponse, error) {
|
|
|
|
mifloraService := FindServiceByUUID(client.Profile().Services, common.MifloraServiceUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraService == nil {
|
|
|
|
return common.VersionBatteryResponse{}, errors.New("Failed to get the miflora service")
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
mifloraVersionBatteryChar := FindCharacteristicByUUID(mifloraService.Characteristics, common.MifloraCharVersionBatteryUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraVersionBatteryChar == nil {
|
|
|
|
return common.VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic")
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := client.ReadCharacteristic(mifloraVersionBatteryChar)
|
|
|
|
if err != nil {
|
2018-12-14 16:22:27 +01:00
|
|
|
return common.VersionBatteryResponse{}, errors.Wrap(err, "can't read version battery")
|
2018-12-14 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return common.ParseVersionBattery(bytes), nil
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func RequestModeChange(client ble.Client) error {
|
|
|
|
mifloraService := FindServiceByUUID(client.Profile().Services, common.MifloraServiceUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraService == nil {
|
|
|
|
return errors.New("Failed to get the miflora service")
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
mifloraModeChangeChar := FindCharacteristicByUUID(mifloraService.Characteristics, common.MifloraCharModeChangeUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraModeChangeChar == nil {
|
|
|
|
return errors.New("Failed to discover the mode change characteristic")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.WriteCharacteristic(mifloraModeChangeChar, common.MifloraGetModeChangeData(), false)
|
|
|
|
if err != nil {
|
2018-12-14 16:22:27 +01:00
|
|
|
return errors.Wrap(err, "can't change mode")
|
2018-12-14 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func RequestSensorData(client ble.Client) (common.SensorDataResponse, error) {
|
|
|
|
mifloraService := FindServiceByUUID(client.Profile().Services, common.MifloraServiceUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraService == nil {
|
|
|
|
return common.SensorDataResponse{}, errors.New("Failed to get the miflora service")
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
mifloraSensorDataChar := FindCharacteristicByUUID(mifloraService.Characteristics, common.MifloraCharReadSensorDataUUID)
|
2018-12-14 09:45:13 +01:00
|
|
|
if mifloraSensorDataChar == nil {
|
|
|
|
return common.SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic")
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := client.ReadCharacteristic(mifloraSensorDataChar)
|
|
|
|
if err != nil {
|
2018-12-14 16:22:27 +01:00
|
|
|
return common.SensorDataResponse{}, errors.Wrap(err, "can't read sensor data")
|
2018-12-14 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return common.ParseSensorData(bytes), nil
|
|
|
|
}
|