-
Notifications
You must be signed in to change notification settings - Fork 7
/
read.js
178 lines (158 loc) · 5.02 KB
/
read.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
// 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
};
// 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);
});
});
// Read first 50 WOs by ascending WONUM at site BEDFORD
// Get more/different fields than before
router.get('/read_WOs_2', function(req, res)
{
var maximo = new Maximo(options);
maximo.resourceobject("MXWODETAIL")
.select(["wonum","description","siteid","status","asset.assetnum","location"])
.where("siteid").equal("BEDFORD")
.orderby('wonum','asc')
.pagesize(50)
.fetch()
.then(function(resourceset)
{
jsondata = resourceset.thisResourceSet();
res.json(jsondata);
})
.fail(function (error)
{
console.log('****** Error Code = '+error);
});
});
// REGISTER ROUTES -------------------------------
// all routes will be prefixed with /api/ver/
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;
}