Save sensor values to persistent-state.json
This commit is contained in:
parent
42483df78c
commit
44143c0c2d
|
@ -54,4 +54,12 @@ type PersistentStateV14 struct {
|
|||
Open bool `json:"open"`
|
||||
LastChange int64 `json:"lastchange"`
|
||||
} `json:"state"`
|
||||
Sensors map[string][]PersistentEnvironmentSensor `json:"sensors"`
|
||||
}
|
||||
|
||||
type PersistentEnvironmentSensor struct {
|
||||
Value float64 `json:"value"`
|
||||
Location string `json:"location"`
|
||||
Name string `json:"name"`
|
||||
LastChange int64 `json:"lastchange"`
|
||||
}
|
||||
|
|
37
util/util.go
37
util/util.go
|
@ -18,8 +18,9 @@ const (
|
|||
// the file containing the old state.
|
||||
func MergeOldState(response *types.SpaceAPIResponseV14) {
|
||||
var (
|
||||
err error
|
||||
oldState []byte
|
||||
err error
|
||||
oldState []byte
|
||||
persistedState types.PersistentStateV14
|
||||
)
|
||||
|
||||
log.Println("Merging old state from", savedStateJSONPath, "...")
|
||||
|
@ -34,18 +35,35 @@ func MergeOldState(response *types.SpaceAPIResponseV14) {
|
|||
goto removeOld
|
||||
}
|
||||
|
||||
// Read file and merge
|
||||
// Read file and load persisted state
|
||||
oldState, err = os.ReadFile(savedStateJSONPath)
|
||||
if err != nil {
|
||||
log.Println("Error reading old state from", savedStateJSONPath, ", skipping merge... error:", err)
|
||||
goto removeOld
|
||||
}
|
||||
err = json.Unmarshal(oldState, response)
|
||||
err = json.Unmarshal(oldState, &persistedState)
|
||||
if err != nil {
|
||||
log.Println(savedStateJSONPath, "doesn't seem to contain valid data... error:", err)
|
||||
goto removeOld
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete old state json
|
||||
removeOld:
|
||||
err = os.RemoveAll(savedStateJSONPath)
|
||||
|
@ -75,6 +93,17 @@ func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
|||
LastChange int64 `json:"lastchange"`
|
||||
}{Open: response.State.Open, LastChange: response.State.LastChange},
|
||||
}
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize persistent state
|
||||
marshal, err := json.MarshalIndent(persistentStateV14, "", "\t")
|
||||
|
|
Loading…
Reference in a new issue