Store things in sqlite

This commit is contained in:
2025-12-18 22:51:36 -05:00
parent 66ffc5f747
commit 76d55c572c
3 changed files with 199 additions and 68 deletions

15
go.mod
View File

@ -1,3 +1,18 @@
module furnacecheck module furnacecheck
go 1.24.4 go 1.24.4
require modernc.org/sqlite v1.40.1
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
golang.org/x/sys v0.36.0 // indirect
modernc.org/libc v1.66.10 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
)

51
go.sum
View File

@ -1,2 +1,49 @@
gocv.io/x/gocv v0.42.0 h1:AAsrFJH2aIsQHukkCovWqj0MCGZleQpVyf5gNVRXjQI= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
gocv.io/x/gocv v0.42.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
modernc.org/cc/v4 v4.26.5 h1:xM3bX7Mve6G8K8b+T11ReenJOT+BmVqQj0FY5T4+5Y4=
modernc.org/cc/v4 v4.26.5/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
modernc.org/ccgo/v4 v4.28.1 h1:wPKYn5EC/mYTqBO373jKjvX2n+3+aK7+sICCv4Fjy1A=
modernc.org/ccgo/v4 v4.28.1/go.mod h1:uD+4RnfrVgE6ec9NGguUNdhqzNIeeomeXf6CL0GTE5Q=
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
modernc.org/libc v1.66.10 h1:yZkb3YeLx4oynyR+iUsXsybsX4Ubx7MQlSYEw4yj59A=
modernc.org/libc v1.66.10/go.mod h1:8vGSEwvoUoltr4dlywvHqjtAqHBaw0j1jI7iFBTAr2I=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/sqlite v1.40.1 h1:VfuXcxcUWWKRBuP8+BR9L7VnmusMgBNNnBYGEe9w/iY=
modernc.org/sqlite v1.40.1/go.mod h1:9fjQZ0mB1LLP0GYrp39oOJXx/I2sxEnZtzCmEQIKvGE=
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

187
main.go
View File

@ -3,79 +3,152 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"database/sql"
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
"log" "log"
_ "modernc.org/sqlite"
"net/http" "net/http"
"os"
"strconv"
"strings" "strings"
"time" "time"
) )
const ( func getEnv(key, fallback string) string {
streamURL = "http://furnace.service:8082/stream" if val, ok := os.LookupEnv(key); ok {
return val
}
return fallback
}
// 🔧 TUNE THESE ONCE func getEnvInt(key string, fallback int) int {
roiX = 1050 if val, ok := os.LookupEnv(key); ok {
roiY = 760 if i, err := strconv.Atoi(val); err == nil {
roiW = 60 return i
roiH = 10 }
}
return fallback
}
// Thresholds func getEnvDuration(key string, fallback time.Duration) time.Duration {
minAvgRed = 90 // below this → light OFF if val, ok := os.LookupEnv(key); ok {
checkInterval = 2 * time.Second if d, err := time.ParseDuration(val); err == nil {
) return d
}
}
return fallback
}
type Light struct {
Name string
X, Y, W, H int
Priority string // high, default, low, urgent
}
var lights = map[string]Light{
"furnace_power": {"Furnace Power", 1135, 785, 15, 5, "urgent"},
"living_room_stat": {"Living Room Thermostat", 885, 230, 10, 5, "default"},
"living_room_heat": {"Living Room Heating", 1395, 224, 10, 5, "default"},
"dining_room_stat": {"Dining Room Thermostat", 885, 263, 10, 10, "default"},
"dining_room_heat": {"Dining Room Heating", 1395, 267, 10, 5, "default"},
"hot_water_stat": {"Hot Water Thermostat", 880, 440, 10, 5, "default"},
"hot_water_heat": {"Hot Water Heating", 1405, 445, 10, 5, "default"},
"first_bed_stat": {"First Bedroom Thermostat", 880, 480, 10, 5, "default"},
"first_bed_heat": {"First Bedroom Heating", 1407, 480, 10, 5, "default"},
"master_bed_stat": {"Master Bedroom Thermostat", 880, 520, 10, 5, "default"},
"master_bed_heat": {"Master Bedroom Heating", 1418, 540, 10, 5, "default"},
"basement_stat": {"Basement Thermostat", 875, 560, 10, 5, "default"},
"basement_heat": {"Basement Heating", 1410, 560, 10, 5, "default"},
"second_bed_stat": {"Second Bedroom Thermostat", 880, 600, 10, 5, "default"},
"second_bed_heat": {"Second Bedroom Heating", 1412, 600, 10, 5, "default"},
"inducer": {"Inducer", 1415, 645, 10, 5, "default"},
"burner": {"Burner", 1420, 690, 10, 5, "default"},
"circulator": {"Circulator", 1422, 734, 10, 5, "default"},
}
func main() { func main() {
fmt.Println("Starting furnace light monitor") fmt.Println("Starting multi-light monitor")
var prevState bool // false = Off, true = On dbPath := getEnv("LIGHT_DB_PATH", "/var/furnacecheck/lightlog.db")
webcamURL := getEnv("WEBCAM_URL", "http://furnace.service:8082/stream")
minBrightness := getEnvInt("MIN_BRIGHTNESS", 150)
checkInterval := getEnvDuration("CHECK_INTERVAL", 2*time.Second)
fmt.Println("Using DB:", dbPath)
fmt.Println("Using webcam:", webcamURL)
fmt.Println("Min Brightness:", minBrightness)
fmt.Println("Check Interval:", checkInterval)
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS light_checks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
state TEXT,
timestamp DATETIME
)`)
if err != nil {
log.Fatal(err)
}
prevStates := make(map[string]bool)
for { for {
on, err := checkOnce() img, err := grabFrame(webcamURL)
if err != nil { if err != nil {
log.Println("Error:", err) log.Println("Error grabbing frame:", err)
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue
} }
// Check for state change for key, light := range lights {
if on != prevState { on := isLightOn(img, light, minBrightness)
if on {
fmt.Println("RED LIGHT: ON") // Only act if state changed
} else { if on != prevStates[key] {
fmt.Println("RED LIGHT: OFF") stateStr := onOff(on)
// Log to SQLite
_, err := db.Exec(`INSERT INTO light_checks(name, state, timestamp) VALUES(?, ?, ?)`,
key, stateStr, time.Now().UTC())
if err != nil {
log.Println("Failed to insert into DB:", err)
} }
// Send notification // Notify
sendNtfyNotification(on) fmt.Printf("%s: %s\n", light.Name, stateStr)
sendNtfyNotification(light, on)
prevState = on // Update previous state
prevStates[key] = on
}
} }
time.Sleep(checkInterval) time.Sleep(checkInterval)
} }
} }
func checkOnce() (bool, error) { func grabFrame(url string) (image.Image, error) {
resp, err := http.Get(streamURL) resp, err := http.Get(url)
if err != nil { if err != nil {
return false, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
reader := bufio.NewReader(resp.Body) reader := bufio.NewReader(resp.Body)
for {
frame, err := readJPEGFrame(reader) frame, err := readJPEGFrame(reader)
if err != nil { if err != nil {
return false, err return nil, err
}
img, err := jpeg.Decode(bytes.NewReader(frame))
return isRedLightOn(img), nil
} }
return jpeg.Decode(bytes.NewReader(frame))
} }
func readJPEGFrame(r *bufio.Reader) ([]byte, error) { func readJPEGFrame(r *bufio.Reader) ([]byte, error) {
@ -108,47 +181,40 @@ func readJPEGFrame(r *bufio.Reader) ([]byte, error) {
} }
buf.WriteByte(b) buf.WriteByte(b)
if buf.Len() > 2 {
data := buf.Bytes() data := buf.Bytes()
if data[len(data)-2] == 0xFF && data[len(data)-1] == 0xD9 { if len(data) > 2 && data[len(data)-2] == 0xFF && data[len(data)-1] == 0xD9 {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
} }
}
} }
func isRedLightOn(img image.Image) bool { func isLightOn(img image.Image, light Light, minBrightness int) bool {
bounds := img.Bounds() bounds := img.Bounds()
x0 := clamp(roiX, bounds.Min.X, bounds.Max.X) x0 := clamp(light.X, bounds.Min.X, bounds.Max.X)
y0 := clamp(roiY, bounds.Min.Y, bounds.Max.Y) y0 := clamp(light.Y, bounds.Min.Y, bounds.Max.Y)
x1 := clamp(roiX+roiW, bounds.Min.X, bounds.Max.X) x1 := clamp(light.X+light.W, bounds.Min.X, bounds.Max.X)
y1 := clamp(roiY+roiH, bounds.Min.Y, bounds.Max.Y) y1 := clamp(light.Y+light.H, bounds.Min.Y, bounds.Max.Y)
var redSum int var lumSum float64
var count int var count int
for y := y0; y < y1; y++ { for y := y0; y < y1; y++ {
for x := x0; x < x1; x++ { for x := x0; x < x1; x++ {
r, g, b, _ := img.At(x, y).RGBA() r, g, b, _ := img.At(x, y).RGBA()
red := int(r >> 8) r8, g8, b8 := int(r>>8), int(g>>8), int(b>>8)
green := int(g >> 8) lum := 0.299*float64(r8) + 0.587*float64(g8) + 0.114*float64(b8)
blue := int(b >> 8) lumSum += lum
// ensure it's "red", not white glare
if red > 100 && red > green+30 && red > blue+30 {
redSum += red
count++ count++
} }
} }
}
if count == 0 { if count == 0 {
return false return false
} }
avgRed := redSum / count avgLum := lumSum / float64(count)
return avgRed > minAvgRed return avgLum > float64(minBrightness)
} }
func clamp(v, min, max int) int { func clamp(v, min, max int) int {
@ -161,21 +227,24 @@ func clamp(v, min, max int) int {
return v return v
} }
func sendNtfyNotification(on bool) { func onOff(on bool) string {
state := "OFF"
if on { if on {
state = "ON" return "ON"
} }
return "OFF"
}
func sendNtfyNotification(light Light, on bool) {
state := onOff(on)
url := "https://ntfy.unbl.ink/furnace" url := "https://ntfy.unbl.ink/furnace"
body := bytes.NewBufferString(state) body := bytes.NewBufferString(fmt.Sprintf("%s: %s", light.Name, state))
req, err := http.NewRequest("POST", url, body) req, err := http.NewRequest("POST", url, body)
if err != nil { if err != nil {
log.Println("Failed to create request:", err) log.Println("Failed to create request:", err)
return return
} }
req.Header.Set("Title", "Furnace Power") req.Header.Set("Title", "Light State Change")
req.Header.Set("Priority", "high") req.Header.Set("Priority", light.Priority)
client := &http.Client{Timeout: 5 * time.Second} client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req) resp, err := client.Do(req)