-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
211cf6a
commit cc4687b
Showing
10 changed files
with
208 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
web | ||
public/js/graph.js | ||
public/js/graph.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
graph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
//go:generate gopherjs build main.go -o ../public/js/graph.js -m | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/danielfireman/temp-to-go/server/weather" | ||
|
||
charts "github.com/cnguy/gopherjs-frappe-charts" | ||
) | ||
|
||
type weatherResponse struct { | ||
Weather []weather.State `json:"weather,omitempty"` | ||
} | ||
|
||
func main() { | ||
resp, err := http.Get("http://localhost:8080/restricted/weather") | ||
if err != nil { | ||
println(err) | ||
return | ||
} | ||
if resp.StatusCode != 200 { | ||
println("StatusCode:", resp.StatusCode) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(resp.Body) | ||
println(buf.String()) | ||
|
||
var wr weatherResponse | ||
if err := json.Unmarshal(buf.Bytes(), &wr); err != nil { | ||
println(err) | ||
return | ||
} | ||
var temps []float64 | ||
var labels []string | ||
for _, s := range wr.Weather { | ||
d := s.Timestamp.Hour() | ||
labels = append(labels, strconv.Itoa(d)) | ||
temps = append(temps, s.Temp) | ||
} | ||
|
||
chartData := charts.NewChartData() | ||
chartData.Labels = labels | ||
chartData.Datasets = []*charts.Dataset{ | ||
charts.NewDataset( | ||
"Weather", | ||
temps, | ||
)} | ||
|
||
_ = charts.NewLineChart("#chart", chartData).Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
{{define "main"}} | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Welcome to temp-to-go</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Welcome!</h1> | ||
<form method="post" action="/restricted/logout"> | ||
<button type="submit">Logout</button> | ||
</form> | ||
<hr> | ||
Current Fan Status: <b>{{.Speed}}</b> | ||
Current Fan Status: | ||
<b>{{.Speed}}</b> | ||
<form method="post" action={{.Action}}> | ||
Update Status: | ||
{{range .Opts}} | ||
<input type="radio" name={{.Name}} value={{.Value}} checked>{{.Label}} | ||
{{end}} | ||
Update Status: {{range .Opts}} | ||
<input type="radio" name={{.Name}} value={{.Value}} checked>{{.Label}} {{end}} | ||
<button type="submit">Submit</button> | ||
</form> | ||
</form> | ||
<hr> | ||
<br> | ||
<form method="post" action="/restricted/logout"> | ||
<button type="submit">Logout</button> | ||
</form> | ||
<b>Charts</b> | ||
<div id="chart"></div> | ||
|
||
<!-- Imports (must stay at the end) --> | ||
<script src="https://unpkg.com/[email protected]/dist/frappe-charts.min.iife.js"></script> | ||
<script src="/../js/graph.js" data-cover></script> | ||
</body> | ||
|
||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/danielfireman/temp-to-go/server/status" | ||
"github.com/danielfireman/temp-to-go/server/weather" | ||
"github.com/labstack/echo" | ||
) | ||
|
||
func weatherHandlerFunc(db *status.DB) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
var resp weatherResponse | ||
var err error | ||
resp.Weather, err = db.FetchWeather(time.Now().Add(-24*time.Hour), time.Now()) | ||
if err != nil { | ||
return c.NoContent(http.StatusInternalServerError) | ||
} | ||
return c.JSON(http.StatusOK, resp) | ||
} | ||
} | ||
|
||
type weatherResponse struct { | ||
Weather []weather.State `json:"weather,omitempty"` | ||
} |