fix: send only to enabled remotes data

This commit is contained in:
Markus Pesch 2018-11-30 16:14:47 +01:00
parent cc16bb9555
commit 7b18b115c8
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 28 additions and 21 deletions

View File

@ -28,25 +28,27 @@ func SendTemperatures(temperatures []*stypes.Temperature, configDir string) erro
temperaturesAsReader := bytes.NewReader(temperaturesAsBytes)
for _, remote := range cnf.Remotes {
requestURL := fmt.Sprintf("%s%s", remote.Address, "/temperatures")
req, err := http.NewRequest("POST", requestURL, temperaturesAsReader)
if err != nil {
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
b, err := ioutil.ReadAll(resp.Body)
if remote.Enabled {
requestURL := fmt.Sprintf("%s%s", remote.Address, "/temperatures")
req, err := http.NewRequest("POST", requestURL, temperaturesAsReader)
if err != nil {
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
}
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
}
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
}
}

View File

@ -37,15 +37,20 @@ func Get(sensorNames []string, push bool, configDir string, w io.Writer) error {
if len(sensorNames) > 0 {
for _, sensorName := range sensorNames {
for _, sensor := range cnf.Sensors {
if sensorName == *sensor.SensorName ||
sensorName == sensor.SensorID ||
sensorName == *sensor.WireID {
if sensorName == *sensor.SensorName && *sensor.SensorEnabled ||
sensorName == sensor.SensorID && *sensor.SensorEnabled ||
sensorName == *sensor.WireID && *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
} else {
sensors = append(sensors, cnf.Sensors...)
for _, sensor := range cnf.Sensors {
if *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
// check if sensor exists in cnf and has a wire id