main.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "github.com/brutella/hc"
  9. "github.com/brutella/hc/accessory"
  10. "github.com/bwdezend/astoria-hc/internal/core"
  11. "github.com/bwdezend/astoria-hc/internal/telemetry"
  12. )
  13. var acc accessory.Thermostat
  14. var deviceName = flag.String("name", "Astoria Boiler Thermostat", "Device name")
  15. var dbPath = flag.String("db", "./db", "Database path")
  16. var enableMetics = flag.Bool("metrics", true, "Enable prometheus metrics")
  17. var prometheusPort = flag.Int("promPort", 2112, "Port to reigster /metrics handler on")
  18. var temperatureMinimim = flag.Float64("minTemp", 10.0, "Minimum temperature value")
  19. var temperatureMaximum = flag.Float64("maxTemp", 130.0, "Maximum temperature value")
  20. var temperatureStepSize = flag.Float64("stepTemp", 0.1, "Temperature setting step size")
  21. var homekitPin = flag.String("pin", "00102003", "Homekit Pairing PIN")
  22. var gpio = flag.Bool("gpio", true, "load gpio code")
  23. func init() {
  24. flag.Parse()
  25. info := accessory.Info{
  26. Name: *deviceName,
  27. }
  28. acc = *accessory.NewThermostat(info, 10.0, *temperatureMinimim, *temperatureMaximum, *temperatureStepSize)
  29. }
  30. func check(e error) {
  31. if e != nil {
  32. panic(e)
  33. }
  34. }
  35. var p = 3.0
  36. func main() {
  37. c := make(chan os.Signal)
  38. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  39. go func() {
  40. <-c
  41. os.Exit(0)
  42. }()
  43. config := hc.Config{Pin: *homekitPin}
  44. t, err := hc.NewIPTransport(config, acc.Accessory)
  45. check(err)
  46. hc.OnTermination(func() {
  47. t.Stop()
  48. })
  49. if *enableMetics {
  50. go telemetry.PrometheusMetrics(*prometheusPort)
  51. }
  52. // Recover previously set temperature on program start
  53. core.SetTargetTemp(acc, core.RecoverTemp())
  54. // Picks up the current temperature every second from *path/current_temp
  55. go core.GetCurrentTemp(acc)
  56. // Run the PID loop itself
  57. go core.TemperatureProportional(acc, *gpio)
  58. // Enable the "boiler isn't heating" error handling
  59. go core.TemperatureErrorDetection(acc)
  60. // Runs the REST-y interface for adjusting the setpoint and causing the program to exit.
  61. //go telemetry.SetpointHandler()
  62. // If the -gpio flag is set to true load the Raspberry Pi gpio handling code.
  63. if *gpio {
  64. log.Println("rpi-gpio enabled - enabling power button on pin 26")
  65. go core.PowerButton(acc)
  66. }
  67. // Enable the homekit code to change the setpoint
  68. acc.Thermostat.TargetTemperature.OnValueRemoteUpdate(func(temp float64) {
  69. core.SetTargetTemp(acc, temp)
  70. })
  71. t.Start()
  72. }