gobot.io/x/gobot/v2@v2.1.0/examples/firmata_travis.go (about)

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