Last active
August 29, 2015 14:27
-
-
Save voxxit/8200bb1a66d25c681672 to your computer and use it in GitHub Desktop.
Listens for webhooks from PagerDuty, and lights up all lights on the first Hue bridge found on the local network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "os" | |
| "strconv" | |
| "github.com/codegangsta/cli" | |
| "github.com/gin-gonic/gin" | |
| "github.com/mediocregopher/radix.v2/redis" | |
| "github.com/voxxit/go.hue" | |
| ) | |
| var locator *hue.BridgeLocator | |
| // PagerDutyRequest ... | |
| type PagerDutyRequest struct { | |
| Messages []PagerDutyMessage `json:"messages"` | |
| } | |
| // PagerDutyMessage ... | |
| type PagerDutyMessage struct { | |
| Type string `json:"type"` | |
| } | |
| // catchError is the default handler for errors. | |
| func catchError(err error) { | |
| if err != nil { | |
| log.Panic(err) | |
| } | |
| } | |
| // SendAlertToLightGroup ... | |
| func SendAlertToLightGroup(b *hue.Bridge, groupID string, sceneID string) { | |
| b.SetGroupState(groupID, hue.GroupState{ | |
| On: true, | |
| Alert: "lselect", | |
| Scene: sceneID, | |
| }) | |
| } | |
| // GetCount ... | |
| func GetCount(r *redis.Client) int { | |
| resp, err := r.Cmd("GET", "incident:count").Str() | |
| catchError(err) | |
| count, _ := strconv.Atoi(resp) | |
| return count | |
| } | |
| // Server handles new PagerDuty webhooks. Once received, it will start a loop on | |
| // the given group ID using the desired scene (usually one that will wake you up!) | |
| func Server(r *redis.Client, c *cli.Context) { | |
| mux := gin.Default() | |
| // Connect to the Hue bridge | |
| bridge := hue.NewBridge(c.String("addr"), c.String("username")) | |
| // Handles a webhook received from PagerDuty. Starts a color loop on | |
| // Hue if a message with type 'incident.trigger' is found, | |
| // and returns 201 to acknowledge the message. | |
| mux.POST("/hooks/pagerduty/", func(g *gin.Context) { | |
| var json PagerDutyRequest | |
| if g.Bind(&json) == nil { | |
| initialCount := GetCount(r) | |
| for _, message := range json.Messages { | |
| switch message.Type { | |
| case "incident.trigger": | |
| r.Cmd("INCR", "incident:count").Str() | |
| case "incident.resolve": | |
| r.Cmd("DECR", "incident:count").Str() | |
| } | |
| } | |
| currentCount := GetCount(r) | |
| if currentCount == initialCount { | |
| return | |
| } | |
| if currentCount >= 1 { | |
| // Go red! | |
| go SendAlertToLightGroup(bridge, c.String("group"), c.String("alert-scene")) | |
| } else { | |
| // Go green! | |
| go SendAlertToLightGroup(bridge, c.String("group"), c.String("resolved-scene")) | |
| } | |
| g.JSON(201, nil) | |
| } else { | |
| g.JSON(400, gin.H{"error": "Unable to parse request"}) | |
| } | |
| }) | |
| // Listen and serve on 0.0.0.0:8080 | |
| mux.Run(":8080") | |
| } | |
| // Register discovers bridges running on the local network, | |
| // selects the first one and registers the 'PagerDuty' | |
| // user. Returns a Bridge{} struct to use in subsequent | |
| // requests. | |
| func Register(r *redis.Client) { | |
| locators, err := hue.DiscoverBridges(false) | |
| catchError(err) | |
| if len(locators) == 0 { | |
| log.Fatal("No Hue bridges found!") | |
| } | |
| locator := locators[0] | |
| bridge, err := locator.CreateUser("PagerDuty") | |
| catchError(err) | |
| if bridge.Username == "" { | |
| log.Fatal("Username is blank; did you remember to press the button on your Hue bridge?") | |
| } | |
| fmt.Println(bridge) | |
| } | |
| func main() { | |
| r, err := redis.Dial("tcp", ":6379") | |
| catchError(err) | |
| defer r.Close() | |
| r.Cmd("SET", "incident:count", 0).Str() | |
| c := cli.NewApp() | |
| c.Name = "hueduty" | |
| c.Usage = "Register with a Philips Hue bridge, and then listen for incidents and alert using lights!" | |
| c.Version = "0.1.0" | |
| c.Commands = []cli.Command{ | |
| cli.Command{ | |
| Name: "register", | |
| Usage: "Find and register with a local Hue bridge", | |
| Description: "NOTE: Remember to push the button on your Hue bridge -before- executing this command!", | |
| Action: func(c *cli.Context) { | |
| Register(r) | |
| }, | |
| }, | |
| cli.Command{ | |
| Name: "server", | |
| Usage: "Starts a server to listen to PagerDuty incidents, and starts a disco if triggered", | |
| Flags: []cli.Flag{ | |
| cli.StringFlag{ | |
| Name: "addr", | |
| Usage: "Hue bridge IP address", | |
| EnvVar: "HUE_ADDR", | |
| }, | |
| cli.StringFlag{ | |
| Name: "username", | |
| Usage: "Hue bridge username", | |
| EnvVar: "HUE_USERNAME", | |
| }, | |
| cli.StringFlag{ | |
| Name: "group", | |
| Usage: "Hue light group ID to trigger", | |
| EnvVar: "HUE_GROUP", | |
| }, | |
| cli.StringFlag{ | |
| Name: "alert-scene", | |
| Usage: "Hue scene ID when alerts are triggered", | |
| EnvVar: "HUE_ALERT_SCENE", | |
| }, | |
| cli.StringFlag{ | |
| Name: "resolved-scene", | |
| Usage: "Hue scene ID when all alerts are resolved", | |
| EnvVar: "HUE_RESOLVED_SCENE", | |
| }, | |
| }, | |
| Action: func(c *cli.Context) { | |
| Server(r, c) | |
| }, | |
| }, | |
| } | |
| c.Run(os.Args) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment