2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-07-13 08:08:46 +08:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass serial port to use as the first param:
|
|
|
|
|
|
|
|
go run examples/firmata_travis.go /dev/ttyACM0
|
|
|
|
*/
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-11 01:25:01 +08:00
|
|
|
"io"
|
2014-04-26 18:11:51 +08:00
|
|
|
"net/http"
|
2017-07-13 08:08:46 +08:00
|
|
|
"os"
|
2014-05-23 10:22:14 +08:00
|
|
|
"time"
|
2014-07-10 07:51:00 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/firmata"
|
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) {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Device(device).(*gpio.LedDriver).On(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
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) {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Device("red").(*gpio.LedDriver).Off(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
if err := robot.Device("green").(*gpio.LedDriver).Off(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
if err := robot.Device("blue").(*gpio.LedDriver).Off(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkTravis(robot *gobot.Robot) {
|
|
|
|
resetLeds(robot)
|
|
|
|
user := "hybridgroup"
|
|
|
|
name := "gobot"
|
2023-10-21 02:50:42 +08:00
|
|
|
// name := "broken-arrow"
|
2014-04-26 18:11:51 +08:00
|
|
|
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))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-04 18:36:20 +08:00
|
|
|
defer resp.Body.Close()
|
2023-06-11 01:25:01 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var travis TravisResponse
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := json.Unmarshal(body, &travis); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
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() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
2017-07-13 08:08:46 +08:00
|
|
|
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
|
2023-12-04 01:03:02 +08:00
|
|
|
red := gpio.NewLedDriver(firmataAdaptor, "7", gpio.WithName("red"))
|
|
|
|
green := gpio.NewLedDriver(firmataAdaptor, "6", gpio.WithName("green"))
|
|
|
|
blue := gpio.NewLedDriver(firmataAdaptor, "5", gpio.WithName("blue"))
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
work := func() {
|
2024-02-13 23:30:25 +08:00
|
|
|
checkTravis(manager.Robot("travis"))
|
2014-05-23 10:22:14 +08:00
|
|
|
gobot.Every(10*time.Second, func() {
|
2024-02-13 23:30:25 +08:00
|
|
|
checkTravis(manager.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
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddRobot(robot)
|
|
|
|
if err := manager.Start(); err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|