2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2014-05-23 10:22:14 +08:00
|
|
|
"time"
|
2014-07-10 07:51:00 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/hybridgroup/gobot/platforms/firmata"
|
|
|
|
"github.com/hybridgroup/gobot/platforms/gpio"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TravisResponse struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
PublicKey string `json:"public_key"`
|
|
|
|
LastBuildID int `json:"last_build_id"`
|
|
|
|
LastBuildNumber string `json:"last_build_number"`
|
|
|
|
LastBuildStatus int `json:"last_build_status"`
|
|
|
|
LastBuildResult int `json:"last_build_result"`
|
|
|
|
LastBuildDuration int `json:"last_build_duration"`
|
|
|
|
LastBuildLanguage string `json:"last_build_language"`
|
|
|
|
LastBuildStartedAt string `json:"last_build_started_at"`
|
|
|
|
LastBuildFinishedAt string `json:"last_build_finished_at"`
|
|
|
|
}
|
|
|
|
|
2014-06-14 02:19:01 +08:00
|
|
|
func turnOn(robot *gobot.Robot, device string) {
|
2014-07-09 09:36:14 +08:00
|
|
|
robot.Device(device).(*gpio.LedDriver).On()
|
2014-06-14 02:19:01 +08:00
|
|
|
}
|
2014-07-09 09:36:14 +08:00
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
func resetLeds(robot *gobot.Robot) {
|
2014-07-09 09:36:14 +08:00
|
|
|
robot.Device("red").(*gpio.LedDriver).Off()
|
|
|
|
robot.Device("green").(*gpio.LedDriver).Off()
|
|
|
|
robot.Device("blue").(*gpio.LedDriver).Off()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkTravis(robot *gobot.Robot) {
|
|
|
|
resetLeds(robot)
|
|
|
|
user := "hybridgroup"
|
|
|
|
name := "gobot"
|
|
|
|
//name := "broken-arrow"
|
|
|
|
fmt.Printf("Checking repo %s/%s\n", user, name)
|
2014-06-14 02:19:01 +08:00
|
|
|
turnOn(robot, "blue")
|
2014-04-26 18:11:51 +08:00
|
|
|
resp, err := http.Get(fmt.Sprintf("https://api.travis-ci.org/repos/%s/%s.json", user, name))
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var travis TravisResponse
|
|
|
|
json.Unmarshal(body, &travis)
|
|
|
|
resetLeds(robot)
|
|
|
|
if travis.LastBuildStatus == 0 {
|
2014-06-14 02:19:01 +08:00
|
|
|
turnOn(robot, "green")
|
2014-04-26 18:11:51 +08:00
|
|
|
} else {
|
2014-06-14 02:19:01 +08:00
|
|
|
turnOn(robot, "red")
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-07-09 09:36:14 +08:00
|
|
|
gbot := gobot.NewGobot()
|
|
|
|
|
2014-05-23 10:22:14 +08:00
|
|
|
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
|
2014-07-10 07:51:00 +08:00
|
|
|
red := gpio.NewLedDriver(firmataAdaptor, "red", "7")
|
|
|
|
green := gpio.NewLedDriver(firmataAdaptor, "green", "6")
|
|
|
|
blue := gpio.NewLedDriver(firmataAdaptor, "blue", "5")
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
work := func() {
|
2014-07-09 09:36:14 +08:00
|
|
|
checkTravis(gbot.Robot("travis"))
|
2014-05-23 10:22:14 +08:00
|
|
|
gobot.Every(10*time.Second, func() {
|
2014-07-09 09:36:14 +08:00
|
|
|
checkTravis(gbot.Robot("travis"))
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
robot := gobot.NewRobot("travis",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{red, green, blue},
|
|
|
|
work,
|
2014-06-14 02:19:01 +08:00
|
|
|
)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
gbot.AddRobot(robot)
|
|
|
|
|
|
|
|
gbot.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|