Skip to content

Commit a51c8c1

Browse files
committed
Add new methods
1 parent 887e52d commit a51c8c1

6 files changed

Lines changed: 98 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ By default, the REST API's host address is set to `http://127.0.0.1:18622`.
4242

4343
## Node.js API
4444
- `getDevices(host, scannerType)` - Get all available scanners. It returns an array of scanner objects.
45-
- `scanDocument(host, parameters, timeout)` - Create a scanner job by feeding one or multiple physical documents. It returns the job id.
45+
- `createJob(host, parameters)` - Create a scanner job by feeding one or multiple physical documents. It returns the job id.
4646
- `getImageFile(host, jobId, directory)` - Get one document image by job id. The directory specifies the physical location to save the images. It returns the image path.
4747
- `getImageFiles(host, jobId, directory)` - Get document images by job id. The directory specifies the physical location to save the images. It returns an array of image paths.
4848
- `deleteJob(host, jobId)` - Delete a scan job by job id. It can interrupt the scan process.

examples/REST/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ app.get('/devices', (req, res) => {
2020
});
2121
});
2222

23-
app.post('/scandocument', async (req, res) => {
23+
app.post('/createJob', async (req, res) => {
2424
const data = req.body;
2525

2626
let parameters = {
@@ -38,7 +38,7 @@ app.post('/scandocument', async (req, res) => {
3838
IfDuplexEnabled: false,
3939
};
4040

41-
let job = await docscan4nodejs.scanDocument(dynamsoftService, parameters);
41+
let job = await docscan4nodejs.createJob(dynamsoftService, parameters);
4242
let json = JSON.parse(job);
4343
let jobId = json.jobuid;
4444
let filename = await docscan4nodejs.getImageFile(dynamsoftService, jobId, './public');

examples/REST/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h1> Document Scanning Sample</h1>
6262

6363
async function acquireImage() {
6464
if (devices.length > 0 && selectSources.selectedIndex >= 0) {
65-
const response = await fetch('/scandocument', {
65+
const response = await fetch('/createJob', {
6666
method: 'POST',
6767
headers: {
6868
'Content-Type': 'application/json'

examples/command-line/app.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function askQuestion() {
5454
let parameters = {
5555
license: "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==",
5656
device: devices[index].device,
57+
autoRun: false
5758
};
5859

5960
parameters.config = {
@@ -66,21 +67,34 @@ function askQuestion() {
6667
IfDuplexEnabled: false,
6768
};
6869

69-
docscan4nodejs.scanDocument(host, parameters).then((job) => {
70+
docscan4nodejs.createJob(host, parameters).then((job) => {
7071
try {
7172
let json = JSON.parse(job);
72-
let jobid = json.jobuid;
73+
let jobId = json.jobuid;
74+
7375
(async () => {
74-
let images = await docscan4nodejs.getImageFiles(host, jobid, './');
76+
let status = await docscan4nodejs.checkJob(host, jobId);
77+
console.log('Job status:', status);
78+
79+
let caps = await docscan4nodejs.getScannerCapabilities(host, jobId);
80+
console.log('Capabilities:', caps);
81+
82+
let updateStatus = await docscan4nodejs.updateJob(host, jobId, {
83+
status: docscan4nodejs.JobStatus.RUNNING
84+
});
85+
console.log('Update status:', updateStatus);
86+
87+
let images = await docscan4nodejs.getImageFiles(host, jobId, './');
7588
for (let i = 0; i < images.length; i++) {
7689
console.log('Image ' + i + ': ' + images[i]);
7790
}
78-
await docscan4nodejs.deleteJob(host, jobid);
91+
await docscan4nodejs.deleteJob(host, jobId);
7992
askQuestion();
8093
})();
8194
}
8295
catch (error) {
8396
console.error('Job creation failed:', error.message);
97+
askQuestion();
8498
}
8599
});
86100
}

examples/web/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ io.on('connection', (socket) => {
4545
IfDuplexEnabled: false,
4646
};
4747

48-
let job = await docscan4nodejs.scanDocument(host, parameters);
48+
let job = await docscan4nodejs.createJob(host, parameters);
4949
let json = JSON.parse(job);
5050
let jobId = json.jobuid;
5151

52+
let status = await docscan4nodejs.checkJob(host, jobId);
53+
console.log('Job status:', status);
54+
5255
let streams = await docscan4nodejs.getImageStreams(host, jobId);
5356
for (let i = 0; i < streams.length; i++) {
5457
await new Promise((resolve, reject) => {

index.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const ScannerType = {
1515
WIATWAINSCANNER: 0x800
1616
};
1717

18+
const JobStatus = {
19+
RUNNING: 'running',
20+
CANCELED: 'canceled',
21+
};
22+
1823
// Unified HTTP/HTTPS request handler
1924
function request(options) {
2025
return new Promise((resolve, reject) => {
@@ -171,6 +176,7 @@ async function getDevices(host, scannerType) {
171176
try {
172177
const response = await request({
173178
url,
179+
method: 'GET',
174180
json: true
175181
});
176182

@@ -185,7 +191,7 @@ async function getDevices(host, scannerType) {
185191
}
186192

187193
// Create new scan job
188-
async function scanDocument(host, parameters) {
194+
async function createJob(host, parameters) {
189195
const url = `${host}/api/device/scanners/jobs`;
190196

191197
try {
@@ -207,6 +213,24 @@ async function scanDocument(host, parameters) {
207213
}
208214
}
209215

216+
// Retrive the job status
217+
async function checkJob(host, jobId) {
218+
const url = `${host}/api/device/scanners/jobs/${jobId}`;
219+
220+
try {
221+
const response = await request({
222+
url,
223+
method: 'GET',
224+
json: true
225+
});
226+
227+
return response.status === 200 ? response.data : '';
228+
} catch (error) {
229+
console.error('Scan job creation failed:', error.message);
230+
return '';
231+
}
232+
}
233+
210234
// Delete existing scan job
211235
async function deleteJob(host, jobId) {
212236
if (!jobId) return;
@@ -222,6 +246,46 @@ async function deleteJob(host, jobId) {
222246
}
223247
}
224248

249+
// Update existing scan job status (e.g. 'running', canceled)
250+
async function updateJob(host, jobId, parameters) {
251+
const url = `${host}/api/device/scanners/jobs/${jobId}`;
252+
253+
try {
254+
const response = await request({
255+
url,
256+
method: 'PATCH',
257+
headers: {
258+
'Content-Type': 'application/json',
259+
'Content-Length': Buffer.byteLength(JSON.stringify(parameters))
260+
},
261+
body: parameters
262+
});
263+
264+
return response.status === 200 ? response.data : '';
265+
} catch (error) {
266+
console.error('Scan job creation failed:', error.message);
267+
return '';
268+
}
269+
}
270+
271+
async function getScannerCapabilities(host, jobId) {
272+
const url = `${host}/api/device/scanners/jobs/${jobId}/scanner/capabilities`;
273+
274+
try {
275+
const response = await request({
276+
url,
277+
method: 'GET',
278+
json: true
279+
});
280+
281+
return response.status === 200 ? response.data : '';
282+
}
283+
catch (error) {
284+
console.error('Scan job creation failed:', error.message);
285+
return '';
286+
}
287+
}
288+
225289
// Get multiple image files
226290
async function getImageFiles(host, jobId, directory) {
227291
const images = [];
@@ -249,13 +313,17 @@ async function getImageStreams(host, jobId) {
249313
}
250314

251315
module.exports = {
316+
ScannerType,
317+
JobStatus,
318+
getServerInfo,
252319
getDevices,
253-
scanDocument,
320+
createJob,
254321
deleteJob,
322+
updateJob,
323+
checkJob,
255324
getImageFile,
256325
getImageStream,
257326
getImageFiles,
258327
getImageStreams,
259-
ScannerType,
260-
getServerInfo
328+
getScannerCapabilities
261329
};

0 commit comments

Comments
 (0)