-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsos-client.ts
237 lines (217 loc) · 7.77 KB
/
sos-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/// <reference path="./external-ts-definitions/node.d.ts" />
/// <reference path="./external-ts-definitions/async.d.ts" />
/// <reference path="./external-ts-definitions/sos-device.d.ts" />
import fs = require('fs');
import async = require('async');
import sos = require('sos-device');
import PluginBase = require('./plugin');
import Bamboo = require('./plugins/bamboo');
import Jenkins = require('./plugins/jenkins');
import TeamCity = require('./plugins/teamcity');
var useMockDevice: boolean = false;
interface IConfigBuild {
name: string;
type: string;
interval: number;
config?: any;
plugin: PluginBase.PluginBase;
lastPollResult?: PluginBase.PollResult;
}
interface IConfig {
builds: IConfigBuild[];
}
interface IStartupData {
config: IConfig;
sosDevice: SosDevice;
sosDeviceInfo: SosDeviceAllInfo;
}
function run(callback: (err: Error) => void): void {
async.auto({
config: readConfig,
sosDevice: connectToDevice,
sosDeviceInfo: ['sosDevice', getSosDeviceInfo],
poll: ['config', 'sosDeviceInfo', poll]
}, callback);
}
function poll(callback: (err?: Error) => void, startupData: IStartupData): void {
var nameId: number = 0;
startupData.config.builds.forEach((build) => {
build.interval = build.interval || 30000;
build.name = build.name || (build.type + (nameId++));
build.lastPollResult = build.lastPollResult || {
status: PluginBase.PollResultStatus.SUCCESS,
id: '0'
};
switch(build.type) {
case 'bamboo':
build.plugin = new Bamboo.Bamboo();
break;
case 'jenkins':
build.plugin = new Jenkins.Jenkins();
break;
case 'teamcity':
build.plugin = new TeamCity.TeamCity();
break;
default:
return callback(new Error("Invalid build type: " + build.type));
}
process.nextTick(pollBuild.bind(null, build, startupData));
});
return callback();
}
function pollBuild(build: IConfigBuild, startupData: IStartupData): void {
//console.log('polling build:', build.name);
build.plugin.poll(build.config, (err?, pollResult?: PluginBase.PollResult) => {
if(err) {
console.error('Failed to poll: ' + build.name, err);
}
if(pollResult) {
if(pollResult.status !== build.lastPollResult.status
|| pollResult.id !== build.lastPollResult.id) {
build.lastPollResult = pollResult;
console.log('New poll results:', pollResult, build.lastPollResult);
updateSiren(startupData, build.lastPollResult);
}
}
setTimeout(pollBuild.bind(null, build, startupData), build.interval);
});
}
function getOnSuccessConfigOrDefault(startupData: any) {
if (startupData.config.onSuccess) {
return startupData.config.onSuccess;
}
return {
"audioPatternIndex": 1,
"audioDuration": 500,
"ledPatternIndex": 0,
"ledPlayDuration": 500
};
}
function getOnFailConfigOrDefault(startupData: any) {
if (startupData.config.onFail) {
return startupData.config.onFail;
}
return {
"audioPatternIndex": 0,
"audioDuration": 1000,
"ledPatternIndex": 0,
"ledPlayDuration": 5000
};
}
function getMode(patterns, patternIndex?: number)
{
if (patternIndex === null) return null;
return patterns[patternIndex].id;
}
function playConfig(sosDeviceInfo, playConfig, sosDevice) {
var controlPacket = {
audioMode: getMode(sosDeviceInfo.audioPatterns, playConfig.audioPatternIndex),
audioPlayDuration: playConfig.audioDuration,
ledMode: getMode(sosDeviceInfo.ledPatterns, playConfig.ledPatternIndex),
ledPlayDuration: playConfig.ledPlayDuration
};
console.log(controlPacket);
sosDevice.sendControlPacket(controlPacket, (err?) => {
if (err) {
console.error("Could not send SoS control packet", err);
}
});
}
function updateSiren(startupData : IStartupData, pollResult: PluginBase.PollResult) {
var sosDevice = startupData.sosDevice;
var sosDeviceInfo = startupData.sosDeviceInfo;
if(pollResult.status === PluginBase.PollResultStatus.FAILURE) {
var onFailConfig = getOnFailConfigOrDefault(startupData);
playConfig(sosDeviceInfo, onFailConfig, sosDevice);
} else if(pollResult.status === PluginBase.PollResultStatus.SUCCESS) {
var onSuccessConfig = getOnSuccessConfigOrDefault(startupData);
playConfig(sosDeviceInfo, onSuccessConfig, sosDevice);
}
}
function readConfig(callback: (err: Error, config?: IConfig) => void): void {
fs.readFile('./config.json', 'utf8', (err, data) => {
if(err) {
return callback(err);
}
var config: IConfig = JSON.parse(data);
return callback(null, config);
});
}
function connectToDevice(callback: (err: Error, sosDevice?: SosDevice) => void): void {
if(useMockDevice) {
return callback(null, {
readInfo: (callback: (err?: Error, deviceInfo?: SosDeviceInfo) => void) => {
console.log("MOCK SoS: readInfo:");
return callback(null, {
audioMode: 0,
audioPlayDuration: 0,
externalMemorySize: 0,
hardwareType: 0,
hardwareVersion: 0,
ledMode: 0,
ledPlayDuration: 0,
version: 0
});
},
readAllInfo(callback: (err?: Error, deviceInfo?: SosDeviceAllInfo) => void) {
console.log("MOCK SoS: readInfo:");
return callback(null, {
audioMode: 0,
audioPlayDuration: 0,
externalMemorySize: 0,
hardwareType: 0,
hardwareVersion: 0,
ledMode: 0,
ledPlayDuration: 0,
version: 0,
ledPatterns: [
{ id: 1, name: 'led1' },
{ id: 2, name: 'led2' }
],
audioPatterns: [
{ id: 1, name: 'audio1' },
{ id: 2, name: 'audio2' }
]
});
},
sendControlPacket(controlPacket: SosDeviceControlPacket, callback?: (err?: Error) => void) {
console.log("MOCK SoS: sendControlPacket:", controlPacket);
return callback();
},
readLedPatterns(callback: (err?: Error, ledPatters?: SosDeviceLedPattern[]) => void) {
return callback(null, [
{ id: 1, name: 'led1' },
{ id: 2, name: 'led2' }
]);
},
readAudioPatterns(callback: (err?: Error, audioPatters?: SosDeviceAudioPattern[]) => void) {
return callback(null, [
{ id: 1, name: 'audio1' },
{ id: 2, name: 'audio2' }
]);
}
});
}
return sos.connect((err?, sosDevice?) => {
if(err) {
return callback(err);
}
return callback(null, sosDevice);
});
}
function getSosDeviceInfo(callback: (err?: Error, sosDeviceInfo?: SosDeviceAllInfo) => void, startupData: IStartupData): void {
return startupData.sosDevice.readAllInfo((err?, deviceInfo?) => {
if(err) {
return callback(err);
}
console.log("deviceInfo:", deviceInfo);
return callback(null, deviceInfo);
});
}
run(err => {
if(err) {
console.error(err);
return process.exit(-1);
}
console.log("startup successful");
});