-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
209 lines (190 loc) · 5.83 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#if 0
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include "client.hpp"
#include "sstream"
#include <string>
#include <thread>
#include <chrono>
#include "parser.hpp"
#include "clockmanger.hpp"
#include <map>
/*
> GET /v1/timingsByCity?city=Giza&country=Arab%20Rebuplic%20Egypt&method=8 HTTP/1.1
> Host: api.aladhan.com
> User-Agent: curl/7.68.0
> Accept:
Content-Type: application/json
< Content-Length: 1194
**/
int main()
{
Client client;
while (1)
{
client.init("api.aladhan.com", 80);
// get public ip
client.connectToServer();
/* adjust http request*/
std::stringstream ss;
ss << "GET /v1/timingsByCity?city=Giza&country=Arab%20Rebuplic%20Egypt&method=8"
<< " HTTP/1.1\r\n"
<< "Host: api.aladhan.com\r\n"
<< "Content-Type: application/json\r\n"
<< "\r\n\r\n";
std::string request = ss.str();
client.sendrequest(request);
std::string body = client.readRespose();
// std::cout << "body " << body << std::endl;
std::cout << "--------------------------------" << std::endl;
std::cout << " الـصـلــولا تــ، الـخـا مــسـ " << std::endl;
std::cout << "--------------------------------" << std::endl;
std::map<std::string, std::string> ret = Parser::list_prayers(body);
std::cout << "time now : " << ClockManger::Clock_now() << std::endl;
client.closeConnection();
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
}
#endif
#if 1
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include "client.hpp"
#include "sstream"
#include <string>
#include <thread>
#include <chrono>
#include <map>
#include<stdio.h>
#include "parser.hpp"
#include "clockmanger.hpp"
#include <SFML/Audio.hpp>
#include"services.hpp"
#include"UI_Manger.hpp"
using std::cout;
using std::cin;
using std::endl;
std::map<std::string, std::string> prayers;
void play_azan(){
sf::SoundBuffer buf;
if (!buf.loadFromFile(GetCurrentWorkingDir().substr(0,GetCurrentWorkingDir().size()-5)+"/azan.wav")) {//5 represent "build" keyword size
std::cerr << "Fatal error: Could not load sound file.";
return ;
}
sf::Sound sound(buf);
sound.play();
sf::sleep(sf::milliseconds(1000*60*3+6*1000));//audio duration
}
void check_Azan_time(){
while (1)
{
if(ClockManger::Clock_now()==prayers["Dhuhr"] ||
ClockManger::Clock_now()==prayers["Asr"] ||
ClockManger::Clock_now()==prayers["Maghrib"] ||
ClockManger::Clock_now()==prayers["Isha"] ||
ClockManger::Clock_now()==prayers["Fajr"]
){
std::string current_prayer;
for(auto it = prayers.begin(); it != prayers.end(); ++it)
{
if(it->second == ClockManger::Clock_now()){
current_prayer=it->first;
break;
}
}
cout<<"--> current prayer : "<<current_prayer<<endl;
cout<<"--> Come to prayer\n--> Come for the gain\n";
std::thread aa(play_azan);
aa.join();
}
}
}
std::string create_request(std::string city,std::string country,int method){
std::string x = " ", y = "%20";
size_t pos;
while ((pos = city.find(x)) != std::string::npos) {
city.replace(pos, 1, y);
}
while ((pos = country.find(x)) != std::string::npos) {
country.replace(pos, 1, y);
}
std::stringstream ss;
ss << "GET /v1/timingsByCity?city="
<<city
<<"&country="
<<country
<<"&method="
<<(char)('0'+method)
<< " HTTP/1.1\r\n"
<< "Host: api.aladhan.com\r\n"
<< "Content-Type: application/json\r\n"
<< "\r\n\r\n";
return ss.str();
}
void update_prayers(std::map<std::string, std::string> &prayers_,std::string city,std::string country,int method){
std::string request=create_request(city,country,method);
//cout<<request<<endl;
//cin.ignore(); cin.get();
////////////////////////
Client client;
client.init("api.aladhan.com", 80);
// get public ip
client.connectToServer();
client.sendrequest(request);
std::string body = client.readRespose();
///////////////////////
Parser p;
prayers_.clear();
prayers_= p.list_prayers(body);
}
int main()
{
//test
// prayers["Dhuhr"]="00:02";
// prayers["Asr"] ="23:17";
// prayers["Maghrib"] ="23:22";
// prayers["Isha"] ="23:52";
// prayers["Fajr"]="23:57";
//init
std::string city="cairo";
std::string country="Egypt";
int calculation_method=5; //Egyptian General Authority of Survey
update_prayers(prayers,city,country,calculation_method);
std::thread azan_thread(check_Azan_time);
UI_Manger::welcome_message();
int input;
while (1)
{
input=UI_Manger::home_option();
if(input==1){
update_prayers(prayers,city,country,calculation_method);
UI_Manger::show_prayers(prayers);
}else if(input==2){
UI_Manger::show_time(ClockManger::Clock_now());
}else if(input ==3){
UI_Manger::show_current_config(city,country,calculation_method);
}else if(input==4){
UI_Manger::edit_city_country(city,country);
update_prayers(prayers,city,country,calculation_method);
}else if(input==5){
UI_Manger::edit_calc_method(calculation_method);
update_prayers(prayers,city,country,calculation_method);
}else if(input==9){
return 0;
}else{
continue;
}
cout<<endl<<"press Enter to back to Main"<<endl;
cin.ignore(); cin.get();
}
return 0;
}
#endif