Last active
March 10, 2017 11:26
-
-
Save yenjoji/f29553357d3a28566e7e to your computer and use it in GitHub Desktop.
Bamboo XFD
This file contains 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
@Grab('com.pi4j:pi4j-core:0.0.5') | |
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1') | |
import com.pi4j.io.gpio.GpioFactory | |
import com.pi4j.io.gpio.Pin | |
import com.pi4j.io.gpio.PinState | |
import com.pi4j.io.gpio.RaspiPin | |
import groovyx.net.http.HttpResponseException | |
import groovyx.net.http.RESTClient | |
import org.apache.http.HttpRequest | |
import org.apache.http.HttpRequestInterceptor | |
import org.apache.http.protocol.HttpContext | |
public class BambooFlasher implements Runnable { | |
static int INTERVAL_IN_SEC = 15 | |
def gpiopins = [:] | |
// cange to your environment | |
def bambooUrl = "http://192.168.33.10:8085/rest/api/latest/" | |
def uid = "bamboo" | |
def pass = "bamboo" | |
def projectKey = "PROJECTKEY" | |
def branchName = "branch_name" | |
// cange to your environment | |
public BambooFlasher() { | |
} | |
public BambooFlasher(String pk, String bn) { | |
this.projectKey = pk | |
this.branchName = bn | |
} | |
public RESTClient getBambooClient() { | |
def bambooClient = new RESTClient("${bambooUrl}") | |
bambooClient.auth.basic(uid, pass) | |
bambooClient.ignoreSSLIssues() | |
bambooClient.client.addRequestInterceptor( | |
new HttpRequestInterceptor() { | |
void process(HttpRequest httpRequest, | |
HttpContext httpContext) { | |
httpRequest.addHeader('Authorization', 'Basic ' + "${uid}:${pass}".bytes.encodeBase64().toString()) | |
} | |
}) | |
return bambooClient; | |
} | |
public boolean getBuildState(String proj, String branch) { | |
println "get build state project: ${proj}, branch: ${branch}" | |
def bambooClient = getBambooClient() | |
def plans = bambooClient.get(path: "result/${proj}.json") | |
def state = true | |
plans.data.results.result.each { | |
try { | |
def branches = bambooClient.get(path: "result/${it.plan.key}/branch/${branch}.json") | |
def latestBuild = branches.data.results.result.max { it.buildNumber } | |
println "${latestBuild.buildResultKey} : ${latestBuild.buildState}" | |
if (latestBuild.buildState == "Failed") { | |
state = false | |
} | |
} catch (HttpResponseException e) { | |
} | |
} | |
return state | |
} | |
public void changeLedState(Pin pin, PinState state) { | |
def gpioController = GpioFactory.getInstance() | |
def gpiopin | |
def pinName = pin.getName() | |
if (gpiopins[pinName] == null) { | |
gpiopin = gpioController.provisionDigitalOutputPin(pin, state) | |
gpiopins[pinName] = gpiopin | |
} else { | |
gpiopin = gpiopins[pinName] | |
} | |
gpiopin.setState(state); | |
gpioController.shutdown() | |
} | |
@Override | |
void run() { | |
while (true) { | |
def result = this.getBuildState(projectKey, branchName); | |
if (result) { | |
println "normal" | |
this.changeLedState(RaspiPin.GPIO_04, PinState.LOW) | |
} else { | |
println "error" | |
this.changeLedState(RaspiPin.GPIO_04, PinState.HIGH) | |
} | |
sleep(INTERVAL_IN_SEC * 1000); | |
} | |
} | |
public static void main(String[] args) { | |
def flasher | |
if (args.length == 2) { | |
flasher = new BambooFlasher(args[0], args[1]); | |
} else { | |
flasher = new BambooFlasher() | |
} | |
def t = new Thread(flasher) | |
t.start() | |
t.join() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment