Skip to content

Commit

Permalink
Fixed TLV integer reading code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyxiscoo1 committed Mar 2, 2017
1 parent 3ea21af commit 79d117c
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Source/Core/src/com/shtrih/fiscalprinter/TLVItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Date;

/**
*
* @author V.Kravtsov
*/
public class TLVItem {
Expand Down Expand Up @@ -41,8 +40,9 @@ public int getLevel() {

public long toInt(byte[] d) {
long result = 0;
for (int i = d.length; i <= 0; i--) {
result = result * 0x100 + d[i];
for (int i = d.length-1; i >= 0; i--) {
result <<= 8;
result |= d[i] & 0xFF;
}
return result;
}
Expand All @@ -66,7 +66,7 @@ public String calcTypeToStr(int type) {
}
}

//
//
/*
0 Общая
1 Упрощенная Доход
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -13,9 +16,55 @@ public class TLVItemTests {
public void Should_decode_phrases() throws Exception {
String s = "Мама, мыла; раму!_AaE)";

byte[] data = new byte[]{(byte)0x8C, (byte)0xA0, (byte)0xAC, (byte)0xA0, (byte)0x2C, (byte)0x20, (byte)0xAC, (byte)0xEB, (byte)0xAB, (byte)0xA0, (byte)0x3B, (byte)0x20, (byte)0xE0, (byte)0xA0, (byte)0xAC, (byte)0xE3, (byte)0x21, (byte)0x5F, (byte)0x41, (byte)0x61, (byte)0x45, (byte)0x29} ;
byte[] data = new byte[]{(byte) 0x8C, (byte) 0xA0, (byte) 0xAC, (byte) 0xA0, (byte) 0x2C, (byte) 0x20, (byte) 0xAC, (byte) 0xEB, (byte) 0xAB, (byte) 0xA0, (byte) 0x3B, (byte) 0x20, (byte) 0xE0, (byte) 0xA0, (byte) 0xAC, (byte) 0xE3, (byte) 0x21, (byte) 0x5F, (byte) 0x41, (byte) 0x61, (byte) 0x45, (byte) 0x29};

TLVItem item = new TLVItem(new TLVInfo(666, TLVInfo.TLVType.itASCII), data, 12);
assertEquals(s, item.getText());
}

@Test
public void Should_decode_int() throws Exception {
byte[] data = fsWriteTag(12345);

TLVItem item = new TLVItem(new TLVInfo(666, TLVInfo.TLVType.itVLN), data, 12);
assertEquals(12345, item.toInt(item.getData()));
assertEquals("123,45", item.getText());
}

@Test
public void Should_decode_vln() throws Exception {
byte[] data = new byte[]{-124};

TLVItem item = new TLVItem(new TLVInfo(666, TLVInfo.TLVType.itVLN), data, 12);
assertEquals(132, item.toInt(item.getData()));
assertEquals("1,32", item.getText());
}

@Test
public void Should_decode_vln2() throws Exception {
byte[] data = new byte[]{-124, 0};

TLVItem item = new TLVItem(new TLVInfo(666, TLVInfo.TLVType.itVLN), data, 12);
assertEquals(132, item.toInt(item.getData()));
assertEquals("1,32", item.getText());
}

@Test
public void Should_decode_vln3() throws Exception {
byte[] data = new byte[]{-124, 0, 0, 0};

TLVItem item = new TLVItem(new TLVInfo(666, TLVInfo.TLVType.itVLN), data, 12);
assertEquals(132, item.toInt(item.getData()));
assertEquals("1,32", item.getText());
}

private byte[] fsWriteTag(final int data) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(8);

buffer.order(ByteOrder.LITTLE_ENDIAN);

buffer.putInt(data);

return buffer.array();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.shtrih.fiscalprinter;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
* @author P.Zhirkov
*/
public class TLVReaderTests {

@Test
public void Should_decode_vln_tag() throws Exception {
byte[] data = fsWriteTag(1011, 12345);

TLVReader reader = new TLVReader();
reader.read(data);

List<TLVItem> items = reader.getItems();
assertEquals(1, items.size());

TLVItem item = items.get(0);

assertEquals(12345, item.toInt(item.getData()));
assertEquals("123,45", item.getText());
}

private byte[] fsWriteTag(final int tag, final int data) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(8);

buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort((short)(tag));
buffer.putShort((short)(4));
buffer.putInt(data);

return buffer.array();
}
}
74 changes: 73 additions & 1 deletion Source/FiscalPrinterTest/src/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
/////////////////////////////////////////////////////////////////////

import java.math.BigDecimal;
import java.util.Vector;
import java.io.FileReader;
import java.io.BufferedReader;
Expand Down Expand Up @@ -693,7 +694,7 @@ public void writeCashierName() {

public void printFiscalReceipt()
{
printFiscalReceipt105();
printFiscalReceipt666();
}

public void printPaperReport() {
Expand Down Expand Up @@ -981,6 +982,77 @@ public void printFiscalReceipt105() {
}
}

public void printFiscalReceipt666() {
try {
printer.resetPrinter();
printer.clearLogo();
printer.clearImages();
int numHeaderLines = printer.getNumHeaderLines();
for (int i = 1; i <= numHeaderLines; i++) {
printer.setHeaderLine(i, "Header line " + i, false);
}

long payment = 0;
printer.resetPrinter();
printer.setFiscalReceiptType(jpos.FiscalPrinterConst.FPTR_RT_SALES);
printer.beginFiscalReceipt(false);

double unitPrice = 0.1;
double qty = 1.0;

printer.printRecItem("Водка БонАква сильногаз 1,0л ПЭТ", 0, (int)(qty*1000), 4, (long)(unitPrice*100), "");

payment+=(long)(0.1*100);

printer.fsWriteTag(1074, "8-913-919-1205"); // Телефон платежного агента

printer.printRecTotal(payment, payment, "1");

printer.directIO(0x39, null, "foo@example.com");

printer.fsWriteTLV(new byte[] {-13, 3, 1, 0, -124}); // тег: 1011, длина: 1, значение: 132

printer.endFiscalReceipt(false);

} catch (Exception e) {
e.printStackTrace();
}
}

public void printNonFiscalReceipt666() {
try {
printer.resetPrinter();
printer.clearLogo();
printer.clearImages();
int numHeaderLines = printer.getNumHeaderLines();
for (int i = 1; i <= numHeaderLines; i++) {
printer.setHeaderLine(i, "Header line " + i, false);
}

int howMuch = 1;

long payment = 0;
printer.resetPrinter();
//printer.setFiscalReceiptType(jpos.FiscalPrinterConst.FPTR_RT_SALES);
printer.beginNonFiscal();
for (int i = 0; i < howMuch; i++) {
long price = 1234;
payment += price;

String itemName = "Item 1234";
printer.printRecItem(itemName, price, 0, 0, 0, "");
}
printer.printRecTotal(payment, payment, "1");

//printer.directIO(0x39, null, "foo@example.com");

printer.endFiscalReceipt(false);

} catch (Exception e) {
e.printStackTrace();
}
}

String additionalHeader = "Ваш кассир сегодня:\r\n"
+ "ИВАНИЛОВА Г.Л.\r\n"
+ "*0562 1007/008/011 18.06.14 14:04 AC-00\r\n"
Expand Down

0 comments on commit 79d117c

Please sign in to comment.