2023-11-05 20:23:31 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-11-10 03:24:12 +01:00
|
|
|
"errors"
|
2023-11-05 20:23:31 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-11-10 03:24:12 +01:00
|
|
|
"path"
|
2023-11-05 20:23:31 +01:00
|
|
|
|
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
|
|
|
)
|
|
|
|
|
2024-01-13 23:26:04 +01:00
|
|
|
const (
|
|
|
|
savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
|
|
|
|
)
|
2023-11-10 03:24:12 +01:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
func MergeOldState(response *types.SpaceAPIResponseV14) {
|
|
|
|
var (
|
2024-01-14 01:29:41 +01:00
|
|
|
err error
|
|
|
|
oldState []byte
|
|
|
|
persistedState types.PersistentStateV14
|
2023-11-10 03:24:12 +01:00
|
|
|
)
|
|
|
|
|
2024-01-13 23:14:56 +01:00
|
|
|
log.Println("Merging old state from", savedStateJSONPath, "...")
|
|
|
|
|
2023-11-10 03:24:12 +01:00
|
|
|
// Check if state.json is present
|
|
|
|
_, err = os.Stat(savedStateJSONPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Old state json not accessible at", savedStateJSONPath, ", skipping merge... error:", err)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
goto removeOld
|
|
|
|
}
|
|
|
|
|
2024-01-14 01:29:41 +01:00
|
|
|
// Read file and load persisted state
|
2023-11-10 03:24:12 +01:00
|
|
|
oldState, err = os.ReadFile(savedStateJSONPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error reading old state from", savedStateJSONPath, ", skipping merge... error:", err)
|
|
|
|
goto removeOld
|
|
|
|
}
|
2024-01-14 01:29:41 +01:00
|
|
|
err = json.Unmarshal(oldState, &persistedState)
|
2023-11-10 03:24:12 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(savedStateJSONPath, "doesn't seem to contain valid data... error:", err)
|
|
|
|
goto removeOld
|
|
|
|
}
|
|
|
|
|
2024-01-14 01:29:41 +01:00
|
|
|
// Merge state
|
|
|
|
response.State = persistedState.State
|
|
|
|
|
|
|
|
// Merge sensors
|
|
|
|
for key, environmentSensors := range persistedState.Sensors {
|
|
|
|
for _, sensor := range environmentSensors {
|
|
|
|
// Order or amount of sensors might have changed, so check sensors already present in response
|
|
|
|
// and then look for matching values in persisted state
|
|
|
|
for i := range response.Sensors[key] {
|
|
|
|
if rs := &response.Sensors[key][i]; rs.Location == sensor.Location && rs.Name == sensor.Name {
|
|
|
|
rs.Value = sensor.Value
|
|
|
|
rs.LastChange = sensor.LastChange
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 03:24:12 +01:00
|
|
|
// Delete old state json
|
|
|
|
removeOld:
|
|
|
|
err = os.RemoveAll(savedStateJSONPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to remove", savedStateJSONPath, ", continuing... error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
2023-11-10 03:49:05 +01:00
|
|
|
// Create state directory if not present
|
|
|
|
err := os.MkdirAll(path.Dir(savedStateJSONPath), 0750)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err)
|
|
|
|
}
|
|
|
|
|
2024-01-06 19:43:51 +01:00
|
|
|
// Open persistent state file
|
2024-01-14 21:52:23 +01:00
|
|
|
file, err := os.OpenFile(savedStateJSONPath, os.O_WRONLY|os.O_CREATE, 0644)
|
2023-11-10 03:24:12 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err)
|
|
|
|
}
|
2024-01-06 19:43:51 +01:00
|
|
|
defer file.Close()
|
2023-11-10 03:24:12 +01:00
|
|
|
|
|
|
|
// Create persistent state
|
|
|
|
persistentStateV14 := types.PersistentStateV14{
|
|
|
|
State: struct {
|
|
|
|
Open bool `json:"open"`
|
|
|
|
LastChange int64 `json:"lastchange"`
|
|
|
|
}{Open: response.State.Open, LastChange: response.State.LastChange},
|
|
|
|
}
|
2024-01-14 01:29:41 +01:00
|
|
|
// Save sensor state
|
|
|
|
persistentStateV14.Sensors = make(map[string][]types.PersistentEnvironmentSensor)
|
|
|
|
for key, environmentSensors := range response.Sensors {
|
|
|
|
persistentStateV14.Sensors[key] = make([]types.PersistentEnvironmentSensor, len(environmentSensors))
|
|
|
|
for i, sensor := range environmentSensors {
|
|
|
|
persistentStateV14.Sensors[key][i].Value = sensor.Value
|
|
|
|
persistentStateV14.Sensors[key][i].Location = sensor.Location
|
|
|
|
persistentStateV14.Sensors[key][i].Name = sensor.Name
|
|
|
|
persistentStateV14.Sensors[key][i].LastChange = sensor.LastChange
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 03:24:12 +01:00
|
|
|
|
|
|
|
// Serialize persistent state
|
|
|
|
marshal, err := json.MarshalIndent(persistentStateV14, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed serializing persistent state... error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
_, err = file.Write(marshal)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed writing persistent state to file", file.Name(), "... error:", err)
|
|
|
|
}
|
|
|
|
}
|