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() 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", http.HandleFunc("PUT /state/open",
handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), 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 // Register handler for environmental sensors
for sensorType, envSensorConfigs := range conf.Dynamic.Sensors { for sensorType, envSensorConfigs := range conf.Dynamic.Sensors {
for i, envSensorConfig := range envSensorConfigs { for i, envSensorConfig := range envSensorConfigs {

View file

@ -89,9 +89,10 @@ func SaveCurrentState(response types.SpaceAPIResponseV14) {
// Create persistent state // Create persistent state
persistentStateV14 := types.PersistentStateV14{ persistentStateV14 := types.PersistentStateV14{
State: struct { State: struct {
Open bool `json:"open"` Open bool `json:"open"`
LastChange int64 `json:"lastchange"` 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 // Save sensor state
persistentStateV14.Sensors = make(map[string][]types.PersistentEnvironmentSensor) persistentStateV14.Sensors = make(map[string][]types.PersistentEnvironmentSensor)

View file

@ -38,8 +38,9 @@ type SpaceAPIResponseV14 struct {
} }
type SpaceState struct { type SpaceState struct {
Open bool `json:"open"` Open bool `json:"open"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange"`
Message string `json:"message,omitempty"`
} }
type EnvironmentSensor struct { type EnvironmentSensor struct {
@ -53,8 +54,9 @@ type EnvironmentSensor struct {
type PersistentStateV14 struct { type PersistentStateV14 struct {
State struct { State struct {
Open bool `json:"open"` Open bool `json:"open"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange"`
Message string `json:"message,omitempty"`
} `json:"state"` } `json:"state"`
Sensors map[string][]PersistentEnvironmentSensor `json:"sensors,omitempty"` Sensors map[string][]PersistentEnvironmentSensor `json:"sensors,omitempty"`
} }