kima2/src/printer/posprinter.cpp

217 lines
7.7 KiB
C++
Raw Normal View History

2018-08-05 21:51:30 +02:00
#include "posprinter.h"
2019-10-10 08:09:16 +02:00
#include <core/marketplace.h>
2018-08-06 16:38:55 +02:00
2018-08-06 20:57:12 +02:00
#include <boost/date_time/posix_time/posix_time.hpp>
2018-08-06 16:38:55 +02:00
#include <algorithm>
2018-08-06 16:14:45 +02:00
#include <boost/algorithm/string.hpp>
2018-08-05 21:51:30 +02:00
#include <exception>
#include <iostream>
2018-08-06 12:52:45 +02:00
#include <sstream>
2018-08-05 21:51:30 +02:00
#include <string>
2018-08-06 12:52:45 +02:00
const std::string PosPrinter::Command::RESET = {0x1b, 0x40};
2018-08-15 08:53:04 +02:00
const std::string PosPrinter::Command::ENCODING = {0x1b, 0x74, 16};
const std::string PosPrinter::Command::CENTERING = {0x1b, 0x61, 0x01};
2018-08-06 16:14:45 +02:00
const std::string PosPrinter::Command::LEFT_ALIGN = {0x1b, 0x61, 0x00};
const std::string PosPrinter::Command::RIGHT_ALIGN = {0x1b, 0x61, 0x02};
2018-08-15 08:53:04 +02:00
const std::string PosPrinter::Command::FONT_SIZE_BIG = {0x1b, 0x21, 0x10};
const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {0x1b, 0x21, 0x00};
2018-08-06 12:52:45 +02:00
const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03};
2018-08-15 08:51:39 +02:00
PosPrinter::PosPrinter() : PosPrinter(PrinterDevice()) {}
2018-08-15 08:50:46 +02:00
2018-08-15 08:51:39 +02:00
PosPrinter::PosPrinter(const PrinterDevice& printerDevice) : printerDevice_(printerDevice)
2018-08-05 21:51:30 +02:00
{
int retValue;
retValue = libusb_init(&contextPtr_);
if (retValue < 0) {
throw std::runtime_error("Init error");
}
2018-08-06 09:59:06 +02:00
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");
}
2018-08-15 08:02:28 +02:00
2018-08-06 09:59:06 +02:00
int numDevice = -1;
2018-08-15 08:02:28 +02:00
2018-08-06 09:59:06 +02:00
for (int i = 0; i < devCount; ++i) {
libusb_device_descriptor desc;
libusb_get_device_descriptor(devList[i], &desc);
2018-08-15 08:02:28 +02:00
if (printerDevice_.idVendor == 0) {
for (const auto& supported : supportedPrinters_.models) {
2018-10-12 11:49:52 +02:00
if (desc.idVendor == std::get<0>(supported) &&
desc.idProduct == std::get<1>(supported)) {
2018-08-15 08:02:28 +02:00
numDevice = i;
2018-08-15 10:51:57 +02:00
printerEndpoint_ = std::get<2>(supported);
2018-08-15 08:02:28 +02:00
break;
}
}
} else {
if (desc.idVendor == printerDevice_.idVendor &&
desc.idProduct == printerDevice_.idProduct) {
2018-08-06 09:59:06 +02:00
numDevice = i;
2018-08-15 10:51:57 +02:00
printerEndpoint_ = printerDevice.endpoint;
2018-08-06 09:59:06 +02:00
break;
}
}
}
if (numDevice < 0) {
return;
}
2018-08-05 21:51:30 +02:00
2018-08-06 09:59:06 +02:00
retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID
if (retValue != 0) {
libusb_free_device_list(devList, 1);
libusb_exit(contextPtr_);
2018-08-05 21:51:30 +02:00
throw std::runtime_error("Cannot open printer device");
}
2018-08-06 09:59:06 +02:00
if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached
2018-08-06 21:41:48 +02:00
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");
}
2018-08-06 09:59:06 +02:00
}
retValue = libusb_claim_interface(
devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1)
if (retValue < 0) {
2018-08-06 10:00:22 +02:00
throw std::runtime_error("Cannot claim printer interface");
2018-08-06 09:59:06 +02:00
}
libusb_free_device_list(devList, 1);
2018-08-05 21:51:30 +02:00
}
PosPrinter::~PosPrinter()
{
2018-08-06 20:57:12 +02:00
if (devicePtr_) {
int retValue;
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
if (retValue != 0) {
2018-08-06 21:41:48 +02:00
std::cerr << "Cannot release printer interface" << std::endl;
2018-08-06 20:57:12 +02:00
}
2018-08-05 21:51:30 +02:00
2018-08-06 20:57:12 +02:00
libusb_close(devicePtr_); // close the device we opened
}
if (contextPtr_) {
libusb_exit(contextPtr_); // close the session
}
2018-08-05 21:51:30 +02:00
}
2018-08-06 12:52:45 +02:00
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();
2018-08-06 12:52:45 +02:00
int actual{0};
int retValue =
2018-08-15 10:51:57 +02:00
libusb_bulk_transfer(devicePtr_, (printerEndpoint_ | LIBUSB_ENDPOINT_OUT),
(unsigned char*)text_escaped.c_str(), length, &actual, 10000);
2018-08-06 12:52:45 +02:00
if (retValue != 0 || actual != length)
std::cerr << "Write Error" << std::endl;
}
void PosPrinter::printHeader(const std::string& commune)
2018-08-06 12:52:45 +02:00
{
std::stringstream commandStream;
commandStream << Command::RESET << Command::ENCODING << Command::CENTERING
<< Command::FONT_SIZE_BIG;
commandStream << "Kindersachenmarkt\n" << commune << "\n\n";
2018-08-06 12:52:45 +02:00
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.";
2018-08-06 12:52:45 +02:00
commandStream << Command::FEED;
printHeader();
write(commandStream.str());
2018-08-06 16:14:45 +02:00
}
void PosPrinter::printSaleReceipt(Sale* sale, const std::string& commune)
2018-08-06 16:14:45 +02:00
{
std::stringstream commandStream;
printHeader(commune);
2018-08-06 16:14:45 +02:00
commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN;
2018-08-06 21:31:40 +02:00
commandStream << sale->getTimestampFormatted() << "\n\n";
2018-08-06 16:14:45 +02:00
commandStream << Command::LEFT_ALIGN;
2018-08-06 20:57:12 +02:00
for (const auto& article : sale->getArticles()) {
2018-10-12 11:49:52 +02:00
commandStream << "Verk.Nr. " << article->getSeller()->getSellerNoAsString()
<< "........... " << article->getPriceAsString() << "\n";
2018-08-06 16:14:45 +02:00
}
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());
2018-08-06 16:38:55 +02:00
}
void PosPrinter::printSellerReceipt(Seller* seller, const int percent, const int maxFeeInCent,
const std::string& commune)
2018-08-06 16:38:55 +02:00
{
std::stringstream commandStream;
printHeader(commune);
2018-08-06 16:38:55 +02:00
commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN;
2018-08-06 20:57:12 +02:00
std::string timeStr =
boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time());
2018-08-06 16:38:55 +02:00
commandStream << timeStr << "\n\n";
commandStream << Command::LEFT_ALIGN;
2018-08-06 21:36:26 +02:00
commandStream << "Name: " << seller->getFirstName() << " " << seller->getLastName() << "\n";
2018-08-06 16:38:55 +02:00
commandStream << "Verkäufernummer: " << std::setw(4) << seller->getSellerNo() << "\n\n";
2018-08-06 20:57:12 +02:00
commandStream << "Anzahl Artikel geliefert: " << std::setw(4) << seller->numArticlesOffered()
<< "\n";
commandStream << "Anzahl Artikel verkauft: " << std::setw(4) << seller->numArticlesSold()
<< "\n\n";
2018-08-06 16:38:55 +02:00
if (seller->getArticles(true).size() == 0) {
commandStream << "Verkaufte Artikel:\n *** keine ***\n";
} else {
commandStream << "Verkaufte Artikel:\n";
}
for (auto& article : seller->getArticles(true)) {
2018-08-06 20:57:12 +02:00
commandStream << "Art. " << article->getCompleteArticleNo() << "........... "
<< article->getPriceAsString() << "\n";
2018-08-06 16:38:55 +02:00
}
2018-08-06 21:36:26 +02:00
commandStream << "\nGesamt................. " << seller->sumAsString() << "\n";
commandStream << "./. Gebühr............. "
2018-08-06 21:24:10 +02:00
<< marketFeeAsString(seller->sumInCents(), percent, maxFeeInCent) << "\n";
2018-08-06 21:36:26 +02:00
commandStream << "\nAuszahlung............. "
2018-08-06 21:24:10 +02:00
<< paymentAsString(seller->sumInCents(), percent, maxFeeInCent) << "\n";
2018-08-06 16:38:55 +02:00
commandStream << Command::FEED;
2018-08-06 21:24:10 +02:00
write(commandStream.str());
2018-08-06 20:57:12 +02:00
}
bool PosPrinter::isValid()
{
if (devicePtr_)
return true;
else
return false;
2019-10-10 08:09:16 +02:00
}