-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtimers.cpp
851 lines (734 loc) · 27.1 KB
/
timers.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#include "timers.h"
using namespace std;
void TimersResponder::reply(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler::addHeader(reply);
if ( request.method() == "OPTIONS" ) {
reply.addHeader("Allow", "GET, POST, DELETE, PUT");
reply.httpReturn(200, "OK");
return;
}
if ( request.method() == "GET" ) {
showTimers(out, request, reply);
} else if ( request.method() == "DELETE" && (int)request.url().find("/timers/bulkdelete") == 0 ) {
replyBulkdelete(out, request, reply);
} else if (request.method() == "DELETE") {
deleteTimer(out, request, reply);
} else if ( request.method() == "POST" ) {
createOrUpdateTimer(out, request, reply, false);
} else if ( request.method() == "PUT" ) {
createOrUpdateTimer(out, request, reply, true);
} else {
reply.httpReturn(501, "Only GET, DELETE, POST and PUT methods are supported.");
}
}
void TimersResponder::createOrUpdateTimer(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply, bool update)
{
QueryHandler q("/timers", request);
#if APIVERSNUM < 20300
cTimers& timers = Timers;
if ( timers.BeingEdited() ) {
reply.httpReturn(502, "Timers are being edited - try again later");
return;
}
#endif
int error = false;
string error_values = "";
static TimerValues v;
int flags = v.ConvertFlags(q.getBodyAsString("flags"));
string aux = v.ConvertAux(q.getBodyAsString("aux"));
string file = v.ConvertFile(q.getBodyAsString("file"));
int lifetime = v.ConvertLifetime(q.getBodyAsString("lifetime"));
int priority = v.ConvertPriority(q.getBodyAsString("priority"));
int stop = v.ConvertStop(q.getBodyAsString("stop"));
int start = v.ConvertStart(q.getBodyAsString("start"));
string weekdays = q.getBodyAsString("weekdays");
string day = v.ConvertDay(q.getBodyAsString("day"));
const cChannel* chan = v.ConvertChannel(q.getBodyAsString("channel"));
cTimer* timer_orig = v.ConvertTimer(q.getBodyAsString("timer_id"));
const cTimer* timerOrigRead = VdrExtension::getTimer(q.getBodyAsString("timer_id"));
int eventid = q.getBodyAsInt("eventid");
int minpre = q.getBodyAsInt("minpre");
int minpost = q.getBodyAsInt("minpost");
if (minpre < 0) minpre = 0;
if (minpost < 0) minpost = 0;
if ( update == false ) { //create timer by event ID
if (eventid >= 0 && chan != NULL) {
const cEvent* event = VdrExtension::GetEventById((tEventID)eventid, chan);
if (event == NULL) {
reply.httpReturn(407, "eventid invalid");
return;
} else {
if (!v.IsFlagsValid(flags)) flags = 1;
if (!v.IsFileValid(file)) file = (string)event->Title();
if (!v.IsWeekdaysValid(weekdays)) weekdays = "-------";
if (!v.IsLifetimeValid(lifetime)) lifetime = 50;
if (!v.IsPriorityValid(priority)) priority = 99;
chan = VdrExtension::getChannel((const char*)event->ChannelID().ToString());
if (!v.IsStartValid(start) || !v.IsStopValid(stop) || !v.IsDayValid(day)) {
time_t estart = event->StartTime()-minpre*60;
time_t estop = event->EndTime()+minpost*60;
struct tm *starttime = localtime(&estart);
ostringstream daystream;
daystream << StringExtension::addZeros((starttime->tm_year + 1900), 4) << "-"
<< StringExtension::addZeros((starttime->tm_mon + 1), 2) << "-"
<< StringExtension::addZeros((starttime->tm_mday), 2);
day = daystream.str();
start = starttime->tm_hour * 100 + starttime->tm_min;
struct tm *stoptime = localtime(&estop);
stop = stoptime->tm_hour * 100 + stoptime->tm_min;
}
}
} else {
if ( !v.IsFlagsValid(flags) ) { flags = 1; }
if ( !v.IsFileValid(file) ) { error = true; error_values += "file, "; }
if ( !v.IsLifetimeValid(lifetime) ) { lifetime = 50; }
if ( !v.IsPriorityValid(priority) ) { priority = 99; }
if ( !v.IsStopValid(stop) ) { error = true; error_values += "stop, "; }
if ( !v.IsStartValid(start) ) { error = true; error_values += "start, "; }
if ( !v.IsWeekdaysValid(weekdays) ) { error = true; error_values += "weekdays, "; }
if ( !v.IsDayValid(day)&& !day.empty() ) { error = true; error_values += "day, "; }
if ( chan == NULL ) { error = true; error_values += "channel, "; }
}
} else { //update
if ( (timer_orig == NULL) || (timerOrigRead == NULL) ) { error = true; error_values += "timer_id, "; }
if ( !error )
{
if (!v.IsStopValid(stop) || !v.IsStartValid(start)) /* update timer based on premin and postmin */
{
tEventID eventid_;
eventid_ = timerOrigRead->Event()->EventID();
chan = timerOrigRead->Channel();
const cEvent* event = VdrExtension::GetEventById((tEventID)eventid_, chan);
if (event == NULL) {
reply.httpReturn(407, "eventid invalid");
return;
}
time_t estart = event->StartTime() - minpre * 60;
time_t estop = event->EndTime() + minpost * 60;
struct tm *starttime = localtime(&estart);
ostringstream daystream;
daystream << StringExtension::addZeros((starttime->tm_year + 1900), 4) << "-"
<< StringExtension::addZeros((starttime->tm_mon + 1), 2) << "-"
<< StringExtension::addZeros((starttime->tm_mday), 2);
day = daystream.str();
start = starttime->tm_hour * 100 + starttime->tm_min;
struct tm *stoptime = localtime(&estop);
stop = stoptime->tm_hour * 100 + stoptime->tm_min;
}
if ( !v.IsFlagsValid(flags) ) { flags = timer_orig->Flags(); }
if ( !v.IsFileValid(file) ) { file = v.ConvertFile((string)timer_orig->File()); }
if ( !v.IsLifetimeValid(lifetime) ) { lifetime = timer_orig->Lifetime(); }
if ( !v.IsPriorityValid(priority) ) { priority = timer_orig->Priority(); }
if ( !v.IsStopValid(stop) ) { stop = timer_orig->Stop(); }
if ( !v.IsStartValid(start) ) { start = timer_orig->Start(); }
if ( !v.IsWeekdaysValid(weekdays) ) { weekdays = v.ConvertWeekdays(timer_orig->WeekDays()); }
if ( !v.IsDayValid(day) ) { day = v.ConvertDay(timer_orig->Day()); }
if ( chan == NULL ) { chan = (cChannel*)timer_orig->Channel();}
if (aux == "")
{
if (timer_orig->Aux() != NULL)
{
aux = v.ConvertAux(timer_orig->Aux());
}
}
}
}
if (error) {
string error_message = (string)"The following parameters aren't valid: " + error_values.substr(0, error_values.length()-2) + (string)"!";
reply.httpReturn(403, error_message);
return;
}
ostringstream builder;
builder << flags << ":"
<< (const char*)chan->GetChannelID().ToString() << ":"
<< ( weekdays != "-------" ? weekdays : "" )
<< ( weekdays == "-------" || day.empty() ? "" : "@" ) << day << ":"
<< start << ":"
<< stop << ":"
<< priority << ":"
<< lifetime << ":"
<< file << ":"
<< aux;
dsyslog("restfulapi timer info: /%s/ ", builder.str().c_str());
chan = NULL;
if ( update == false ) { // create timer
cTimer* timer = new cTimer();
if ( timer->Parse(builder.str().c_str()) ) {
#if APIVERSNUM > 20300
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
cTimer* checkTimer = Timers->GetTimer(timer);
#else
cTimer* checkTimer = timers.GetTimer(timer);
#endif
if ( checkTimer != NULL ) {
delete timer;
reply.httpReturn(403, "Timer already defined!");
esyslog("restfulapi: Timer already defined!");
} else {
replyCreatedId(timer, request, reply, out);
#if APIVERSNUM > 20300
LOCK_SCHEDULES_READ;
timer->SetEventFromSchedule(Schedules);
Timers->Add(timer);
Timers->SetModified();
#else
timer->SetEventFromSchedule();
timers.Add(timer);
timers.SetModified();
#endif
esyslog("restfulapi: timer created!");
}
} else {
reply.httpReturn(403, "Creating timer failed!");
esyslog("restfulapi: timer creation failed!");
}
} else {
if ( timer_orig->Parse(builder.str().c_str()) ) {
#if APIVERSNUM > 20300
LOCK_SCHEDULES_READ;
timer_orig->SetEventFromSchedule(Schedules);
//Timers->SetModified();
#else
timer_orig->SetEventFromSchedule();
timers.SetModified();
#endif
replyCreatedId(timer_orig, request, reply, out);
esyslog("restfulapi: updating timer successful!");
} else {
reply.httpReturn(403, "updating timer failed!");
esyslog("restfulapi: updating timer failed!");
}
}
}
void TimersResponder::replyCreatedId(cTimer* timer, cxxtools::http::Request& request, cxxtools::http::Reply& reply, ostream& out)
{
QueryHandler q("/timers", request);
TimerList* timerList;
if ( q.isFormat(".html") ) {
reply.addHeader("Content-Type", "text/html; charset=utf-8");
timerList = (TimerList*)new HtmlTimerList(&out);
} else if ( q.isFormat(".xml") ) {
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
timerList = (TimerList*)new XmlTimerList(&out);
} else {
reply.addHeader("Content-Type", "application/json; charset=utf-8");
timerList = (TimerList*)new JsonTimerList(&out);
}
timerList->init();
timerList->addTimer(timer);
timerList->setTotal(1);
timerList->finish();
delete timerList;
}
void TimersResponder::deleteTimer(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler q("/timers", request);
#if APIVERSNUM < 20300
cTimers& timers = Timers;
if ( timers.BeingEdited() ) {
reply.httpReturn(502, "Timers are being edited - try again later");
return;
}
#endif
TimerValues v;
cTimer* timer = v.ConvertTimer(q.getParamAsString(0));
if ( timer == NULL) {
reply.httpReturn(404, "Timer id invalid!");
} else {
#if APIVERSNUM > 20300
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
#endif
if ( timer->Recording() ) {
timer->Skip();
#if APIVERSNUM > 20300
cRecordControls::Process(Timers, time(NULL));
}
Timers->Del(timer);
Timers->SetModified();
#else
cRecordControls::Process(time(NULL));
}
timers.Del(timer);
timers.SetModified();
#endif
reply.httpReturn(200, "Timer deleted.");
}
}
void TimersResponder::replyBulkdelete(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply) {
QueryHandler q("/timers/bulkdelete", request);
#if APIVERSNUM < 20300
cTimers& timers = Timers;
if ( timers.BeingEdited() ) {
reply.httpReturn(502, "Timers are being edited - try again later");
return;
}
#endif
TimerDeletedList* list;
if ( q.isFormat(".json") ) {
reply.addHeader("Content-Type", "application/json; charset=utf-8");
list = (TimerDeletedList*)new JsonTimerDeletedList(&out);
} else if ( q.isFormat(".html") ) {
reply.addHeader("Content-Type", "text/html; charset=utf-8");
list = (TimerDeletedList*)new HtmlTimerDeletedList(&out);
} else if ( q.isFormat(".xml") ) {
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
list = (TimerDeletedList*)new XmlTimerDeletedList(&out);
} else {
reply.httpReturn(404, "Resources are not available for the selected format. (Use: .json, .html or .xml)");
return;
}
TimerValues v;
cTimer* timer;
vector< string > deleteTimers = q.getBodyAsStringArray("timers");
vector< SerBulkDeleted > results;
SerBulkDeleted result;
size_t i;
list->init();
for ( i = 0; i < deleteTimers.size(); i++ ) {
timer = v.ConvertTimer(deleteTimers[i]);
result.id = deleteTimers[i];
if ( timer == NULL ) {
result.deleted = false;
} else {
#if APIVERSNUM > 20300
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
#endif
if ( timer->Recording() ) {
timer->Skip();
#if APIVERSNUM > 20300
cRecordControls::Process(Timers, time(NULL));
}
Timers->Del(timer);
Timers->SetModified();
#else
cRecordControls::Process(time(NULL));
}
timers.Del(timer);
timers.SetModified();
#endif
result.deleted = true;
}
list->addDeleted(result);
}
list->setTotal((int)deleteTimers.size());
list->finish();
delete list;
};
void TimersResponder::showTimers(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler q("/timers", request);
TimerList* timerList;
if ( q.isFormat(".json") ) {
reply.addHeader("Content-Type", "application/json; charset=utf-8");
timerList = (TimerList*)new JsonTimerList(&out);
} else if ( q.isFormat(".html") ) {
reply.addHeader("Content-Type", "text/html; charset=utf-8");
timerList = (TimerList*)new HtmlTimerList(&out);
} else if ( q.isFormat(".xml") ) {
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
timerList = (TimerList*)new XmlTimerList(&out);
} else {
reply.httpReturn(404, "Resources are not available for the selected format. (Use: .json, .html or .xml)");
return;
}
int start_filter = q.getOptionAsInt("start");
int limit_filter = q.getOptionAsInt("limit");
string timer_id = q.getParamAsString(0);
if ( start_filter >= 0 && limit_filter >= 1 ) {
timerList->activateLimit(start_filter, limit_filter);
}
timerList->init();
vector< const cTimer* > timers = VdrExtension::SortedTimers();
for (int i=0;i<(int)timers.size();i++)
{
if ( VdrExtension::getTimerID(timers[i]) == timer_id || timer_id.length() == 0 ) {
timerList->addTimer(timers[i]);
}
}
timerList->setTotal((int)timers.size());
timerList->finish();
delete timerList;
}
void operator<<= (cxxtools::SerializationInfo& si, const SerTimer& t)
{
si.addMember("id") <<= t.Id;
si.addMember("index") <<= t.Index;
si.addMember("flags") <<= t.Flags;
si.addMember("start") <<= t.Start;
si.addMember("start_timestamp") <<= t.StartTimeStamp;
si.addMember("stop_timestamp") <<= t.StopTimeStamp;
si.addMember("stop") <<= t.Stop;
si.addMember("priority") <<= t.Priority;
si.addMember("lifetime") <<= t.Lifetime;
si.addMember("event_id") <<= t.EventID;
si.addMember("weekdays") <<= t.WeekDays;
si.addMember("day") <<= t.Day;
si.addMember("channel") <<= t.Channel;
si.addMember("filename") <<= t.FileName;
si.addMember("channel_name") <<= t.ChannelName;
si.addMember("is_pending") <<= t.IsPending;
si.addMember("is_recording") <<= t.IsRecording;
si.addMember("is_active") <<= t.IsActive;
si.addMember("aux") <<= t.Aux;
}
TimerList::TimerList(ostream *out)
{
s = new StreamExtension(out);
}
TimerList::~TimerList()
{
delete s;
}
void HtmlTimerList::init()
{
s->writeHtmlHeader("HtmlTimerList");
s->write("<ul>");
}
void HtmlTimerList::addTimer(const cTimer* timer)
{
if ( filtered() ) return;
s->write("<li>");
s->write((char*)timer->File()); //TODO: add date, time and duration
s->write("\n");
}
void HtmlTimerList::finish()
{
s->write("</ul>");
s->write("</body></html>");
}
void JsonTimerList::addTimer(const cTimer* timer)
{
if ( filtered() ) return;
static TimerValues v;
SerTimer serTimer;
serTimer.Id = StringExtension::UTF8Decode(VdrExtension::getTimerID(timer));
serTimer.Index = timer->Index() + 1;
serTimer.Flags = timer->Flags();
serTimer.Start = timer->Start();
serTimer.Stop = timer->Stop();
serTimer.Priority = timer->Priority();
serTimer.Lifetime = timer->Lifetime();
serTimer.EventID = timer->Event() != NULL ? timer->Event()->EventID() : -1;
serTimer.WeekDays = StringExtension::UTF8Decode(v.ConvertWeekdays(timer->WeekDays()));
serTimer.Day = StringExtension::UTF8Decode(v.ConvertDay(timer->Day()));
serTimer.Channel = StringExtension::UTF8Decode((const char*)timer->Channel()->GetChannelID().ToString());
serTimer.IsRecording = timer->Recording();
serTimer.IsPending = timer->Pending();
serTimer.FileName = StringExtension::UTF8Decode(timer->File());
serTimer.ChannelName = StringExtension::UTF8Decode(timer->Channel()->Name());
serTimer.IsActive = (timer->Flags() & tfActive) == tfActive ? true : false;
serTimer.Aux = StringExtension::UTF8Decode(timer->Aux() != NULL ? timer->Aux() : "");
serTimer.StartTimeStamp = v.GetStartStopTimestamp(timer);
serTimer.StopTimeStamp = v.GetStartStopTimestamp(timer, true);
serTimers.push_back(serTimer);
}
void JsonTimerList::finish()
{
cxxtools::JsonSerializer serializer(*s->getBasicStream());
serializer.serialize(serTimers, "timers");
serializer.serialize(serTimers.size(), "count");
serializer.serialize(total, "total");
serializer.finish();
}
void XmlTimerList::init()
{
counter = 0;
s->writeXmlHeader();
s->write("<timers xmlns=\"http://www.domain.org/restfulapi/2011/timers-xml\">\n");
}
void XmlTimerList::addTimer(const cTimer* timer)
{
if ( filtered() ) return;
static TimerValues v;
s->write(" <timer>\n");
s->write(cString::sprintf(" <param name=\"id\">%s</param>\n", StringExtension::encodeToXml(VdrExtension::getTimerID(timer)).c_str()));
s->write(cString::sprintf(" <param name=\"index\">%i</param>\n", timer->Index() + 1));
s->write(cString::sprintf(" <param name=\"flags\">%i</param>\n", timer->Flags()));
s->write(cString::sprintf(" <param name=\"start\">%i</param>\n", timer->Start()) );
s->write(cString::sprintf(" <param name=\"stop\">%i</param>\n", timer->Stop()) );
s->write(cString::sprintf(" <param name=\"start_timestamp\">%s</param>\n", StringExtension::encodeToXml(v.GetStartStopTimestamp(timer)).c_str()));
s->write(cString::sprintf(" <param name=\"stop_timestamp\">%s</param>\n", StringExtension::encodeToXml(v.GetStartStopTimestamp(timer, true)).c_str()));
s->write(cString::sprintf(" <param name=\"priority\">%i</param>\n", timer->Priority()) );
s->write(cString::sprintf(" <param name=\"lifetime\">%i</param>\n", timer->Lifetime()) );
s->write(cString::sprintf(" <param name=\"event_id\">%i</param>\n", timer->Event() != NULL ? timer->Event()->EventID() : -1) );
s->write(cString::sprintf(" <param name=\"weekdays\">%s</param>\n", StringExtension::encodeToXml(v.ConvertWeekdays(timer->WeekDays())).c_str()));
s->write(cString::sprintf(" <param name=\"day\">%s</param>\n", StringExtension::encodeToXml(v.ConvertDay(timer->Day())).c_str()));
s->write(cString::sprintf(" <param name=\"channel\">%s</param>\n", StringExtension::encodeToXml((const char*)timer->Channel()->GetChannelID().ToString()).c_str()) );
s->write(cString::sprintf(" <param name=\"is_recording\">%s</param>\n", timer->Recording() ? "true" : "false" ) );
s->write(cString::sprintf(" <param name=\"is_pending\">%s</param>\n", timer->Pending() ? "true" : "false" ));
s->write(cString::sprintf(" <param name=\"file_name\">%s</param>\n", StringExtension::encodeToXml(timer->File()).c_str()) );
s->write(cString::sprintf(" <param name=\"channel_name\">%s</param>\n", StringExtension::encodeToXml(timer->Channel()->Name()).c_str()));
s->write(cString::sprintf(" <param name=\"is_active\">%s</param>\n", (timer->Flags() & tfActive) == tfActive ? "true" : "false" ));
s->write(cString::sprintf(" <param name=\"aux\">%s</param>\n", (timer->Aux() != NULL ? StringExtension::encodeToXml(timer->Aux()).c_str() : "")));
s->write(" </timer>\n");
}
void XmlTimerList::finish()
{
s->write((const char*)cString::sprintf(" <count>%i</count><total>%i</total>", Count(), total));
s->write("</timers>");
}
// Timerdeleted list
void operator<<= (cxxtools::SerializationInfo& si, const SerBulkDeleted& t)
{
si.addMember("id") <<= t.id;
si.addMember("deleted") <<= t.deleted;
}
TimerDeletedList::TimerDeletedList(ostream *out)
{
s = new StreamExtension(out);
}
TimerDeletedList::~TimerDeletedList()
{
delete s;
}
void HtmlTimerDeletedList::init()
{
s->writeHtmlHeader("HtmlTimerDeletedList");
s->write("<ul>");
}
void HtmlTimerDeletedList::addDeleted(SerBulkDeleted &timer)
{
s->write(cString::sprintf("<li>%s deleted: %s</li>\n", StringExtension::toString(timer.id).c_str(), timer.deleted ? "true" : "false"));
}
void HtmlTimerDeletedList::finish()
{
s->write("</ul>");
s->write("</body></html>");
}
void JsonTimerDeletedList::addDeleted(SerBulkDeleted &timer)
{
serDeleted.push_back(timer);
}
void JsonTimerDeletedList::finish()
{
cxxtools::JsonSerializer serializer(*s->getBasicStream());
serializer.serialize(serDeleted, "timers");
serializer.serialize(serDeleted.size(), "count");
serializer.serialize(total, "total");
serializer.finish();
}
void XmlTimerDeletedList::init()
{
counter = 0;
s->writeXmlHeader();
s->write("<timers_deleted xmlns=\"http://www.domain.org/restfulapi/2011/timers-xml\">\n");
}
void XmlTimerDeletedList::addDeleted(SerBulkDeleted &timer)
{
if ( filtered() ) return;
static TimerValues v;
s->write(" <timer>\n");
s->write(cString::sprintf("<id>%s</id>\n", StringExtension::toString(timer.id).c_str()));
s->write(cString::sprintf("<deleted>%s</deleted>\n", timer.deleted ? "true" : "false"));
s->write(" </timer>\n");
}
void XmlTimerDeletedList::finish()
{
s->write((const char*)cString::sprintf(" <count>%i</count><total>%i</total>", Count(), total));
s->write("</timers_deleted>");
}
// --- TimerValues class ------------------------------------------------------------
queue<int> TimerValues::ConvertToBinary(int v)
{
int b;
queue <int> res;
while ( v != 0) {
b = v % 2;
res.push(b);
v = (v-b) / 2;
}
return res;
}
bool TimerValues::IsDayValid(string v)
{
static cxxtools::Regex regex("[0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}");
return regex.match(v);
}
bool TimerValues::IsFlagsValid(int v)
{
if (v == tfAll) return true;
int valid_tf = (tfNone | tfActive | tfInstant | tfVps | tfRecording);
return (valid_tf & v) == v;
}
bool TimerValues::IsFileValid(string v)
{
if ( v.length() > 0 && v.length() <= 99 )
return true;
return false;
}
bool TimerValues::IsLifetimeValid(int v)
{
if ( v >= 0 && v <= 99 )
return true;
return false;
}
bool TimerValues::IsPriorityValid(int v)
{
return IsLifetimeValid(v); //uses the same values as the lifetime
}
bool TimerValues::IsStopValid(int v)
{
int minutes = v % 100;
int hours = (v - minutes) / 100;
if ( minutes >= 0 && minutes < 60 && hours >= 0 && hours < 24 )
return true;
return false;
}
bool TimerValues::IsStartValid(int v)
{
return IsStopValid(v); //uses the syntax as start time, f.e. 2230 means half past ten in the evening
}
bool TimerValues::IsWeekdaysValid(string v)
{
/*static cxxtools::Regex regex("[\\-M][\\-T][\\-W][\\-T][\\-F][\\-S][\\-S]");
return regex.match(v);*/
if ( v.length() != 7 ) return false;
const char* va = v.c_str();
if ( va[0] != '-' && va[0] != 'M' ) return false;
if ( va[1] != '-' && va[1] != 'T' ) return false;
if ( va[2] != '-' && va[2] != 'W' ) return false;
if ( va[3] != '-' && va[3] != 'T' ) return false;
if ( va[4] != '-' && va[4] != 'F' ) return false;
if ( va[5] != '-' && va[5] != 'S' ) return false;
if ( va[6] != '-' && va[6] != 'S' ) return false;
return true;
}
int TimerValues::ConvertFlags(string v)
{
return StringExtension::strtoi(v);
}
cEvent* TimerValues::ConvertEvent(string event_id, cChannel* channel)
{
if ( channel == NULL ) return NULL;
int eventid = StringExtension::strtoi(event_id);
if ( eventid <= -1 ) return NULL;
#if APIVERSNUM > 20300
LOCK_SCHEDULES_READ;
#else
cSchedulesLock MutexLock;
const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
#endif
if ( !Schedules ) return NULL;
const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
if ( !Schedule ) return NULL;
#if APIVERSNUM >= 20502
return (cEvent*)Schedule->GetEventById(eventid);
#else
return (cEvent*)Schedule->GetEvent(eventid);
#endif
}
string TimerValues::ConvertFile(string v)
{
return StringExtension::replace(v, ":", "|");
}
string TimerValues::ConvertAux(string v)
{
return ConvertFile(v);
}
int TimerValues::ConvertLifetime(string v)
{
return StringExtension::strtoi(v);
}
int TimerValues::ConvertPriority(string v)
{
return StringExtension::strtoi(v);
}
int TimerValues::ConvertStop(string v)
{
return StringExtension::strtoi(v);
}
int TimerValues::ConvertStart(string v)
{
return StringExtension::strtoi(v);
}
string TimerValues::ConvertDay(time_t v)
{
if (v==0) return "";
struct tm *timeinfo = localtime(&v); //must not be deleted because it's statically allocated by localtime
ostringstream str;
int year = timeinfo->tm_year + 1900;
int month = timeinfo->tm_mon + 1; //append 0, vdr wants two digits!
int day = timeinfo->tm_mday; //append 0, vdr wants two digits!
str << year << "-"
<< (month < 10 ? "0" : "") << month << "-"
<< (day < 10 ? "0" : "") << day;
return str.str();
}
string TimerValues::ConvertDay(string v)
{
if ( !IsDayValid(v) ) return "wrong format";
//now append 0 (required by vdr) if month/day don't already have two digits
int a = v.find_first_of('-');
int b = v.find_last_of('-');
string year = v.substr(0, a);
string month = v.substr(a+1, b-a-1);
string day = v.substr(b+1);
ostringstream res;
res << year << "-"
<< (month.length() == 1 ? "0" : "") << month << "-"
<< (day.length() == 1 ? "0" : "") << day;
return res.str();
}
const cChannel* TimerValues::ConvertChannel(string v)
{
return VdrExtension::getChannel(v);
}
cTimer* TimerValues::ConvertTimer(string v)
{
return VdrExtension::getTimerWrite(v);
}
string TimerValues::ConvertWeekdays(int v)
{
queue<int> b = ConvertToBinary(v);
int counter = 0;
ostringstream res;
while ( !b.empty() && counter < 7 ) {
int val = b.front();
switch(counter) {
case 0: res << (val == 1 ? 'M' : '-'); break;
case 1: res << (val == 1 ? 'T' : '-'); break;
case 2: res << (val == 1 ? 'W' : '-'); break;
case 3: res << (val == 1 ? 'T' : '-'); break;
case 4: res << (val == 1 ? 'F' : '-'); break;
case 5: res << (val == 1 ? 'S' : '-'); break;
case 6: res << (val == 1 ? 'S' : '-'); break;
}
b.pop();
counter++;
}
while ( counter < 7 ) {
res << '-';
counter++;
}
return res.str();
}
int TimerValues::ConvertWeekdays(string v)
{
const char* str = v.c_str();
int res = 0;
if ( str[0] == 'M' ) res += 64;
if ( str[1] == 'T' ) res += 32;
if ( str[2] == 'W' ) res += 16;
if ( str[3] == 'T' ) res += 8;
if ( str[4] == 'F' ) res += 4;
if ( str[5] == 'S' ) res += 2;
if ( str[6] == 'S' ) res += 1;
return res;
}
string TimerValues::GetStartStopTimestamp(const cTimer* timer, bool stopTime) {
char buffer[80];
time_t t = timer->Day();
struct tm *tmTimer = localtime(&t);
tmTimer->tm_isdst = -1;
tmTimer->tm_hour = ( stopTime ? timer->Stop() : timer->Start() ) / 100;
tmTimer->tm_min = ( stopTime ? timer->Stop() : timer->Start() ) % 100;
tmTimer->tm_mday += ( stopTime && timer->Stop() < timer->Start() ) ? 1 : 0;
t = mktime(tmTimer);
tmTimer = localtime(&t);
strftime (buffer,80,"%Y-%m-%d %H:%M:%S",tmTimer);
return (string)buffer;
};