2019-06-13 19:25:32 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/internal/format"
|
2019-06-16 18:11:10 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-13 19:25:32 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
2019-06-15 11:56:41 +00:00
|
|
|
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
|
2019-07-02 20:40:00 +00:00
|
|
|
types.BME280: types.BME280,
|
2019-06-30 12:34:13 +00:00
|
|
|
types.DHT11: types.DHT11,
|
|
|
|
types.DHT22: types.DHT22,
|
2019-07-02 20:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pressureSensorModels = map[types.SensorModel]types.SensorModel{
|
2019-06-30 12:34:13 +00:00
|
|
|
types.BME280: types.BME280,
|
2019-06-15 11:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
2019-07-02 20:40:00 +00:00
|
|
|
types.BME280: types.BME280,
|
2019-06-15 11:56:41 +00:00
|
|
|
types.DHT11: types.DHT11,
|
|
|
|
types.DHT22: types.DHT22,
|
|
|
|
types.DS18B20: types.DS18B20,
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// Configuration of flucky
|
|
|
|
type Configuration struct {
|
|
|
|
Device *types.Device `json:"device"`
|
2019-06-16 11:00:50 +00:00
|
|
|
RGBLEDs []*types.RGBLED `json:"rgb_leds"`
|
2019-06-13 19:25:32 +00:00
|
|
|
Sensors []*types.Sensor `json:"sensors"`
|
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
// AddRGBLED add a new RGBLED
|
|
|
|
func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error {
|
|
|
|
|
|
|
|
// check if RGBLEDID is a valid UUID string
|
|
|
|
if !validUUID.MatchString(rgbLED.RGBLEDID) {
|
|
|
|
rgbLED.RGBLEDID = uuid.NewV4().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if sensor name and sensor uuid already exists
|
|
|
|
for _, l := range c.RGBLEDs {
|
|
|
|
if l.RGBLEDName == rgbLED.RGBLEDName {
|
|
|
|
return fmt.Errorf("RGBLED %v already exists", rgbLED.RGBLEDName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.RGBLEDID == rgbLED.RGBLEDID {
|
|
|
|
return fmt.Errorf("RGBLED %v with UUID %v already exists", rgbLED.RGBLEDName, rgbLED.RGBLEDID)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if sensor has a valid device id
|
|
|
|
if rgbLED.DeviceID != c.Device.DeviceID {
|
|
|
|
rgbLED.DeviceID = c.Device.DeviceID
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite creation date
|
|
|
|
rgbLED.CreationDate = time.Now()
|
|
|
|
|
|
|
|
// check
|
|
|
|
c.RGBLEDs = append(c.RGBLEDs, rgbLED)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// AddSensor add a new sensor
|
|
|
|
func (c *Configuration) AddSensor(sensor *types.Sensor) error {
|
|
|
|
|
|
|
|
// check if sensorID is a valid UUID string
|
|
|
|
if !validUUID.MatchString(sensor.SensorID) {
|
|
|
|
sensor.SensorID = uuid.NewV4().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if sensor name and sensor uuid already exists
|
|
|
|
for _, s := range c.Sensors {
|
|
|
|
if s.SensorName == sensor.SensorName {
|
|
|
|
return fmt.Errorf("Sensor %v already exists", s.SensorName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.SensorID == sensor.SensorID {
|
|
|
|
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
|
|
|
|
}
|
|
|
|
|
2019-06-30 12:34:13 +00:00
|
|
|
if sensor.WireID != nil {
|
|
|
|
if *s.WireID == *sensor.WireID {
|
|
|
|
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", *s.WireID, s.SensorName)
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if sensor has a valid device id
|
|
|
|
if sensor.DeviceID != c.Device.DeviceID {
|
|
|
|
sensor.DeviceID = c.Device.DeviceID
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite creation date
|
2019-08-20 19:37:45 +00:00
|
|
|
sensor.CreationDate = format.FormatedTime()
|
2019-06-13 19:25:32 +00:00
|
|
|
|
|
|
|
//TODO: check if wire sensor exists in /dev/bus/w1/devices
|
|
|
|
|
|
|
|
// check
|
|
|
|
c.Sensors = append(c.Sensors, sensor)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
// DisableRGBLED enables a rgb led by its name or its unique UUID
|
|
|
|
func (c *Configuration) DisableRGBLED(name string) error {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, rgbled := range c.RGBLEDs {
|
|
|
|
|
|
|
|
// disable sensor matched after name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
rgbled.RGBLEDName == name {
|
|
|
|
rgbled.RGBLEDEnabled = false
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable sensor matched by uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
rgbled.RGBLEDID == name {
|
|
|
|
rgbled.RGBLEDEnabled = false
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not found RGB-LED %v", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// DisableSensor disables a sensor by its name or its unique UUID
|
|
|
|
func (c *Configuration) DisableSensor(name string) error {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, sensor := range c.Sensors {
|
|
|
|
|
|
|
|
// disable sensor matched after name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorName == name {
|
|
|
|
sensor.SensorEnabled = false
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove machted uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorID == name {
|
|
|
|
sensor.SensorEnabled = false
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not found sensor %v", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
// EnableRGBLED enables a rgb led by its name or its unique UUID
|
|
|
|
func (c *Configuration) EnableRGBLED(name string) error {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, rgbled := range c.RGBLEDs {
|
|
|
|
|
|
|
|
// disable sensor matched after name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
rgbled.RGBLEDName == name {
|
|
|
|
rgbled.RGBLEDEnabled = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable sensor matched by uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
rgbled.RGBLEDID == name {
|
|
|
|
rgbled.RGBLEDEnabled = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not found RGB-LED %v", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// EnableSensor enables a sensor by its name or its unique UUID
|
|
|
|
func (c *Configuration) EnableSensor(name string) error {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, sensor := range c.Sensors {
|
|
|
|
|
|
|
|
// disable sensor matched after name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorName == name {
|
|
|
|
sensor.SensorEnabled = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove machted uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorID == name {
|
|
|
|
sensor.SensorEnabled = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not found sensor %v", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHumiditySensors returns a list of humidity sensors
|
2019-06-25 20:22:34 +00:00
|
|
|
func (c *Configuration) GetHumiditySensors(option Option) []sensor.Sensor {
|
2019-06-27 16:43:16 +00:00
|
|
|
sensors := c.getHumiditySensors()
|
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
2019-06-15 11:02:52 +00:00
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
2019-06-27 16:43:16 +00:00
|
|
|
for _, sensor := range sensors {
|
|
|
|
if sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
case DISABLED:
|
2019-06-27 16:43:16 +00:00
|
|
|
for _, sensor := range sensors {
|
|
|
|
if !sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
default:
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:40:00 +00:00
|
|
|
// GetHumiditySensorsByName returns a list of humidity sensors by name,
|
2019-06-15 11:02:52 +00:00
|
|
|
// uuid or wire-id
|
2019-06-25 20:22:34 +00:00
|
|
|
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor {
|
2019-06-15 11:02:52 +00:00
|
|
|
configHumiditySensors := make(map[string]*types.Sensor, 0)
|
|
|
|
|
|
|
|
for _, name := range names {
|
2019-06-15 11:56:41 +00:00
|
|
|
for _, s := range c.getHumiditySensors() {
|
2019-06-15 11:02:52 +00:00
|
|
|
switch name {
|
|
|
|
case s.SensorID:
|
|
|
|
configHumiditySensors[s.SensorID] = s
|
|
|
|
case s.SensorName:
|
|
|
|
configHumiditySensors[s.SensorID] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
humiditySensors := make([]*types.Sensor, 0)
|
|
|
|
for _, cs := range configHumiditySensors {
|
|
|
|
humiditySensors = append(humiditySensors, cs)
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
return c.convertSensors(humiditySensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:40:00 +00:00
|
|
|
// GetPressureSensors returns a list of pressure sensors
|
|
|
|
func (c *Configuration) GetPressureSensors(option Option) []sensor.Sensor {
|
|
|
|
sensors := c.getPressureSensors()
|
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
if sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
case DISABLED:
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
if !sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
default:
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPressureSensorsByName returns a list of pressure sensors by name,
|
|
|
|
// uuid or wire-id
|
|
|
|
func (c *Configuration) GetPressureSensorsByName(names []string) []sensor.Sensor {
|
|
|
|
configPressureSensors := make(map[string]*types.Sensor, 0)
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
for _, s := range c.getPressureSensors() {
|
|
|
|
switch name {
|
|
|
|
case s.SensorID:
|
|
|
|
configPressureSensors[s.SensorID] = s
|
|
|
|
case s.SensorName:
|
|
|
|
configPressureSensors[s.SensorID] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pressureSensors := make([]*types.Sensor, 0)
|
|
|
|
for _, cs := range configPressureSensors {
|
|
|
|
pressureSensors = append(pressureSensors, cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.convertSensors(pressureSensors)
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:11:10 +00:00
|
|
|
func (c *Configuration) GetRGBLEDs(option Option) []rgbled.RGBLED {
|
|
|
|
rgbLEDs := c.RGBLEDs
|
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
|
|
|
for i, rgbLED := range c.RGBLEDs {
|
|
|
|
if !rgbLED.RGBLEDEnabled {
|
|
|
|
rgbLEDs = append(rgbLEDs[:i], rgbLEDs[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertRGBLEDs(rgbLEDs)
|
|
|
|
case DISABLED:
|
|
|
|
for i, rgbLED := range c.RGBLEDs {
|
|
|
|
if rgbLED.RGBLEDEnabled {
|
|
|
|
rgbLEDs = append(rgbLEDs[:i], rgbLEDs[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertRGBLEDs(rgbLEDs)
|
|
|
|
default:
|
|
|
|
return c.convertRGBLEDs(rgbLEDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) GetRGBLEDsByName(names []string) []rgbled.RGBLED {
|
|
|
|
configRGBLEDs := make(map[string]*types.RGBLED, 0)
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
for _, led := range c.RGBLEDs {
|
|
|
|
switch name {
|
|
|
|
case led.RGBLEDID:
|
|
|
|
configRGBLEDs[led.RGBLEDID] = led
|
|
|
|
case led.RGBLEDName:
|
|
|
|
configRGBLEDs[led.RGBLEDID] = led
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rgbLEDs := make([]*types.RGBLED, 0)
|
|
|
|
for _, rgbLED := range configRGBLEDs {
|
|
|
|
rgbLEDs = append(rgbLEDs, rgbLED)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.convertRGBLEDs(rgbLEDs)
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:33:40 +00:00
|
|
|
// GetSensors returns a list of humidity sensors
|
|
|
|
func (c *Configuration) GetSensors(option Option) []sensor.Sensor {
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
|
|
|
for _, sensor := range c.Sensors {
|
|
|
|
if sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
case DISABLED:
|
|
|
|
for _, sensor := range c.Sensors {
|
|
|
|
if !sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
default:
|
|
|
|
return c.convertSensors(cachedSensors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
// GetTemperatureSensors returns a list of temperature sensors
|
2019-06-25 20:22:34 +00:00
|
|
|
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Sensor {
|
2019-06-27 16:43:16 +00:00
|
|
|
sensors := c.getTemperatureSensors()
|
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
2019-06-15 09:49:46 +00:00
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
2019-06-27 16:43:16 +00:00
|
|
|
for _, sensor := range sensors {
|
|
|
|
if sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
2019-06-15 09:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
case DISABLED:
|
2019-06-27 16:43:16 +00:00
|
|
|
for _, sensor := range sensors {
|
|
|
|
if !sensor.SensorEnabled {
|
|
|
|
cachedSensors = append(cachedSensors, sensor)
|
2019-06-15 09:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
default:
|
2019-06-27 16:43:16 +00:00
|
|
|
return c.convertSensors(cachedSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
|
|
|
|
// uuid or wire-id
|
2019-06-25 20:22:34 +00:00
|
|
|
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Sensor {
|
2019-06-15 11:02:52 +00:00
|
|
|
configTemperatureSensors := make(map[string]*types.Sensor, 0)
|
|
|
|
|
|
|
|
for _, name := range names {
|
2019-06-15 11:56:41 +00:00
|
|
|
for _, s := range c.getTemperatureSensors() {
|
2019-06-15 11:02:52 +00:00
|
|
|
switch name {
|
|
|
|
case s.SensorID:
|
|
|
|
configTemperatureSensors[s.SensorID] = s
|
|
|
|
case s.SensorName:
|
|
|
|
configTemperatureSensors[s.SensorID] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatureSensors := make([]*types.Sensor, 0)
|
|
|
|
for _, cs := range configTemperatureSensors {
|
|
|
|
temperatureSensors = append(temperatureSensors, cs)
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
return c.convertSensors(temperatureSensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
// RemoveRGBLED deletes a LED by its name or its unique UUID
|
|
|
|
func (c *Configuration) RemoveRGBLED(name string) error {
|
|
|
|
for i, rgbLED := range c.RGBLEDs {
|
|
|
|
// remove machted name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
rgbLED.RGBLEDName == name {
|
|
|
|
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// remove machted uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
rgbLED.RGBLEDID == name {
|
2019-06-24 20:57:29 +00:00
|
|
|
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
2019-06-16 11:00:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Can not find RGBLED %v", name)
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// RemoveSensor deletes a sensor by its name or its unique UUID
|
|
|
|
func (c *Configuration) RemoveSensor(name string) error {
|
|
|
|
for i, sensor := range c.Sensors {
|
|
|
|
// remove machted name
|
|
|
|
if !validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorName == name {
|
|
|
|
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// remove machted uuid
|
|
|
|
if validUUID.MatchString(name) &&
|
|
|
|
sensor.SensorID == name {
|
|
|
|
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Can not find sensor %v", name)
|
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
// RenameRGBLED renames a sensor identified by the name or the UUID
|
|
|
|
func (c *Configuration) RenameRGBLED(oldName, newName string) error {
|
|
|
|
for _, rgbled := range c.RGBLEDs {
|
|
|
|
if rgbled.RGBLEDName == oldName ||
|
|
|
|
rgbled.RGBLEDID == oldName {
|
|
|
|
rgbled.RGBLEDName = newName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Could not find rgb-led %v to replace into with %v", oldName, newName)
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// RenameSensor renames a sensor identified by the name or the UUID
|
|
|
|
func (c *Configuration) RenameSensor(oldName, newName string) error {
|
|
|
|
for _, sensor := range c.Sensors {
|
|
|
|
if sensor.SensorName == oldName ||
|
|
|
|
sensor.SensorID == oldName {
|
|
|
|
sensor.SensorName = newName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
func (c *Configuration) convertSensors(sensors []*types.Sensor) []sensor.Sensor {
|
|
|
|
cachedSensors := make([]sensor.Sensor, 0)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
for _, s := range sensors {
|
2019-06-13 19:25:32 +00:00
|
|
|
switch s.SensorModel {
|
2019-06-30 12:34:13 +00:00
|
|
|
case types.BME280:
|
|
|
|
cachedSensors = append(cachedSensors, &sensor.BME280{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
2019-06-13 19:25:32 +00:00
|
|
|
case types.DHT11:
|
2019-06-25 20:22:34 +00:00
|
|
|
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
2019-06-13 19:25:32 +00:00
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
case types.DHT22:
|
2019-06-25 20:22:34 +00:00
|
|
|
cachedSensors = append(cachedSensors, &sensor.DHT22{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
case types.DS18B20:
|
|
|
|
cachedSensors = append(cachedSensors, &sensor.DS18B20{
|
2019-06-13 19:25:32 +00:00
|
|
|
Sensor: s,
|
|
|
|
})
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-25 20:22:34 +00:00
|
|
|
return cachedSensors
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 18:11:10 +00:00
|
|
|
func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED {
|
|
|
|
leds := make([]rgbled.RGBLED, 0)
|
|
|
|
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
2019-06-24 20:57:29 +00:00
|
|
|
leds = append(leds, &rgbled.DefaultRGBLED{
|
2019-06-16 18:11:10 +00:00
|
|
|
RGBLED: rgbLED,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return leds
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:56:41 +00:00
|
|
|
func (c *Configuration) getHumiditySensors() []*types.Sensor {
|
|
|
|
humiditySensors := make([]*types.Sensor, 0)
|
|
|
|
for _, s := range c.Sensors {
|
|
|
|
if _, ok := humiditySensorModels[s.SensorModel]; ok {
|
|
|
|
humiditySensors = append(humiditySensors, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return humiditySensors
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:40:00 +00:00
|
|
|
func (c *Configuration) getPressureSensors() []*types.Sensor {
|
|
|
|
pressureSensors := make([]*types.Sensor, 0)
|
|
|
|
for _, s := range c.Sensors {
|
|
|
|
if _, ok := pressureSensorModels[s.SensorModel]; ok {
|
|
|
|
pressureSensors = append(pressureSensors, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pressureSensors
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:56:41 +00:00
|
|
|
func (c *Configuration) getTemperatureSensors() []*types.Sensor {
|
|
|
|
temperatureSensors := make([]*types.Sensor, 0)
|
|
|
|
for _, s := range c.Sensors {
|
|
|
|
if _, ok := temperatureSensorModels[s.SensorModel]; ok {
|
|
|
|
temperatureSensors = append(temperatureSensors, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return temperatureSensors
|
|
|
|
}
|