1
0
mirror of https://github.com/cmur2/miflorad.git synced 2025-06-26 12:30:23 +02:00

munin-miflora: refactor into two implementations using go-ble and gatt libraries, have common package

This commit is contained in:
cn
2018-12-14 09:45:13 +01:00
parent 51bb5a93c5
commit e75c04785a
11 changed files with 476 additions and 232 deletions

84
common/ble/impl.go Normal file
View File

@ -0,0 +1,84 @@
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
}