Skip to content

Commit

Permalink
Add status page that shows recent commands
Browse files Browse the repository at this point in the history
Recent commands are stored in memory. They're not persisted to any
database, so the log is gone if the instance dies. It's sufficient for
my needs though.
  • Loading branch information
mtraver committed Sep 25, 2019
1 parent f2ea370 commit a6d0e28
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
13 changes: 12 additions & 1 deletion web/actionhandler.go
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"fmt"
"net/http"
"time"

jwt "github.com/dgrijalva/jwt-go"
"github.com/golang/protobuf/jsonpb"
Expand Down Expand Up @@ -123,9 +124,19 @@ func (h actionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Region: h.Region,
}, action)

if err != nil || resp.HTTPStatusCode != http.StatusOK {
success := err == nil && resp.HTTPStatusCode == http.StatusOK
if !success {
lg.Errorf("Failed to send command to device. Error: %q Response: %v", err, resp)
}

actionLogMux.Lock()
defer actionLogMux.Unlock()

actionLog = append(actionLog, actionRecord{
Timestamp: time.Now(),
Success: success,
Action: action,
})
}

func sendCommand(device iotcore.Device, action *ipb.Action) (*cloudiot.SendCommandToDeviceResponse, error) {
Expand Down
25 changes: 25 additions & 0 deletions web/main.go
Expand Up @@ -3,12 +3,27 @@ package main
import (
"crypto/ecdsa"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"

jwt "github.com/dgrijalva/jwt-go"
ipb "github.com/mtraver/rpi-ir-remote/irremotepb"
)

type actionRecord struct {
Timestamp time.Time
Success bool
Action *ipb.Action
}

var (
actionLog = make([]actionRecord, 0, 16)
actionLogMux sync.Mutex
)

func mustGetenv(varName string) string {
Expand Down Expand Up @@ -43,13 +58,23 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
}

func main() {
templates := template.Must(template.New("status.html").Funcs(
template.FuncMap{
"RFC3339": func(t time.Time) string {
return t.Format(time.RFC3339)
},
}).ParseGlob("web/templates/*"))

http.HandleFunc("/", rootHandler)
http.Handle("/action", actionHandler{
ProjectID: mustGetenv("GOOGLE_CLOUD_PROJECT"),
RegistryID: mustGetenv("IOTCORE_REGISTRY"),
Region: mustGetenv("IOTCORE_REGION"),
PublicKey: mustParseKey(mustGetenv("PUB_KEY_PATH")),
})
http.Handle("/status", statusHandler{
Template: templates,
})

port := os.Getenv("PORT")
if port == "" {
Expand Down
27 changes: 27 additions & 0 deletions web/status.go
@@ -0,0 +1,27 @@
package main

import (
"html/template"
"net/http"

"github.com/mtraver/gaelog"
)

type statusHandler struct {
Template *template.Template
}

func (h statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
lg, err := gaelog.New(r)
if err != nil {
lg.Errorf("%v", err)
}
defer lg.Close()

actionLogMux.Lock()
defer actionLogMux.Unlock()

if err := h.Template.ExecuteTemplate(w, "status", actionLog); err != nil {
lg.Errorf("Could not execute template: %v", err)
}
}
17 changes: 17 additions & 0 deletions web/templates/status.html
@@ -0,0 +1,17 @@
{{ define "status" }}
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>RPi IR Remote | Status</title>
</head>
<body>
<ul>
{{ range $record := . }}
<li>{{ if $record.Success }}OK{{ else }}FAIL{{ end }} | {{ RFC3339 $record.Timestamp }} | {{ $record.Action }}</li>
{{ end }}
</ul>
</body>
</html>
{{ end }}

0 comments on commit a6d0e28

Please sign in to comment.