216 lines
7.8 KiB
C++
216 lines
7.8 KiB
C++
#include "posprinter.h"
|
|
|
|
#include <core/marketplace.h>
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <boost/algorithm/string.hpp>
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
const std::string PosPrinter::Command::RESET = {0x1b, 0x40};
|
|
const std::string PosPrinter::Command::ENCODING = {0x1b, 0x74, 16};
|
|
const std::string PosPrinter::Command::CENTERING = {0x1b, 0x61, 0x01};
|
|
const std::string PosPrinter::Command::LEFT_ALIGN = {0x1b, 0x61, 0x00};
|
|
const std::string PosPrinter::Command::RIGHT_ALIGN = {0x1b, 0x61, 0x02};
|
|
const std::string PosPrinter::Command::FONT_SIZE_BIG = {0x1b, 0x21, 0x10};
|
|
const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {0x1b, 0x21, 0x00};
|
|
const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03};
|
|
|
|
PosPrinter::PosPrinter() : PosPrinter(PrinterDevice()) {}
|
|
|
|
PosPrinter::PosPrinter(const PrinterDevice& printerDevice) : printerDevice_(printerDevice)
|
|
{
|
|
int retValue;
|
|
|
|
retValue = libusb_init(&contextPtr_);
|
|
if (retValue < 0) {
|
|
throw std::runtime_error("Init error");
|
|
}
|
|
|
|
libusb_device** devList;
|
|
int devCount = libusb_get_device_list(contextPtr_, &devList);
|
|
if (devCount <= 0) {
|
|
libusb_exit(contextPtr_);
|
|
throw std::runtime_error("Could not receive device list");
|
|
}
|
|
|
|
int numDevice = -1;
|
|
|
|
for (int i = 0; i < devCount; ++i) {
|
|
libusb_device_descriptor desc;
|
|
libusb_get_device_descriptor(devList[i], &desc);
|
|
|
|
if (printerDevice_.idVendor == 0) {
|
|
for (const auto& supported : supportedPrinters_.models) {
|
|
if (desc.idVendor == std::get<0>(supported) &&
|
|
desc.idProduct == std::get<1>(supported)) {
|
|
numDevice = i;
|
|
printerEndpoint_ = std::get<2>(supported);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (desc.idVendor == printerDevice_.idVendor &&
|
|
desc.idProduct == printerDevice_.idProduct) {
|
|
numDevice = i;
|
|
printerEndpoint_ = printerDevice.endpoint;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (numDevice < 0) {
|
|
return;
|
|
}
|
|
|
|
retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID
|
|
if (retValue != 0) {
|
|
libusb_free_device_list(devList, 1);
|
|
libusb_exit(contextPtr_);
|
|
throw std::runtime_error("Cannot open printer device");
|
|
}
|
|
|
|
if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached
|
|
if (libusb_detach_kernel_driver(devicePtr_, 0) != 0) { // detach it
|
|
libusb_free_device_list(devList, 1);
|
|
libusb_exit(contextPtr_);
|
|
throw std::runtime_error("Could not detach kernel printer driver");
|
|
}
|
|
}
|
|
|
|
retValue = libusb_claim_interface(
|
|
devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1)
|
|
if (retValue < 0) {
|
|
throw std::runtime_error("Cannot claim printer interface");
|
|
}
|
|
|
|
libusb_free_device_list(devList, 1);
|
|
}
|
|
|
|
PosPrinter::~PosPrinter()
|
|
{
|
|
if (devicePtr_) {
|
|
int retValue;
|
|
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
|
|
if (retValue != 0) {
|
|
std::cerr << "Cannot release printer interface" << std::endl;
|
|
}
|
|
|
|
libusb_close(devicePtr_); // close the device we opened
|
|
}
|
|
if (contextPtr_) {
|
|
libusb_exit(contextPtr_); // close the session
|
|
}
|
|
}
|
|
|
|
void PosPrinter::write(const std::string& text)
|
|
{
|
|
if (devicePtr_ == NULL)
|
|
return;
|
|
|
|
using namespace std::string_literals;
|
|
std::string text_escaped = text;
|
|
boost::replace_all(text_escaped, "ä", "\xe4");
|
|
boost::replace_all(text_escaped, "ö", "\xf6");
|
|
boost::replace_all(text_escaped, "ü", "\xfc");
|
|
boost::replace_all(text_escaped, "Ä", "\xc4");
|
|
boost::replace_all(text_escaped, "Ö", "\xd6");
|
|
boost::replace_all(text_escaped, "Ü", "\xdc");
|
|
boost::replace_all(text_escaped, "€", "\x80");
|
|
|
|
int length = text_escaped.length();
|
|
int actual{0};
|
|
int retValue =
|
|
libusb_bulk_transfer(devicePtr_, (printerEndpoint_ | LIBUSB_ENDPOINT_OUT),
|
|
(unsigned char*)text_escaped.c_str(), length, &actual, 10000);
|
|
if (retValue != 0 || actual != length)
|
|
std::cerr << "Write Error" << std::endl;
|
|
}
|
|
|
|
void PosPrinter::printHeader(const std::string& commune)
|
|
{
|
|
std::stringstream commandStream;
|
|
|
|
commandStream << Command::RESET << Command::ENCODING << Command::CENTERING
|
|
<< Command::FONT_SIZE_BIG;
|
|
commandStream << "Kindersachenmarkt\n" << commune << "\n\n";
|
|
commandStream << Command::LEFT_ALIGN << Command::Command::FONT_SIZE_NORMAL;
|
|
|
|
write(commandStream.str());
|
|
}
|
|
|
|
void PosPrinter::printTest()
|
|
{
|
|
std::stringstream commandStream;
|
|
commandStream << Command::ENCODING;
|
|
commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n"
|
|
<< "Beachten Sie, dass nicht\nalle Modelle Strichcodes\nausdrucken können.";
|
|
commandStream << Command::FEED;
|
|
printHeader();
|
|
write(commandStream.str());
|
|
}
|
|
|
|
void PosPrinter::printSaleReceipt(Sale* sale, const std::string& commune)
|
|
{
|
|
std::stringstream commandStream;
|
|
printHeader(commune);
|
|
commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN;
|
|
commandStream << sale->getTimestampFormatted() << "\n\n";
|
|
commandStream << Command::LEFT_ALIGN;
|
|
for (const auto& article : sale->getArticles()) {
|
|
commandStream << "Verk.Nr. " << article->getSeller()->getSellerNoAsString()
|
|
<< "........... " << article->getPriceAsString() << "\n";
|
|
}
|
|
commandStream << "\nGesamt................. " << sale->sumAsString() << "\n";
|
|
commandStream << Command::CENTERING;
|
|
commandStream << "\n\nVielen Dank für Ihren Einkauf!";
|
|
commandStream << Command::LEFT_ALIGN << Command::FEED;
|
|
write(commandStream.str());
|
|
}
|
|
|
|
void PosPrinter::printSellerReceipt(Seller* seller, const int percent, const int maxFeeInCent,
|
|
const std::string& commune)
|
|
{
|
|
std::stringstream commandStream;
|
|
printHeader(commune);
|
|
commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN;
|
|
std::string timeStr =
|
|
boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time());
|
|
commandStream << timeStr << "\n\n";
|
|
commandStream << Command::LEFT_ALIGN;
|
|
commandStream << "Name: " << seller->getFirstName() << " " << seller->getLastName() << "\n";
|
|
commandStream << "Verkäufernummer: " << std::setw(4) << seller->getSellerNo() << "\n\n";
|
|
commandStream << "Anzahl Artikel geliefert: " << std::setw(4) << seller->numArticlesOffered()
|
|
<< "\n";
|
|
commandStream << "Anzahl Artikel verkauft: " << std::setw(4) << seller->numArticlesSold()
|
|
<< "\n\n";
|
|
if (seller->getArticles(true).size() == 0) {
|
|
commandStream << "Verkaufte Artikel:\n *** keine ***\n";
|
|
} else {
|
|
commandStream << "Verkaufte Artikel:\n";
|
|
}
|
|
for (auto& article : seller->getArticles(true)) {
|
|
commandStream << "Art. " << article->getCompleteArticleNo() << "........... "
|
|
<< article->getPriceAsString() << "\n";
|
|
}
|
|
commandStream << "\nGesamt................. " << seller->sumAsString() << "\n";
|
|
commandStream << "./. Gebühr............. "
|
|
<< marketFeeAsString(seller->sumInCents(), percent, maxFeeInCent) << "\n";
|
|
commandStream << "\nAuszahlung............. "
|
|
<< paymentAsString(seller->sumInCents(), percent, maxFeeInCent) << "\n";
|
|
commandStream << Command::FEED << Command::FEED;
|
|
|
|
write(commandStream.str());
|
|
}
|
|
|
|
bool PosPrinter::isValid()
|
|
{
|
|
if (devicePtr_)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|