fix: breaking changes
changes: - remove remote operations - add function to write measured values into a channel - add get humidity sensors from config - add get temperature sensors from config - remove FileLogger - exclude some functions from pkf into internal
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| @@ -10,9 +11,9 @@ import ( | ||||
| var validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$") | ||||
|  | ||||
| // Read the configuration file | ||||
| func Read(configFile string) (*FluckyConfig, error) { | ||||
| func Read(configFile string) (*Configuration, error) { | ||||
|  | ||||
| 	fc := &FluckyConfig{} | ||||
| 	fc := &Configuration{} | ||||
|  | ||||
| 	f, err := os.Open(configFile) | ||||
| 	if err != nil { | ||||
| @@ -20,9 +21,9 @@ func Read(configFile string) (*FluckyConfig, error) { | ||||
| 	} | ||||
| 	defer f.Close() | ||||
|  | ||||
| 	err = fc.JSONDecoder(f) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("Can not decode json file %v: %v", configFile, err) | ||||
| 	jsonDecoder := json.NewDecoder(f) | ||||
| 	if err := jsonDecoder.Decode(&fc); err != nil { | ||||
| 		return nil, fmt.Errorf("Can not unmarshal JSON: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	return fc, nil | ||||
| @@ -30,7 +31,7 @@ func Read(configFile string) (*FluckyConfig, error) { | ||||
| } | ||||
|  | ||||
| // Write the configuration into a file, specified by the configuration filepath | ||||
| func Write(cfg *FluckyConfig, configFile string) error { | ||||
| func Write(cfg *Configuration, configFile string) error { | ||||
|  | ||||
| 	if _, err := os.Stat(configFile); os.IsNotExist(err) { | ||||
| 		configDir := filepath.Dir(configFile) | ||||
| @@ -46,9 +47,11 @@ func Write(cfg *FluckyConfig, configFile string) error { | ||||
| 	} | ||||
| 	defer f.Close() | ||||
|  | ||||
| 	err = cfg.JSONWriter(f) | ||||
| 	encoder := json.NewEncoder(f) | ||||
| 	encoder.SetIndent("", "  ") | ||||
| 	err = encoder.Encode(cfg) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 		return fmt.Errorf("Error in encoding struct to json: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
|   | ||||
							
								
								
									
										191
									
								
								pkg/config/flucky.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										191
									
								
								pkg/config/flucky.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,191 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
|  | ||||
| 	"fmt" | ||||
|  | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/volker-raschek/flucky/pkg/sensor" | ||||
|  | ||||
| 	uuid "github.com/satori/go.uuid" | ||||
| 	"github.com/volker-raschek/flucky/pkg/types" | ||||
| ) | ||||
|  | ||||
| // Configuration of flucky | ||||
| type Configuration struct { | ||||
| 	Device  *types.Device   `json:"device"` | ||||
| 	Sensors []*types.Sensor `json:"sensors"` | ||||
| } | ||||
|  | ||||
| // 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 != "" && | ||||
| 			*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 = time.Now() | ||||
|  | ||||
| 	//TODO: check if wire sensor exists in /dev/bus/w1/devices | ||||
|  | ||||
| 	// check | ||||
| 	c.Sensors = append(c.Sensors, sensor) | ||||
|  | ||||
| 	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 | ||||
| } | ||||
|  | ||||
| // 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() []sensor.HumiditySensor{ | ||||
|   humiditySensors, _ := c.splitSensors() | ||||
|   return humiditySensors | ||||
| } | ||||
|  | ||||
| // GetTemperatureSensors returns a list of humidity sensors | ||||
| func (c *Configuration) GetTemperatureSensors() []sensor.TemperatureSensor{ | ||||
|   _, temperatureSensors := c.splitSensors() | ||||
|   return temperatureSensors | ||||
| } | ||||
|  | ||||
| // 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) | ||||
| } | ||||
|  | ||||
| // 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) | ||||
| } | ||||
|  | ||||
| func (c *Configuration) splitSensors() ([]sensor.HumiditySensor, []sensor.TemperatureSensor) { | ||||
| 	humiditySensors := make([]sensor.HumiditySensor, 0) | ||||
| 	temperatureSensors := make([]sensor.TemperatureSensor, 0) | ||||
|  | ||||
| 	for _, s := range c.Sensors { | ||||
| 		switch s.SensorModel { | ||||
| 		case types.DHT11: | ||||
| 			humiditySensors = append(humiditySensors, &sensor.DHT11{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 			temperatureSensors = append(temperatureSensors, &sensor.DHT11{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case types.DHT22: | ||||
| 			humiditySensors = append(humiditySensors, &sensor.DHT22{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 			temperatureSensors = append(temperatureSensors, &sensor.DHT22{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case types.DS18B20: | ||||
| 			temperatureSensors = append(temperatureSensors, &sensor.DS18B20{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		} | ||||
| 	} | ||||
| 	return humiditySensors, temperatureSensors | ||||
| } | ||||
| @@ -1,384 +0,0 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/volker-raschek/flucky/pkg/logger" | ||||
|  | ||||
| 	"github.com/volker-raschek/flucky/pkg/sensor" | ||||
|  | ||||
| 	uuid "github.com/satori/go.uuid" | ||||
| 	"github.com/volker-raschek/flucky/pkg/types" | ||||
| ) | ||||
|  | ||||
| // FluckyConfig dasd | ||||
| type FluckyConfig struct { | ||||
| 	Device *types.Device `json:"device"` | ||||
| 	*logger.FileLogger | ||||
| 	Remotes []*Remote       `json:"remotes"` | ||||
| 	Sensors []*types.Sensor `json:"sensors"` | ||||
| } | ||||
|  | ||||
| // AddSensor add a new sensor | ||||
| func (fc *FluckyConfig) 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 fc.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 != "" && | ||||
| 			*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 != fc.Device.DeviceID { | ||||
| 		sensor.DeviceID = fc.Device.DeviceID | ||||
| 	} | ||||
|  | ||||
| 	// overwrite creation date | ||||
| 	sensor.CreationDate = time.Now() | ||||
|  | ||||
| 	//TODO: check if wire sensor exists in /dev/bus/w1/devices | ||||
|  | ||||
| 	// check | ||||
| 	fc.Sensors = append(fc.Sensors, sensor) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // AddRemote add a new remote address | ||||
| func (fc *FluckyConfig) AddRemote(remote *Remote) error { | ||||
|  | ||||
| 	// check if remoteID is a valid UUID string | ||||
| 	if !validUUID.MatchString(remote.RemoteID) { | ||||
| 		remote.RemoteID = uuid.NewV4().String() | ||||
| 	} | ||||
|  | ||||
| 	// check if remote name or remiteid already exists | ||||
| 	for _, r := range fc.Remotes { | ||||
| 		if r.Name == remote.Name { | ||||
| 			return fmt.Errorf("Remote %v -> %v already exists", r.Name, r.Address) | ||||
| 		} | ||||
|  | ||||
| 		if r.RemoteID == remote.RemoteID { | ||||
| 			return fmt.Errorf("Remote %v with UUID %v already exists", r.Name, r.RemoteID) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	fc.Remotes = append(fc.Remotes, remote) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // DisableRemote disables a remote address by its name or its unique UUID | ||||
| func (fc *FluckyConfig) DisableRemote(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	for _, remote := range fc.Remotes { | ||||
|  | ||||
| 		// disable sensor matched after name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.Name == nameOrUUID { | ||||
| 			remote.Enabled = false | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.RemoteID == nameOrUUID { | ||||
| 			remote.Enabled = false | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not found remote name %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // DisableSensor disables a sensor by its name or its unique UUID | ||||
| func (fc *FluckyConfig) DisableSensor(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	for _, sensor := range fc.Sensors { | ||||
|  | ||||
| 		// disable sensor matched after name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorName == nameOrUUID { | ||||
| 			sensor.SensorEnabled = false | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorID == nameOrUUID { | ||||
| 			sensor.SensorEnabled = false | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not found sensor %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // EnableRemote enable a remote address by its name or its unique UUID | ||||
| func (fc *FluckyConfig) EnableRemote(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	for _, remote := range fc.Remotes { | ||||
|  | ||||
| 		// disable sensor matched after name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.Name == nameOrUUID { | ||||
| 			remote.Enabled = true | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.RemoteID == nameOrUUID { | ||||
| 			remote.Enabled = true | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not found sensor %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // EnableSensor enables a sensor by its name or its unique UUID | ||||
| func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	for _, sensor := range fc.Sensors { | ||||
|  | ||||
| 		// disable sensor matched after name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorName == nameOrUUID { | ||||
| 			sensor.SensorEnabled = true | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorID == nameOrUUID { | ||||
| 			sensor.SensorEnabled = true | ||||
| 			found = true | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not found sensor %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (fc *FluckyConfig) GetHumiditySensors(namesOrUUIDs []string) []sensor.HumiditySensor { | ||||
| 	hs := []sensor.HumiditySensor{} | ||||
| 	for _, s := range fc.Sensors { | ||||
| 		// select only named sensors | ||||
| 		if len(namesOrUUIDs) > 0 { | ||||
| 			found := false | ||||
| 			for _, nameOrUUID := range namesOrUUIDs { | ||||
| 				if nameOrUUID == s.SensorID || nameOrUUID == s.SensorName { | ||||
| 					found = true | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 			if !found { | ||||
| 				continue | ||||
| 			} | ||||
| 		} | ||||
| 		// skip disabled sensors | ||||
| 		if !s.SensorEnabled { | ||||
| 			continue | ||||
| 		} | ||||
| 		switch s.SensorModel { | ||||
| 		case types.DHT11: | ||||
| 			hs = append(hs, &sensor.DHT11Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case types.DHT22: | ||||
| 			hs = append(hs, &sensor.DHT22Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		} | ||||
| 	} | ||||
| 	return hs | ||||
| } | ||||
|  | ||||
| func (fc *FluckyConfig) GetTemperatureSensors(namesOrUUIDs []string) ([]sensor.TemperatureSensor, error) { | ||||
| 	ts := []sensor.TemperatureSensor{} | ||||
| 	for _, s := range fc.Sensors { | ||||
| 		// select only named sensors | ||||
| 		if len(namesOrUUIDs) > 0 { | ||||
| 			found := false | ||||
| 			for _, nameOrUUID := range namesOrUUIDs { | ||||
| 				if nameOrUUID == s.SensorID || nameOrUUID == s.SensorName { | ||||
| 					found = true | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 			if !found { | ||||
| 				continue | ||||
| 			} | ||||
| 		} | ||||
| 		// skip disabled sensors | ||||
| 		if !s.SensorEnabled { | ||||
| 			continue | ||||
| 		} | ||||
| 		switch s.SensorModel { | ||||
| 		case types.DHT11: | ||||
| 			ts = append(ts, &sensor.DHT11Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case types.DHT22: | ||||
| 			ts = append(ts, &sensor.DHT22Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case types.DS18B20: | ||||
| 			ts = append(ts, &sensor.DS18B20{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		default: | ||||
| 			return nil, fmt.Errorf("Sensor Model %v is not a valid sensor model. Please remove the sensor named %v", s.SensorModel, s.Name()) | ||||
| 		} | ||||
| 	} | ||||
| 	return ts, nil | ||||
| } | ||||
|  | ||||
| // JSONDecoder decode a JSON string from a reader into a struct | ||||
| func (fc *FluckyConfig) JSONDecoder(r io.Reader) error { | ||||
| 	jsonDecoder := json.NewDecoder(r) | ||||
| 	if err := jsonDecoder.Decode(&fc); err != nil { | ||||
| 		return fmt.Errorf("Can not unmarshal JSON: %v", err) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // JSONWriter needs a writer to write the struct into JSON string | ||||
| func (fc *FluckyConfig) JSONWriter(w io.Writer) error { | ||||
| 	encoder := json.NewEncoder(w) | ||||
| 	encoder.SetIndent("", "  ") | ||||
| 	err := encoder.Encode(&fc) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("Error in encoding struct to json: %v", err) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // RemoveSensor deletes a sensor by its name or its unique UUID | ||||
| func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error { | ||||
| 	for i, sensor := range fc.Sensors { | ||||
| 		// remove machted name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorName == nameOrUUID { | ||||
| 			fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...) | ||||
| 			return nil | ||||
| 		} | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorID == nameOrUUID { | ||||
| 			fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...) | ||||
| 			return nil | ||||
| 		} | ||||
| 	} | ||||
| 	return fmt.Errorf("Can not find sensor %v", nameOrUUID) | ||||
| } | ||||
|  | ||||
| // RemoveRemote deletes a remote address by its name or its unique UUID | ||||
| func (fc *FluckyConfig) RemoveRemote(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	for i, remote := range fc.Remotes { | ||||
|  | ||||
| 		// remove machted name | ||||
| 		if !validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.Name == nameOrUUID { | ||||
| 			fc.Remotes = append(fc.Remotes[:i], fc.Remotes[i+1:]...) | ||||
| 			found = true | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			remote.RemoteID == nameOrUUID { | ||||
| 			fc.Remotes = append(fc.Remotes[:i], fc.Remotes[i+1:]...) | ||||
| 			found = true | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not find remote %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // RenameSensor renames a sensor identified by the name or the UUID | ||||
| func (fc *FluckyConfig) RenameSensor(oldName, newName string) error { | ||||
| 	for _, sensor := range fc.Sensors { | ||||
| 		if sensor.SensorName == oldName || | ||||
| 			sensor.SensorID == oldName { | ||||
| 			sensor.SensorName = newName | ||||
| 			return nil | ||||
| 		} | ||||
| 	} | ||||
| 	return fmt.Errorf("Could not find sensor %v to replace into ", oldName) | ||||
| } | ||||
|  | ||||
| // RenameRemote renames a remote address identified by the name or the UUID | ||||
| func (fc *FluckyConfig) RenameRemote(oldName, newName string) error { | ||||
| 	for _, remote := range fc.Remotes { | ||||
| 		if remote.Name == oldName || | ||||
| 			remote.RemoteID == oldName { | ||||
| 			remote.Name = newName | ||||
| 			return nil | ||||
| 		} | ||||
| 	} | ||||
| 	return fmt.Errorf("Could not find remote name %v to replace into %v", oldName, newName) | ||||
| } | ||||
|  | ||||
| // ToJSON returns the struct as JSON string | ||||
| func (fc *FluckyConfig) ToJSON() (string, error) { | ||||
| 	var b bytes.Buffer | ||||
| 	err := fc.JSONWriter(&b) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
|  | ||||
| 	return b.String(), nil | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| package config | ||||
|  | ||||
| import "github.com/volker-raschek/flucky/pkg/types" | ||||
|  | ||||
| // Remote ... | ||||
| type Remote struct { | ||||
| 	RemoteID   string `json:"remote_id"` | ||||
| 	Name       string `json:"remote_name"` | ||||
| 	Address    string `json:"remote_address"` | ||||
| 	Registered bool   `json:"remote_registered"` | ||||
| 	Enabled    bool   `json:"remote_enabled"` | ||||
| } | ||||
|  | ||||
| func (r *Remote) AddSensor(s *types.Sensor) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *Remote) RegisterDevice(d *types.Device) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *Remote) RemoveSensor(s *types.Sensor) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *Remote) UnregisterDevice(d *types.Device) error { | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user