Documentation ¶
Index ¶
- Constants
- Variables
- func AreConfigsEqual(a ControllersConfig, b ControllersConfig) bool
- func IndexCheck404Middleware(next http.Handler) http.Handler
- func IndexCheckForFormGetSubmit(next http.Handler) http.Handler
- func IsJSON(str []byte) bool
- func LogRequestMiddleware(next http.Handler) http.Handler
- func SecureHeadersMiddleware(next http.Handler) http.Handler
- func ValidateAndParseBody(next http.Handler) http.Handler
- func WithLogger(l Logger, next http.Handler) http.Handler
- type ApiMessage
- type ApiStatus
- type ClientDb
- type ConfigGopher
- func (cg *ConfigGopher) FetchConfig() (ControllersConfig, ConfigSource, error)
- func (cg *ConfigGopher) GetSourceKind() (ConfigSource, bool)
- func (cg *ConfigGopher) HasError() error
- func (cg *ConfigGopher) NotifyServer(message string, urgency ServerNotificationUrgency)
- func (cg *ConfigGopher) SendConfig(config ControllersConfig) error
- type ConfigSource
- type Control
- type ControlLooper
- type Controller
- type ControllersConfig
- type DS18B20Reader
- type HeatOrCoolController
- type KasaHeatOrCoolController
- type Logger
- type Notification
- type PostResult
- type Server
- func (s *Server) GetConfigurationHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) IndexHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) ListenAndServe()
- func (s *Server) PostConfigurationHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) PostHtmlConfigurationHandler(w http.ResponseWriter, r *http.Request, result PostResult)
- func (s *Server) PostJsonConfigurationHandler(w http.ResponseWriter, r *http.Request, l Logger, result PostResult)
- type ServerDb
- type ServerNotificationUrgency
- type SmsNotifier
- type SqliteClientDb
- func (dbo SqliteClientDb) Close() error
- func (dbo SqliteClientDb) FetchTmpLogsNotYetSentToServer() ([]TmpLog, error)
- func (dbo SqliteClientDb) GetAverageRecentTemperature(controllerName string, d time.Duration) (float32, error)
- func (dbo SqliteClientDb) MarkTmpLogsAsSentToServer(ids []int) error
- func (dbo SqliteClientDb) PersistTmpLog(tmplog TmpLog) error
- type SqliteServerDb
- func (dbo SqliteServerDb) Close() error
- func (dbo SqliteServerDb) CreateOrUpdateConfig(clientId string, config ControllersConfig) error
- func (dbo SqliteServerDb) GetConfig(clientId string) (ControllersConfig, bool, error)
- func (dbo SqliteServerDb) ListNotifications(clientId string) ([]Notification, error)
- func (dbo SqliteServerDb) PutNotification(clientId string, note Notification) error
- type TempType
- type TemperatureReader
- type TmpLog
Constants ¶
const ThermometerDevicesRootPath = "/sys/bus/w1/devices/"
ThermometerDevicesRootPath where to look for DS18B20 devices
Variables ¶
var AtLeastOneHostControlFailed = fmt.Errorf("at least one host control failed")
var ClientIdentifiersRegex = regexp.MustCompile(`^[-a-zA-Z0-9]{3,50}$`)
ClientIdentifiersRegex pattern to which clientIdentifiers and controllerNames should adhere
var PostUnmarshalableJson error
var TemperatureReadError = errors.New("there was a problem reading the current temperature")
Functions ¶
func AreConfigsEqual ¶
func AreConfigsEqual(a ControllersConfig, b ControllersConfig) bool
AreConfigsEqual Based on https://stackoverflow.com/questions/48253423/unique-hash-from-struct
func LogRequestMiddleware ¶
LogRequestMiddleware logs basic info of a HTTP request RemoteAddr: Network address that sent the request (IP:port) Proto: Protocol version Method: HTTP method URL: Request URL
func SecureHeadersMiddleware ¶
SecureHeadersMiddleware adds two basic security headers to each HTTP response X-XSS-Protection: 1; mode-block can help to prevent XSS attacks X-Frame-Options: deny can help to prevent clickjacking attacks
Types ¶
type ApiMessage ¶
type ConfigGopher ¶
type ConfigGopher struct { LocalConfigPath string //ServerRoot includes the protocol scheme, hostname and port. Trailing '/' is optional ServerRoot string //ClientId the client identifier to let the server know who we are ClientId string ConfigFetchInterval time.Duration //if a Writer is defined, server notifications will be written additionally to this Writer NotifyOutput io.Writer }
func (*ConfigGopher) FetchConfig ¶
func (cg *ConfigGopher) FetchConfig() (ControllersConfig, ConfigSource, error)
func (*ConfigGopher) GetSourceKind ¶
func (cg *ConfigGopher) GetSourceKind() (ConfigSource, bool)
func (*ConfigGopher) HasError ¶
func (cg *ConfigGopher) HasError() error
func (*ConfigGopher) NotifyServer ¶
func (cg *ConfigGopher) NotifyServer(message string, urgency ServerNotificationUrgency)
NotifyServer Send the server a message If the server can't be contacted, queue the message in a text file maybe we can restructure all the logging code to use structured messages (with error levels). Above a certain error level could be automatically reported
func (*ConfigGopher) SendConfig ¶
func (cg *ConfigGopher) SendConfig(config ControllersConfig) error
type ConfigSource ¶
type ConfigSource int
const ( ConfigSourceLocalFile ConfigSource = iota + 1 ConfigSourceServer )
func (ConfigSource) String ¶
func (c ConfigSource) String() string
type ControlLooper ¶
type ControlLooper struct { Cg *ConfigGopher HeatOrCoolController HeatOrCoolController TemperatureReader TemperatureReader Logger Logger // contains filtered or unexported fields }
func NewControlLooper ¶
func NewControlLooper(cg *ConfigGopher, HeatOrCoolController HeatOrCoolController, logger Logger) *ControlLooper
func (*ControlLooper) StartControlLoop ¶
func (cl *ControlLooper) StartControlLoop()
type Controller ¶
type Controller struct { Name string `json:"name"` ThermometerPath string `json:"thermometerPath"` ControlType string `json:"controlType"` SwitchHosts []string `json:"switchHosts"` TemperatureSchedule map[time.Time]float32 `json:"temperatureSchedule"` DisableFreezeProtection bool `json:"disableFreezeProtection"` }
func (*Controller) GetCurrentDesiredTemperature ¶
func (controller *Controller) GetCurrentDesiredTemperature() (float32, bool)
type ControllersConfig ¶
type ControllersConfig struct {
Controllers []Controller `json:"controllers"`
}
type DS18B20Reader ¶
type DS18B20Reader struct {
// contains filtered or unexported fields
}
func NewDS18B20Reader ¶
func NewDS18B20Reader(logger Logger) *DS18B20Reader
func (DS18B20Reader) EnumerateThermometerPaths ¶
func (t DS18B20Reader) EnumerateThermometerPaths() []string
EnumerateThermometerPaths Assuming we're on a Raspberry Pi, check if we can find any DS18B20 devices running
func (DS18B20Reader) ReadTemperatureInF ¶
func (t DS18B20Reader) ReadTemperatureInF(temperaturePath string) (float32, error)
type HeatOrCoolController ¶
type KasaHeatOrCoolController ¶
type KasaHeatOrCoolController struct {
// contains filtered or unexported fields
}
func NewKasaHeatOrCoolController ¶
func NewKasaHeatOrCoolController(kasaPath string) *KasaHeatOrCoolController
func (*KasaHeatOrCoolController) ControlDevice ¶
func (k *KasaHeatOrCoolController) ControlDevice(host string, action Control) error
type Notification ¶
type Notification struct { NotificationId int `json:"notificationId"` ReportedAt time.Time `json:"reportedAt"` ClientId string `json:"clientId"` Message string `json:"message"` Severity string `json:"severity"` HasUserBeenNotified bool `json:"hasUserBeenNotified"` }
Notification /*
type PostResult ¶
type PostResult struct {
// contains filtered or unexported fields
}
type Server ¶
func (*Server) GetConfigurationHandler ¶
func (s *Server) GetConfigurationHandler(w http.ResponseWriter, r *http.Request)
func (*Server) IndexHandler ¶
func (s *Server) IndexHandler(w http.ResponseWriter, r *http.Request)
func (*Server) ListenAndServe ¶
func (s *Server) ListenAndServe()
func (*Server) PostConfigurationHandler ¶
func (s *Server) PostConfigurationHandler(w http.ResponseWriter, r *http.Request)
func (*Server) PostHtmlConfigurationHandler ¶
func (s *Server) PostHtmlConfigurationHandler(w http.ResponseWriter, r *http.Request, result PostResult)
func (*Server) PostJsonConfigurationHandler ¶
func (s *Server) PostJsonConfigurationHandler(w http.ResponseWriter, r *http.Request, l Logger, result PostResult)
type ServerDb ¶
type ServerDb interface { //Configs: the main purpose of this server, to receive and server client config CreateOrUpdateConfig(clientId string, config ControllersConfig) error GetConfig(clientId string) (ControllersConfig, bool, error) ListNotifications(clientId string) ([]Notification, error) PutNotification(clientId string, note Notification) error //Check-ins: meant to detect offline clients //ClientIdCheckIn(clientId string) error //GetLastClientIdCheckIn(clientId string) (time.Time, error) io.Closer }
type ServerNotificationUrgency ¶
type ServerNotificationUrgency int
const ( InfoNotification ServerNotificationUrgency = iota + 1 ProblemNotification SeriousNotification )
func (ServerNotificationUrgency) MarshalJSON ¶
func (s ServerNotificationUrgency) MarshalJSON() ([]byte, error)
func (ServerNotificationUrgency) String ¶
func (s ServerNotificationUrgency) String() string
func (*ServerNotificationUrgency) UnmarshalJSON ¶
func (s *ServerNotificationUrgency) UnmarshalJSON(b []byte) error
type SmsNotifier ¶
type SmsNotifier struct{}
type SqliteClientDb ¶
type SqliteClientDb struct {
// contains filtered or unexported fields
}
func NewSqliteDbFromFilename ¶
func NewSqliteDbFromFilename(filename string, logger Logger) (SqliteClientDb, error)
func (SqliteClientDb) Close ¶
func (dbo SqliteClientDb) Close() error
func (SqliteClientDb) FetchTmpLogsNotYetSentToServer ¶
func (dbo SqliteClientDb) FetchTmpLogsNotYetSentToServer() ([]TmpLog, error)
func (SqliteClientDb) GetAverageRecentTemperature ¶
func (SqliteClientDb) MarkTmpLogsAsSentToServer ¶
func (dbo SqliteClientDb) MarkTmpLogsAsSentToServer(ids []int) error
MarkTmpLogsAsSentToServer TODO implement a limit to how many can be marked complete at at time
func (SqliteClientDb) PersistTmpLog ¶
func (dbo SqliteClientDb) PersistTmpLog(tmplog TmpLog) error
type SqliteServerDb ¶
type SqliteServerDb struct {
// contains filtered or unexported fields
}
func NewSqliteServerDbFromFilename ¶
func NewSqliteServerDbFromFilename(filename string, logger Logger) (SqliteServerDb, error)
func (SqliteServerDb) Close ¶
func (dbo SqliteServerDb) Close() error
func (SqliteServerDb) CreateOrUpdateConfig ¶
func (dbo SqliteServerDb) CreateOrUpdateConfig(clientId string, config ControllersConfig) error
CreateOrUpdateConfig TODO test me
func (SqliteServerDb) GetConfig ¶
func (dbo SqliteServerDb) GetConfig(clientId string) (ControllersConfig, bool, error)
func (SqliteServerDb) ListNotifications ¶
func (dbo SqliteServerDb) ListNotifications(clientId string) ([]Notification, error)
func (SqliteServerDb) PutNotification ¶
func (dbo SqliteServerDb) PutNotification(clientId string, note Notification) error