Created
October 8, 2010 10:43
-
-
Save raldred/616615 to your computer and use it in GitHub Desktop.
arduino_hudson_status
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
#include <Ethernet.h> | |
#include <WString.h> | |
int red = 6; | |
int yellow = 7; | |
int green = 8; | |
byte mac[] = { 0x00, 0x0C, 0x29, 0x8F, 0xA9, 0xE5 }; | |
byte ip[] = { 192,168,3,244 }; | |
byte gateway[] = { 192,168,3,1 }; | |
byte subnet[] = { 255,255,255,0 }; | |
byte server[] = { 192,168,3,15 }; // kanban | |
#define maxResponseLength 1000 | |
String response = String(maxResponseLength); | |
int notConnectedMode = 0; | |
int connectedMode = 1; | |
int mode = 0; | |
int pingInterval = 10 * 1000; //10 seconds | |
long maxLightOnTime = 300000; //5 minutes | |
unsigned long lastPingTime = 0; | |
unsigned long lastBuild = 0; | |
int currentLight = yellow; | |
Client client(server, 8080); | |
void setup() | |
{ | |
pinMode(red, OUTPUT); | |
pinMode(yellow, OUTPUT); | |
pinMode(green, OUTPUT); | |
delay(500); | |
Ethernet.begin(mac, ip, gateway, subnet); | |
Serial.begin(9600); | |
delay(1000); | |
} | |
void loop() | |
{ | |
watchBuildStateTimeout(); | |
if((millis() - lastPingTime) >= pingInterval){ | |
checkHudson(); | |
} else { | |
flashIfBuilding(); | |
} | |
} | |
void flashIfBuilding() { | |
if((currentLight == yellow) && (mode == notConnectedMode)) { | |
digitalWrite(currentLight, HIGH); | |
delay(1000); | |
digitalWrite(currentLight, LOW); | |
delay(1000); | |
lastBuild = millis(); | |
// Serial.println("current Light is Building"); | |
} | |
} | |
void watchBuildStateTimeout() { | |
if((currentLight != yellow) && (mode == notConnectedMode)) { | |
digitalWrite(currentLight, HIGH); | |
} | |
} | |
void checkHudson(){ | |
if(mode == notConnectedMode){ | |
// try to connect | |
Serial.println("connecting..."); | |
if (client.connect()) { | |
Serial.println("connected"); | |
client.println("GET /api/xml?xpath=//job/name[.='office-kitten-multi-threaded']/following-sibling::color HTTP/1.0"); | |
client.println(); | |
mode = connectedMode; | |
} | |
else { | |
Serial.println("connection failed"); | |
delay(2000); | |
Serial.println("trying again..."); | |
} | |
} | |
// in connectedMode | |
else { | |
if (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
response.append(c); | |
} | |
if (!client.connected()) { | |
lastPingTime = millis(); | |
if(response.contains("anime")){ | |
Serial.println("YELLOW - 412"); | |
currentLight = yellow; | |
digitalWrite(red, LOW); | |
digitalWrite(green, LOW); | |
} | |
else if(response.contains("<color>blue") || response.contains("<color>green")){ | |
Serial.println("GREEN - 200"); | |
currentLight = green; | |
digitalWrite(red, LOW); | |
digitalWrite(yellow, LOW); | |
} | |
else if(response.contains("<color>")) { | |
Serial.println("RED - 412"); | |
currentLight = red; | |
digitalWrite(yellow, LOW); | |
digitalWrite(green, LOW); | |
} | |
// Serial.println(); | |
Serial.println("disconnecting."); | |
client.flush(); | |
client.stop(); | |
response = ""; | |
mode = notConnectedMode; | |
delay(500); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment