mirror of
https://github.com/yawaflua/tzeva-adom.git
synced 2025-12-08 19:39:30 +02:00
Add support to OREF (-o flag)
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/build
|
||||||
|
/cmake-build-debug
|
||||||
|
/.idea
|
||||||
@@ -16,7 +16,14 @@ add_executable(tzeva_adom main.cpp
|
|||||||
utils/audio_play.cpp
|
utils/audio_play.cpp
|
||||||
locale/localization_get.cpp
|
locale/localization_get.cpp
|
||||||
utils/localization_manager.h
|
utils/localization_manager.h
|
||||||
utils/localization_manager.h
|
models/OrefAlertResponse.cpp
|
||||||
|
CONFIG.cpp
|
||||||
|
models/Interfaces/IAlertResponse.cpp
|
||||||
|
models/Interfaces/IAlert.cpp
|
||||||
|
locale/OREF_localization.cpp
|
||||||
|
models/AlertMessage_OREF.cpp
|
||||||
|
utils/OREFToTzevaAdom.cpp
|
||||||
|
models/OrefAlertResponse.cpp
|
||||||
)
|
)
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(GLIB REQUIRED glib-2.0)
|
pkg_check_modules(GLIB REQUIRED glib-2.0)
|
||||||
|
|||||||
16
CONFIG.cpp
Normal file
16
CONFIG.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 30/10/2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#define OREF_URL "https://www.oref.org.il/warningMessages/alert/History/AlertsHistory.json"
|
||||||
|
#define TZEVA_ADOM_URL "https://api.tzevaadom.co.il/alerts-history"
|
||||||
|
#define THREAT_IMAGES_URL "https://www.tzevaadom.co.il/static/images/threat{}.{}"
|
||||||
|
#define BELL_SOUND_URL "https://www.tzevaadom.co.il/static/sounds/bell.mp3"
|
||||||
|
#define CITIES_OREF_URL_RU "https://www.oref.org.il/districts/cities_rus.json"
|
||||||
|
#define CITIES_OREF_URL_EN "https://www.oref.org.il/districts/cities_eng.json"
|
||||||
|
#define CITIES_OREF_URL_HE "https://www.oref.org.il/districts/cities_heb.json"
|
||||||
|
#define CITIES_TZEVA_ADOM "https://www.tzevaadom.co.il/static/cities.json"
|
||||||
|
#define ALERTS_OREF "https://www.oref.org.il/alerts/alertCategories.json"
|
||||||
|
#define ALERT_TRANSLATIONS_OREF "https://www.oref.org.il/alerts/alertsTranslation.json"
|
||||||
|
|
||||||
|
|
||||||
11
lang/ru.json
11
lang/ru.json
@@ -9,6 +9,17 @@
|
|||||||
"threat_7": "Неконвенциональная ракета",
|
"threat_7": "Неконвенциональная ракета",
|
||||||
"threat_8": "предупреждение",
|
"threat_8": "предупреждение",
|
||||||
"threat_9": "Учения Службы Тыла",
|
"threat_9": "Учения Службы Тыла",
|
||||||
|
"missilealert": "Атака ракетами",
|
||||||
|
"uav": "Проникновение беспилотного самолета",
|
||||||
|
"nonconventional": "Неконвенциональная ракета",
|
||||||
|
"warning": "Предупреждение",
|
||||||
|
"earthquakealert1": "Землетрясение",
|
||||||
|
"earthquakealert2": "Землетрясение",
|
||||||
|
"terrorattack": "Подозрение на проникновение террористов",
|
||||||
|
"tsunami": "Цунами",
|
||||||
|
"hazmat": "Утечка опасных веществ",
|
||||||
|
"missilealertdrill": "Учения атаки ракетами",
|
||||||
|
"uavdrill": "Учения атаки беспилотников",
|
||||||
"default": "Неназвано",
|
"default": "Неназвано",
|
||||||
"drill": "упражнения: ",
|
"drill": "упражнения: ",
|
||||||
"true": "да",
|
"true": "да",
|
||||||
|
|||||||
89
locale/OREF_localization.cpp
Normal file
89
locale/OREF_localization.cpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 01/11/2024.
|
||||||
|
//
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
#ifndef OREF_LOCALIZATION_CPP
|
||||||
|
#define OREF_LOCALIZATION_CPP
|
||||||
|
|
||||||
|
#include "models/AlertMessage_OREF.cpp"
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp> // Подключение библиотеки JSON
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include "./CONFIG.cpp"
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
//class AlertMessage;
|
||||||
|
|
||||||
|
class LocalizationManager {
|
||||||
|
private:
|
||||||
|
std::vector<AlertMessage> jsonAlertsTranslate;
|
||||||
|
size_t static WriteCallbackAlerts(void* contents, size_t size, size_t nmemb, std::string* output) {
|
||||||
|
output->append(static_cast<char*>(contents), size * nmemb);
|
||||||
|
return size * nmemb;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string loadLocalizations(std::string lang) {
|
||||||
|
CURL* curl = curl_easy_init();
|
||||||
|
std::string readBuffer;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, ALERT_TRANSLATIONS_OREF);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallbackAlerts);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||||
|
CURLcode res = curl_easy_perform(curl);
|
||||||
|
auto jsonResponseFromLocalization = nlohmann::json::parse(readBuffer);
|
||||||
|
jsonAlertsTranslate = jsonResponseFromLocalization.get<std::vector<AlertMessage>>();
|
||||||
|
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
LocalizationManager() = default;
|
||||||
|
virtual ~LocalizationManager() = default;
|
||||||
|
|
||||||
|
// Загрузить языковой файл
|
||||||
|
bool loadLanguage(std::string lang) {
|
||||||
|
std::string path = std::filesystem::current_path().c_str() + fmt::format("/lang/{}.json", lang);
|
||||||
|
std::ifstream file(path);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
std::cerr << "Failed to open localization file: " << lang << std::endl;
|
||||||
|
std::cerr << "Failed to open localization file: " << path << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json json;
|
||||||
|
file >> json;
|
||||||
|
translations[lang] = json;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Установить текущий язык
|
||||||
|
void setCurrentLanguage(const std::string& langCode) {
|
||||||
|
loadLanguage(langCode);
|
||||||
|
if (translations.find(langCode) != translations.end()) {
|
||||||
|
currentLanguage = langCode;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
std::cerr << "Language code not loaded: " << langCode << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получить переведенную строку по ключу
|
||||||
|
std::string getString(const std::string& key) const {
|
||||||
|
if (!translations.find(currentLanguage)->second.empty()) {
|
||||||
|
auto& langData = translations.at(currentLanguage);
|
||||||
|
if (langData.contains(key)) {
|
||||||
|
return langData[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "Translation not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, nlohmann::json> translations;
|
||||||
|
std::string currentLanguage;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // LOCALIZATION_MANAGER_H
|
||||||
110
main.cpp
110
main.cpp
@@ -10,9 +10,12 @@
|
|||||||
#include <libnotify/notify.h>
|
#include <libnotify/notify.h>
|
||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
#include <boost/algorithm/string/split.hpp>
|
||||||
|
|
||||||
|
#include "CONFIG.cpp"
|
||||||
#include "utils/audio_play.cpp"
|
#include "utils/audio_play.cpp"
|
||||||
#include "models/AlertResponse.cpp"
|
#include "models/AlertResponse.cpp"
|
||||||
|
#include "models/OrefAlertResponse.cpp"
|
||||||
#include "utils/image_downloader.cpp"
|
#include "utils/image_downloader.cpp"
|
||||||
#include "utils/localization_manager.h"
|
#include "utils/localization_manager.h"
|
||||||
|
|
||||||
@@ -20,13 +23,14 @@ class AlertResponse;
|
|||||||
|
|
||||||
tzeva_adom::LocalizationManager localization_manager;
|
tzeva_adom::LocalizationManager localization_manager;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
int16_t lastId = 0;
|
int64_t lastId = 0;
|
||||||
json cities_n_areas_list;
|
json cities_n_areas_list;
|
||||||
bool is_cities_loaded = false;
|
bool is_cities_loaded = false;
|
||||||
bool is_test = false;
|
bool is_test = false;
|
||||||
std::string this_path = boost::filesystem::current_path().c_str();
|
std::string this_path = boost::filesystem::current_path().c_str();
|
||||||
std::string lang = "en";
|
std::string lang = "en";
|
||||||
std::vector<tzeva_adom::Alert> test_alert_variable;
|
std::vector<tzeva_adom::Alert> test_alert_variable;
|
||||||
|
bool is_oref = false;
|
||||||
|
|
||||||
// Функция для записи данных от curl
|
// Функция для записи данных от curl
|
||||||
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
|
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
|
||||||
@@ -52,56 +56,55 @@ void process_alert(const std::string& data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
spdlog::debug("Get first object from answer");
|
spdlog::debug("Get first object from answer");
|
||||||
auto* first_alert = new tzeva_adom::AlertResponseElement(response.at(0));
|
|
||||||
|
|
||||||
spdlog::debug("Answer: {}", first_alert->get_id());
|
std::unique_ptr<tzeva_adom::IAlertResponse> first_alert;
|
||||||
|
if (is_oref) {
|
||||||
auto id = first_alert->get_id();
|
spdlog::debug("OREF_alertDate: {}", response[0]["alertDate"]);
|
||||||
|
spdlog::debug("OREF_title: {}", response[0]["title"]);
|
||||||
if (lastId == 0 || id == lastId) {
|
spdlog::debug("OREF_data: {}", response[0]["data"]);
|
||||||
lastId = id;
|
spdlog::debug("OREF_category: {}",std::to_string(response[0]["category"].get<int>()));
|
||||||
} else {
|
spdlog::debug("OREF selected, create oref object");
|
||||||
is_alert = !is_alert;
|
auto response_oref = response.at(0);
|
||||||
lastId = id;
|
first_alert = std::make_unique<tzeva_adom::OrefAlertResponse>(response_oref["alertDate"], response_oref["title"], response_oref["data"], response_oref["category"]);
|
||||||
spdlog::debug("This is alert!");
|
}
|
||||||
|
else {
|
||||||
|
spdlog::debug("TzevaAdom selected, create object");
|
||||||
|
auto response_tzeva = response.at(0);
|
||||||
|
first_alert = std::make_unique<tzeva_adom::AlertResponseElement>(
|
||||||
|
response_tzeva["id"].get<int>(),
|
||||||
|
response_tzeva["description"].get<json>(),
|
||||||
|
response_tzeva["alerts"].get<json>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto id = first_alert->get_id();
|
||||||
|
spdlog::debug("Answer: {}", id);
|
||||||
|
|
||||||
|
if (!(lastId == 0 || id == lastId)) {
|
||||||
|
is_alert = true;
|
||||||
|
spdlog::debug("This is alert!");
|
||||||
|
}
|
||||||
|
lastId = id;
|
||||||
|
|
||||||
if (is_alert || is_test) {
|
if (is_alert || is_test) {
|
||||||
is_test = !is_test;
|
is_test = false;
|
||||||
// auto description = first_alert["description"].is_string() ?"": first_alert["description"].get<std::string>() ;
|
is_alert = false;
|
||||||
std::string description = "";
|
|
||||||
const std::vector<tzeva_adom::Alert> alerts_array = is_test ? test_alert_variable : first_alert->get_alerts();
|
|
||||||
std::vector<std::string> cities;
|
|
||||||
std::vector<int> threats;
|
|
||||||
|
|
||||||
for (auto alerts: alerts_array) {
|
std::string cities = first_alert->get_cities();
|
||||||
auto time = alerts.get_time();
|
int threat = first_alert->get_threat();
|
||||||
for (auto city: alerts.get_cities()) {
|
|
||||||
cities.insert(cities.begin(), city);
|
|
||||||
}
|
|
||||||
|
|
||||||
threats.insert(threats.begin(), alerts.get_threat());
|
// spdlog::debug("Time: {}", time);
|
||||||
auto isDrill = alerts.get_is_drill();
|
spdlog::debug("Cities: {}", cities);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
spdlog::debug("Time: {}", time);
|
|
||||||
spdlog::debug("Cities: {}", fmt::join(cities, ", "));
|
|
||||||
|
|
||||||
spdlog::debug("Threat Level: {}", alerts.get_threat());
|
|
||||||
spdlog::debug("Is Drill: {}", isDrill);
|
|
||||||
}
|
|
||||||
|
|
||||||
int threat = *std::max_element( threats.begin(), threats.end() );
|
|
||||||
std::string icon_url = boost::filesystem::current_path().c_str()+fmt::format("/threat{}.{}", std::to_string(threat), threat == 0 ? "png" : "svg");
|
std::string icon_url = boost::filesystem::current_path().c_str()+fmt::format("/threat{}.{}", std::to_string(threat), threat == 0 ? "png" : "svg");
|
||||||
|
|
||||||
spdlog::debug("Threat: {}", threat);
|
spdlog::debug("Threat: {}", first_alert->get_threat());
|
||||||
spdlog::debug("Threat name: {}", localization_manager.getString(fmt::format("threat_{}", threat)));
|
spdlog::debug("Threat name: {}", localization_manager.getString(fmt::format("threat_{}", threat)));
|
||||||
spdlog::debug("Language: {}", lang);
|
spdlog::debug("Language: {}", lang);
|
||||||
spdlog::debug("Icon path: {}", icon_url);
|
spdlog::debug("Icon path: {}", icon_url);
|
||||||
|
|
||||||
std::string localised_cities_names = "";
|
std::string localised_cities_names = "";
|
||||||
for (auto city: cities) {
|
for (auto city: first_alert->get_cities_arr()) {
|
||||||
localised_cities_names += fmt::format("{} ", cities_n_areas_list["cities"][city][lang]);
|
localised_cities_names += fmt::format("{} ", cities_n_areas_list["cities"][city][lang]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,8 +136,9 @@ void process_alert(const std::string& data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (...) {
|
||||||
spdlog::error("Alert error: {}", e.what());
|
std::exception_ptr e = std::current_exception();
|
||||||
|
spdlog::error("Alert error: {}", e ? (e.__cxa_exception_type()->name()) : "null");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,9 +154,14 @@ void fetch_alerts_history(std::atomic<bool>& running) {
|
|||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
|
|
||||||
// Parse cities and areas names on he, ru and en
|
// Parse cities and areas names on he, ru and en
|
||||||
|
if (is_oref && lang == "he") {
|
||||||
|
spdlog::error("Lang is not supported. Use TZEVA_ADOM");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!is_cities_loaded) {
|
if (!is_cities_loaded) {
|
||||||
std::string readBuffer;
|
std::string readBuffer;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, "https://www.tzevaadom.co.il/static/cities.json");
|
//Only tzeva_adom variable of cities list, because thats easier to work, than oref variable(without linq)
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, CITIES_TZEVA_ADOM);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||||
|
|
||||||
@@ -169,7 +178,7 @@ void fetch_alerts_history(std::atomic<bool>& running) {
|
|||||||
if (curl) {
|
if (curl) {
|
||||||
std::string readBuffer;
|
std::string readBuffer;
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0");
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0");
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.tzevaadom.co.il/alerts-history");
|
curl_easy_setopt(curl, CURLOPT_URL, !is_oref ? TZEVA_ADOM_URL : OREF_URL);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||||
|
|
||||||
@@ -213,15 +222,6 @@ int main(int argc, char** argv) {
|
|||||||
spdlog::set_level(spdlog::level::debug); // Set global log level to debug if provided --debug
|
spdlog::set_level(spdlog::level::debug); // Set global log level to debug if provided --debug
|
||||||
else if (argv[i] == "-t"sv || argv[i] == "--test") {
|
else if (argv[i] == "-t"sv || argv[i] == "--test") {
|
||||||
is_test = true; // Set tested variable is true
|
is_test = true; // Set tested variable is true
|
||||||
std::vector alert_to_array {tzeva_adom::Alert()};
|
|
||||||
const auto p1 = std::chrono::system_clock::now();
|
|
||||||
ulong t = std::chrono::duration_cast<std::chrono::seconds>(
|
|
||||||
p1.time_since_epoch()).count();
|
|
||||||
std::vector<std::string> city_vectors{"אשדוד -יא,יב,טו,יז,מרינה,סיט", "אשדוד - ח,ט,י,יג,יד,טז"};
|
|
||||||
alert_to_array[0].set_cities(city_vectors);
|
|
||||||
alert_to_array[0].set_time(t);
|
|
||||||
alert_to_array[0].set_threat(0);
|
|
||||||
test_alert_variable.push_back(alert_to_array[0]);
|
|
||||||
}
|
}
|
||||||
else if (argv[i] == "-l"sv || argv[i] == "--lang"sv) {
|
else if (argv[i] == "-l"sv || argv[i] == "--lang"sv) {
|
||||||
is_accept_values = true;
|
is_accept_values = true;
|
||||||
@@ -231,7 +231,6 @@ int main(int argc, char** argv) {
|
|||||||
is_accept_values = false;
|
is_accept_values = false;
|
||||||
lang = argv[i];
|
lang = argv[i];
|
||||||
spdlog::debug("Language setted to {}", lang);
|
spdlog::debug("Language setted to {}", lang);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (argv[i] == "-h"sv || argv[i] == "--help"sv) {
|
else if (argv[i] == "-h"sv || argv[i] == "--help"sv) {
|
||||||
spdlog::info(
|
spdlog::info(
|
||||||
@@ -240,10 +239,15 @@ int main(int argc, char** argv) {
|
|||||||
" -h --help: Show this message\n"\
|
" -h --help: Show this message\n"\
|
||||||
" -d --debug: Show debug messages\n"\
|
" -d --debug: Show debug messages\n"\
|
||||||
" -t --test: Create test alert end exit\n"\
|
" -t --test: Create test alert end exit\n"\
|
||||||
" -l --lang: Choose language: ru, en, he,"
|
" -l --lang: Choose language: ru, en, he\n"\
|
||||||
|
" -o --oref: Use OREF uri`s for alerts"\
|
||||||
"");
|
"");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
else if (argv[i] == "-o"sv || argv[i] == "--oref"sv) {
|
||||||
|
is_oref = true;
|
||||||
|
spdlog::debug("Selected provider: OREF");
|
||||||
|
}
|
||||||
last_flag = argv[i];
|
last_flag = argv[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +257,7 @@ int main(int argc, char** argv) {
|
|||||||
if (!boost::filesystem::exists(this_path+fmt::format("/threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg"))) {
|
if (!boost::filesystem::exists(this_path+fmt::format("/threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg"))) {
|
||||||
tzeva_adom::download_file(
|
tzeva_adom::download_file(
|
||||||
fmt::format(
|
fmt::format(
|
||||||
"https://www.tzevaadom.co.il/static/images/threat{}.{}",
|
THREAT_IMAGES_URL,
|
||||||
std::to_string(i), i == 0 ? "png" : "svg"),
|
std::to_string(i), i == 0 ? "png" : "svg"),
|
||||||
fmt::format("threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg")
|
fmt::format("threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg")
|
||||||
);
|
);
|
||||||
@@ -265,7 +269,7 @@ int main(int argc, char** argv) {
|
|||||||
//Download tzeva-adom bell sound
|
//Download tzeva-adom bell sound
|
||||||
if (!boost::filesystem::exists(this_path+fmt::format("/bell.mp3"))) {
|
if (!boost::filesystem::exists(this_path+fmt::format("/bell.mp3"))) {
|
||||||
tzeva_adom::download_file(
|
tzeva_adom::download_file(
|
||||||
"https://www.tzevaadom.co.il/static/sounds/bell.mp3",
|
BELL_SOUND_URL,
|
||||||
fmt::format("bell.mp3")
|
fmt::format("bell.mp3")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
69
models/AlertMessage_OREF.cpp
Normal file
69
models/AlertMessage_OREF.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 01/11/2024.
|
||||||
|
//
|
||||||
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
class AlertMessage {
|
||||||
|
public:
|
||||||
|
std::string heb;
|
||||||
|
std::string eng;
|
||||||
|
std::string rus;
|
||||||
|
std::string arb;
|
||||||
|
int catId;
|
||||||
|
int matrixCatId;
|
||||||
|
std::string hebTitle;
|
||||||
|
std::string engTitle;
|
||||||
|
std::string rusTitle;
|
||||||
|
std::string arbTitle;
|
||||||
|
|
||||||
|
// AlertMessage(
|
||||||
|
// const std::string& heb, const std::string& eng, const std::string& rus, const std::string& arb,
|
||||||
|
// int catId, int matrixCatId,
|
||||||
|
// const std::string& hebTitle, const std::string& engTitle,
|
||||||
|
// const std::string& rusTitle, const std::string& arbTitle
|
||||||
|
// )
|
||||||
|
// : heb(heb), eng(eng), rus(rus), arb(arb),
|
||||||
|
// catId(catId), matrixCatId(matrixCatId),
|
||||||
|
// hebTitle(hebTitle), engTitle(engTitle),
|
||||||
|
// rusTitle(rusTitle), arbTitle(arbTitle) {}
|
||||||
|
|
||||||
|
AlertMessage() = default;
|
||||||
|
virtual ~AlertMessage() = default;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// JSON сериализация
|
||||||
|
friend void to_json(nlohmann::json& j, const AlertMessage& message) {
|
||||||
|
j = nlohmann::json{
|
||||||
|
{"heb", message.heb},
|
||||||
|
{"eng", message.eng},
|
||||||
|
{"rus", message.rus},
|
||||||
|
{"arb", message.arb},
|
||||||
|
{"catId", message.catId},
|
||||||
|
{"matrixCatId", message.matrixCatId},
|
||||||
|
{"hebTitle", message.hebTitle},
|
||||||
|
{"engTitle", message.engTitle},
|
||||||
|
{"rusTitle", message.rusTitle},
|
||||||
|
{"arbTitle", message.arbTitle}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON десериализация
|
||||||
|
friend void from_json(const nlohmann::json& j, AlertMessage& message) {
|
||||||
|
j.at("heb").get_to(message.heb);
|
||||||
|
j.at("eng").get_to(message.eng);
|
||||||
|
j.at("rus").get_to(message.rus);
|
||||||
|
j.at("arb").get_to(message.arb);
|
||||||
|
j.at("catId").get_to(message.catId);
|
||||||
|
j.at("matrixCatId").get_to(message.matrixCatId);
|
||||||
|
j.at("hebTitle").get_to(message.hebTitle);
|
||||||
|
j.at("engTitle").get_to(message.engTitle);
|
||||||
|
j.at("rusTitle").get_to(message.rusTitle);
|
||||||
|
j.at("arbTitle").get_to(message.arbTitle);
|
||||||
|
}
|
||||||
|
|
||||||
|
using AlertingMessages = std::vector<AlertMessage>;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -11,8 +11,11 @@
|
|||||||
|
|
||||||
#include <boost/optional.hpp>
|
#include <boost/optional.hpp>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "models/Interfaces/IAlertResponse.cpp"
|
||||||
|
#include "models/Interfaces/IAlert.cpp"
|
||||||
|
|
||||||
namespace tzeva_adom {
|
namespace tzeva_adom {
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
@@ -31,9 +34,16 @@ namespace tzeva_adom {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class Alert {
|
class Alert : public IAlert{
|
||||||
public:
|
public:
|
||||||
Alert() = default;
|
Alert() = default;
|
||||||
|
Alert(
|
||||||
|
const int64_t time,
|
||||||
|
const std::vector<std::string>& cities,
|
||||||
|
int64_t threat,
|
||||||
|
bool is_drill
|
||||||
|
) :
|
||||||
|
time(time), cities(cities), threat(threat), is_drill(is_drill) {};
|
||||||
virtual ~Alert() = default;
|
virtual ~Alert() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -48,10 +58,12 @@ namespace tzeva_adom {
|
|||||||
void set_time(const int64_t & value) { this->time = value; }
|
void set_time(const int64_t & value) { this->time = value; }
|
||||||
|
|
||||||
const std::vector<std::string> & get_cities() const { return cities; }
|
const std::vector<std::string> & get_cities() const { return cities; }
|
||||||
|
std::vector<std::string> get_cities() override { return cities; }
|
||||||
std::vector<std::string> & get_mutable_cities() { return cities; }
|
std::vector<std::string> & get_mutable_cities() { return cities; }
|
||||||
void set_cities(const std::vector<std::string> & value) { this->cities = value; }
|
void set_cities(const std::vector<std::string> & value) { this->cities = value; }
|
||||||
|
|
||||||
const int64_t & get_threat() const { return threat; }
|
const int64_t & get_threat() const { return threat; }
|
||||||
|
int get_threat() override { return threat; }
|
||||||
int64_t & get_mutable_threat() { return threat; }
|
int64_t & get_mutable_threat() { return threat; }
|
||||||
void set_threat(const int64_t & value) { this->threat = value; }
|
void set_threat(const int64_t & value) { this->threat = value; }
|
||||||
|
|
||||||
@@ -60,9 +72,25 @@ namespace tzeva_adom {
|
|||||||
void set_is_drill(const bool & value) { this->is_drill = value; }
|
void set_is_drill(const bool & value) { this->is_drill = value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class AlertResponseElement {
|
class AlertResponseElement : public IAlertResponse {
|
||||||
public:
|
public:
|
||||||
AlertResponseElement() = default;
|
AlertResponseElement() = default;
|
||||||
|
AlertResponseElement(
|
||||||
|
const int id,
|
||||||
|
const nlohmann::json description,
|
||||||
|
const nlohmann::json alerts)
|
||||||
|
: id(id), description(description) {
|
||||||
|
for (const auto& alertJson : alerts) {
|
||||||
|
|
||||||
|
int64_t time = alertJson["time"];
|
||||||
|
std::vector<std::string> cities = alertJson["cities"].get<std::vector<std::string>>();
|
||||||
|
int64_t threat = alertJson["threat"];
|
||||||
|
bool is_drill = alertJson["isDrill"];
|
||||||
|
|
||||||
|
Alert _alert = *std::make_unique<Alert>(time, cities, threat, is_drill).get();
|
||||||
|
this->alerts.push_back(_alert);
|
||||||
|
}
|
||||||
|
}
|
||||||
virtual ~AlertResponseElement() = default;
|
virtual ~AlertResponseElement() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -71,10 +99,19 @@ namespace tzeva_adom {
|
|||||||
std::vector<Alert> alerts;
|
std::vector<Alert> alerts;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const int64_t & get_id() const { return id; }
|
int get_id() override { return id; }
|
||||||
|
const int64_t &get_id() const { return id; }
|
||||||
int64_t & get_mutable_id() { return id; }
|
int64_t & get_mutable_id() { return id; }
|
||||||
void set_id(const int64_t & value) { this->id = value; }
|
void set_id(const int64_t & value) { this->id = value; }
|
||||||
|
|
||||||
|
int get_threat() override {
|
||||||
|
int i = 0;
|
||||||
|
for (auto alert: alerts) {
|
||||||
|
i += alert.get_threat();
|
||||||
|
}
|
||||||
|
return i / alerts.size();
|
||||||
|
}
|
||||||
|
|
||||||
const nlohmann::json & get_description() const { return description; }
|
const nlohmann::json & get_description() const { return description; }
|
||||||
nlohmann::json & get_mutable_description() { return description; }
|
nlohmann::json & get_mutable_description() { return description; }
|
||||||
void set_description(const nlohmann::json & value) { this->description = value; }
|
void set_description(const nlohmann::json & value) { this->description = value; }
|
||||||
@@ -82,6 +119,26 @@ namespace tzeva_adom {
|
|||||||
const std::vector<Alert> & get_alerts() const { return alerts; }
|
const std::vector<Alert> & get_alerts() const { return alerts; }
|
||||||
std::vector<Alert> & get_mutable_alerts() { return alerts; }
|
std::vector<Alert> & get_mutable_alerts() { return alerts; }
|
||||||
void set_alerts(const std::vector<Alert> & value) { this->alerts = value; }
|
void set_alerts(const std::vector<Alert> & value) { this->alerts = value; }
|
||||||
|
|
||||||
|
std::string get_cities() override {
|
||||||
|
return fmt::format("{}", fmt::join(get_cities_arr(), ", "));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> get_cities_arr() override {
|
||||||
|
std::vector<std::string> cities_to_return = {} ;
|
||||||
|
for (auto alert : alerts) {
|
||||||
|
cities_to_return.push_back(fmt::format("{}", fmt::join(alert.get_cities(), "")));
|
||||||
|
}
|
||||||
|
return cities_to_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<IAlert> get_alerts() override {
|
||||||
|
std::vector<IAlert> alertsToReturn {};
|
||||||
|
for (IAlert alert : alerts) {
|
||||||
|
alertsToReturn.insert(alertsToReturn.begin(), alert);
|
||||||
|
}
|
||||||
|
return alertsToReturn;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
using AlertResponse = std::vector<AlertResponseElement>;
|
using AlertResponse = std::vector<AlertResponseElement>;
|
||||||
|
|||||||
16
models/Interfaces/IAlert.cpp
Normal file
16
models/Interfaces/IAlert.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 30/10/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
class IAlert {
|
||||||
|
public:
|
||||||
|
virtual ~IAlert() = default;
|
||||||
|
|
||||||
|
virtual std::vector<std::string> get_cities() {};
|
||||||
|
virtual int get_threat() {};
|
||||||
|
};
|
||||||
|
}
|
||||||
24
models/Interfaces/IAlertResponse.cpp
Normal file
24
models/Interfaces/IAlertResponse.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 30/10/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "IAlert.cpp"
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
class IAlertResponse
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// IAlertResponse() = default;
|
||||||
|
virtual ~IAlertResponse() = default;
|
||||||
|
|
||||||
|
virtual int get_id() = 0;
|
||||||
|
virtual int get_threat() = 0;
|
||||||
|
virtual std::string get_cities() = 0;
|
||||||
|
virtual std::vector<std::string> get_cities_arr() = 0;
|
||||||
|
virtual std::vector<IAlert> get_alerts() = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
125
models/OrefAlertResponse.cpp
Normal file
125
models/OrefAlertResponse.cpp
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 30/10/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#ifndef OREFALERTRESPONSE
|
||||||
|
#define OREFALERTRESPONSE
|
||||||
|
|
||||||
|
#include "models/Interfaces/IAlertResponse.cpp"
|
||||||
|
#include "utils/OREFToTzevaAdom.cpp"
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
namespace tzeva_adom{
|
||||||
|
|
||||||
|
class OrefAlert : public IAlert {
|
||||||
|
public:
|
||||||
|
OrefAlert() = default;
|
||||||
|
//virtual ~OrefAlert() = default;
|
||||||
|
|
||||||
|
void set_values(int _threat, std::vector<std::string> _cities) {
|
||||||
|
this->cities = _cities;
|
||||||
|
this->threat = _threat;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> get_cities() override { return cities; }
|
||||||
|
int get_threat() override { return threat; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int threat;
|
||||||
|
std::vector<std::string> cities;
|
||||||
|
};
|
||||||
|
|
||||||
|
class OrefAlertResponse : public IAlertResponse {
|
||||||
|
public:
|
||||||
|
OrefAlertResponse(const std::string& alertDate, const std::string& title,
|
||||||
|
const std::string& data, int64_t threat)
|
||||||
|
: alert_date(alertDate), title(title), data(data), threat(threat) {}
|
||||||
|
virtual ~OrefAlertResponse() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string alert_date;
|
||||||
|
std::string title;
|
||||||
|
std::string data;
|
||||||
|
int64_t threat;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
inline void set_data(std::string alert, std::string title_provided, std::string data_provided, int threat_provided) {
|
||||||
|
alert_date = alert;
|
||||||
|
title = title_provided;
|
||||||
|
data = data_provided;
|
||||||
|
threat = threat_provided;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string & get_alert_date() const { return alert_date; }
|
||||||
|
std::string & get_mutable_alert_date() { return alert_date; }
|
||||||
|
void set_alert_date(const std::string & value) { this->alert_date = value; }
|
||||||
|
|
||||||
|
const std::string & get_title() const { return title; }
|
||||||
|
std::string & get_mutable_title() { return title; }
|
||||||
|
void set_title(const std::string & value) { this->title = value; }
|
||||||
|
|
||||||
|
const std::string & get_data() const { return data; }
|
||||||
|
std::string & get_mutable_data() { return data; }
|
||||||
|
void set_data(const std::string & value) { this->data = value; }
|
||||||
|
|
||||||
|
const int64_t &get_threat() const { return threat; }
|
||||||
|
int get_threat() override { return oref_threat_to_tzeva_adom(threat); }
|
||||||
|
int64_t & get_mutable_threat() { return threat; }
|
||||||
|
void set_threat(const int64_t & value) { this->threat = value; }
|
||||||
|
|
||||||
|
int get_id() override {
|
||||||
|
std::tm t = {};
|
||||||
|
std::istringstream ss(alert_date);
|
||||||
|
|
||||||
|
if (ss >> std::get_time(&t, "%Y-%m-%d %H:%M:%S"))
|
||||||
|
{
|
||||||
|
return std::mktime(&t) % 1000000;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string get_cities() override {return data; };
|
||||||
|
|
||||||
|
std::vector<std::string> get_cities_arr() override { return std::vector {data}; }
|
||||||
|
|
||||||
|
std::vector<IAlert> get_alerts() override {
|
||||||
|
auto orefAlertReturn = new OrefAlert();
|
||||||
|
orefAlertReturn->set_values(threat, get_cities_arr());
|
||||||
|
IAlert iAlertToReturn = static_cast<IAlert>(std::ref(*orefAlertReturn));
|
||||||
|
return std::vector {iAlertToReturn};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
// void from_json(const nlohmann::json & j, OrefAlertResponse & x);
|
||||||
|
// void to_json(nlohmann::json & j, const OrefAlertResponse & x);
|
||||||
|
|
||||||
|
inline void to_json(nlohmann::json& j, const OrefAlertResponse& alert) {
|
||||||
|
j = nlohmann::json{
|
||||||
|
{"alert_date", alert.get_alert_date()},
|
||||||
|
{"title", alert.get_title()},
|
||||||
|
{"data", alert.get_data()},
|
||||||
|
{"threat", alert.get_threat()}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void from_json(const nlohmann::json& j, OrefAlertResponse& alert) {
|
||||||
|
if (j.contains("alert_date")) {
|
||||||
|
alert.set_alert_date(j.at("alert_date").get<std::string>());
|
||||||
|
}
|
||||||
|
if (j.contains("title")) {
|
||||||
|
alert.set_title(j.at("title").get<std::string>());
|
||||||
|
}
|
||||||
|
if (j.contains("data")) {
|
||||||
|
alert.set_data(j.at("data").get<std::string>());
|
||||||
|
}
|
||||||
|
if (j.contains("threat")) {
|
||||||
|
alert.set_threat(j.at("threat").get<int64_t>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
26
utils/OREFToTzevaAdom.cpp
Normal file
26
utils/OREFToTzevaAdom.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by yawaflua on 13/11/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef OREFTOTZEVAADOM_CPP
|
||||||
|
#define OREFTOTZEVAADOM_CPP
|
||||||
|
|
||||||
|
namespace tzeva_adom {
|
||||||
|
inline int oref_threat_to_tzeva_adom(int threat) {
|
||||||
|
switch (threat) {
|
||||||
|
case 1: return 0;
|
||||||
|
case 2: return 5;
|
||||||
|
case 3: return 7;
|
||||||
|
case 4: return 8;
|
||||||
|
case 6:
|
||||||
|
case 7: return 3;
|
||||||
|
case 8: return 0;
|
||||||
|
case 9: return 5;
|
||||||
|
case 10: return 4;
|
||||||
|
case 11: return 6;
|
||||||
|
default: return 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user