~$26.00
$1.00
~$10.00
#!/usr/bin/env python
import urllib
import json
import sys
import time
import RPi.GPIO as GPIO
import datetime
def main_loop():
while True:
now = datetime.datetime.now()
# If it is after hours, set the signal to low and skip evaluating
if now.hour < 8 or now.hour > 20:
GPIO.output(12, GPIO.LOW)
time.sleep(3600)
continue
try:
resp = urllib.urlopen('http://MYJENKINS_HOST/job/MYJENKINS_JOB/api/json')
if resp.getcode() != 200:
print 'Invalid HTTP response code at ', now.strftime("%Y-%m-%d %H:%M"), str(resp.getcode())
continue
# Sometimes we get invalid responses from Jenkins: ValueError("No JSON object could be decoded")
jsonResp = json.load(resp)
color = jsonResp['color']
# Checking that it does not equal red, vs. checking that it is blue (successful), as it appears the color changes
# when checking when the build is occurring.
if color != 'red':
# Set the signal low
GPIO.output(12, GPIO.LOW)
else:
# The job is failed, spin the light
print 'Jenkins job is failed at ', now.strftime("%Y-%m-%d %H:%M")
GPIO.output(12, GPIO.HIGH)
except Exception, err:
print 'Failed to get JSON from Jenkins at ', now.strftime("%Y-%m-%d %H:%M"), ' with ', str(err)
pass
time.sleep(30)
if __name__ == '__main__':
try:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nShutting down...\n'
GPIO.cleanup()
sys.exit(0)