fix: sensor typ to static sensor model
This commit is contained in:
		| @@ -207,41 +207,48 @@ func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error { | ||||
|  | ||||
| func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor { | ||||
| 	hs := []sensor.HumiditySensor{} | ||||
| 	// for _, s := range fc.Sensors { | ||||
| 	// 	switch s.SensorType { | ||||
| 	// 	case "DHT11": | ||||
| 	// 		hs = append(hs, sensor.DHT11Sensor{ | ||||
| 	// 			Sensor: s, | ||||
| 	// 		}) | ||||
| 	// 	case "DHT22": | ||||
| 	// 		hs = append(hs, sensor.DHT22Sensor{ | ||||
| 	// 			Sensor: s, | ||||
| 	// 		}) | ||||
| 	// 	} | ||||
| 	// } | ||||
| 	return hs | ||||
| } | ||||
|  | ||||
| func (fc *FluckyConfig) GetTemperatureSensors() []sensor.TemperatureSensor { | ||||
| 	ts := []sensor.TemperatureSensor{} | ||||
|  | ||||
| 	for _, s := range fc.Sensors { | ||||
| 		switch s.SensorType { | ||||
| 		case "DHT11": | ||||
| 			ts = append(ts, &sensor.DHT11Sensor{ | ||||
| 		switch s.SensorModel { | ||||
| 		case types.DHT11: | ||||
| 			hs = append(hs, &sensor.DHT11Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case "DHT22": | ||||
| 			ts = append(ts, &sensor.DHT22Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		case "DS18B20": | ||||
| 			ts = append(ts, &sensor.DS18B20{ | ||||
| 		case types.DHT22: | ||||
| 			hs = append(hs, &sensor.DHT22Sensor{ | ||||
| 				Sensor: s, | ||||
| 			}) | ||||
| 		} | ||||
| 	} | ||||
| 	return ts | ||||
| 	return hs | ||||
| } | ||||
|  | ||||
| func (fc *FluckyConfig) GetTemperatureSensors() ([]sensor.TemperatureSensor, error) { | ||||
| 	ts := []sensor.TemperatureSensor{} | ||||
|  | ||||
| 	for _, s := range fc.Sensors { | ||||
| 		// 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 | ||||
| @@ -289,7 +296,7 @@ func (fc *FluckyConfig) PrintSensors(w io.Writer) error { | ||||
| 	fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n") | ||||
|  | ||||
| 	for _, sensor := range fc.Sensors { | ||||
| 		fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorType, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled) | ||||
| 		fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled) | ||||
| 	} | ||||
|  | ||||
| 	tw.Flush() | ||||
| @@ -299,30 +306,21 @@ func (fc *FluckyConfig) PrintSensors(w io.Writer) error { | ||||
|  | ||||
| // RemoveSensor deletes a sensor by its name or its unique UUID | ||||
| func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error { | ||||
| 	found := false | ||||
|  | ||||
| 	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:]...) | ||||
| 			found = true | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// remove machted uuid | ||||
| 		if validUUID.MatchString(nameOrUUID) && | ||||
| 			sensor.SensorID == nameOrUUID { | ||||
| 			fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...) | ||||
| 			found = true | ||||
| 			return nil | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !found { | ||||
| 		return fmt.Errorf("Can not find sensor %v", nameOrUUID) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return fmt.Errorf("Can not find sensor %v", nameOrUUID) | ||||
| } | ||||
|  | ||||
| // RemoveRemote deletes a remote address by its name or its unique UUID | ||||
|   | ||||
		Reference in New Issue
	
	Block a user