gobot.io/x/gobot@v1.16.0/examples/firmata_travis.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7   Pass serial port to use as the first param:
     8  
     9  	go run examples/firmata_travis.go /dev/ttyACM0
    10  */
    11  
    12  package main
    13  
    14  import (
    15  	"encoding/json"
    16  	"fmt"
    17  	"io/ioutil"
    18  	"net/http"
    19  	"os"
    20  	"time"
    21  
    22  	"gobot.io/x/gobot"
    23  	"gobot.io/x/gobot/drivers/gpio"
    24  	"gobot.io/x/gobot/platforms/firmata"
    25  )
    26  
    27  type TravisResponse struct {
    28  	ID                  int    `json:"id"`
    29  	Slug                string `json:"slug"`
    30  	Description         string `json:"description"`
    31  	PublicKey           string `json:"public_key"`
    32  	LastBuildID         int    `json:"last_build_id"`
    33  	LastBuildNumber     string `json:"last_build_number"`
    34  	LastBuildStatus     int    `json:"last_build_status"`
    35  	LastBuildResult     int    `json:"last_build_result"`
    36  	LastBuildDuration   int    `json:"last_build_duration"`
    37  	LastBuildLanguage   string `json:"last_build_language"`
    38  	LastBuildStartedAt  string `json:"last_build_started_at"`
    39  	LastBuildFinishedAt string `json:"last_build_finished_at"`
    40  }
    41  
    42  func turnOn(robot *gobot.Robot, device string) {
    43  	robot.Device(device).(*gpio.LedDriver).On()
    44  }
    45  
    46  func resetLeds(robot *gobot.Robot) {
    47  	robot.Device("red").(*gpio.LedDriver).Off()
    48  	robot.Device("green").(*gpio.LedDriver).Off()
    49  	robot.Device("blue").(*gpio.LedDriver).Off()
    50  }
    51  
    52  func checkTravis(robot *gobot.Robot) {
    53  	resetLeds(robot)
    54  	user := "hybridgroup"
    55  	name := "gobot"
    56  	//name := "broken-arrow"
    57  	fmt.Printf("Checking repo %s/%s\n", user, name)
    58  	turnOn(robot, "blue")
    59  	resp, err := http.Get(fmt.Sprintf("https://api.travis-ci.org/repos/%s/%s.json", user, name))
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	defer resp.Body.Close()
    64  	body, err := ioutil.ReadAll(resp.Body)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	var travis TravisResponse
    69  	json.Unmarshal(body, &travis)
    70  	resetLeds(robot)
    71  	if travis.LastBuildStatus == 0 {
    72  		turnOn(robot, "green")
    73  	} else {
    74  		turnOn(robot, "red")
    75  	}
    76  }
    77  
    78  func main() {
    79  	master := gobot.NewMaster()
    80  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    81  	red := gpio.NewLedDriver(firmataAdaptor, "7")
    82  	red.SetName("red")
    83  	green := gpio.NewLedDriver(firmataAdaptor, "6")
    84  	green.SetName("green")
    85  	blue := gpio.NewLedDriver(firmataAdaptor, "5")
    86  	blue.SetName("blue")
    87  
    88  	work := func() {
    89  		checkTravis(master.Robot("travis"))
    90  		gobot.Every(10*time.Second, func() {
    91  			checkTravis(master.Robot("travis"))
    92  		})
    93  	}
    94  
    95  	robot := gobot.NewRobot("travis",
    96  		[]gobot.Connection{firmataAdaptor},
    97  		[]gobot.Device{red, green, blue},
    98  		work,
    99  	)
   100  
   101  	master.AddRobot(robot)
   102  	master.Start()
   103  }