seven365-zyprinter 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -23,7 +23,6 @@ npx cap sync
23
23
  * [`printReceipt(...)`](#printreceipt)
24
24
  * [`getPrinterStatus(...)`](#getprinterstatus)
25
25
  * [Interfaces](#interfaces)
26
- * [Type Aliases](#type-aliases)
27
26
 
28
27
  </docgen-index>
29
28
 
@@ -130,12 +129,12 @@ printText(options: { text: string; identifier: string; }) => Promise<{ success:
130
129
  ### printReceipt(...)
131
130
 
132
131
  ```typescript
133
- printReceipt(options: { template: Record<string, any>; identifier: string; }) => Promise<{ success: boolean; }>
132
+ printReceipt(options: { template: ReceiptTemplate; identifier: string; }) => Promise<{ success: boolean; }>
134
133
  ```
135
134
 
136
- | Param | Type |
137
- | ------------- | ----------------------------------------------------------------------------------------------- |
138
- | **`options`** | <code>{ template: <a href="#record">Record</a>&lt;string, any&gt;; identifier: string; }</code> |
135
+ | Param | Type |
136
+ | ------------- | ---------------------------------------------------------------------------------------------- |
137
+ | **`options`** | <code>{ template: <a href="#receipttemplate">ReceiptTemplate</a>; identifier: string; }</code> |
139
138
 
140
139
  **Returns:** <code>Promise&lt;{ success: boolean; }&gt;</code>
141
140
 
@@ -173,14 +172,78 @@ getPrinterStatus(options: { identifier: string; }) => Promise<{ status: string;
173
172
  | **`rssi`** | <code>number</code> |
174
173
 
175
174
 
176
- ### Type Aliases
177
-
178
-
179
- #### Record
180
-
181
- Construct a type with a set of properties K of type T
182
-
183
- <code>{
184
175
  [P in K]: T;
185
176
  }</code>
177
+ #### ReceiptTemplate
178
+
179
+ | Prop | Type |
180
+ | ---------------- | ------------------------------------------------------------------------------ |
181
+ | **`header`** | <code>string</code> |
182
+ | **`items`** | <code><a href="#array">Array</a>&lt;{ name: string; price: string }&gt;</code> |
183
+ | **`total`** | <code>string</code> |
184
+ | **`footer`** | <code>string</code> |
185
+ | **`formatting`** | <code><a href="#receiptformatting">ReceiptFormatting</a></code> |
186
+
187
+
188
+ #### Array
189
+
190
+ | Prop | Type | Description |
191
+ | ------------ | ------------------- | ------------------------------------------------------------------------------------------------------ |
192
+ | **`length`** | <code>number</code> | Gets or sets the length of the array. This is a number one higher than the highest index in the array. |
193
+
194
+ | Method | Signature | Description |
195
+ | ------------------ | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
196
+ | **toString** | () =&gt; string | Returns a string representation of an array. |
197
+ | **toLocaleString** | () =&gt; string | Returns a string representation of an array. The elements are converted to string using their toLocalString methods. |
198
+ | **pop** | () =&gt; T \| undefined | Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. |
199
+ | **push** | (...items: T[]) =&gt; number | Appends new elements to the end of an array, and returns the new length of the array. |
200
+ | **concat** | (...items: <a href="#concatarray">ConcatArray</a>&lt;T&gt;[]) =&gt; T[] | Combines two or more arrays. This method returns a new array without modifying any existing arrays. |
201
+ | **concat** | (...items: (T \| <a href="#concatarray">ConcatArray</a>&lt;T&gt;)[]) =&gt; T[] | Combines two or more arrays. This method returns a new array without modifying any existing arrays. |
202
+ | **join** | (separator?: string \| undefined) =&gt; string | Adds all the elements of an array into a string, separated by the specified separator string. |
203
+ | **reverse** | () =&gt; T[] | Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array. |
204
+ | **shift** | () =&gt; T \| undefined | Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. |
205
+ | **slice** | (start?: number \| undefined, end?: number \| undefined) =&gt; T[] | Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array. |
206
+ | **sort** | (compareFn?: ((a: T, b: T) =&gt; number) \| undefined) =&gt; this | Sorts an array in place. This method mutates the array and returns a reference to the same array. |
207
+ | **splice** | (start: number, deleteCount?: number \| undefined) =&gt; T[] | Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
208
+ | **splice** | (start: number, deleteCount: number, ...items: T[]) =&gt; T[] | Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
209
+ | **unshift** | (...items: T[]) =&gt; number | Inserts new elements at the start of an array, and returns the new length of the array. |
210
+ | **indexOf** | (searchElement: T, fromIndex?: number \| undefined) =&gt; number | Returns the index of the first occurrence of a value in an array, or -1 if it is not present. |
211
+ | **lastIndexOf** | (searchElement: T, fromIndex?: number \| undefined) =&gt; number | Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. |
212
+ | **every** | &lt;S extends T&gt;(predicate: (value: T, index: number, array: T[]) =&gt; value is S, thisArg?: any) =&gt; this is S[] | Determines whether all the members of an array satisfy the specified test. |
213
+ | **every** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; boolean | Determines whether all the members of an array satisfy the specified test. |
214
+ | **some** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; boolean | Determines whether the specified callback function returns true for any element of an array. |
215
+ | **forEach** | (callbackfn: (value: T, index: number, array: T[]) =&gt; void, thisArg?: any) =&gt; void | Performs the specified action for each element in an array. |
216
+ | **map** | &lt;U&gt;(callbackfn: (value: T, index: number, array: T[]) =&gt; U, thisArg?: any) =&gt; U[] | Calls a defined callback function on each element of an array, and returns an array that contains the results. |
217
+ | **filter** | &lt;S extends T&gt;(predicate: (value: T, index: number, array: T[]) =&gt; value is S, thisArg?: any) =&gt; S[] | Returns the elements of an array that meet the condition specified in a callback function. |
218
+ | **filter** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; T[] | Returns the elements of an array that meet the condition specified in a callback function. |
219
+ | **reduce** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T) =&gt; T | Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
220
+ | **reduce** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T, initialValue: T) =&gt; T | |
221
+ | **reduce** | &lt;U&gt;(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) =&gt; U, initialValue: U) =&gt; U | Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
222
+ | **reduceRight** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T) =&gt; T | Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
223
+ | **reduceRight** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T, initialValue: T) =&gt; T | |
224
+ | **reduceRight** | &lt;U&gt;(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) =&gt; U, initialValue: U) =&gt; U | Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
225
+
226
+
227
+ #### ConcatArray
228
+
229
+ | Prop | Type |
230
+ | ------------ | ------------------- |
231
+ | **`length`** | <code>number</code> |
232
+
233
+ | Method | Signature |
234
+ | --------- | ------------------------------------------------------------------ |
235
+ | **join** | (separator?: string \| undefined) =&gt; string |
236
+ | **slice** | (start?: number \| undefined, end?: number \| undefined) =&gt; T[] |
237
+
238
+
239
+ #### ReceiptFormatting
240
+
241
+ | Prop | Type |
242
+ | ---------------- | ---------------------------------------------------------------- |
243
+ | **`headerSize`** | <code>1 \| 2 \| 4 \| 3 \| 'normal' \| 'large' \| 'xlarge'</code> |
244
+ | **`itemSize`** | <code>1 \| 2 \| 4 \| 3 \| 'normal' \| 'large' \| 'xlarge'</code> |
245
+ | **`itemBold`** | <code>boolean</code> |
246
+ | **`totalSize`** | <code>1 \| 2 \| 4 \| 3 \| 'normal' \| 'large' \| 'xlarge'</code> |
247
+ | **`totalBold`** | <code>boolean</code> |
248
+ | **`footerSize`** | <code>1 \| 2 \| 4 \| 3 \| 'normal' \| 'large' \| 'xlarge'</code> |
186
249
 
187
250
  </docgen-api>
188
251
  # seven365-zyprinter-sdk
@@ -247,7 +247,22 @@ public class Zyprint {
247
247
 
248
248
  // Header
249
249
  if (template.has("header")) {
250
+ // Get header size from formatting
251
+ byte sizeCode = 0x00; // Default: normal
252
+ if (template.has("formatting")) {
253
+ JSObject formatting = template.getJSObject("formatting");
254
+ if (formatting != null && formatting.has("headerSize")) {
255
+ sizeCode = mapHeaderSizeToCode(formatting.get("headerSize"));
256
+ }
257
+ }
258
+
259
+ // Set font size (GS ! n)
260
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) sizeCode);
261
+
250
262
  receiptText.append(template.getString("header")).append("\n\n");
263
+
264
+ // Reset to normal size
265
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) 0x00);
251
266
  }
252
267
 
253
268
  // Left align for items
@@ -255,6 +270,27 @@ public class Zyprint {
255
270
 
256
271
  // Items
257
272
  if (template.has("items")) {
273
+ // Get item formatting
274
+ byte itemSizeCode = 0x00;
275
+ boolean itemBold = false;
276
+ if (template.has("formatting")) {
277
+ JSObject formatting = template.getJSObject("formatting");
278
+ if (formatting != null) {
279
+ if (formatting.has("itemSize")) {
280
+ itemSizeCode = mapHeaderSizeToCode(formatting.get("itemSize"));
281
+ }
282
+ itemBold = formatting.optBoolean("itemBold", false);
283
+ }
284
+ }
285
+
286
+ // Apply item formatting
287
+ if (itemBold) {
288
+ receiptText.append((char) 0x1B).append((char) 0x45).append((char) 0x01); // Bold on
289
+ }
290
+ if (itemSizeCode != 0x00) {
291
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) itemSizeCode); // Set size
292
+ }
293
+
258
294
  JSArray items = template.getJSArray("items");
259
295
  for (int i = 0; i < items.length(); i++) {
260
296
  JSObject item = items.getJSObject(i);
@@ -262,11 +298,72 @@ public class Zyprint {
262
298
  String price = item.optString("price", "");
263
299
  receiptText.append(name).append("\t").append(price).append("\n");
264
300
  }
301
+
302
+ // Reset item formatting
303
+ if (itemSizeCode != 0x00) {
304
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) 0x00); // Normal size
305
+ }
306
+ if (itemBold) {
307
+ receiptText.append((char) 0x1B).append((char) 0x45).append((char) 0x00); // Bold off
308
+ }
265
309
  }
266
310
 
267
311
  // Total
268
312
  if (template.has("total")) {
313
+ // Get total formatting
314
+ byte totalSizeCode = 0x00;
315
+ boolean totalBold = false;
316
+ if (template.has("formatting")) {
317
+ JSObject formatting = template.getJSObject("formatting");
318
+ if (formatting != null) {
319
+ if (formatting.has("totalSize")) {
320
+ totalSizeCode = mapHeaderSizeToCode(formatting.get("totalSize"));
321
+ }
322
+ totalBold = formatting.optBoolean("totalBold", false);
323
+ }
324
+ }
325
+
326
+ // Apply total formatting
327
+ if (totalBold) {
328
+ receiptText.append((char) 0x1B).append((char) 0x45).append((char) 0x01); // Bold on
329
+ }
330
+ if (totalSizeCode != 0x00) {
331
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) totalSizeCode); // Set size
332
+ }
333
+
269
334
  receiptText.append("\nTotal: ").append(template.getString("total")).append("\n");
335
+
336
+ // Reset total formatting
337
+ if (totalSizeCode != 0x00) {
338
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) 0x00); // Normal size
339
+ }
340
+ if (totalBold) {
341
+ receiptText.append((char) 0x1B).append((char) 0x45).append((char) 0x00); // Bold off
342
+ }
343
+ }
344
+
345
+ // Footer
346
+ if (template.has("footer")) {
347
+ // Get footer formatting
348
+ byte footerSizeCode = 0x00;
349
+ if (template.has("formatting")) {
350
+ JSObject formatting = template.getJSObject("formatting");
351
+ if (formatting != null && formatting.has("footerSize")) {
352
+ footerSizeCode = mapHeaderSizeToCode(formatting.get("footerSize"));
353
+ }
354
+ }
355
+
356
+ // Apply footer formatting
357
+ if (footerSizeCode != 0x00) {
358
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) footerSizeCode); // Set size
359
+ }
360
+
361
+ receiptText.append(template.getString("footer")).append("\n");
362
+
363
+ // Reset footer formatting
364
+ if (footerSizeCode != 0x00) {
365
+ receiptText.append((char) 0x1D).append((char) 0x21).append((char) 0x00); // Normal size
366
+ }
270
367
  }
271
368
 
272
369
  // Line feeds and cut
@@ -280,6 +377,28 @@ public class Zyprint {
280
377
  }
281
378
  }
282
379
 
380
+ private byte mapHeaderSizeToCode(Object size) {
381
+ if (size instanceof Integer) {
382
+ int sizeInt = (Integer) size;
383
+ switch (sizeInt) {
384
+ case 1: return 0x00;
385
+ case 2: return 0x11;
386
+ case 3: return 0x22;
387
+ case 4: return 0x33;
388
+ default: return 0x00;
389
+ }
390
+ } else if (size instanceof String) {
391
+ String sizeStr = (String) size;
392
+ switch (sizeStr) {
393
+ case "normal": return 0x00;
394
+ case "large": return 0x11;
395
+ case "xlarge": return 0x22;
396
+ default: return 0x00;
397
+ }
398
+ }
399
+ return 0x00;
400
+ }
401
+
283
402
  private static class PrinterConnection {
284
403
  private BluetoothSocket bluetoothSocket;
285
404
  private Socket wifiSocket;