-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
289 lines (256 loc) · 8.55 KB
/
server.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var cookieParser = require('cookie-parser');
var session = require('express-session');
var Q = require('q');
var fs = require("fs");
var Maximo = require('ibm-maximo-api'); // Reference to Maximo OSLC API
const minimist = require('minimist');
var port = process.env.PORT || 3000; // set port,use 3000 for now
var ver = "v1"; // our API version
app.use(cookieParser());
app.use(session({
secret: '&25653666%^',
resave: true,
saveUninitialized: true
}));
// Input arguments sent from command line
// Can change default so don't have to enter on command line every time
let args = minimist(process.argv.slice(2), {
string: [ 'hostname', 'port', 'user', 'password' ],
integer: [ 'islean' ],
alias: {
h: 'hostname',
p: 'port',
u: 'user',
w: 'password',
l: 'islean'
},
default: {
hostname : 'qasite.swg.usma.ibm.com',
port : '9080',
user : 'user',
password : 'password',
islean : 1
}
});
console.log('args:', args);
// ROUTES FOR OUR API
// =============================================================================
// Create basics for maximo instance and router --------------------------------
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next)
{
console.log('Request received by node middleware ...');
next(); // make sure we go to the next routes and don't stop here
});
// Maximo connection details
var options = {
protocol: 'http',
hostname: args.hostname,
port: args.port,
user: args.user,
password: args.password,
auth_scheme: '/maximo',
authtype:'maxauth',
islean: args.islean
};
// -----------------------------------------------------------------------------
// Authentication for maximo instance ------------------------------------------
router.get('/authenticate', function(req, res)
{
var maximo = new Maximo(options);
maximo.authenticate()
.then(function(setcookie)
{
jsondata = setcookie;
req.session.authcookie = jsondata; // Set the cookie in the session so we can use it for future requests
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
//Read specific WO by WONUM ----------------------------------------------------
router.get('/read_1_WO', function(req, res)
{
var maximo = new Maximo(options);
maximo.resourceobject("MXWODETAIL")
.select(["wonum","description","location","status"])
.where("wonum").equal("A123A")
.orderby('wonum','desc')
.fetch()
.then(function(resourceset)
{
jsondata = resourceset.thisResourceSet();
res.json(jsondata);
uri = jsondata[0]["href"];
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
// Creates a WOrkorder --------------------------------------------------------
// In required section, fill in information about WO
router.get('/create_WO', function(req, res)
{
var wo = '';
var required =
{
"description": "Created from API",
"siteid": "BEDFORD",
"wonum": "A123A"
}
var authcookie = req.session.authcookie;
var maximo = new Maximo(options,authcookie);
maximo.resourceobject("MXWODETAIL")
.create(required,["spi:wonum","spi:description"])
.then(function(resource)
{
jsondata = resource.JSON();
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
// Read first 20 WOs by descending WONUM at site BEDFORD -----------------------
router.get('/read_WOs', function(req, res)
{
var maximo = new Maximo(options);
maximo.resourceobject("MXWODETAIL")
.select(["wonum","description","location","status"])
.where("status").in(["WAPPR","APPR"])
.and("siteid").equal('BEDFORD')
.orderby('wonum','desc')
.pagesize(20)
.fetch()
.then(function(resourceset)
{
jsondata = resourceset.thisResourceSet();
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
// Updates a WOrkorder ---------------------------------------------------------
// Run read_1_WO route with specific WONUM to get URI first
// Add updates to WO in updates section
router.get('/update_WO', function(req, res)
{
var updates =
{
"spi:description":"Updated from Node API - test crudconnector"
}
var URI = uri;
var authcookie = req.session.authcookie;
var maximo = new Maximo(options,authcookie);
maximo.resourceobject("MXWODETAIL")
.resource(URI) //Pass the URI
.update(updates,["spi:description"])
.then(function(resource)
{
var jsondata = resource.JSON();
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
// Deletes a WOrkorder ---------------------------------------------------------
// Run read_1_WO route with specific WONUM to get URI first
router.get('/delete_WO', function(req, res)
{
var updates =
{
}
var URI = uri;
var authcookie = req.session.authcookie;
var maximo = new Maximo(options);
maximo.resourceobject("MXWODETAIL")
.resource(URI) //Pass the URI
.delete(updates,["spi:wonum","spi:description"])
.then(function(resource)
{
var jsondata = resource.JSON();
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// -----------------------------------------------------------------------------
// REGISTER ROUTES -------------------------------------------------------------
// all routes will be prefixed with /api/ver/
// in this case, go to localhost:3000/api/v1/read_1_WO to start
app.use('/api/'+ver, router);
// -----------------------------------------------------------------------------
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Maximo Node API server is running on port ' + port);
console.log('API version is ' + ver);
function getFileBytes(path)
{
var deferred = Q.defer();
var fileSize = 0
var buf = new Buffer(fileSize);
// ******** Start buffering the file bytes **********************
fs.stat(path, function (err, stats)
{
if (err)
{
return console.error(err);
}
console.log(stats.size);
fileSize = stats.size;
buf = new Buffer(fileSize);
var actualBytes = 0;
fs.open(path, 'r', function(err, fd)
{
if (err)
{
return console.error(err);
}
console.log("Reading ... ");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes)
{
if (err)
{
console.log(err);
}
console.log(bytes + " bytes read");
console.log("Actual Buffer Size: "+buf.slice(0,bytes).length);
deferred.resolve(buf.slice(0,bytes));
//return buf.slice(0,bytes);
});
// Close the opened file.
fs.close(fd, function(err)
{
if (err){
console.log(err);
}
console.log("File closed successfully.");
});
});
//******* End buffering file bytes ******
});
return deferred.promise;
}