Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A small web page to adjust the heating per room from a phone on the home network

<img src="docs/screenshot.png" alt="The heating page at phone width: a green All OK status in the top right, then Office and Bedroom cards, each with the room temperature, whether it is heating, the target, buttons to change it, and Air 5, 15, 30 and 60 buttons" width="375">

The page can be added to the phone's home screen with its own icon: "Share → Add to Home Screen" in Safari, "Add to home screen" in Chrome. On iPhone it then opens full-screen. Android Chrome only installs a page as an app over HTTPS, so over plain HTTP it adds a shortcut, which may open as a normal browser tab. The page has no service worker and caches nothing, so it always shows the live state and only works on the home network. Offline use or notifications would need HTTPS too. The icons are drawn by `internal/web/icon.go`: change it and run `go generate ./...`.

## Airing

"Air 5/15/30/60" sends `system_mode: off` for that many minutes, with 5 for a quick Stoßlüften. When the time is up, or on "Heat again", the room gets back the mode and target from before.
Expand Down
100 changes: 100 additions & 0 deletions internal/web/icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build ignore

// Draws the home screen icons into static/: a white thermometer on the page's
// accent colour. The square is filled to the edge, and the thermometer stays
// inside the middle circle that Android keeps when it masks an icon.
// Run with go generate ./internal/web.
package main

import (
"image"
"image/color"
"image/png"
"log"
"math"
"os"
"path/filepath"
)

var (
accent = color.NRGBA{0xb4, 0x49, 0x0f, 0xff}
white = color.NRGBA{0xff, 0xff, 0xff, 0xff}
)

// A thermometer in a unit square: a round-topped stem down into a bulb.
type thermometer struct {
stem, bulb, top float64
}

// inside reports whether (x, y) is inside the thermometer.
func (t thermometer) inside(x, y float64) bool {
const cx, bulbY = 0.5, 0.655
if math.Hypot(x-cx, y-bulbY) <= t.bulb {
return true
}
half := t.stem / 2
if y >= t.top && y <= bulbY && math.Abs(x-cx) <= half {
return true
}
return math.Hypot(x-cx, y-t.top) <= half
}

var (
outline = thermometer{stem: 0.13, bulb: 0.15, top: 0.26}
glass = thermometer{stem: 0.07, bulb: 0.12, top: 0.26}
mercury = thermometer{stem: 0.03, bulb: 0.08, top: 0.42}
)

func colorAt(x, y float64) color.NRGBA {
switch {
case mercury.inside(x, y):
return white
case glass.inside(x, y):
return accent
case outline.inside(x, y):
return white
default:
return accent
}
}

func draw(size int) *image.NRGBA {
const samples = 4
img := image.NewNRGBA(image.Rect(0, 0, size, size))
for py := range size {
for px := range size {
var r, g, b float64
for sy := range samples {
for sx := range samples {
x := (float64(px) + (float64(sx)+0.5)/samples) / float64(size)
y := (float64(py) + (float64(sy)+0.5)/samples) / float64(size)
c := colorAt(x, y)
r, g, b = r+float64(c.R), g+float64(c.G), b+float64(c.B)
}
}
n := float64(samples * samples)
img.SetNRGBA(px, py, color.NRGBA{uint8(math.Round(r / n)), uint8(math.Round(g / n)), uint8(math.Round(b / n)), 0xff})
}
}
return img
}

func main() {
for name, size := range map[string]int{
"icon-192.png": 192,
"icon-512.png": 512,
"apple-touch-icon.png": 180,
} {
f, err := os.Create(filepath.Join("static", name))
if err != nil {
log.Fatal(err)
}
enc := png.Encoder{CompressionLevel: png.BestCompression}
if err := enc.Encode(f, draw(size)); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
}
Binary file added internal/web/static/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/web/static/icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/web/static/icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions internal/web/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<meta name="theme-color" content="#191a1c" media="(prefers-color-scheme: dark)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Heating">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<!-- Installable to the home screen, but with no service worker: nothing is
cached offline, so the page always shows the live state. -->
<link rel="manifest" href="manifest.webmanifest">
<link rel="icon" href="icon-192.png" type="image/png">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<title>Heating</title>
<style>
:root {
Expand Down
15 changes: 15 additions & 0 deletions internal/web/static/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Heating",
"short_name": "Heating",
"description": "Adjust the heating per room on the home network.",
"start_url": "/",
"scope": "/",
"display": "standalone",
"background_color": "#f6f5f2",
"theme_color": "#f6f5f2",
"icons": [
{ "src": "icon-192.png", "sizes": "192x192", "type": "image/png", "purpose": "any" },
{ "src": "icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "any" },
{ "src": "icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }
]
}
10 changes: 10 additions & 0 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"io/fs"
"log"
"mime"
"net"
"net/http"
"strings"
Expand All @@ -18,9 +19,18 @@ import (
"github.com/subpathdev/heating-control/internal/health"
)

//go:generate go run icon.go

//go:embed static
var static embed.FS

// The image is FROM scratch, so there's no /etc/mime.types to look this up in.
func init() {
if err := mime.AddExtensionType(".webmanifest", "application/manifest+json"); err != nil {
panic(err)
}
}

const maxBodyBytes = 1024

// Rooms is what the page can read and change.
Expand Down
61 changes: 61 additions & 0 deletions internal/web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"image/png"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -87,6 +90,64 @@ func TestPageIsServed(t *testing.T) {
}
}

func TestHomeScreenFilesAreServed(t *testing.T) {
page := get(t, "/").Body.String()
for _, link := range []string{`rel="manifest" href="manifest.webmanifest"`, `rel="apple-touch-icon" href="apple-touch-icon.png"`} {
if !strings.Contains(page, link) {
t.Errorf("page has no %s", link)
}
}
// Offline caching would show old values, and needs HTTPS anyway.
if strings.Contains(page, "serviceWorker") {
t.Error("page registers a service worker")
}

rec := get(t, "/manifest.webmanifest")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if got := rec.Header().Get("Content-Type"); got != "application/manifest+json" {
t.Errorf("Content-Type = %q", got)
}
var manifest struct {
StartURL string `json:"start_url"`
Display string `json:"display"`
Icons []struct {
Src string `json:"src"`
Sizes string `json:"sizes"`
} `json:"icons"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &manifest); err != nil {
t.Fatal(err)
}
if manifest.StartURL != "/" || manifest.Display != "standalone" {
t.Errorf("manifest = %+v", manifest)
}

icons := map[string]string{"apple-touch-icon.png": "180x180"}
var sizes []string
for _, icon := range manifest.Icons {
icons[icon.Src] = icon.Sizes
sizes = append(sizes, icon.Sizes)
}
for _, want := range []string{"192x192", "512x512"} {
if !slices.Contains(sizes, want) {
t.Errorf("manifest has no %s icon", want)
}
}
for src, sizes := range icons {
rec := get(t, "/"+src)
cfg, err := png.DecodeConfig(rec.Body)
if rec.Code != http.StatusOK || err != nil {
t.Errorf("%s: status %d, %v", src, rec.Code, err)
continue
}
if got := fmt.Sprintf("%dx%d", cfg.Width, cfg.Height); got != sizes {
t.Errorf("%s is %s, want %s", src, got, sizes)
}
}
}

func TestSpecIsServed(t *testing.T) {
rec := get(t, "/api/openapi.yaml")
if rec.Code != http.StatusOK || !strings.HasPrefix(rec.Body.String(), "openapi: 3") {
Expand Down
Loading