Skip to content

Commit

Permalink
golint (add multiple comments and adapt golang naming conventions)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanwichmann committed Mar 19, 2017
1 parent 6771e88 commit adb6598
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 191 deletions.
4 changes: 2 additions & 2 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func extractBinaryFromZipArchive(archiveFile string, binaryName string, destinat
}
}

return "", errors.New("Binary not found in archive.")
return "", errors.New("Binary not found in archive")
}

func extractBinaryFromTarArchive(archiveFile string, binaryName string, destinationFolder string) (binaryFile string, err error) {
Expand Down Expand Up @@ -135,5 +135,5 @@ func extractBinaryFromTarArchive(archiveFile string, binaryName string, destinat
}
}

return "", errors.New("Binary not found in archive.")
return "", errors.New("Binary not found in archive")
}
47 changes: 27 additions & 20 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"time"
)

// HueBridge represents the Philips Hue bridge in
// your system.
// It is used to communicate with all devices.
type HueBridge struct {
bridge hue.Bridge
bridgeIP string
Expand All @@ -39,6 +42,9 @@ type HueBridge struct {

const hueBridgeAppName = "kelvin"

// InitializeBridge creates and returns an initialized HueBridge.
// If you have a valid configuration this will be used. Otherwise a local
// discovery will be started, followed by a user registration on your bridge.
func InitializeBridge(ip string, username string) (HueBridge, error) {
var bridge HueBridge
if ip != "" && username != "" {
Expand All @@ -47,7 +53,7 @@ func InitializeBridge(ip string, username string) (HueBridge, error) {
bridge.bridgeIP = ip
bridge.username = username

err := bridge.Connect()
err := bridge.connect()
if err != nil {
return bridge, err
}
Expand All @@ -57,17 +63,18 @@ func InitializeBridge(ip string, username string) (HueBridge, error) {

// no known bridge or username
log.Println("⌘ No bridge configuration found. Starting local discovery...")
err := bridge.Discover()
err := bridge.discover()
if err != nil {
return bridge, err
}

return bridge, nil
}

func (self *HueBridge) Lights() ([]Light, error) {
// Lights return all known lights on your bridge.
func (bridge *HueBridge) Lights() ([]Light, error) {
var lights []Light
hueLights, err := self.bridge.GetAllLights()
hueLights, err := bridge.bridge.GetAllLights()
if err != nil {
return lights, err
}
Expand All @@ -86,8 +93,8 @@ func (self *HueBridge) Lights() ([]Light, error) {
return lights, nil
}

func (self *HueBridge) printDevices() error {
lights, err := self.Lights()
func (bridge *HueBridge) printDevices() error {
lights, err := bridge.Lights()
if err != nil {
return err
}
Expand All @@ -106,7 +113,7 @@ func (self *HueBridge) printDevices() error {
return nil
}

func (self *HueBridge) Discover() error {
func (bridge *HueBridge) discover() error {
locators, err := hue.DiscoverBridges(false)
if err != nil {
return err
Expand All @@ -119,36 +126,36 @@ func (self *HueBridge) Discover() error {
time.Sleep(5 * time.Second)
fmt.Printf(".")
// try user creation, will fail if the button wasn't pressed.
bridge, err := locator.CreateUser(hueBridgeAppName)
newBridge, err := locator.CreateUser(hueBridgeAppName)
if err != nil {
return err
}

if bridge.Username != "" {
if newBridge.Username != "" {
// registration successful
fmt.Printf(" Success!\n")

self.bridge = *bridge
self.username = bridge.Username
self.bridgeIP = bridge.IpAddr
bridge.bridge = *newBridge
bridge.username = newBridge.Username
bridge.bridgeIP = newBridge.IpAddr
return nil
}
}
return errors.New("Registration at bridge timed out!")
return errors.New("Registration at bridge timed out")
}

func (self *HueBridge) Connect() error {
if self.bridgeIP == "" {
return errors.New("No bridge IP configured.")
func (bridge *HueBridge) connect() error {
if bridge.bridgeIP == "" {
return errors.New("No bridge IP configured")
}

if self.username == "" {
return errors.New("No username on bridge configured.")
if bridge.username == "" {
return errors.New("No username on bridge configured")
}
self.bridge = *hue.NewBridge(self.bridgeIP, self.username)
bridge.bridge = *hue.NewBridge(bridge.bridgeIP, bridge.username)

// Test bridge
_, err := self.bridge.Search()
_, err := bridge.bridge.Search()
if err != nil {
return err
}
Expand Down
66 changes: 41 additions & 25 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@ import "errors"
import "time"
import "log"

// Bridge respresents the hue bridge in your system.
type Bridge struct {
IP string `json:"ip"`
Username string `json:"username"`
}

// Location represents the geolocation for which sunrise and sunset will be calculated.
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}

// TimedColorTemperature represents a light configuration which will be
// reached at the given time.
type TimedColorTemperature struct {
Time string `json:"time"`
Color int `json:"color"`
Brightness int `json:"brightness"`
}

// Configuration encapsulates all relevant parameters for Kelvin to operate.
type Configuration struct {
ConfigurationFile string `json:"-"`
Bridge Bridge `json:"bridge"`
Expand All @@ -54,13 +59,14 @@ type Configuration struct {
BeforeSunrise []TimedColorTemperature `json:"beforeSunrise"`
}

// TimeStamp represents a parsed and validated TimedColorTemperature.
type TimeStamp struct {
Time time.Time
Color int
Brightness int
}

func (self *Configuration) initializeDefaults() {
func (configuration *Configuration) initializeDefaults() {
var bridge Bridge
bridge.IP = ""
bridge.Username = ""
Expand All @@ -84,15 +90,19 @@ func (self *Configuration) initializeDefaults() {
wakeupTime.Color = 2000
wakeupTime.Brightness = 60

self.ConfigurationFile = "config.json"
self.Bridge = bridge
self.Location = location
self.DefaultColorTemperature = 2750
self.DefaultBrightness = 100
self.AfterSunset = []TimedColorTemperature{tvTime, bedTime}
self.BeforeSunrise = []TimedColorTemperature{wakeupTime}
configuration.ConfigurationFile = "config.json"
configuration.Bridge = bridge
configuration.Location = location
configuration.DefaultColorTemperature = 2750
configuration.DefaultBrightness = 100
configuration.AfterSunset = []TimedColorTemperature{tvTime, bedTime}
configuration.BeforeSunrise = []TimedColorTemperature{wakeupTime}
}

// InitializeConfiguration creates and returns an initialized
// configuration.
// If no configuration can be found on disk, one with default values
// will be created.
func InitializeConfiguration() (Configuration, error) {
var configuration Configuration
configuration.initializeDefaults()
Expand All @@ -113,61 +123,67 @@ func InitializeConfiguration() (Configuration, error) {
return configuration, nil
}

func (self *Configuration) Write() error {
if self.ConfigurationFile == "" {
return errors.New("No configuration filename configured.")
// Write saves a configuration to disk.
func (configuration *Configuration) Write() error {
if configuration.ConfigurationFile == "" {
return errors.New("No configuration filename configured")
}

json, err := json.MarshalIndent(self, "", " ")
json, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
return err
}

err = ioutil.WriteFile(self.ConfigurationFile, json, 0644)
err = ioutil.WriteFile(configuration.ConfigurationFile, json, 0644)
if err != nil {
return err
}

return nil
}

func (self *Configuration) Read() error {
if self.ConfigurationFile == "" {
return errors.New("No configuration filename configured.")
// Read loads a configuration from disk.
func (configuration *Configuration) Read() error {
if configuration.ConfigurationFile == "" {
return errors.New("No configuration filename configured")
}

raw, err := ioutil.ReadFile(self.ConfigurationFile)
raw, err := ioutil.ReadFile(configuration.ConfigurationFile)
if err != nil {
return err
}

err = json.Unmarshal(raw, self)
err = json.Unmarshal(raw, configuration)
if err != nil {
return err
}

return nil
}

func (self *Configuration) Exists() bool {
if self.ConfigurationFile == "" {
// Exists return true if a configuration file is found on disk.
// False otherwise.
func (configuration *Configuration) Exists() bool {
if configuration.ConfigurationFile == "" {
return false
}

if _, err := os.Stat(self.ConfigurationFile); os.IsNotExist(err) {
if _, err := os.Stat(configuration.ConfigurationFile); os.IsNotExist(err) {
return false
}
return true
}

func (self *TimedColorTemperature) AsTimestamp(referenceTime time.Time) (TimeStamp, error) {
// AsTimestamp parses and validates a TimedColorTemperature and returns
// a corresponding TimeStamp.
func (color *TimedColorTemperature) AsTimestamp(referenceTime time.Time) (TimeStamp, error) {
layout := "3:04PM"
t, err := time.Parse(layout, self.Time)
t, err := time.Parse(layout, color.Time)
if err != nil {
return TimeStamp{time.Now(), self.Color, self.Brightness}, err
return TimeStamp{time.Now(), color.Color, color.Brightness}, err
}
yr, mth, day := referenceTime.Date()
targetTime := time.Date(yr, mth, day, t.Hour(), t.Minute(), t.Second(), 0, referenceTime.Location())

return TimeStamp{targetTime, self.Color, self.Brightness}, nil
return TimeStamp{targetTime, color.Color, color.Brightness}, nil
}
Loading

0 comments on commit adb6598

Please sign in to comment.