Skip to content

Commit b61b6fa

Browse files
committed
Merge branch 'master' into model
2 parents 5420e0d + 8d2ad61 commit b61b6fa

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

config.json.sample

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"host": "host from jenkins",
44
"port": port from jenkins,
55
"path": "/api/json",
6+
"pathNova": "/job/s16-backend-nova/api/json?tree=activeConfigurations[name,color,url]",
67
"user": "the user",
7-
"password": "the passwort of the user"
8+
"password": "the passwort of the user",
9+
"useLibrary": "pcLamp.js",
10+
"delay": 10000,
11+
"ignore": ["job-name-to-exclude"]
812
}
913
}

jenkinsLamp.js

+84-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
var config = require('./config.json');
22
var async = require("async");
3+
var clear = require('clear');
34
const util = require('util');
45
const https = require('https');
56

6-
var delay = 30000;
7-
var Lamp = require("./pcLamp.js");
8-
//var Lamp = require("./raspberryPiLamp.js");
7+
clear();
8+
9+
// config
10+
var delay = getConfigValue(config.jenkins.delay, 60000)
11+
var ignoreJobs = getConfigValue(config.jenkins.ignores, [""]);
12+
var useLibrary = getConfigValue(config.jenkins.useLibrary, "pcLamp.js")
13+
console.log('=> delay is ' + delay + 'ms');
14+
console.log('=> ignore jobs: ' + ignoreJobs);
15+
console.log('=> use the output library: ' + useLibrary);
16+
17+
// select library
18+
var Lamp = require("./" + useLibrary);
919

1020
var LampState = {
1121
ON: 1,
1222
OFF: 2,
1323
BLINK: 3
1424
};
1525

26+
function getConfigValue(value, defaultValue) {
27+
if(value === undefined ) {
28+
return defaultValue;
29+
};
30+
return value;
31+
}
32+
1633
function LampData() {
1734
var red, orange, green;
1835
this.red = LampState.OFF;
@@ -21,11 +38,12 @@ function LampData() {
2138
}
2239

2340
function JenkinsLamp() {
24-
var jenkinsData, lampState;
41+
var jenkinsData, lampState, firstLoop;
2542

2643
// initialize data
2744
this.jenkinsData = {};
2845
this.lampData = new LampData();
46+
this.firstLoop = true;
2947

3048
// initialize Lamp
3149
this.Lamp = new Lamp();
@@ -65,7 +83,8 @@ JenkinsLamp.prototype.callJenkins = function() {
6583
if (jsonData && jsonData.jobs) {
6684
for (let jobIndex in jsonData.jobs) {
6785
let job = jsonData.jobs[jobIndex];
68-
console.log('job : ' + job.name + ' => ' + job.color);
86+
let state = this.colorify(job.color);
87+
console.log(job.name + ' : ' + state);
6988
}
7089
}
7190
this.saveJenkinsData(jsonData);
@@ -74,7 +93,47 @@ JenkinsLamp.prototype.callJenkins = function() {
7493
}).on('error', (e) => {
7594
console.error(e);
7695
});
77-
};
96+
}
97+
98+
JenkinsLamp.prototype.callJenkinsNova = function() {
99+
let options = {
100+
host: config.jenkins.host,
101+
port: config.jenkins.port,
102+
path: config.jenkins.pathNova,
103+
headers: {
104+
'Authorization': 'Basic ' + new Buffer(config.jenkins.user + ':' + config.jenkins.password).toString('base64')
105+
}
106+
};
107+
https.get(options, (res) => {
108+
res.on('data', (d) => {
109+
let jsonData = JSON.parse(d);
110+
//console.log(JSON.stringify(jsonData));
111+
if (jsonData && jsonData.activeConfigurations) {
112+
console.log('------------------------------');
113+
for (let configIndex in jsonData.activeConfigurations) {
114+
let config = jsonData.activeConfigurations[configIndex];
115+
let state = this.colorify(config.color);
116+
console.log(config.name + ' : ' + state);
117+
}
118+
console.log('------------------------------');
119+
}
120+
//this.saveJenkinsData(jsonData);
121+
});
122+
123+
}).on('error', (e) => {
124+
console.error(e);
125+
});
126+
}
127+
128+
JenkinsLamp.prototype.colorify = function(color, text=color) {
129+
if(color.startsWith('red'))
130+
return '\x1b[31m' + text + '\x1b[0m';
131+
if(color.startsWith('yellow'))
132+
return '\x1b[33m' + text + '\x1b[0m';
133+
if(color.startsWith('blue'))
134+
return '\x1b[34m' + text + '\x1b[0m';
135+
return text;
136+
}
78137

79138
JenkinsLamp.prototype.saveJenkinsData = function(jenkinsData) {
80139
// TODO: validate jenkinsData before saveJenkinsData
@@ -84,9 +143,10 @@ JenkinsLamp.prototype.saveJenkinsData = function(jenkinsData) {
84143
}
85144

86145
JenkinsLamp.prototype.processJenkinsData = function() {
87-
console.log('processJenkinsData');
88146
if (this.jenkinsData && this.jenkinsData.jobs) {
89-
let jobs = this.jenkinsData.jobs;
147+
let jobs = this.jenkinsData.jobs.filter(function(job) {
148+
return config.jenkins.ignore.indexOf(job.name) == -1;
149+
});
90150

91151
// disable all lamps
92152
this.lampData.red = LampState.OFF;
@@ -131,8 +191,7 @@ JenkinsLamp.prototype.hasAnimeJob = function(jobs) {
131191
}
132192

133193
JenkinsLamp.prototype.updateLamp = function() {
134-
console.log('updateLamp');
135-
194+
console.log(' ˏ__ˎ');
136195
// red lamp
137196
if (this.lampData.red === LampState.ON) {
138197
this.Lamp.enableRed();
@@ -153,18 +212,30 @@ JenkinsLamp.prototype.updateLamp = function() {
153212
} else {
154213
this.Lamp.disableGreen();
155214
}
215+
console.log(' \\ˉˉ/');
216+
console.log(' ⎞⎛');
156217
}
157218

158219
JenkinsLamp.prototype.work = function() {
159220
let self = this;
160-
async.forever(
161221

222+
async.forever(
162223
function(next) {
163-
self.callJenkins();
224+
if (self.firstLoop) {
225+
self.firstLoop = false;
226+
} else {
227+
clear();
228+
}
229+
230+
self.callJenkinsNova();
231+
232+
setTimeout(function() {
233+
self.callJenkins();
234+
}, 150);
164235

165236
setTimeout(function() {
166237
next();
167-
}, delay)
238+
}, delay);
168239
},
169240
function(err) {
170241
console.error(err);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "test app",
55
"dependencies": {
66
"onoff": "latest",
7-
"async": "latest"
7+
"async": "latest",
8+
"clear": "latest"
89
},
910
"author": "Jonas Schmid"
1011
}

pcLamp.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Led.prototype.disable = function() {
2323
}
2424
Led.prototype.printLed = function() {
2525
if(this.state === 0) {
26-
console.log(' ');
26+
console.log(' |○ |');
2727
} else {
28-
console.log(this.color, '', '\x1b[0m');
28+
console.log(this.color, '|◉ |', '\x1b[0m');
2929
}
3030
}
3131

0 commit comments

Comments
 (0)