2023-04-24 15:31:46 +02:00
|
|
|
#include "calculate_price.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "calculator_functions.h"
|
|
|
|
#include "payment_option.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
static Calculator calculator;
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
int CALCULATE_LIBRARY_API get_zone_nr() {
|
2023-04-24 15:31:46 +02:00
|
|
|
QFile zone("/etc/zone_nr");
|
|
|
|
if (zone.exists()) {
|
|
|
|
QFileInfo finfo(zone);
|
|
|
|
if (finfo.size() <= 4) { // decimal 000\n
|
|
|
|
if (zone.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2023-05-05 13:19:02 +02:00
|
|
|
|
|
|
|
QTextStream in(&zone);
|
|
|
|
return in.readLine(100).toInt();
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
bool CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff, char const *config_file) {
|
2023-04-24 15:31:46 +02:00
|
|
|
*tariff = new Configuration();
|
|
|
|
|
|
|
|
int const zone = get_zone_nr();
|
2023-05-05 13:19:02 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << "init_tariff:";
|
|
|
|
qCritical() << " ... zone = " << zone;
|
|
|
|
|
|
|
|
if (zone <= 0) {
|
2023-04-24 15:31:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString confFile(config_file);
|
|
|
|
if (!confFile.endsWith(QChar('/'))) {
|
|
|
|
confFile += "/";
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:19:02 +02:00
|
|
|
char buffer[32];
|
2023-04-24 15:31:46 +02:00
|
|
|
memset(buffer, 0x00, sizeof(buffer));
|
|
|
|
snprintf(buffer, sizeof(buffer)-1, "tariff%02d.json", zone);
|
|
|
|
confFile += buffer;
|
|
|
|
|
2023-05-05 13:19:36 +02:00
|
|
|
// DEBUG
|
|
|
|
qCritical() << " ... confFile = " << confFile;
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
QFile fname(confFile);
|
|
|
|
if (fname.exists()) {
|
2023-05-05 13:19:36 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << " ... confFile exists";
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
if (fname.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2023-05-05 13:19:36 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << " ... confFile is open";
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
QString json = fname.readAll();
|
|
|
|
return (*tariff)->ParseJson(*tariff, json.toStdString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:19:36 +02:00
|
|
|
qCritical() << "init_tariff: Parsing tariff config (" << confFile << ") failed!";
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
|
2023-04-24 15:31:46 +02:00
|
|
|
delete tariff;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
bool CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
2023-04-24 15:31:46 +02:00
|
|
|
time_t start_parking_time, // in minutes
|
|
|
|
time_t end_parking_time, // in minutes
|
|
|
|
struct price_t *price) {
|
|
|
|
int const duration = end_parking_time - start_parking_time;
|
|
|
|
if (duration > 0) {
|
|
|
|
QDate const d(1970, 1, 1);
|
|
|
|
QTime const t(0, 0, 0);
|
|
|
|
QDateTime start(d, t, Qt::UTC);
|
|
|
|
start = start.toLocalTime().addSecs(start_parking_time * 60);
|
|
|
|
if (start.isValid()) {
|
|
|
|
QString cs = start.toString(Qt::ISODate);
|
|
|
|
double cost = calculator.GetCostFromDuration(
|
|
|
|
tariff, PaymentOption::Option1,
|
|
|
|
cs.toLocal8Bit().constData(),
|
|
|
|
duration, false, true);
|
|
|
|
price->units = cost;
|
|
|
|
price->netto = cost;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (duration == 0) {
|
|
|
|
memset(price, 0x00, sizeof(*price));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qCritical() << "ERROR start_parking_time (" << start_parking_time << ") <"
|
|
|
|
<< "end_parking_time (" << end_parking_time << ")";
|
|
|
|
return false;
|
|
|
|
}
|