First implementation of new config format

- Functionally equivalent to old version at present
- Temperature and Humidity sensors not handled yet
- Moved HTTP handlers around
This commit is contained in:
Bendodroid 2024-01-13 23:26:04 +01:00
commit b2f62c7bb0
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
7 changed files with 125 additions and 121 deletions

View file

@ -1,42 +1,18 @@
package util
import (
"bytes"
"encoding/json"
"errors"
"log"
"os"
"path"
"slices"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
const savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
// ParseTemplate parses the given file and
func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) {
// Read template file
template, err := os.ReadFile(file)
if err != nil {
log.Fatalln("Failed reading file:", err)
}
// Parse JSON
dec := json.NewDecoder(bytes.NewReader(template))
dec.DisallowUnknownFields()
err = dec.Decode(&resp)
if err != nil {
log.Fatalln("Could not parse SpaceAPI response template:", err)
}
// Check if compatible with v14
if !slices.Contains(resp.APICompatibility, "14") {
log.Fatalln("Provided template doesn't specify compatibility with API version 14")
}
return
}
const (
savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
)
// MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes
// the file containing the old state.