node-hp-scan-to 1.6.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/README.md +247 -143
  2. package/dist/Destination.js +17 -7
  3. package/dist/Destination.js.map +1 -1
  4. package/dist/DeviceCapabilities.d.ts +2 -0
  5. package/dist/DeviceCapabilities.js.map +1 -1
  6. package/dist/DiscoveryTree.js.map +1 -1
  7. package/dist/Event.js.map +1 -1
  8. package/dist/EventTable.js.map +1 -1
  9. package/dist/HPApi.js +23 -15
  10. package/dist/HPApi.js.map +1 -1
  11. package/dist/InputSource.js +1 -1
  12. package/dist/InputSource.js.map +1 -1
  13. package/dist/Job.js +6 -6
  14. package/dist/Job.js.map +1 -1
  15. package/dist/JpegUtil.d.ts +0 -2
  16. package/dist/JpegUtil.js.map +1 -1
  17. package/dist/KnownShortcut.js.map +1 -1
  18. package/dist/PathHelper.js +2 -0
  19. package/dist/PathHelper.js.map +1 -1
  20. package/dist/ScanCaps.d.ts +6 -0
  21. package/dist/ScanCaps.js +18 -0
  22. package/dist/ScanCaps.js.map +1 -1
  23. package/dist/ScanContent.js.map +1 -1
  24. package/dist/ScanJobManifest.js.map +1 -1
  25. package/dist/ScanJobSettings.js.map +1 -1
  26. package/dist/ScanStatus.js.map +1 -1
  27. package/dist/WalkupScanDestination.js.map +1 -1
  28. package/dist/WalkupScanDestinations.js.map +1 -1
  29. package/dist/WalkupScanManifest.js.map +1 -1
  30. package/dist/WalkupScanToCompCaps.js.map +1 -1
  31. package/dist/WalkupScanToCompDestination.js.map +1 -1
  32. package/dist/WalkupScanToCompDestinations.js.map +1 -1
  33. package/dist/WalkupScanToCompEvent.js.map +1 -1
  34. package/dist/WalkupScanToCompManifest.js.map +1 -1
  35. package/dist/commitInfo.json +1 -1
  36. package/dist/delay.js +1 -2
  37. package/dist/delay.js.map +1 -1
  38. package/dist/healthcheck.js +1 -2
  39. package/dist/healthcheck.js.map +1 -1
  40. package/dist/index.js +22 -12
  41. package/dist/index.js.map +1 -1
  42. package/dist/listening.js +5 -6
  43. package/dist/listening.js.map +1 -1
  44. package/dist/nextcloud/NextcloudConfig.js.map +1 -1
  45. package/dist/nextcloud/nextcloud.js +4 -6
  46. package/dist/nextcloud/nextcloud.js.map +1 -1
  47. package/dist/paperless/PaperlessConfig.js.map +1 -1
  48. package/dist/paperless/paperless.js +4 -5
  49. package/dist/paperless/paperless.js.map +1 -1
  50. package/dist/pdfProcessing.js +4 -5
  51. package/dist/pdfProcessing.js.map +1 -1
  52. package/dist/postProcessing.js +2 -4
  53. package/dist/postProcessing.js.map +1 -1
  54. package/dist/readDeviceCapabilities.js +24 -20
  55. package/dist/readDeviceCapabilities.js.map +1 -1
  56. package/dist/scanProcessing.d.ts +2 -2
  57. package/dist/scanProcessing.js +28 -30
  58. package/dist/scanProcessing.js.map +1 -1
  59. package/package.json +19 -17
  60. package/dist/PaperlessCOnfig.d.ts +0 -7
  61. package/dist/PaperlessCOnfig.js +0 -3
  62. package/dist/PaperlessCOnfig.js.map +0 -1
  63. package/dist/PaperlessConfig.d.ts +0 -7
  64. package/dist/PaperlessConfig.js +0 -3
  65. package/dist/PaperlessConfig.js.map +0 -1
  66. package/dist/paperless.d.ts +0 -7
  67. package/dist/paperless.js +0 -83
  68. package/dist/paperless.js.map +0 -1
  69. package/dist/test/DiscoveryTree.test.d.ts +0 -1
  70. package/dist/test/DiscoveryTree.test.js +0 -68
  71. package/dist/test/DiscoveryTree.test.js.map +0 -1
  72. package/dist/test/EtagEventTable.test.d.ts +0 -1
  73. package/dist/test/EtagEventTable.test.js +0 -93
  74. package/dist/test/EtagEventTable.test.js.map +0 -1
  75. package/dist/test/Job.test.d.ts +0 -1
  76. package/dist/test/Job.test.js +0 -104
  77. package/dist/test/Job.test.js.map +0 -1
  78. package/dist/test/JpegUtil.test.d.ts +0 -1
  79. package/dist/test/JpegUtil.test.js +0 -53
  80. package/dist/test/JpegUtil.test.js.map +0 -1
  81. package/dist/test/PathHelper.test.d.ts +0 -1
  82. package/dist/test/PathHelper.test.js +0 -98
  83. package/dist/test/PathHelper.test.js.map +0 -1
  84. package/dist/test/ScanCaps.test.d.ts +0 -1
  85. package/dist/test/ScanCaps.test.js +0 -96
  86. package/dist/test/ScanCaps.test.js.map +0 -1
  87. package/dist/test/ScanJobManifest.test.d.ts +0 -1
  88. package/dist/test/ScanJobManifest.test.js +0 -56
  89. package/dist/test/ScanJobManifest.test.js.map +0 -1
  90. package/dist/test/ScanJobSettings.test.d.ts +0 -1
  91. package/dist/test/ScanJobSettings.test.js +0 -59
  92. package/dist/test/ScanJobSettings.test.js.map +0 -1
  93. package/dist/test/ScanStatus.test.d.ts +0 -1
  94. package/dist/test/ScanStatus.test.js +0 -75
  95. package/dist/test/ScanStatus.test.js.map +0 -1
  96. package/dist/test/WalkupScanDestination.test.d.ts +0 -1
  97. package/dist/test/WalkupScanDestination.test.js +0 -80
  98. package/dist/test/WalkupScanDestination.test.js.map +0 -1
  99. package/dist/test/WalkupScanDestinations.test.d.ts +0 -1
  100. package/dist/test/WalkupScanDestinations.test.js +0 -68
  101. package/dist/test/WalkupScanDestinations.test.js.map +0 -1
  102. package/dist/test/WalkupScanManifest.test.d.ts +0 -1
  103. package/dist/test/WalkupScanManifest.test.js +0 -46
  104. package/dist/test/WalkupScanManifest.test.js.map +0 -1
  105. package/dist/test/WalkupScanToCompCaps.test.d.ts +0 -1
  106. package/dist/test/WalkupScanToCompCaps.test.js +0 -56
  107. package/dist/test/WalkupScanToCompCaps.test.js.map +0 -1
  108. package/dist/test/WalkupScanToCompDestination.test.d.ts +0 -1
  109. package/dist/test/WalkupScanToCompDestination.test.js +0 -58
  110. package/dist/test/WalkupScanToCompDestination.test.js.map +0 -1
  111. package/dist/test/WalkupScanToCompDestinations.test.d.ts +0 -1
  112. package/dist/test/WalkupScanToCompDestinations.test.js +0 -58
  113. package/dist/test/WalkupScanToCompDestinations.test.js.map +0 -1
  114. package/dist/test/WalkupScanToCompEvent.test.d.ts +0 -1
  115. package/dist/test/WalkupScanToCompEvent.test.js +0 -46
  116. package/dist/test/WalkupScanToCompEvent.test.js.map +0 -1
  117. package/dist/test/WalkupScanToCompManifest.test.d.ts +0 -1
  118. package/dist/test/WalkupScanToCompManifest.test.js +0 -46
  119. package/dist/test/WalkupScanToCompManifest.test.js.map +0 -1
  120. package/dist/test/clean.d.ts +0 -1
  121. package/dist/test/clean.js +0 -21
  122. package/dist/test/clean.js.map +0 -1
  123. package/dist/test/nextcloud.test.d.ts +0 -1
  124. package/dist/test/nextcloud.test.js +0 -165
  125. package/dist/test/nextcloud.test.js.map +0 -1
  126. package/dist/test/scanProcessing.test.d.ts +0 -1
  127. package/dist/test/scanProcessing.test.js +0 -105
  128. package/dist/test/scanProcessing.test.js.map +0 -1
  129. package/dist/test-bonjour.d.ts +0 -1
  130. package/dist/test-bonjour.js +0 -20
  131. package/dist/test-bonjour.js.map +0 -1
  132. package/nodemon.json +0 -6
  133. package/src/Destination.ts +0 -63
  134. package/src/DeviceCapabilities.ts +0 -8
  135. package/src/DiscoveryTree.ts +0 -56
  136. package/src/Event.ts +0 -55
  137. package/src/EventTable.ts +0 -45
  138. package/src/HPApi.ts +0 -510
  139. package/src/InputSource.ts +0 -4
  140. package/src/Job.ts +0 -134
  141. package/src/JpegUtil.ts +0 -327
  142. package/src/KnownShortcut.ts +0 -6
  143. package/src/PathHelper.ts +0 -106
  144. package/src/ScanCaps.ts +0 -81
  145. package/src/ScanContent.ts +0 -15
  146. package/src/ScanJobManifest.ts +0 -56
  147. package/src/ScanJobSettings.ts +0 -84
  148. package/src/ScanStatus.ts +0 -48
  149. package/src/WalkupScanDestination.ts +0 -74
  150. package/src/WalkupScanDestinations.ts +0 -46
  151. package/src/WalkupScanManifest.ts +0 -59
  152. package/src/WalkupScanToCompCaps.ts +0 -36
  153. package/src/WalkupScanToCompDestination.ts +0 -79
  154. package/src/WalkupScanToCompDestinations.ts +0 -47
  155. package/src/WalkupScanToCompEvent.ts +0 -30
  156. package/src/WalkupScanToCompManifest.ts +0 -59
  157. package/src/commitInfo.json +0 -3
  158. package/src/delay.ts +0 -5
  159. package/src/healthcheck.ts +0 -17
  160. package/src/index.ts +0 -588
  161. package/src/listening.ts +0 -144
  162. package/src/nextcloud/NextcloudConfig.ts +0 -7
  163. package/src/nextcloud/nextcloud.ts +0 -146
  164. package/src/paperless/PaperlessConfig.ts +0 -7
  165. package/src/paperless/paperless.ts +0 -110
  166. package/src/pdfProcessing.ts +0 -67
  167. package/src/postProcessing.ts +0 -165
  168. package/src/readDeviceCapabilities.ts +0 -52
  169. package/src/scanProcessing.ts +0 -570
  170. package/tsconfig.json +0 -34
package/src/index.ts DELETED
@@ -1,588 +0,0 @@
1
- #!/usr/bin/env node
2
- // noinspection XmlDeprecatedElement,HtmlDeprecatedTag
3
-
4
- "use strict";
5
-
6
- import os from "os";
7
- import { Command, Option, OptionValues, program } from "commander";
8
- import { Bonjour } from "bonjour-service";
9
- import config from "config";
10
- import HPApi from "./HPApi";
11
- import PathHelper from "./PathHelper";
12
- import { delay } from "./delay";
13
- import { readDeviceCapabilities } from "./readDeviceCapabilities";
14
- import {
15
- clearRegistrations,
16
- RegistrationConfig,
17
- waitScanEvent,
18
- } from "./listening";
19
- import {
20
- AdfAutoScanConfig,
21
- DirectoryConfig,
22
- saveScanFromEvent,
23
- ScanConfig,
24
- scanFromAdf,
25
- waitAdfLoaded,
26
- } from "./scanProcessing";
27
- import * as commitInfo from "./commitInfo.json";
28
- import { PaperlessConfig } from "./paperless/PaperlessConfig";
29
- import { NextcloudConfig } from "./nextcloud/NextcloudConfig";
30
- import { startHealthCheckServer } from "./healthcheck";
31
- import fs from "fs";
32
-
33
- let iteration = 0;
34
-
35
- async function listenCmd(
36
- registrationConfig: RegistrationConfig,
37
- scanConfig: ScanConfig,
38
- deviceUpPollingInterval: number,
39
- ) {
40
- // first make sure the device is reachable
41
- await HPApi.waitDeviceUp(deviceUpPollingInterval);
42
- let deviceUp = true;
43
-
44
- const folder = await PathHelper.getOutputFolder(
45
- scanConfig.directoryConfig.directory,
46
- );
47
- console.log(`Target folder: ${folder}`);
48
-
49
- const tempFolder = await PathHelper.getOutputFolder(
50
- scanConfig.directoryConfig.tempDirectory,
51
- );
52
- console.log(`Temp folder: ${tempFolder}`);
53
-
54
- const deviceCapabilities = await readDeviceCapabilities();
55
-
56
- let scanCount = 0;
57
- let keepActive = true;
58
- let errorCount = 0;
59
- while (keepActive) {
60
- iteration++;
61
- console.log(`Running iteration: ${iteration} - errorCount: ${errorCount}`);
62
- try {
63
- const event = await waitScanEvent(deviceCapabilities, registrationConfig);
64
- scanCount = await PathHelper.getNextScanNumber(
65
- folder,
66
- scanCount,
67
- scanConfig.directoryConfig.filePattern,
68
- );
69
- console.log(`Scan event captured, saving scan #${scanCount}`);
70
- await saveScanFromEvent(
71
- event,
72
- folder,
73
- tempFolder,
74
- scanCount,
75
- deviceCapabilities,
76
- scanConfig,
77
- );
78
- } catch (e) {
79
- if (await HPApi.isAlive()) {
80
- console.log(e);
81
- errorCount++;
82
- } else {
83
- if (HPApi.isDebug()) {
84
- console.log(e);
85
- }
86
- deviceUp = false;
87
- }
88
- }
89
-
90
- if (errorCount === 50) {
91
- keepActive = false;
92
- }
93
-
94
- if (!deviceUp) {
95
- await HPApi.waitDeviceUp(deviceUpPollingInterval);
96
- } else {
97
- await delay(1000);
98
- }
99
- }
100
- }
101
-
102
- async function adfAutoscanCmd(
103
- adfAutoScanConfig: AdfAutoScanConfig,
104
- deviceUpPollingInterval: number,
105
- ) {
106
- // first make sure the device is reachable
107
- await HPApi.waitDeviceUp(deviceUpPollingInterval);
108
- let deviceUp = true;
109
-
110
- const folder = await PathHelper.getOutputFolder(
111
- adfAutoScanConfig.directoryConfig.directory,
112
- );
113
- console.log(`Target folder: ${folder}`);
114
-
115
- const tempFolder = await PathHelper.getOutputFolder(
116
- adfAutoScanConfig.directoryConfig.tempDirectory,
117
- );
118
- console.log(`Temp folder: ${tempFolder}`);
119
-
120
- const deviceCapabilities = await readDeviceCapabilities();
121
-
122
- let scanCount = 0;
123
- let keepActive = true;
124
- let errorCount = 0;
125
- while (keepActive) {
126
- iteration++;
127
- console.log(`Running iteration: ${iteration} - errorCount: ${errorCount}`);
128
- try {
129
- await waitAdfLoaded(
130
- adfAutoScanConfig.pollingInterval,
131
- adfAutoScanConfig.startScanDelay,
132
- );
133
-
134
- scanCount++;
135
-
136
- console.log(`Scan event captured, saving scan #${scanCount}`);
137
-
138
- await scanFromAdf(
139
- scanCount,
140
- folder,
141
- tempFolder,
142
- adfAutoScanConfig,
143
- deviceCapabilities,
144
- new Date(),
145
- );
146
- } catch (e) {
147
- console.log(e);
148
- if (await HPApi.isAlive()) {
149
- errorCount++;
150
- } else {
151
- deviceUp = false;
152
- }
153
- }
154
-
155
- if (errorCount === 50) {
156
- keepActive = false;
157
- }
158
-
159
- if (!deviceUp) {
160
- await HPApi.waitDeviceUp(deviceUpPollingInterval);
161
- } else {
162
- await delay(1000);
163
- }
164
- }
165
- }
166
-
167
- async function clearRegistrationsCmd(cmd: Command) {
168
- const parentOption = cmd.parent!.opts();
169
-
170
- const ip = await getDeviceIp(parentOption);
171
- HPApi.setDeviceIP(ip);
172
-
173
- const isDebug = getIsDebug(parentOption);
174
- HPApi.setDebug(isDebug);
175
- await clearRegistrations();
176
- }
177
-
178
- function findOfficejetIp(deviceNamePrefix: string): Promise<string> {
179
- return new Promise((resolve) => {
180
- const bonjour = new Bonjour();
181
- console.log("Searching device...");
182
- const browser = bonjour.find(
183
- {
184
- type: "http",
185
- },
186
- (service) => {
187
- console.log(".");
188
- if (
189
- service.name.startsWith(deviceNamePrefix) &&
190
- service.port === 80 &&
191
- service.type === "http" &&
192
- service.addresses != null
193
- ) {
194
- browser.stop();
195
- bonjour.destroy();
196
- console.log(`Found: ${service.name}`);
197
- resolve(service.addresses[0]);
198
- }
199
- },
200
- );
201
- browser.start();
202
- });
203
- }
204
-
205
- function getConfig<T>(name: string): T | undefined {
206
- return config.has(name) ? config.get<T>(name) : undefined;
207
- }
208
-
209
- function setupScanParameters(command: Command): Command {
210
- command.option(
211
- "-d, --directory <dir>",
212
- "Directory where scans are saved (default: /tmp/scan-to-pcRANDOM)",
213
- );
214
- command.option(
215
- "-t, --temp-directory <dir>",
216
- "Temp directory used for processing (default: /tmp/scan-to-pcRANDOM)",
217
- );
218
- command.option(
219
- "-p, --pattern <pattern>",
220
- 'Pattern for filename (i.e. "scan"_dd.mm.yyyy_hh:MM:ss, without this its scanPageNUMBER)',
221
- );
222
- command.option(
223
- "-r, --resolution <dpi>",
224
- "Resolution in DPI of the scans (default: 200)",
225
- );
226
- command.option(
227
- "-w, --width <width>",
228
- "With in pixel of the scans (default: 2481)",
229
- );
230
- command.option(
231
- "-h, --height <height>",
232
- "Height in pixel of the scans (default: 3507)",
233
- );
234
- command.option(
235
- "-s, --paperless-post-document-url <paperless_post_document_url>",
236
- "The paperless post document url (example: https://domain.tld/api/documents/post_document/)",
237
- );
238
-
239
- command.option(
240
- "-o, --paperless-token <paperless_token>",
241
- "The paperless token",
242
- );
243
- command.option(
244
- "--paperless-group-multi-page-scan-into-a-pdf",
245
- "Combine multiple scanned images into a single PDF document",
246
- );
247
- command.option(
248
- "--paperless-always-send-as-pdf-file",
249
- "Always convert scan job to pdf before sending to paperless",
250
- );
251
- command.option(
252
- "-k, --keep-files",
253
- "Keep the scan files on the file system (default: false)",
254
- );
255
-
256
- command.option(
257
- "--nextcloud-url <nextcloud_url>",
258
- "The nextcloud url (example: https://domain.tld)",
259
- );
260
- command.option(
261
- "--nextcloud-username <nextcloud_username>",
262
- "The nextcloud username",
263
- );
264
- command.option(
265
- "--nextcloud-password <nextcloud_app_password>",
266
- "The nextcloud app password for username. Either this or nextcloud-password-file is required",
267
- );
268
- command.option(
269
- "--nextcloud-password-file <nextcloud_app_password_file>",
270
- "File name that contains the nextcloud app password for username. Either this or nextcloud-password is required",
271
- );
272
- command.option(
273
- "--nextcloud-upload-folder <nextcloud_upload_folder>",
274
- "The upload folder where documents or images are uploaded (default: scan)",
275
- );
276
- return command;
277
- }
278
-
279
- function setupParameterOpts(command: Command): Command {
280
- command.option(
281
- "-a, --address <ip>",
282
- "IP address of the device (this overrides -p)",
283
- );
284
- command.option(
285
- "--device-up-polling-interval <deviceUpPollingInterval>",
286
- "Device up polling interval in milliseconds",
287
- parseFloat,
288
- );
289
- command.option(
290
- "-n, --name <name>",
291
- "Name of the device for service discovery",
292
- ); // i.e. 'Deskjet 3520 series'
293
-
294
- command.option("-D, --debug", "Enable debug");
295
- return command;
296
- }
297
-
298
- async function getDeviceIp(options: OptionValues) {
299
- let ip = options.address || getConfig("ip");
300
- if (!ip) {
301
- const name = options.name || getConfig("name");
302
- ip = await findOfficejetIp(name || "HP Smart Tank Plus 570 series");
303
- }
304
- console.log(`Using device ip: ${ip}`);
305
- return ip;
306
- }
307
-
308
- function getIsDebug(options: OptionValues) {
309
- const debug =
310
- options.debug != null ? true : getConfig<boolean>("debug") || false;
311
-
312
- if (debug) {
313
- console.log(`IsDebug: ${debug}`);
314
- }
315
- return debug;
316
- }
317
-
318
- function getPaperlessConfig(
319
- parentOption: OptionValues,
320
- ): PaperlessConfig | undefined {
321
- const paperlessPostDocumentUrl: string =
322
- parentOption.paperlessPostDocumentUrl ||
323
- getConfig("paperless_post_document_url");
324
- const configPaperlessToken: string =
325
- parentOption.paperlessToken || getConfig("paperless_token");
326
-
327
- if (paperlessPostDocumentUrl && configPaperlessToken) {
328
- const configPaperlessKeepFiles: boolean =
329
- parentOption.keepFiles || getConfig("keep_files") || false;
330
- const groupMultiPageScanIntoAPdf: boolean =
331
- parentOption.paperlessGroupMultiPageScanIntoAPdf ||
332
- getConfig("paperless_group_multi_page_scan_into_a_pdf") ||
333
- false;
334
- const alwaysSendAsPdfFile: boolean =
335
- parentOption.paperlessAlwaysSendAsPdfFile ||
336
- getConfig("paperless_always_send_as_pdf_file") ||
337
- false;
338
-
339
- console.log(
340
- `Paperless configuration provided, post document url: ${paperlessPostDocumentUrl}, the token length: ${configPaperlessToken.length}, keepFiles: ${configPaperlessKeepFiles}`,
341
- );
342
- return {
343
- postDocumentUrl: paperlessPostDocumentUrl,
344
- authToken: configPaperlessToken,
345
- keepFiles: configPaperlessKeepFiles,
346
- groupMultiPageScanIntoAPdf: groupMultiPageScanIntoAPdf,
347
- alwaysSendAsPdfFile: alwaysSendAsPdfFile,
348
- };
349
- } else {
350
- return undefined;
351
- }
352
- }
353
-
354
- function getNextcloudConfig(
355
- parentOption: OptionValues,
356
- ): NextcloudConfig | undefined {
357
- const configNextcloudUrl: string =
358
- parentOption.nextcloudUrl || getConfig("nextcloud_url");
359
- const configNextcloudUsername: string =
360
- parentOption.nextcloudUsername || getConfig("nextcloud_username");
361
- let configNextcloudPassword: string =
362
- parentOption.nextcloudPassword || getConfig("nextcloud_password");
363
- const configNextcloudPasswordFile: string =
364
- parentOption.nextcloudPasswordFile || getConfig("nextcloud_password_file");
365
-
366
- if (
367
- configNextcloudUrl &&
368
- configNextcloudUsername &&
369
- (configNextcloudPassword || configNextcloudPasswordFile)
370
- ) {
371
- const configNextcloudUploadFolder =
372
- parentOption.nextcloudUploadFolder ||
373
- getConfig("nextcloud_upload_folder") ||
374
- "scan";
375
- const configNextcloudKeepFiles: boolean =
376
- parentOption.keepFiles || getConfig("keep_files") || false;
377
-
378
- if (configNextcloudPasswordFile) {
379
- configNextcloudPassword = fs
380
- .readFileSync(configNextcloudPasswordFile, "utf8")
381
- .trimEnd();
382
- }
383
-
384
- console.log(
385
- `Nextcloud configuration provided, url: ${configNextcloudUrl}, username: ${configNextcloudUsername}, password length: ${configNextcloudPassword.length}, upload folder: ${configNextcloudUploadFolder}, keepFiles: ${configNextcloudKeepFiles}`,
386
- );
387
- return {
388
- baseUrl: configNextcloudUrl,
389
- username: configNextcloudUsername,
390
- password: configNextcloudPassword,
391
- uploadFolder: configNextcloudUploadFolder,
392
- keepFiles: configNextcloudKeepFiles,
393
- };
394
- } else {
395
- return undefined;
396
- }
397
- }
398
-
399
- function getHealthCheckSetting(option: OptionValues) {
400
- const healthCheckEnabled: boolean =
401
- option.healthCheck || getConfig("enableHealthCheck") === true;
402
- let healthCheckPort: number;
403
- if (option.healthCheckPort) {
404
- healthCheckPort = parseInt(option.healthCheckPort, 10);
405
- } else {
406
- healthCheckPort = getConfig<number>("healthCheckPort") || 3000;
407
- }
408
- return {
409
- isHealthCheckEnabled: healthCheckEnabled,
410
- healthCheckPort: healthCheckPort,
411
- };
412
- }
413
-
414
- function getScanConfiguration(option: OptionValues) {
415
- const directoryConfig: DirectoryConfig = {
416
- directory: option.directory || getConfig("directory"),
417
- tempDirectory: option.tempDirectory || getConfig("tempDirectory"),
418
- filePattern: option.pattern || getConfig("pattern"),
419
- };
420
-
421
- const configWidth = (option.width || getConfig("width") || 0).toString();
422
- const width =
423
- configWidth.toLowerCase() === "max"
424
- ? Number.MAX_SAFE_INTEGER
425
- : parseInt(configWidth, 10);
426
-
427
- const configHeight = (option.width || getConfig("height") || "0").toString();
428
- const height =
429
- configWidth.toLowerCase() === "max"
430
- ? Number.MAX_SAFE_INTEGER
431
- : parseInt(configHeight, 10);
432
-
433
- const paperlessConfig = getPaperlessConfig(option);
434
- const nextcloudConfig = getNextcloudConfig(option);
435
-
436
- const scanConfig: ScanConfig = {
437
- resolution: parseInt(
438
- option.resolution || getConfig("resolution") || "200",
439
- 10,
440
- ),
441
- width: width,
442
- height: height,
443
- directoryConfig,
444
- paperlessConfig,
445
- nextcloudConfig,
446
- };
447
- return scanConfig;
448
- }
449
-
450
- function getDeviceUpPollingInterval(parentOption: OptionValues) {
451
- return (
452
- parentOption.deviceUpPollingInterval ||
453
- getConfig("deviceUpPollingInterval") ||
454
- 1000
455
- );
456
- }
457
-
458
- async function main() {
459
- setupParameterOpts(program);
460
- const cmdListen = program.createCommand("listen");
461
- setupScanParameters(cmdListen)
462
- .description("Listen the device for new scan job to save to this target")
463
- .option(
464
- "-l, --label <label>",
465
- "The label to display on the device (the default is the hostname)",
466
- )
467
- .addOption(
468
- new Option("--health-check", "Start an http health check endpoint"),
469
- )
470
- .addOption(
471
- new Option(
472
- "--health-check-port <health-check-port>",
473
- "Start an http health check endpoint",
474
- ),
475
- )
476
- .action(async (options, cmd) => {
477
- const parentOption = cmd.parent.opts();
478
-
479
- const ip = await getDeviceIp(parentOption);
480
- HPApi.setDeviceIP(ip);
481
-
482
- const isDebug = getIsDebug(parentOption);
483
- HPApi.setDebug(isDebug);
484
-
485
- const registrationConfig: RegistrationConfig = {
486
- label: options.label || getConfig("label") || os.hostname(),
487
- };
488
-
489
- const deviceUpPollingInterval = getDeviceUpPollingInterval(parentOption);
490
-
491
- const healthCheckSetting = getHealthCheckSetting(options);
492
- if (healthCheckSetting.isHealthCheckEnabled) {
493
- startHealthCheckServer(healthCheckSetting.healthCheckPort);
494
- }
495
-
496
- const scanConfig = getScanConfiguration(options);
497
-
498
- await listenCmd(registrationConfig, scanConfig, deviceUpPollingInterval);
499
- });
500
- program.addCommand(cmdListen, { isDefault: true });
501
-
502
- const cmdAdfAutoscan = program.createCommand("adf-autoscan");
503
- setupScanParameters(cmdAdfAutoscan)
504
- .addOption(
505
- new Option("--duplex", "If specified, the scan will be in duplex"),
506
- )
507
- .addOption(
508
- new Option(
509
- "--pdf",
510
- "If specified, the scan result will be a pdf document, the default is multiple jpeg files",
511
- ),
512
- )
513
- .addOption(
514
- new Option(
515
- "--pollingInterval <pollingInterval>",
516
- "Time interval in millisecond between each lookup for content in the automatic document feeder",
517
- ),
518
- )
519
- .description(
520
- "Automatically trigger a new scan job to this target once paper is detected in the automatic document feeder (adf)",
521
- )
522
- .addOption(
523
- new Option(
524
- "--start-scan-delay <startScanDelay>",
525
- "Once document are detected to be in the adf, this specify the wait delay in millisecond before triggering the scan",
526
- ),
527
- )
528
- .addOption(
529
- new Option("--health-check", "Start an http health check endpoint"),
530
- )
531
- .addOption(
532
- new Option(
533
- "--health-check-port <port>",
534
- "Start an http health check endpoint",
535
- ),
536
- )
537
- .description(
538
- "Automatically trigger a new scan job to this target once paper is detected in the automatic document feeder (adf)",
539
- )
540
- .action(async (options, cmd) => {
541
- const parentOption = cmd.parent.opts();
542
-
543
- const ip = await getDeviceIp(parentOption);
544
- HPApi.setDeviceIP(ip);
545
-
546
- const isDebug = getIsDebug(parentOption);
547
- HPApi.setDebug(isDebug);
548
-
549
- const deviceUpPollingInterval = getDeviceUpPollingInterval(parentOption);
550
-
551
- const healthCheckSetting = getHealthCheckSetting(options);
552
- if (healthCheckSetting.isHealthCheckEnabled) {
553
- startHealthCheckServer(healthCheckSetting.healthCheckPort);
554
- }
555
-
556
- const scanConfig = getScanConfiguration(options);
557
-
558
- const adfScanConfig: AdfAutoScanConfig = {
559
- ...scanConfig,
560
- isDuplex: options.isDuplex || getConfig("autoscan_duplex") || false,
561
- generatePdf: options.pdf || getConfig("autoscan_pdf") || false,
562
- pollingInterval:
563
- options.pollingInterval ||
564
- getConfig("autoscan_pollingInterval") ||
565
- 1000,
566
- startScanDelay:
567
- options.startScanDelay ||
568
- getConfig("autoscan_startScanDelay") ||
569
- 5000,
570
- };
571
-
572
- await adfAutoscanCmd(adfScanConfig, deviceUpPollingInterval);
573
- });
574
- program.addCommand(cmdAdfAutoscan);
575
-
576
- const cmdClearRegistrations = program.createCommand("clear-registrations");
577
- cmdClearRegistrations
578
- .description("Clear the list or registered target on the device")
579
- .action(async (options, cmd) => {
580
- await clearRegistrationsCmd(cmd);
581
- });
582
- program.addCommand(cmdClearRegistrations);
583
-
584
- await program.parseAsync(process.argv);
585
- }
586
-
587
- console.log(`Current commit ID: ${commitInfo.commitId}`);
588
- main().catch((err) => console.log(err));