Commit bdc84c57 authored by David Schäfer's avatar David Schäfer
Browse files

allow multiple sensor configurations in upload

parent fa1b5f33
......@@ -5,7 +5,6 @@ import (
t "dmpapi/transport"
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"net/http"
"net/url"
"strings"
......@@ -45,18 +44,20 @@ func (positions SensorPositions) Count() int {
if pos.Position > out {
out = pos.Position
}
}
return out + 1
}
func (positions SensorPositions) TimePosition(date UploadDate) (int, error) {
func (positions SensorPositions) TimePosition(value Value) (int, error) {
out := -1
date := value.Date
for _, pos := range positions {
if pos.IsTime {
if pos.StartDate.Before(date) && pos.EndDate.After(date) {
startDate := pos.StartDate
endDate := pos.EndDate
if (startDate.Equal(date) || startDate.Before(date)) && (endDate.Equal(date) || endDate.After(date)) {
if out > -1 {
return -1, fmt.Errorf("more than onw timestamp sensor found for: %v", date.Format())
return -1, fmt.Errorf("more than one timestamp sensor found for: %v", date.Format())
}
out = pos.Position
}
......@@ -69,14 +70,17 @@ func (positions SensorPositions) TimePosition(date UploadDate) (int, error) {
}
func (positions SensorPositions) ValuePosition(value Value) (int, error) {
date := value.Date
for _, pos := range positions {
startDate := pos.StartDate
endDate := pos.EndDate
if (pos.SensorName == value.SensorName) || (pos.SensorLabel == value.SensorLabel) {
if pos.StartDate.Before(value.Date) && pos.EndDate.After(value.Date) {
if (startDate.Equal(date) || startDate.Before(date)) && (endDate.Equal(date) || endDate.After(date)) {
return pos.Position, nil
}
}
}
return -1, fmt.Errorf("no position found for: %+v", value)
return -1, fmt.Errorf("no position found for: %+v (%v)", value, value.Date.Format())
}
type Values []Value
......@@ -85,11 +89,6 @@ func (values Values) Encode(positions *SensorPositions) (string, error) {
colCount := positions.Count()
header := make([]string, colCount)
for _, pos := range *positions {
header[pos.Position] = pos.SensorLabel
}
table := map[string][]string{}
for _, value := range values {
......@@ -100,7 +99,7 @@ func (values Values) Encode(positions *SensorPositions) (string, error) {
row, exists := table[date]
if !exists {
// no -> add a new row
timePosition, err := positions.TimePosition(value.Date)
timePosition, err := positions.TimePosition(value)
if err != nil {
return "", err
}
......@@ -109,15 +108,15 @@ func (values Values) Encode(positions *SensorPositions) (string, error) {
table[date] = row
}
// write into the appropiate position
pos, err := positions.ValuePosition(value)
if err != nil {
return "", err
}
row[pos] = fmt.Sprintf("%.12f", value.Value)
}
out := []string{strings.Join(header, ",")}
out := []string{}
for _, row := range table {
out = append(out, strings.Join(row, ","))
}
......@@ -150,11 +149,6 @@ func (db *DataBase) PostLevel1Data(dec *json.Decoder, projectId, loggerId int) (
return nil, err
}
log.Debug().
Int("project-id", projectId).
Int("logger-id", loggerId).
Msgf("level1 upload file content:\n%s\n", content)
req, err := t.FileRequest(uploadURL, []byte(content))
if err != nil {
return nil, err
......
......@@ -49,7 +49,7 @@ func (db *DataBase) queryDeviceID(projectId, loggerId int) (string, error) {
deviceID := (*loggers)[0].DeviceID
if deviceID == "" {
return "", fmt.Errorf("No DEVICE_ID found for logger id: %s", loggerId)
return "", fmt.Errorf("No DEVICE_ID found for logger id: %v", loggerId)
}
return deviceID, nil
......
......@@ -13,7 +13,6 @@ SELECT
AND l.logger_id = :2
AND s.updated = 0 -- the generic rewrite parser only uses the latest setup
AND s.virtual_sensor = 0 -- we don't want virtual sensors, as they don't accept upload data
AND l.deleted = 0 -- leave the dead alone
AND l.deleted = 0 -- leave the dead alone
AND s.deleted = 0
AND c.deleted = 0
AND c.config_end IS NULL -- the generic rewrite parser does not support writing into old sensor configurations
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment