1
1
var config = require ( './config.json' ) ;
2
2
var async = require ( "async" ) ;
3
+ var clear = require ( 'clear' ) ;
3
4
const util = require ( 'util' ) ;
4
5
const https = require ( 'https' ) ;
5
6
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 ) ;
9
19
10
20
var LampState = {
11
21
ON : 1 ,
12
22
OFF : 2 ,
13
23
BLINK : 3
14
24
} ;
15
25
26
+ function getConfigValue ( value , defaultValue ) {
27
+ if ( value === undefined ) {
28
+ return defaultValue ;
29
+ } ;
30
+ return value ;
31
+ }
32
+
16
33
function LampData ( ) {
17
34
var red , orange , green ;
18
35
this . red = LampState . OFF ;
@@ -21,11 +38,12 @@ function LampData() {
21
38
}
22
39
23
40
function JenkinsLamp ( ) {
24
- var jenkinsData , lampState ;
41
+ var jenkinsData , lampState , firstLoop ;
25
42
26
43
// initialize data
27
44
this . jenkinsData = { } ;
28
45
this . lampData = new LampData ( ) ;
46
+ this . firstLoop = true ;
29
47
30
48
// initialize Lamp
31
49
this . Lamp = new Lamp ( ) ;
@@ -65,7 +83,8 @@ JenkinsLamp.prototype.callJenkins = function() {
65
83
if ( jsonData && jsonData . jobs ) {
66
84
for ( let jobIndex in jsonData . jobs ) {
67
85
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 ) ;
69
88
}
70
89
}
71
90
this . saveJenkinsData ( jsonData ) ;
@@ -74,7 +93,47 @@ JenkinsLamp.prototype.callJenkins = function() {
74
93
} ) . on ( 'error' , ( e ) => {
75
94
console . error ( e ) ;
76
95
} ) ;
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
+ }
78
137
79
138
JenkinsLamp . prototype . saveJenkinsData = function ( jenkinsData ) {
80
139
// TODO: validate jenkinsData before saveJenkinsData
@@ -84,9 +143,10 @@ JenkinsLamp.prototype.saveJenkinsData = function(jenkinsData) {
84
143
}
85
144
86
145
JenkinsLamp . prototype . processJenkinsData = function ( ) {
87
- console . log ( 'processJenkinsData' ) ;
88
146
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
+ } ) ;
90
150
91
151
// disable all lamps
92
152
this . lampData . red = LampState . OFF ;
@@ -131,8 +191,7 @@ JenkinsLamp.prototype.hasAnimeJob = function(jobs) {
131
191
}
132
192
133
193
JenkinsLamp . prototype . updateLamp = function ( ) {
134
- console . log ( 'updateLamp' ) ;
135
-
194
+ console . log ( ' ˏ__ˎ' ) ;
136
195
// red lamp
137
196
if ( this . lampData . red === LampState . ON ) {
138
197
this . Lamp . enableRed ( ) ;
@@ -153,18 +212,30 @@ JenkinsLamp.prototype.updateLamp = function() {
153
212
} else {
154
213
this . Lamp . disableGreen ( ) ;
155
214
}
215
+ console . log ( ' \\ˉˉ/' ) ;
216
+ console . log ( ' ⎞⎛' ) ;
156
217
}
157
218
158
219
JenkinsLamp . prototype . work = function ( ) {
159
220
let self = this ;
160
- async . forever (
161
221
222
+ async . forever (
162
223
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 ) ;
164
235
165
236
setTimeout ( function ( ) {
166
237
next ( ) ;
167
- } , delay )
238
+ } , delay ) ;
168
239
} ,
169
240
function ( err ) {
170
241
console . error ( err ) ;
0 commit comments