parent
74a8db7d2b
commit
68d0ec05db
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
6
main.go
6
main.go
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue