node-hp-scan-to 1.0.1 → 1.2.1

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 (68) hide show
  1. package/README.md +92 -11
  2. package/dist/Destination.d.ts +8 -7
  3. package/dist/Destination.js +115 -84
  4. package/dist/Destination.js.map +1 -1
  5. package/dist/Event.d.ts +21 -17
  6. package/dist/Event.js +46 -30
  7. package/dist/Event.js.map +1 -1
  8. package/dist/EventTable.d.ts +11 -11
  9. package/dist/EventTable.js +26 -26
  10. package/dist/EventTable.js.map +1 -1
  11. package/dist/HPApi.d.ts +42 -29
  12. package/dist/HPApi.js +528 -333
  13. package/dist/HPApi.js.map +1 -1
  14. package/dist/Job.d.ts +36 -24
  15. package/dist/Job.js +94 -62
  16. package/dist/Job.js.map +1 -1
  17. package/dist/JpegUtil.d.ts +26 -0
  18. package/dist/JpegUtil.js +238 -0
  19. package/dist/JpegUtil.js.map +1 -0
  20. package/dist/PathHelper.d.ts +5 -0
  21. package/dist/PathHelper.js +79 -0
  22. package/dist/PathHelper.js.map +1 -0
  23. package/dist/ScanContent.d.ts +12 -0
  24. package/dist/ScanContent.js +82 -0
  25. package/dist/ScanContent.js.map +1 -0
  26. package/dist/ScanJobSettings.d.ts +6 -6
  27. package/dist/ScanJobSettings.js +95 -94
  28. package/dist/ScanJobSettings.js.map +1 -1
  29. package/dist/ScanStatus.d.ts +18 -18
  30. package/dist/ScanStatus.js +35 -29
  31. package/dist/ScanStatus.js.map +1 -1
  32. package/dist/WalkupScanDestination.d.ts +24 -24
  33. package/dist/WalkupScanDestination.js +40 -40
  34. package/dist/WalkupScanDestination.js.map +1 -1
  35. package/dist/WalkupScanDestinations.d.ts +11 -11
  36. package/dist/WalkupScanDestinations.js +26 -26
  37. package/dist/WalkupScanDestinations.js.map +1 -1
  38. package/dist/WalkupScanToCompDestination.d.ts +26 -0
  39. package/dist/WalkupScanToCompDestination.js +44 -0
  40. package/dist/WalkupScanToCompDestination.js.map +1 -0
  41. package/dist/WalkupScanToCompDestinations.d.ts +11 -0
  42. package/dist/WalkupScanToCompDestinations.js +27 -0
  43. package/dist/WalkupScanToCompDestinations.js.map +1 -0
  44. package/dist/WalkupScanToCompEvent.d.ts +10 -0
  45. package/dist/WalkupScanToCompEvent.js +17 -0
  46. package/dist/WalkupScanToCompEvent.js.map +1 -0
  47. package/dist/index.d.ts +2 -2
  48. package/dist/index.js +630 -292
  49. package/dist/index.js.map +1 -1
  50. package/nodemon.json +5 -5
  51. package/package.json +72 -57
  52. package/src/Destination.ts +63 -45
  53. package/src/Event.ts +45 -31
  54. package/src/EventTable.ts +25 -25
  55. package/src/HPApi.ts +365 -222
  56. package/src/Job.ts +100 -62
  57. package/src/JpegUtil.ts +319 -0
  58. package/src/PathHelper.ts +44 -0
  59. package/src/ScanContent.ts +34 -0
  60. package/src/ScanJobSettings.ts +55 -55
  61. package/src/ScanStatus.ts +36 -31
  62. package/src/WalkupScanDestination.ts +46 -44
  63. package/src/WalkupScanDestinations.ts +29 -27
  64. package/src/WalkupScanToCompDestination.ts +55 -0
  65. package/src/WalkupScanToCompDestinations.ts +34 -0
  66. package/src/WalkupScanToCompEvent.ts +18 -0
  67. package/src/index.ts +555 -205
  68. package/tsconfig.json +34 -0
package/dist/index.js CHANGED
@@ -1,293 +1,631 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- var os_1 = __importDefault(require("os"));
43
- var fs_1 = __importDefault(require("fs"));
44
- var path_1 = __importDefault(require("path"));
45
- var util_1 = __importDefault(require("util"));
46
- var bonjour_1 = __importDefault(require("bonjour"));
47
- var Destination_1 = __importDefault(require("./Destination"));
48
- var ScanJobSettings_1 = __importDefault(require("./ScanJobSettings"));
49
- var HPApi_1 = __importDefault(require("./HPApi"));
50
- function delay(t) {
51
- return new Promise(function (resolve) {
52
- setTimeout(resolve, t);
53
- });
54
- }
55
- function waitForScanEvent(resourceURI) {
56
- return __awaiter(this, void 0, void 0, function () {
57
- var eventTable, acceptedScanEvent, currentEtag;
58
- return __generator(this, function (_a) {
59
- switch (_a.label) {
60
- case 0:
61
- console.log("Start listening for new ScanEvent");
62
- return [4 /*yield*/, HPApi_1.default.getEvents()];
63
- case 1:
64
- eventTable = _a.sent();
65
- acceptedScanEvent = null;
66
- currentEtag = eventTable.etag;
67
- _a.label = 2;
68
- case 2:
69
- if (!(acceptedScanEvent == null)) return [3 /*break*/, 4];
70
- return [4 /*yield*/, HPApi_1.default.getEvents(currentEtag, 1200)];
71
- case 3:
72
- eventTable = _a.sent();
73
- currentEtag = eventTable.etag;
74
- acceptedScanEvent = eventTable.eventTable.events.find(function (ev) { return ev.isScanEvent && ev.resourceURI === resourceURI; });
75
- return [3 /*break*/, 2];
76
- case 4: return [2 /*return*/, acceptedScanEvent];
77
- }
78
- });
79
- });
80
- }
81
- function waitPrinterUntilItIsReadyToUploadOrCompleted(jobUrl) {
82
- return __awaiter(this, void 0, void 0, function () {
83
- var job, isReadyToUpload;
84
- return __generator(this, function (_a) {
85
- switch (_a.label) {
86
- case 0:
87
- job = null;
88
- isReadyToUpload = false;
89
- _a.label = 1;
90
- case 1: return [4 /*yield*/, HPApi_1.default.getJob(jobUrl)];
91
- case 2:
92
- job = _a.sent();
93
- isReadyToUpload =
94
- job.pageState === "ReadyToUpload" || job.jobState === "Completed";
95
- return [4 /*yield*/, delay(200)];
96
- case 3:
97
- _a.sent();
98
- _a.label = 4;
99
- case 4:
100
- if (!isReadyToUpload) return [3 /*break*/, 1];
101
- _a.label = 5;
102
- case 5: return [2 /*return*/, job];
103
- }
104
- });
105
- });
106
- }
107
- function register() {
108
- return __awaiter(this, void 0, void 0, function () {
109
- var walkupScanDestinations, hostname, destinations, destination, resourceURI;
110
- return __generator(this, function (_a) {
111
- switch (_a.label) {
112
- case 0: return [4 /*yield*/, HPApi_1.default.getWalkupScanDestinations()];
113
- case 1:
114
- walkupScanDestinations = _a.sent();
115
- hostname = os_1.default.hostname();
116
- destinations = walkupScanDestinations.destinations;
117
- console.log("Host destinations fetched:", destinations.map(function (d) { return d.name; }).join(", "));
118
- destination = destinations.find(function (x) { return x.name === hostname; });
119
- if (!destination) return [3 /*break*/, 2];
120
- console.log("Re-using existing destination: " + hostname + " - " + destination.resourceURI);
121
- resourceURI = destination.resourceURI;
122
- return [3 /*break*/, 4];
123
- case 2: return [4 /*yield*/, HPApi_1.default.registerDestination(new Destination_1.default(hostname, hostname))];
124
- case 3:
125
- resourceURI = _a.sent();
126
- console.log("New Destination registered: " + hostname + " - " + resourceURI);
127
- _a.label = 4;
128
- case 4: return [2 /*return*/, resourceURI];
129
- }
130
- });
131
- });
132
- }
133
- function getNextFile(folder, currentPageNumber) {
134
- return __awaiter(this, void 0, void 0, function () {
135
- return __generator(this, function (_a) {
136
- return [2 /*return*/, path_1.default.join(folder, "scanPage" + currentPageNumber + ".jpg")];
137
- });
138
- });
139
- }
140
- function saveScan(event) {
141
- return __awaiter(this, void 0, void 0, function () {
142
- var destination, folder, scanStatus, inputSource, contentType, scanJobSettings, jobUrl, job, filePath, _a, _b, _c;
143
- return __generator(this, function (_d) {
144
- switch (_d.label) {
145
- case 0: return [4 /*yield*/, HPApi_1.default.getDestination(event.resourceURI)];
146
- case 1:
147
- destination = _d.sent();
148
- return [4 /*yield*/, util_1.default.promisify(fs_1.default.mkdtemp)(path_1.default.join(os_1.default.tmpdir(), "scan-to-pc"))];
149
- case 2:
150
- folder = _d.sent();
151
- console.log("Target folder: " + folder);
152
- console.log("Selected shortcut: " + destination.shortcut);
153
- return [4 /*yield*/, HPApi_1.default.getScanStatus()];
154
- case 3:
155
- scanStatus = _d.sent();
156
- console.log("Afd is : " + scanStatus.adfState);
157
- inputSource = scanStatus.getInputSource();
158
- contentType = destination.getContentType();
159
- scanJobSettings = new ScanJobSettings_1.default(inputSource, contentType);
160
- return [4 /*yield*/, HPApi_1.default.postJob(scanJobSettings)];
161
- case 4:
162
- jobUrl = _d.sent();
163
- console.log("New job created:", jobUrl);
164
- return [4 /*yield*/, HPApi_1.default.getJob(jobUrl)];
165
- case 5:
166
- job = _d.sent();
167
- _d.label = 6;
168
- case 6:
169
- if (!(job.jobState !== "Completed")) return [3 /*break*/, 16];
170
- return [4 /*yield*/, waitPrinterUntilItIsReadyToUploadOrCompleted(jobUrl)];
171
- case 7:
172
- job = _d.sent();
173
- if (job.jobState == "Completed") {
174
- return [3 /*break*/, 6];
175
- }
176
- if (!(job.jobState === "Processing")) return [3 /*break*/, 13];
177
- if (!(job.pageState == "ReadyToUpload" &&
178
- job.binaryURL != null &&
179
- job.currentPageNumber != null)) return [3 /*break*/, 10];
180
- console.log("Ready to download page " + job.currentPageNumber + " at:", job.binaryURL);
181
- _b = (_a = HPApi_1.default).downloadPage;
182
- _c = [job.binaryURL];
183
- return [4 /*yield*/, getNextFile(folder, job.currentPageNumber)];
184
- case 8: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent()]))];
185
- case 9:
186
- filePath = _d.sent();
187
- console.log("Page downloaded to:", filePath);
188
- return [3 /*break*/, 12];
189
- case 10:
190
- console.log("Unknown pageState: " + job.pageState);
191
- return [4 /*yield*/, delay(200)];
192
- case 11:
193
- _d.sent();
194
- _d.label = 12;
195
- case 12: return [3 /*break*/, 15];
196
- case 13:
197
- console.log("Unknown jobState: " + job.jobState);
198
- return [4 /*yield*/, delay(200)];
199
- case 14:
200
- _d.sent();
201
- _d.label = 15;
202
- case 15: return [3 /*break*/, 6];
203
- case 16:
204
- console.log("Job state: " + job.jobState + ", totalPages: " + job.totalPageNumber);
205
- return [2 /*return*/];
206
- }
207
- });
208
- });
209
- }
210
- function init() {
211
- return __awaiter(this, void 0, void 0, function () {
212
- var keepActive, errorCount, resourceURI, event_1, e_1;
213
- return __generator(this, function (_a) {
214
- switch (_a.label) {
215
- case 0:
216
- keepActive = true;
217
- errorCount = 0;
218
- _a.label = 1;
219
- case 1:
220
- if (!keepActive) return [3 /*break*/, 9];
221
- _a.label = 2;
222
- case 2:
223
- _a.trys.push([2, 6, , 7]);
224
- return [4 /*yield*/, register()];
225
- case 3:
226
- resourceURI = _a.sent();
227
- console.log("Waiting scan event for:", resourceURI);
228
- return [4 /*yield*/, waitForScanEvent(resourceURI)];
229
- case 4:
230
- event_1 = _a.sent();
231
- console.log("Scan event captured");
232
- return [4 /*yield*/, saveScan(event_1)];
233
- case 5:
234
- _a.sent();
235
- return [3 /*break*/, 7];
236
- case 6:
237
- e_1 = _a.sent();
238
- errorCount++;
239
- console.error(e_1);
240
- return [3 /*break*/, 7];
241
- case 7:
242
- if (errorCount === 50) {
243
- keepActive = false;
244
- }
245
- return [4 /*yield*/, delay(1000)];
246
- case 8:
247
- _a.sent();
248
- return [3 /*break*/, 1];
249
- case 9: return [2 /*return*/];
250
- }
251
- });
252
- });
253
- }
254
- function findOfficejetIp() {
255
- return new Promise(function (resolve) {
256
- var bonjour = bonjour_1.default();
257
- console.log("Searching printer...");
258
- var browser = bonjour.find({
259
- type: "http"
260
- }, function (service) {
261
- console.log(".");
262
- if (service.name.startsWith("Officejet 6500 E710n-z") &&
263
- service.port === 80 &&
264
- service.type === "http" &&
265
- service.addresses != null) {
266
- browser.stop();
267
- bonjour.destroy();
268
- console.log("Found: " + service.name);
269
- resolve(service.addresses[0]);
270
- }
271
- });
272
- browser.start();
273
- });
274
- }
275
- function main() {
276
- return __awaiter(this, void 0, void 0, function () {
277
- var ip;
278
- return __generator(this, function (_a) {
279
- switch (_a.label) {
280
- case 0: return [4 /*yield*/, findOfficejetIp()];
281
- case 1:
282
- ip = _a.sent();
283
- HPApi_1.default.setPrinterIP(ip);
284
- return [4 /*yield*/, init()];
285
- case 2:
286
- _a.sent();
287
- return [2 /*return*/];
288
- }
289
- });
290
- });
291
- }
292
- main();
1
+ #!/usr/bin/env node
2
+ // noinspection XmlDeprecatedElement,HtmlDeprecatedTag
3
+ "use strict";
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
13
+ var __generator = (this && this.__generator) || function (thisArg, body) {
14
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
15
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
16
+ function verb(n) { return function (v) { return step([n, v]); }; }
17
+ function step(op) {
18
+ if (f) throw new TypeError("Generator is already executing.");
19
+ while (_) try {
20
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
21
+ if (y = 0, t) op = [op[0] & 2, t.value];
22
+ switch (op[0]) {
23
+ case 0: case 1: t = op; break;
24
+ case 4: _.label++; return { value: op[1], done: false };
25
+ case 5: _.label++; y = op[1]; op = [0]; continue;
26
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
27
+ default:
28
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
29
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
30
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
31
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
32
+ if (t[2]) _.ops.pop();
33
+ _.trys.pop(); continue;
34
+ }
35
+ op = body.call(thisArg, _);
36
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
37
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
38
+ }
39
+ };
40
+ var __importDefault = (this && this.__importDefault) || function (mod) {
41
+ return (mod && mod.__esModule) ? mod : { "default": mod };
42
+ };
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ var os_1 = __importDefault(require("os"));
45
+ var promises_1 = __importDefault(require("fs/promises"));
46
+ var commander_1 = require("commander");
47
+ var bonjour_1 = __importDefault(require("bonjour"));
48
+ var Destination_1 = __importDefault(require("./Destination"));
49
+ var ScanJobSettings_1 = __importDefault(require("./ScanJobSettings"));
50
+ var HPApi_1 = __importDefault(require("./HPApi"));
51
+ var JpegUtil_1 = __importDefault(require("./JpegUtil"));
52
+ var PathHelper_1 = __importDefault(require("./PathHelper"));
53
+ var ScanContent_1 = require("./ScanContent");
54
+ var program = new commander_1.Command();
55
+ function delay(t) {
56
+ return new Promise(function (resolve) {
57
+ setTimeout(resolve, t);
58
+ });
59
+ }
60
+ function waitForScanEvent(resourceURI, afterEtag) {
61
+ if (afterEtag === void 0) { afterEtag = null; }
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var eventTable, acceptedScanEvent, currentEtag;
64
+ return __generator(this, function (_a) {
65
+ switch (_a.label) {
66
+ case 0:
67
+ console.log("Start listening for new ScanEvent");
68
+ return [4 /*yield*/, HPApi_1.default.getEvents(afterEtag !== null && afterEtag !== void 0 ? afterEtag : "")];
69
+ case 1:
70
+ eventTable = _a.sent();
71
+ acceptedScanEvent = null;
72
+ currentEtag = eventTable.etag;
73
+ _a.label = 2;
74
+ case 2:
75
+ if (!(acceptedScanEvent == null)) return [3 /*break*/, 4];
76
+ return [4 /*yield*/, HPApi_1.default.getEvents(currentEtag, 1200)];
77
+ case 3:
78
+ eventTable = _a.sent();
79
+ currentEtag = eventTable.etag;
80
+ acceptedScanEvent = eventTable.eventTable.events.find(function (ev) {
81
+ return ev.isScanEvent &&
82
+ ev.destinationURI &&
83
+ ev.destinationURI.indexOf(resourceURI) >= 0;
84
+ });
85
+ return [3 /*break*/, 2];
86
+ case 4: return [2 /*return*/, acceptedScanEvent];
87
+ }
88
+ });
89
+ });
90
+ }
91
+ function waitPrinterUntilItIsReadyToUploadOrCompleted(jobUrl) {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var job, isReadyToUpload;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0:
97
+ job = null;
98
+ isReadyToUpload = false;
99
+ _a.label = 1;
100
+ case 1: return [4 /*yield*/, HPApi_1.default.getJob(jobUrl)];
101
+ case 2:
102
+ job = _a.sent();
103
+ if (job.jobState === "Canceled") {
104
+ return [2 /*return*/, job];
105
+ }
106
+ else if (job.pageState === "ReadyToUpload" ||
107
+ job.jobState === "Completed") {
108
+ isReadyToUpload = true;
109
+ }
110
+ else if (job.jobState == "Processing") {
111
+ isReadyToUpload = false;
112
+ }
113
+ else {
114
+ console.log("Unknown jobState: ".concat(job.jobState));
115
+ }
116
+ return [4 /*yield*/, delay(300)];
117
+ case 3:
118
+ _a.sent();
119
+ _a.label = 4;
120
+ case 4:
121
+ if (!isReadyToUpload) return [3 /*break*/, 1];
122
+ _a.label = 5;
123
+ case 5: return [2 /*return*/, job];
124
+ }
125
+ });
126
+ });
127
+ }
128
+ function register() {
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var destination, hostname, toComp, walkupScanDestinations, destinations, walkupScanDestinations, destinations, resourceURI;
131
+ return __generator(this, function (_a) {
132
+ switch (_a.label) {
133
+ case 0:
134
+ hostname = os_1.default.hostname();
135
+ return [4 /*yield*/, HPApi_1.default.getWalkupScanToCompCaps()];
136
+ case 1:
137
+ toComp = _a.sent();
138
+ if (!toComp) return [3 /*break*/, 3];
139
+ return [4 /*yield*/, HPApi_1.default.getWalkupScanToCompDestinations()];
140
+ case 2:
141
+ walkupScanDestinations = _a.sent();
142
+ destinations = walkupScanDestinations.destinations;
143
+ console.log("Host destinations fetched:", destinations.map(function (d) { return d.name; }).join(", "));
144
+ destination = destinations.find(function (x) { return x.name === hostname; });
145
+ return [3 /*break*/, 5];
146
+ case 3: return [4 /*yield*/, HPApi_1.default.getWalkupScanDestinations()];
147
+ case 4:
148
+ walkupScanDestinations = _a.sent();
149
+ destinations = walkupScanDestinations.destinations;
150
+ console.log("Host destinations fetched:", destinations.map(function (d) { return d.name; }).join(", "));
151
+ destination = destinations.find(function (x) { return x.name === hostname; });
152
+ _a.label = 5;
153
+ case 5:
154
+ if (!destination) return [3 /*break*/, 6];
155
+ console.log("Re-using existing destination: ".concat(hostname, " - ").concat(destination.resourceURI));
156
+ resourceURI = destination.resourceURI;
157
+ return [3 /*break*/, 8];
158
+ case 6: return [4 /*yield*/, HPApi_1.default.registerDestination(new Destination_1.default(hostname, hostname, toComp), toComp)];
159
+ case 7:
160
+ resourceURI = _a.sent();
161
+ console.log("New Destination registered: ".concat(hostname, " - ").concat(resourceURI));
162
+ _a.label = 8;
163
+ case 8: return [2 /*return*/, resourceURI];
164
+ }
165
+ });
166
+ });
167
+ }
168
+ function TryGetDestination(event) {
169
+ return __awaiter(this, void 0, void 0, function () {
170
+ var destination, i, destinationURI, shortcut;
171
+ return __generator(this, function (_a) {
172
+ switch (_a.label) {
173
+ case 0:
174
+ destination = null;
175
+ i = 0;
176
+ _a.label = 1;
177
+ case 1:
178
+ if (!(i < 20)) return [3 /*break*/, 7];
179
+ destinationURI = event.destinationURI;
180
+ if (!destinationURI) return [3 /*break*/, 3];
181
+ return [4 /*yield*/, HPApi_1.default.getDestination(destinationURI)];
182
+ case 2:
183
+ destination = _a.sent();
184
+ shortcut = destination.shortcut;
185
+ if (shortcut !== "") {
186
+ return [2 /*return*/, destination];
187
+ }
188
+ return [3 /*break*/, 4];
189
+ case 3:
190
+ console.log("No destination URI found");
191
+ _a.label = 4;
192
+ case 4:
193
+ console.log("No shortcut yet available, attempt: ".concat(i + 1, "/20"));
194
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
195
+ case 5:
196
+ _a.sent(); //wait 1s
197
+ _a.label = 6;
198
+ case 6:
199
+ i++;
200
+ return [3 /*break*/, 1];
201
+ case 7:
202
+ console.log("Failing to detect destination shortcut");
203
+ console.log(JSON.stringify(destination));
204
+ return [2 /*return*/, null];
205
+ }
206
+ });
207
+ });
208
+ }
209
+ function fixJpegSize(filePath) {
210
+ return __awaiter(this, void 0, void 0, function () {
211
+ var buffer, height;
212
+ return __generator(this, function (_a) {
213
+ switch (_a.label) {
214
+ case 0: return [4 /*yield*/, promises_1.default.readFile(filePath)];
215
+ case 1:
216
+ buffer = _a.sent();
217
+ height = JpegUtil_1.default.fixSizeWithDNL(buffer);
218
+ if (!(height != null)) return [3 /*break*/, 3];
219
+ // rewrite the fixed file
220
+ return [4 /*yield*/, promises_1.default.writeFile(filePath, buffer)];
221
+ case 2:
222
+ // rewrite the fixed file
223
+ _a.sent();
224
+ return [2 /*return*/, height];
225
+ case 3: return [2 /*return*/, null];
226
+ }
227
+ });
228
+ });
229
+ }
230
+ function createScanPage(job, currentPageNumber, filePath, sizeFixed) {
231
+ var height = sizeFixed !== null && sizeFixed !== void 0 ? sizeFixed : job.imageHeight;
232
+ return {
233
+ path: filePath,
234
+ pageNumber: currentPageNumber,
235
+ width: job.imageWidth,
236
+ height: height,
237
+ xResolution: job.xResolution,
238
+ yResolution: job.yResolution,
239
+ };
240
+ }
241
+ function handleProcessingState(job, inputSource, folder, scanCount, currentPageNumber) {
242
+ return __awaiter(this, void 0, void 0, function () {
243
+ var destinationFilePath, filePath, sizeFixed;
244
+ return __generator(this, function (_a) {
245
+ switch (_a.label) {
246
+ case 0:
247
+ if (!(job.pageState == "ReadyToUpload" &&
248
+ job.binaryURL != null &&
249
+ job.currentPageNumber != null)) return [3 /*break*/, 4];
250
+ console.log("Ready to download page job page ".concat(job.currentPageNumber, " at:"), job.binaryURL);
251
+ destinationFilePath = PathHelper_1.default.getFileForPage(folder, scanCount, currentPageNumber, program.opts().pattern, "jpg");
252
+ return [4 /*yield*/, HPApi_1.default.downloadPage(job.binaryURL, destinationFilePath)];
253
+ case 1:
254
+ filePath = _a.sent();
255
+ console.log("Page downloaded to:", filePath);
256
+ sizeFixed = null;
257
+ if (!(inputSource == "Adf")) return [3 /*break*/, 3];
258
+ return [4 /*yield*/, fixJpegSize(filePath)];
259
+ case 2:
260
+ sizeFixed = _a.sent();
261
+ if (sizeFixed == null) {
262
+ console.log("File size has not been fixed, DNF may not have been found and approximate height is: ".concat(job.imageHeight));
263
+ }
264
+ _a.label = 3;
265
+ case 3: return [2 /*return*/, createScanPage(job, currentPageNumber, filePath, sizeFixed)];
266
+ case 4:
267
+ console.log("Unknown pageState: ".concat(job.pageState));
268
+ return [4 /*yield*/, delay(200)];
269
+ case 5:
270
+ _a.sent();
271
+ return [2 /*return*/, null];
272
+ }
273
+ });
274
+ });
275
+ }
276
+ function waitScanRequest(compEventURI) {
277
+ return __awaiter(this, void 0, void 0, function () {
278
+ var waitMax, i, walkupScanToCompEvent, message;
279
+ return __generator(this, function (_a) {
280
+ switch (_a.label) {
281
+ case 0:
282
+ waitMax = 50;
283
+ i = 0;
284
+ _a.label = 1;
285
+ case 1:
286
+ if (!(i < waitMax)) return [3 /*break*/, 5];
287
+ return [4 /*yield*/, HPApi_1.default.getWalkupScanToCompEvent(compEventURI)];
288
+ case 2:
289
+ walkupScanToCompEvent = _a.sent();
290
+ message = walkupScanToCompEvent.eventType;
291
+ if (message === "HostSelected") {
292
+ // this ok to wait
293
+ }
294
+ else if (message === "ScanRequested") {
295
+ return [3 /*break*/, 5];
296
+ }
297
+ else if (message === "ScanNewPageRequested") {
298
+ return [3 /*break*/, 5];
299
+ }
300
+ else if (message === "ScanPagesComplete") {
301
+ console.log("no more page to scan, scan is finished");
302
+ return [2 /*return*/, false];
303
+ }
304
+ else {
305
+ console.log("Unknown eventType: ".concat(message));
306
+ return [2 /*return*/, false];
307
+ }
308
+ console.log("Waiting user input: ".concat(i + 1, "/").concat(waitMax));
309
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
310
+ case 3:
311
+ _a.sent(); //wait 1s
312
+ _a.label = 4;
313
+ case 4:
314
+ i++;
315
+ return [3 /*break*/, 1];
316
+ case 5: return [2 /*return*/, true];
317
+ }
318
+ });
319
+ });
320
+ }
321
+ function executeScanJob(scanJobSettings, inputSource, folder, scanCount, scanJobContent) {
322
+ return __awaiter(this, void 0, void 0, function () {
323
+ var jobUrl, job, page;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0: return [4 /*yield*/, HPApi_1.default.postJob(scanJobSettings)];
327
+ case 1:
328
+ jobUrl = _a.sent();
329
+ console.log("New job created:", jobUrl);
330
+ return [4 /*yield*/, HPApi_1.default.getJob(jobUrl)];
331
+ case 2:
332
+ job = _a.sent();
333
+ _a.label = 3;
334
+ case 3:
335
+ if (!(job.jobState !== "Completed")) return [3 /*break*/, 10];
336
+ return [4 /*yield*/, waitPrinterUntilItIsReadyToUploadOrCompleted(jobUrl)];
337
+ case 4:
338
+ job = _a.sent();
339
+ if (job.jobState == "Completed") {
340
+ return [3 /*break*/, 3];
341
+ }
342
+ if (!(job.jobState === "Processing")) return [3 /*break*/, 6];
343
+ return [4 /*yield*/, handleProcessingState(job, inputSource, folder, scanCount, scanJobContent.elements.length + 1)];
344
+ case 5:
345
+ page = _a.sent();
346
+ if (page != null) {
347
+ scanJobContent.elements.push(page);
348
+ }
349
+ return [3 /*break*/, 9];
350
+ case 6:
351
+ if (!(job.jobState === "Canceled")) return [3 /*break*/, 7];
352
+ console.log("Job cancelled by device");
353
+ return [3 /*break*/, 10];
354
+ case 7:
355
+ console.log("Unhandled jobState: ".concat(job.jobState));
356
+ return [4 /*yield*/, delay(200)];
357
+ case 8:
358
+ _a.sent();
359
+ _a.label = 9;
360
+ case 9: return [3 /*break*/, 3];
361
+ case 10:
362
+ console.log("Job state: ".concat(job.jobState, ", totalPages: ").concat(job.totalPageNumber, ":"));
363
+ return [2 /*return*/];
364
+ }
365
+ });
366
+ });
367
+ }
368
+ function waitScanNewPageRequest(compEventURI) {
369
+ return __awaiter(this, void 0, void 0, function () {
370
+ var startNewScanJob, wait, walkupScanToCompEvent, message;
371
+ return __generator(this, function (_a) {
372
+ switch (_a.label) {
373
+ case 0:
374
+ startNewScanJob = false;
375
+ wait = true;
376
+ _a.label = 1;
377
+ case 1:
378
+ if (!wait) return [3 /*break*/, 4];
379
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
380
+ case 2:
381
+ _a.sent(); //wait 1s
382
+ return [4 /*yield*/, HPApi_1.default.getWalkupScanToCompEvent(compEventURI)];
383
+ case 3:
384
+ walkupScanToCompEvent = _a.sent();
385
+ message = walkupScanToCompEvent.eventType;
386
+ if (message === "ScanNewPageRequested") {
387
+ startNewScanJob = true;
388
+ wait = false;
389
+ }
390
+ else if (message === "ScanPagesComplete") {
391
+ wait = false;
392
+ }
393
+ else if (message === "ScanRequested") {
394
+ // continue waiting
395
+ }
396
+ else {
397
+ wait = false;
398
+ console.log("Unknown eventType: ".concat(message));
399
+ }
400
+ return [3 /*break*/, 1];
401
+ case 4: return [2 /*return*/, startNewScanJob];
402
+ }
403
+ });
404
+ });
405
+ }
406
+ function executeScanJobs(scanJobSettings, inputSource, folder, scanCount, scanJobContent, firstEvent) {
407
+ return __awaiter(this, void 0, void 0, function () {
408
+ var lastEvent, startNewScanJob;
409
+ return __generator(this, function (_a) {
410
+ switch (_a.label) {
411
+ case 0: return [4 /*yield*/, executeScanJob(scanJobSettings, inputSource, folder, scanCount, scanJobContent)];
412
+ case 1:
413
+ _a.sent();
414
+ lastEvent = firstEvent;
415
+ if (!(lastEvent.compEventURI &&
416
+ inputSource !== "Adf" &&
417
+ lastEvent.destinationURI)) return [3 /*break*/, 8];
418
+ return [4 /*yield*/, waitForScanEvent(lastEvent.destinationURI, lastEvent.agingStamp)];
419
+ case 2:
420
+ lastEvent = _a.sent();
421
+ if (!lastEvent.compEventURI) {
422
+ return [2 /*return*/];
423
+ }
424
+ return [4 /*yield*/, waitScanNewPageRequest(lastEvent.compEventURI)];
425
+ case 3:
426
+ startNewScanJob = _a.sent();
427
+ _a.label = 4;
428
+ case 4:
429
+ if (!startNewScanJob) return [3 /*break*/, 8];
430
+ return [4 /*yield*/, executeScanJob(scanJobSettings, inputSource, folder, scanCount, scanJobContent)];
431
+ case 5:
432
+ _a.sent();
433
+ if (!lastEvent.destinationURI) {
434
+ return [3 /*break*/, 8];
435
+ }
436
+ return [4 /*yield*/, waitForScanEvent(lastEvent.destinationURI, lastEvent.agingStamp)];
437
+ case 6:
438
+ lastEvent = _a.sent();
439
+ if (!lastEvent.compEventURI) {
440
+ return [2 /*return*/];
441
+ }
442
+ return [4 /*yield*/, waitScanNewPageRequest(lastEvent.compEventURI)];
443
+ case 7:
444
+ startNewScanJob = _a.sent();
445
+ return [3 /*break*/, 4];
446
+ case 8: return [2 /*return*/];
447
+ }
448
+ });
449
+ });
450
+ }
451
+ function mergeToPdf(folder, scanCount, scanJobContent) {
452
+ return __awaiter(this, void 0, void 0, function () {
453
+ var pdfFilePath;
454
+ return __generator(this, function (_a) {
455
+ switch (_a.label) {
456
+ case 0:
457
+ pdfFilePath = PathHelper_1.default.getFileForScan(folder, scanCount, program.opts().pattern, "pdf");
458
+ return [4 /*yield*/, (0, ScanContent_1.createPdfFrom)(scanJobContent, pdfFilePath)];
459
+ case 1:
460
+ _a.sent();
461
+ scanJobContent.elements.forEach(function (e) { return promises_1.default.unlink(e.path); });
462
+ return [2 /*return*/, pdfFilePath];
463
+ }
464
+ });
465
+ });
466
+ }
467
+ function displayPdfScan(pdfFilePath, scanJobContent) {
468
+ console.log("The following page(s) have been rendered inside '".concat(pdfFilePath, "': "));
469
+ scanJobContent.elements.forEach(function (e) {
470
+ return console.log("\t- page ".concat(e.pageNumber.toString().padStart(3, " "), " - ").concat(e.width, "x").concat(e.height));
471
+ });
472
+ }
473
+ function displayJpegScan(scanJobContent) {
474
+ scanJobContent.elements.forEach(function (e) {
475
+ return console.log("\t- page ".concat(e.pageNumber.toString().padStart(3, " "), " - ").concat(e.width, "x").concat(e.height, " - ").concat(e.path));
476
+ });
477
+ }
478
+ function saveScan(event, folder, scanCount) {
479
+ return __awaiter(this, void 0, void 0, function () {
480
+ var proceedToScan, destination, contentType, toPdf, scanStatus, inputSource, scanJobSettings, scanJobContent, pdfFilePath;
481
+ return __generator(this, function (_a) {
482
+ switch (_a.label) {
483
+ case 0:
484
+ if (!event.compEventURI) return [3 /*break*/, 2];
485
+ return [4 /*yield*/, waitScanRequest(event.compEventURI)];
486
+ case 1:
487
+ proceedToScan = _a.sent();
488
+ if (!proceedToScan) {
489
+ return [2 /*return*/];
490
+ }
491
+ _a.label = 2;
492
+ case 2: return [4 /*yield*/, TryGetDestination(event)];
493
+ case 3:
494
+ destination = _a.sent();
495
+ if (!destination) {
496
+ console.log("No shortcut selected!");
497
+ return [2 /*return*/];
498
+ }
499
+ console.log("Selected shortcut: " + destination.shortcut);
500
+ contentType = destination.getContentType();
501
+ toPdf = destination.shortcut === "SavePDF" || destination.shortcut === "EmailPDF";
502
+ return [4 /*yield*/, HPApi_1.default.getScanStatus()];
503
+ case 4:
504
+ scanStatus = _a.sent();
505
+ console.log("Afd is : " + scanStatus.adfState);
506
+ inputSource = scanStatus.getInputSource();
507
+ scanJobSettings = new ScanJobSettings_1.default(inputSource, contentType);
508
+ scanJobContent = { elements: [] };
509
+ return [4 /*yield*/, executeScanJobs(scanJobSettings, inputSource, folder, scanCount, scanJobContent, event)];
510
+ case 5:
511
+ _a.sent();
512
+ console.log("Scan of page(s) completed totalPages: ".concat(scanJobContent.elements.length, ":"));
513
+ if (!toPdf) return [3 /*break*/, 7];
514
+ return [4 /*yield*/, mergeToPdf(folder, scanCount, scanJobContent)];
515
+ case 6:
516
+ pdfFilePath = _a.sent();
517
+ displayPdfScan(pdfFilePath, scanJobContent);
518
+ return [3 /*break*/, 8];
519
+ case 7:
520
+ displayJpegScan(scanJobContent);
521
+ _a.label = 8;
522
+ case 8: return [2 /*return*/];
523
+ }
524
+ });
525
+ });
526
+ }
527
+ var iteration = 0;
528
+ function init() {
529
+ return __awaiter(this, void 0, void 0, function () {
530
+ var folder, scanCount, keepActive, errorCount, resourceURI, event_1, e_1;
531
+ return __generator(this, function (_a) {
532
+ switch (_a.label) {
533
+ case 0: return [4 /*yield*/, PathHelper_1.default.getOutputFolder(program.opts().directory)];
534
+ case 1:
535
+ folder = _a.sent();
536
+ console.log("Target folder: ".concat(folder));
537
+ scanCount = 0;
538
+ keepActive = true;
539
+ errorCount = 0;
540
+ _a.label = 2;
541
+ case 2:
542
+ if (!keepActive) return [3 /*break*/, 10];
543
+ console.log("Running iteration: ".concat(iteration, " - errorCount: ").concat(errorCount));
544
+ _a.label = 3;
545
+ case 3:
546
+ _a.trys.push([3, 7, , 8]);
547
+ return [4 /*yield*/, register()];
548
+ case 4:
549
+ resourceURI = _a.sent();
550
+ console.log("Waiting scan event for:", resourceURI);
551
+ return [4 /*yield*/, waitForScanEvent(resourceURI)];
552
+ case 5:
553
+ event_1 = _a.sent();
554
+ scanCount++;
555
+ console.log("Scan event captured, saving scan #".concat(scanCount));
556
+ return [4 /*yield*/, saveScan(event_1, folder, scanCount)];
557
+ case 6:
558
+ _a.sent();
559
+ return [3 /*break*/, 8];
560
+ case 7:
561
+ e_1 = _a.sent();
562
+ errorCount++;
563
+ console.error(e_1);
564
+ console.log(e_1);
565
+ return [3 /*break*/, 8];
566
+ case 8:
567
+ if (errorCount === 50) {
568
+ keepActive = false;
569
+ }
570
+ return [4 /*yield*/, delay(1000)];
571
+ case 9:
572
+ _a.sent();
573
+ return [3 /*break*/, 2];
574
+ case 10: return [2 /*return*/];
575
+ }
576
+ });
577
+ });
578
+ }
579
+ function findOfficejetIp() {
580
+ return new Promise(function (resolve) {
581
+ var bonjour = (0, bonjour_1.default)();
582
+ console.log("Searching printer...");
583
+ var browser = bonjour.find({
584
+ type: "http",
585
+ }, function (service) {
586
+ console.log(".");
587
+ if (service.name.startsWith(program.opts().name) &&
588
+ service.port === 80 &&
589
+ service.type === "http" &&
590
+ service.addresses != null) {
591
+ browser.stop();
592
+ bonjour.destroy();
593
+ console.log("Found: ".concat(service.name));
594
+ resolve(service.addresses[0]);
595
+ }
596
+ });
597
+ browser.start();
598
+ });
599
+ }
600
+ function main() {
601
+ return __awaiter(this, void 0, void 0, function () {
602
+ var ip, debug;
603
+ return __generator(this, function (_a) {
604
+ switch (_a.label) {
605
+ case 0:
606
+ program.option("-ip, --address <ip>", "IP address of the printer (this overrides -p)");
607
+ program.option("-n, --name <name>", "Name of the printer for service discovery", "HP Smart Tank Plus 570 series"); //or i.e. 'Deskjet 3520 series'
608
+ program.option("-d, --directory <dir>", "Directory where scans are saved (defaults to /tmp/scan-to-pc<random>)");
609
+ program.option("-p, --pattern <pattern>", 'Pattern for filename (i.e. "scan"_dd.mm.yyyy_hh:MM:ss, without this its scanPage<number>)');
610
+ program.option("-D, --debug", "Enable debug");
611
+ program.parse(process.argv);
612
+ ip = program.opts().address || "192.168.1.53";
613
+ if (!!ip) return [3 /*break*/, 2];
614
+ return [4 /*yield*/, findOfficejetIp()];
615
+ case 1:
616
+ ip = _a.sent();
617
+ _a.label = 2;
618
+ case 2:
619
+ debug = program.opts().debug != null;
620
+ HPApi_1.default.setDebug(debug);
621
+ HPApi_1.default.setPrinterIP(ip);
622
+ return [4 /*yield*/, init()];
623
+ case 3:
624
+ _a.sent();
625
+ return [2 /*return*/];
626
+ }
627
+ });
628
+ });
629
+ }
630
+ main();
293
631
  //# sourceMappingURL=index.js.map