-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtimerconflict.cpp
226 lines (199 loc) · 7.74 KB
/
timerconflict.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "timerconflict.h"
#include "tools.h"
#include "epgsearch/services.h"
// STL headers need to be before VDR tools.h (included by <vdr/plugin.h>)
#include <vector>
#include <vdr/timers.h>
#include <vdr/plugin.h>
#include <vdr/svdrp.h>
namespace vdrlive {
bool CheckEpgsearchVersion();
static char ServiceInterface[] = "Epgsearch-services-v1.1";
bool operator<( TimerConflict const& left, TimerConflict const& right )
{
return left.conflictTime < right.conflictTime;
}
TimerConflict::TimerConflict()
{
Init();
}
void TimerConflict::Init()
{
conflictTime = 0;
}
TimerConflict::TimerConflict( std::string const& data )
{
Init();
// dsyslog("live: TimerConflict() data '%s'", data.c_str());
std::vector<std::string> parts = StringSplit( data, ':' );
try {
std::vector<std::string>::const_iterator part = parts.begin();
if (parts.size() > 0) {
conflictTime = parse_int<time_t>( *part++ );
for ( int i = 1; part != parts.end(); ++i, ++part ) {
std::vector<std::string> timerparts = StringSplit( *part, '|' );
std::vector<std::string>::const_iterator timerpart = timerparts.begin();
TimerInConflict timer;
for ( int j = 0; timerpart != timerparts.end(); ++j, ++timerpart ) {
switch (j) {
case 0: timer.timerIndex = parse_int<int>( *timerpart ); break;
case 1: timer.percentage = parse_int<int>( *timerpart ); break;
case 2: {
std::vector<std::string> conctimerparts = StringSplit( *timerpart, '#' );
std::vector<std::string>::const_iterator conctimerpart = conctimerparts.begin();
for ( int k = 0; conctimerpart != conctimerparts.end(); ++k, ++conctimerpart )
timer.concurrentTimerIndices.push_back(parse_int<int>( *conctimerpart ));
break;
}
case 3: {
timer.remote = *timerpart;
break;
}
}
}
conflictingTimers.push_back(timer);
}
}
}
catch ( bad_lexical_cast const& ex ) {
}
}
TimerConflicts::TimerConflicts()
{
Epgsearch_services_v1_1 service;
if ( CheckEpgsearchVersion() && cPluginManager::CallFirstService(ServiceInterface, &service))
{
cServiceHandler_v1_1* handler = dynamic_cast<cServiceHandler_v1_1*>(service.handler.get());
if (handler)
{
std::list<std::string> conflicts = service.handler->TimerConflictList();
// for(std::list<std::string>::const_iterator i = conflicts.begin(); i != conflicts.end(); ++i) {
// dsyslog("live: TimerConflicts::TimerConflicts() conflicts '%s'",i->c_str());
// }
GetRemote(conflicts); // add remote VDR conflicts
m_conflicts.assign( conflicts.begin(), conflicts.end() );
m_conflicts.sort();
}
}
// for (TimerConflicts::iterator conflict=m_conflicts.begin(); conflict!=m_conflicts.end(); conflict++) {
// const std::list<TimerInConflict>& conflTimers = conflict->ConflictingTimers();
// for (std::list<TimerInConflict>::const_iterator confltimer = conflTimers.begin(); confltimer != conflTimers.end(); ++confltimer) {
// dsyslog("live: TimerConflicts::TimerConflictsi() Timer ID with conflict '%d'", confltimer->timerIndex );
// dsyslog("live: TimerConflicts::TimerConflictsi() conflict on server '%s'", confltimer->remote.c_str() );
// for (std::list<int>::const_iterator timerIndex = confltimer->concurrentTimerIndices.begin(); timerIndex != confltimer->concurrentTimerIndices.end(); ++timerIndex) {
// dsyslog("live: TimerConflicts::TimerConflicts() concurrent Timer IDs '%d'", *timerIndex);
// }
// }
// }
}
void TimerConflicts::GetRemote(std::list<std::string> & conflicts )
{
cStringList svdrpServerNames;
if (GetSVDRPServerNames(&svdrpServerNames)) {
svdrpServerNames.Sort(true);
}
for (int i = 0; i < svdrpServerNames.Size(); i++) {
std::string remoteServer = svdrpServerNames[i];
// dsyslog("live: TimerConflicts::GetRemote() found remote server '%s'", remoteServer.c_str());
cStringList response;
std::string command = "PLUG epgsearch lscc";
bool svdrpOK = ExecSVDRPCommand(remoteServer.c_str(), command.c_str(), &response);
if ( !svdrpOK ) {
esyslog("live: TimerConflicts::GetRemote() svdrp command '%s' on remote server '%s'failed", command.c_str(), remoteServer.c_str());
}
else {
for (int i = 0; i < response.Size(); i++) {
int code = SVDRPCode(response[i]);
// dsyslog("live: GetRemote() response[i] '%s'", response[i]);
switch ( code ) {
case 900: {
std::string rConflict = response[i];
std::string remConflict = rConflict.substr(4);
remConflict.append("|");
remConflict.append(remoteServer);
// dsyslog("live: TimerConflicts::GetRemote() found remote conflict '%s' ", remConflict.c_str());
conflicts.push_back(remConflict);
break;
}
case 901: break; // no conflict found
default: {
esyslog("live: TimerConflicts::GetRemote() svdrp command '%s' failed, respone: %s", command.c_str(), response[i]);
svdrpOK = false;
break;
}
}
}
if ( svdrpOK ) {
// dsyslog("live: TimerConflicts::GetRemote() on server '%s' successful", remoteServer.c_str());
}
else {
esyslog("live: TimerConflicts::GetRemote() on server '%s' failed", remoteServer.c_str());
}
}
response.Clear();
}
svdrpServerNames.Clear();
}
bool TimerConflicts::HasConflict(int timerId)
{
for (const auto& conflict : *this)
for (const auto& tic : conflict.ConflictingTimers())
if (tic.timerIndex == timerId)
return true;
return false;
}
bool TimerConflicts::CheckAdvised()
{
Epgsearch_services_v1_1 service;
if (CheckEpgsearchVersion() && cPluginManager::CallFirstService(ServiceInterface, &service))
{
cServiceHandler_v1_1* handler = dynamic_cast<cServiceHandler_v1_1*>(service.handler.get());
if (!handler)
return false;
else
return handler->IsConflictCheckAdvised();
}
else
return false;
}
TimerConflictNotifier::TimerConflictNotifier()
: lastCheck(0)
, lastTimerModification(0)
, conflicts()
{
}
TimerConflictNotifier::~TimerConflictNotifier()
{
}
bool TimerConflictNotifier::ShouldNotify()
{
time_t now = time(0);
bool reCheckAdvised((now - lastCheck) > CHECKINTERVAL);
bool recentTimerChange((now - lastTimerModification) <= CHECKINTERVAL);
if (recentTimerChange || (reCheckAdvised && TimerConflicts::CheckAdvised())) {
lastCheck = now;
conflicts.reset(new TimerConflicts());
return conflicts->size() > 0;
}
return false;
}
void TimerConflictNotifier::SetTimerModification()
{
lastTimerModification = time(0);
}
std::string TimerConflictNotifier::Message() const
{
int count = conflicts ? conflicts->size() : 0;
cToSvConcat msg(tr("Timer conflict check detected "), count, " ");
if (count == 1)
msg += tr("conflict");
else
msg += tr("conflicts");
msg += "!";
return count > 0 ? std::string(msg) : std::string();
}
std::string TimerConflictNotifier::Url() const
{
return "timerconflicts.html";
}
} // namespace vdrlive