-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add DataQueue class #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markirish
wants to merge
5
commits into
master
Choose a base branch
from
issue97-dataqueue-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb66fda
[issue97] Add DataQueue class
markirish 22a7885
DataQueue: remove some log statements
markirish 32aec92
Merge branch 'master' into issue97-dataqueue-class
markirish 646aa5c
Update DataQueue class
markirish 37c37ac
Fix DataQueue implementation
markirish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these methods be defined as async? I'm thinking this could lead to synchronization issues. Async function return a promise and within the function we call
connection.run
with is asynchronous function using the callback pattern. So we wouldn't wait forconnection.run
to complete before returning the promise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this goes with Issue #322 . I have always declared them as
async
since that's what they are. I wasn't aware the spec said I had to return a Promise, just that "Async functions can contain zero or more await expressions." Because of the latter I don't think using callbacks internally is a big deal. Will have to dwell on it, but maybe is a reason people don't mix callbacks/Promises in the same function