595 lines
14 KiB
Go
595 lines
14 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"github.com/go-flucky/flucky/pkg/internal/format"
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
uuid "github.com/satori/go.uuid"
|
|
)
|
|
|
|
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
|
|
types.BME280: types.BME280,
|
|
types.DHT11: types.DHT11,
|
|
types.DHT22: types.DHT22,
|
|
}
|
|
|
|
var pressureSensorModels = map[types.SensorModel]types.SensorModel{
|
|
types.BME280: types.BME280,
|
|
}
|
|
|
|
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
|
types.BME280: types.BME280,
|
|
types.DHT11: types.DHT11,
|
|
types.DHT22: types.DHT22,
|
|
types.DS18B20: types.DS18B20,
|
|
}
|
|
|
|
// Configuration of flucky
|
|
type Configuration struct {
|
|
DatabaseSettings *DatabaseSettings `json:"database_settings"`
|
|
Device *types.Device `json:"device"`
|
|
Logfile string `json:"logfile" xml:"logfile"`
|
|
RGBLEDs []*types.RGBLED `json:"rgb_leds"`
|
|
Sensors []*types.Sensor `json:"sensors"`
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// check if sensor has a valid device id
|
|
if sensor.DeviceID != c.Device.DeviceID {
|
|
sensor.DeviceID = c.Device.DeviceID
|
|
}
|
|
|
|
// overwrite creation date
|
|
sensor.CreationDate = format.FormatedTime()
|
|
|
|
//TODO: check if wire sensor exists in /dev/bus/w1/devices
|
|
|
|
// check
|
|
c.Sensors = append(c.Sensors, sensor)
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
func (c *Configuration) GetHumiditySensors(option Option) []sensor.Sensor {
|
|
sensors := c.getHumiditySensors()
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// GetHumiditySensorsByName returns a list of humidity sensors by name,
|
|
// uuid or wire-id
|
|
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor {
|
|
configHumiditySensors := make(map[string]*types.Sensor, 0)
|
|
|
|
for _, name := range names {
|
|
for _, s := range c.getHumiditySensors() {
|
|
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)
|
|
}
|
|
|
|
return c.convertSensors(humiditySensors)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
// GetTemperatureSensors returns a list of temperature sensors
|
|
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Sensor {
|
|
sensors := c.getTemperatureSensors()
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
|
|
// uuid or wire-id
|
|
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Sensor {
|
|
configTemperatureSensors := make(map[string]*types.Sensor, 0)
|
|
|
|
for _, name := range names {
|
|
for _, s := range c.getTemperatureSensors() {
|
|
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)
|
|
}
|
|
|
|
return c.convertSensors(temperatureSensors)
|
|
}
|
|
|
|
// RemoveDatabaseSettings remove data base setting informations
|
|
func (c *Configuration) RemoveDatabaseSettings() {
|
|
c.DatabaseSettings = nil
|
|
}
|
|
|
|
// 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 {
|
|
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("Can not find RGBLED %v", name)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// SetDatabaseSettings set database setting informations
|
|
func (c *Configuration) SetDatabaseSettings(databaseSettings *DatabaseSettings) {
|
|
c.DatabaseSettings = databaseSettings
|
|
}
|
|
|
|
func (c *Configuration) convertSensors(sensors []*types.Sensor) []sensor.Sensor {
|
|
cachedSensors := make([]sensor.Sensor, 0)
|
|
|
|
for _, s := range sensors {
|
|
switch s.SensorModel {
|
|
case types.BME280:
|
|
cachedSensors = append(cachedSensors, &sensor.BME280{
|
|
Sensor: s,
|
|
})
|
|
case types.DHT11:
|
|
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
|
Sensor: s,
|
|
})
|
|
case types.DHT22:
|
|
cachedSensors = append(cachedSensors, &sensor.DHT22{
|
|
Sensor: s,
|
|
})
|
|
case types.DS18B20:
|
|
cachedSensors = append(cachedSensors, &sensor.DS18B20{
|
|
Sensor: s,
|
|
})
|
|
}
|
|
}
|
|
return cachedSensors
|
|
}
|
|
|
|
func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED {
|
|
leds := make([]rgbled.RGBLED, 0)
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
leds = append(leds, &rgbled.DefaultRGBLED{
|
|
RGBLED: rgbLED,
|
|
})
|
|
}
|
|
|
|
return leds
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|