Skip to content

Commit

Permalink
Print sensors, configure polling interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Perlet committed Apr 29, 2019
1 parent 80056db commit 68c9938
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
## Example

```
./huevent -h
huevent - get events from buttons and sensors
Usage: ./huevent [OPTIONS]
-config string
path to config file (default "/home/mathias/.huevent/config.json")
path to config file (default "~/.huevent/huevent.yml")
-debug
enable some debug output
-exit
exit on event
-pair
pair hue bridge
-sensors
print sensors response and exit
```

```
Expand Down Expand Up @@ -98,6 +99,14 @@ hooks:
deviceFilter: []
```

> The default Hue-Bridge polling rate is 333ms. You can set a *> 1ms* rate via
```
config:
ip: 192.168....
user: nEh...
pollingRateMs: 200
```

### Manual Pairing

```
Expand Down
25 changes: 23 additions & 2 deletions huevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ type config struct {
shouldExit bool
hooks *[]Hook
DEBUG bool
printSensors bool
}

type HueventConfig struct {
Config struct {
Ip string `yaml:"ip"`
User string `yaml:"user"`
Rate int `yaml:"pollingRateMs,omitempty"`
} `yaml:"config"`
Hooks []Hook `yaml:"hooks"`
DeviceFilter []string `yaml:"deviceFilter"`
Expand Down Expand Up @@ -159,6 +161,7 @@ func makeConfig() config {

exitOnEvent := flag.Bool("exit", false, "exit on event")
hueventConfigPath := flag.String("config", configPath(), "path to config file")
printSensors := flag.Bool("sensors", false, "print sensors response and exit")
debug := flag.Bool("debug", false, "enable some debug output")

pair := flag.Bool("pair", false, "pair hue bridge")
Expand All @@ -182,6 +185,16 @@ func makeConfig() config {
fmt.Printf("Error: no Hue-Bridge configured at %s, run huevent with -pair to configure\n", *hueventConfigPath)
os.Exit(1)
}

var pollTimeMs = time.Duration(333)

if hueventConfig.Config.Rate > 0 {
pollTimeMs = time.Duration(hueventConfig.Config.Rate)
}

if *debug {
fmt.Printf("Polling-Rate: %s\n", pollTimeMs * time.Millisecond)
}

stateMap := make(map[string]map[string]string)
var hasFilter = false
Expand All @@ -199,11 +212,12 @@ func makeConfig() config {
stateMap: &stateMap,
responseMap: &responseMap,
hasFilter: hasFilter,
pollTimeMs: 333,
pollTimeMs: pollTimeMs,
logHTTPError: true,
hooks: &hueventConfig.Hooks,
shouldExit: *exitOnEvent,
DEBUG: *debug}
DEBUG: *debug,
printSensors: *printSensors}
}

func poll(conf *config) {
Expand All @@ -229,6 +243,13 @@ func poll(conf *config) {
if !conf.logHTTPError {
conf.logHTTPError = true
}

if conf.printSensors {
var prettyJSON bytes.Buffer
_ = json.Indent(&prettyJSON, body, "", "\t")
fmt.Printf("%s\n", prettyJSON.String())
os.Exit(0)
}
}

func exit(device string, eventType string, triggerOn string, conf *config) {
Expand Down

0 comments on commit 68c9938

Please sign in to comment.