-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDataQueue.js
More file actions
225 lines (188 loc) · 6.76 KB
/
DataQueue.js
File metadata and controls
225 lines (188 loc) · 6.76 KB
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
const { ProgramCall } = require('./ProgramCall');
const { CommandCall } = require('./CommandCall');
// const { xmlToJson } = require('./utils');
// QRCVDTAQ Parameters
const QRCVDTAQParameters = {
DataQueueName: { io: 'in', type: '10A' },
LibraryName: { io: 'in', type: '10A' },
LengthOfData: { io: 'out', type: '5p0' },
Data: { io: 'out' },
WaitTime: { io: 'in', type: '5p0' }
}
class DataQueue {
/**
* @description Creates a new DataQueue object
* @constructor
* @param {object} connection
* @param {string} name
* @param {string} library
*/
constructor(connection, name, library = '') {
this.connection = connection;
if (name.length > 10) {
throw Error("DataQueue name must be 10 or fewer characters long.");
}
if (library.length > 10) {
throw Error("DataQueue library must be 10 of fewer characers long.");
}
this.name = name;
this.library = library ? library : '*CURLIB';
this.qualifiedName = this.name.padEnd(10, ' ') + this.library.padEnd(10, ' ');
this.maxLength = undefined;
}
//////////////////////////////////////////////////////////////////////////////
// Create Data Queue (CLDTAQ) CL Command
//////////////////////////////////////////////////////////////////////////////
/**
* @description Creates a Data Queue on the system of the connection passed in
* the constructor.
* @constructor
* @param {number} maxLength
* @param {function} callback
*/
async create(maxLength, callback) {
this.maxLength = maxLength;
const commandConfig = {
type: 'cl',
command: `CRTDTAQ DTAQ(${this.library ? `${this.library}/` : ''}${this.name}) MAXLEN(${this.maxLength})`
}
this.connection.add(new CommandCall(commandConfig));
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error);
}
callback(null, xmlOutput);
});
}
//////////////////////////////////////////////////////////////////////////////
// Delete Data Queue (DLTDTAQ) CL Command
//////////////////////////////////////////////////////////////////////////////
async delete(callback) {
const commandConfig = {
type: 'cl',
command: `DLTDTAQ DTAQ(${this.library ? `${this.library}/`: ''}${this.name})`
}
this.connection.add(new CommandCall(commandConfig));
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error);
}
callback(null, xmlOutput);
});
}
//////////////////////////////////////////////////////////////////////////////
// Receive Data Queue (QRCVDTAQ) API
//////////////////////////////////////////////////////////////////////////////
async receive(wait, length, callback) {
if (typeof wait === 'function') {
callback = wait;
wait = 0;
}
if (length == undefined) {
if (this.maxLength == undefined) {
const description = await this._getDataQueueDescription((error, result) => {
if (error) console.error(error);
console.log(description);
this.maxLength = 100;
});
}
length = this.maxLength;
}
console.log(`Length in receive is ${length}`);
const program = new ProgramCall('QRCVDTAQ', { lib: 'QSYS' });
program.addParam (
{ value: this.name, ...QRCVDTAQParameters.DataQueueName }
);
program.addParam (
{ value: this.library, ...QRCVDTAQParameters.LibraryName }
);
program.addParam (
{ value: length, ...QRCVDTAQParameters.LengthOfData }
);
program.addParam (
{ value: '', ...QRCVDTAQParameters.Data, type: `${length}A` }
);
program.addParam (
{ value: wait, ...QRCVDTAQParameters.WaitTime}
);
this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
return callback(null, xmlOutput);
});
}
// alias for receive, as the API is "RCV", but jt400 uses the term "read"
async read(wait, callback) {
return this.receive(wait, callback);
}
//////////////////////////////////////////////////////////////////////////////
// Send Data Queue (QSNDDTAQ) API
//////////////////////////////////////////////////////////////////////////////
async send(data, callback) {
console.log("data length is " + data.length);
const program = new ProgramCall('QSNDDTAQ', { lib: 'QSYS' });
program.addParam({value: this.name, type: '10A'}); // Data queue name
program.addParam({value: this.library, type: '10A'}); // Library name
program.addParam({value: data.length, type: '5p0'}); // Length of data
program.addParam({value: data, type: `${data.length}A`}); // Data
this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
return callback(null, xmlOutput);
});
}
// alias for send, as the API is "SND", but jt400 uses the term "write"
async write(data, callback) {
return this.send(data, callback);
}
//////////////////////////////////////////////////////////////////////////////
// Clear Data Queue (QCLRDTAQ) API
//////////////////////////////////////////////////////////////////////////////
async clear(callback) {
const program = new ProgramCall('QCLRDTAQ', { lib: 'QSYS' });
program.addParam({ value: this.name, type: '10A'} ); // Data queue name
program.addParam({ value: this.library, type: '10A'}); // Library name
this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
return callback(null, xmlOutput);
});
}
//////////////////////////////////////////////////////////////////////////////
// "Private" Functions
//////////////////////////////////////////////////////////////////////////////
async _getDataQueueDescription(callback) {
const program = new ProgramCall('QMHQRDQD', { lib: 'QSYS' });
const receiverVariable = [
{ value: 0, type: '10i0' },
{ value: 0, type: '10i0' },
{ value: 0, type: '10i0' },
];
program.addParam ( // Receiver variable
{ value: receiverVariable, fields: receiverVariable, type: 'ds', io: 'out', len: 'varLen' }
);
program.addParam ( // Length of receiver variable
{ value: 0, type: '10i0', setlen: 'varLen' }
);
program.addParam ( // Format name
{ value: 'RDQD0100', type: '8A' }
);
program.addParam ( // Qualified data queue name
{ value: this.qualifiedName, type: '20A' }
);
this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
return callback(null, xmlOutput);
});
}
}
module.exports.DataQueue = DataQueue;