2019-03-04 16:50:41 +00:00
|
|
|
package temperature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-05-12 09:57:53 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2019-03-04 16:50:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSensorsByTemperatures returns commulated list of sensors by temperature values
|
2019-06-13 19:25:32 +00:00
|
|
|
func GetSensorsByTemperatures(temperatures []*types.Temperature, cnf *config.Configuration) []*types.Sensor {
|
2019-03-04 16:50:41 +00:00
|
|
|
sensors := []*types.Sensor{}
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
duplicated := false
|
|
|
|
foundSensor := &types.Sensor{}
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
|
|
|
if temperature.SensorID == cnfSensor.SensorID {
|
|
|
|
foundSensor = cnfSensor
|
|
|
|
|
|
|
|
// compare if id has already been added to list
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
if cnfSensor.SensorID == sensor.SensorID {
|
|
|
|
duplicated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if duplicated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if foundSensor != nil {
|
|
|
|
sensors = append(sensors, foundSensor)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
sensors = append(sensors, &types.Sensor{SensorID: temperature.SensorID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sensors
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperaturesBetweenTimeRange returns a list of temperatures between a given time range
|
|
|
|
func GetTemperaturesBetweenTimeRange(from time.Time, till *time.Time, temperatures []*types.Temperature) ([]*types.Temperature, error) {
|
|
|
|
|
|
|
|
if till != nil && from.After(*till) {
|
|
|
|
fFormat := from.Format("2006-01-02 15:04:05")
|
|
|
|
tFormat := till.Format("2006-01-02 15:04:05")
|
|
|
|
return nil, fmt.Errorf("%v is after %v", fFormat, tFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedTemperatures := []*types.Temperature{}
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
|
2019-06-13 20:22:12 +00:00
|
|
|
if temperature.TemperatureFromDate.After(from) && till == nil {
|
2019-03-04 16:50:41 +00:00
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-13 20:22:12 +00:00
|
|
|
if temperature.TemperatureFromDate.After(from) && temperature.TemperatureTillDate.Before(*till) {
|
2019-03-04 16:50:41 +00:00
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedTemperatures, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperaturesBySensors returns a list of temperatures given by the sensor id.
|
|
|
|
// If the sensor name, wire-id or id can not found in configured sensors, it would be skiped!
|
2019-06-13 19:25:32 +00:00
|
|
|
func GetTemperaturesBySensors(sensorNamesOrIDs []string, temperatures []*types.Temperature, cnf *config.Configuration) []*types.Temperature {
|
2019-03-04 16:50:41 +00:00
|
|
|
cachedSensors := []*types.Sensor{}
|
|
|
|
cachedTemperatures := []*types.Temperature{}
|
|
|
|
|
|
|
|
// match sensor name, wire-id or sensor id with configured sensors
|
|
|
|
// and append the matched sensor to the list
|
|
|
|
for _, sensor := range sensorNamesOrIDs {
|
|
|
|
for _, cs := range cnf.Sensors {
|
|
|
|
if cs.SensorID == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
if cs.WireID != nil && *cs.WireID == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
if cs.SensorName == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// march all temperatures which matched with the sensor id
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
for _, cachedSensor := range cachedSensors {
|
|
|
|
if temperature.SensorID == cachedSensor.SensorID {
|
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedTemperatures
|
|
|
|
}
|