Skip to content

Commit

Permalink
Add relay switching (on/off) actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pawiecz committed Jan 2, 2018
1 parent 2c08026 commit 5915475
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import "errors"
var (
// ErrRelayNotFound is returned when Relay is not found.
ErrRelayNotFound = errors.New("relay not found")
// ErrStateChangeFailed is returned if state change fails.
ErrStateChangeFailed = errors.New("state change failed")
)
37 changes: 37 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/nathan-osman/go-rpigpio"
)

// Index handles the default route (GET /).
Expand Down Expand Up @@ -33,6 +34,18 @@ func RelayShow(w http.ResponseWriter, r *http.Request, params httprouter.Params)
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
}

// RelayOn handles the relays on action (GET /relays/:id/on).
func RelayOn(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
switchRelay(w, id, true, rpi.LOW)
}

// RelayOff handles the relays off action (GET /relays/:id/off).
func RelayOff(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
switchRelay(w, id, false, rpi.HIGH)
}

// findRelay locates Relay with the given ID on the module.
func findRelay(id string) (*Relay, error) {
relay, ok := module[id]
Expand All @@ -43,6 +56,30 @@ func findRelay(id string) (*Relay, error) {
return relay, nil
}

// switchRelay provides unified wrapper for Relay On/Off actions.
func switchRelay(w http.ResponseWriter, id string, state bool, pinState rpi.Value) {
relay, err := findRelay(id)
if err != nil {
writeError(w, http.StatusNotFound, err.Error())
return
}
if err := setState(id, state, pinState); err != nil {
writeError(w, http.StatusServiceUnavailable, err.Error())
return
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
}

// setState switches given pin to requested state.
func setState(id string, state bool, pinState rpi.Value) error {
if err := pins[id].Write(pinState); err != nil {
// Writing requested state to the given pin failed
return ErrStateChangeFailed
}
module[id].State = state
return nil
}

// writeError wraps writing API error response.
func writeError(w http.ResponseWriter, status int, title string) {
apiError := &ApiError{Status: status, Title: title}
Expand Down
2 changes: 2 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func AllRoutes() Routes {
Route{"Index", "GET", "/", Index},
Route{"RelayIndex", "GET", "/relays", RelayIndex},
Route{"RelayShow", "GET", "/relays/:id", RelayShow},
Route{"RelayOn", "GET", "/relays/:id/on", RelayOn},
Route{"RelayOff", "GET", "/relays/:id/off", RelayOff},
}
return routes
}
Expand Down

0 comments on commit 5915475

Please sign in to comment.