-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabellone.cpp
111 lines (101 loc) · 3 KB
/
tabellone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by Alessandro Mascolo on 30/08/23.
//
#include <sstream>
#include "json.hpp"
#include "tabellone.h"
struct {
int w = 0;
int h = 0;
} sz;
void initialize() {
initscr();
noecho();
start_color();
init_pair(2, COLOR_BLACK, COLOR_YELLOW);
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
getmaxyx(stdscr, sz.h, sz.w);
}
void Tabellone::render() {
move(0, 0);
attron(COLOR_PAIR(2));
for (int j = 0; j <= 3; j++) {
hline(' ', sz.w);
move(j, 0);
}
move(1, 0);
printw("PARTENZE");
move(2, 0);
printw("TRENO DESTINAZIONE");
move(2, sz.w - strlen("ORARIO RIT BIN "));
printw("ORARIO RIT BIN ");
draw_time();
attroff(COLOR_PAIR(2));
move(3, 0);
attron(COLOR_PAIR(1));
draw_trains();
attroff(COLOR_PAIR(1));
refresh();
}
using nlohmann::json;
void padTo(std::string &str, const size_t num, const char paddingChar = ' ')
{
if(num > str.size())
str.insert(str.size(), num - str.size(), paddingChar);
}
void Tabellone::draw_trains() {
json data = json::parse(get_partenze());
for (auto & i : data) {
string numTreno = i["compNumeroTreno"];
padTo(numTreno, 9);
printw(numTreno.c_str());
printw(" ");
string destinazione = i["destinazione"];
printw(destinazione.c_str());
int x, y;
getyx(stdscr, y, x);
move(y, sz.w - strlen("ORARIO RIT BIN "));
string orario = i["compOrarioPartenzaZero"];
printw(orario.c_str());
printw(" ");
int ritardo = i["ritardo"];
if (ritardo) printw(std::to_string(ritardo).append("' ").c_str()); else printw(" ");
string binario;
if (i["binarioProgrammatoPartenzaDescrizione"].is_string())
binario = (std::string)i["binarioProgrammatoPartenzaDescrizione"];
printw(binario.c_str());
printw("\n");
}
}
void Tabellone::draw_time() {
move(1, sz.w - 6);
time_t time1 = time(nullptr);
struct tm * curt = gmtime(&time1);
printw("%d:%d", curt->tm_hour, curt->tm_min);
refresh();
}
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp) {
((std::string *) userp)->append((char *) contents, size * nmemb);
return size * nmemb;
}
string Tabellone::get_partenze() {
CURL* curl = curl_easy_init();
std::stringstream ss;
time_t current;
char rfc_2822[40];
time(¤t);
strftime(
rfc_2822,
sizeof(rfc_2822),
"%a, %d %b %Y %T %z",
localtime(¤t));
ss << "http://www.viaggiatreno.it/infomobilita/resteasy/viaggiatreno/partenze/" << stazione << '/' << string(curl_easy_escape(nullptr, rfc_2822, 0));
string url = ss.str();
url.shrink_to_fit();
curl_easy_setopt(curl, CURLOPT_URL, ss.str().c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
string response;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_perform(curl);
return response;
}