feat: Add support for PUT/DELETE state.message

Fix #33
This commit is contained in:
Bendodroid 2024-08-03 19:48:33 +02:00
parent 74a8db7d2b
commit 68d0ec05db
4 changed files with 50 additions and 7 deletions

View file

@ -34,3 +34,37 @@ func StateOpen(
resp.LastChange = time.Now().Unix()
}
}
func StateMessagePUT(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceState,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
if err != nil {
log.Println(err)
return
}
// Set SpaceAPI response values
resp.Message = string(body)
resp.LastChange = time.Now().Unix()
}
}
func StateMessageDELETE(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceState,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
_, err := updateEndpointValidator(authDB, validCredentials, w, r)
if err != nil {
log.Println(err)
return
}
// Set SpaceAPI response values
resp.Message = ""
resp.LastChange = time.Now().Unix()
}
}

View file

@ -57,6 +57,12 @@ func main() {
http.HandleFunc("PUT /state/open",
handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State),
)
http.HandleFunc("PUT /state/message",
handlers.StateMessagePUT(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State),
)
http.HandleFunc("DELETE /state/message",
handlers.StateMessageDELETE(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State),
)
// Register handler for environmental sensors
for sensorType, envSensorConfigs := range conf.Dynamic.Sensors {
for i, envSensorConfig := range envSensorConfigs {

View file

@ -91,7 +91,8 @@ func SaveCurrentState(response types.SpaceAPIResponseV14) {
State: struct {
Open bool `json:"open"`
LastChange int64 `json:"lastchange"`
}{Open: response.State.Open, LastChange: response.State.LastChange},
Message string `json:"message,omitempty"`
}{Open: response.State.Open, LastChange: response.State.LastChange, Message: response.State.Message},
}
// Save sensor state
persistentStateV14.Sensors = make(map[string][]types.PersistentEnvironmentSensor)

View file

@ -40,6 +40,7 @@ type SpaceAPIResponseV14 struct {
type SpaceState struct {
Open bool `json:"open"`
LastChange int64 `json:"lastchange"`
Message string `json:"message,omitempty"`
}
type EnvironmentSensor struct {
@ -55,6 +56,7 @@ type PersistentStateV14 struct {
State struct {
Open bool `json:"open"`
LastChange int64 `json:"lastchange"`
Message string `json:"message,omitempty"`
} `json:"state"`
Sensors map[string][]PersistentEnvironmentSensor `json:"sensors,omitempty"`
}