source: titan/plugins/autotimer/autotimer.c @ 26912

Last change on this file since 26912 was 25878, checked in by gost, 9 years ago

[plugins] autotimer litle fix

  • Property svn:executable set to *
File size: 17.4 KB
Line 
1#include "../../titan/struct.h"
2#include "../../titan/debug.h"
3#include "../../titan/header.h"
4
5char pluginname[] = "Auto Timer";
6char plugindesc[] = "Extensions";
7char pluginpic[] = "%pluginpath%/autotimer/plugin.png";
8
9int pluginaktiv = 0;
10int pluginversion = PLUGINVERSION;
11int autostart = 10;
12//struct skin* pluginmenu = NULL;
13//int pluginflag = 1; //don't show the plugin in pluginmanager
14
15struct rectimer *rectimer;
16
17struct searchoption
18{
19        int starttime;
20        int endtime;
21        char* search;
22        char* channelname;
23};
24
25struct stimerthread* autotimerthread = NULL;
26
27
28void autotimer_thread()
29{
30        char* tmpstr = NULL;
31        char* buf = NULL;
32        char* result = NULL;
33        struct searchoption* search1 = NULL;
34        struct searchoption* search2 = NULL;
35        struct searchoption* searchall = NULL;
36        struct channel *channel1 = NULL;
37        struct channel *channel2 = NULL;
38        struct channel *channelall = NULL;
39        struct epg* epgnode = NULL;
40        struct tm *loctime = NULL;
41        struct rectimer *node = NULL;
42        struct rectimer* recnode = rectimer;
43        int channelfind = 0;
44        int epg_starttime = 0;
45        //int epg_endtime = 0;
46        int tageswechsel = 0;
47        int write_note = 0;
48        int i = 0;
49        int konflikt = 0;
50        int searchpos = 0;
51       
52        buf = malloc(MINMALLOC);
53        if(buf == NULL)
54        {
55                err("no mem");
56                return;
57        }
58       
59        if(getconfigint("at1_akt", NULL) == 1)
60        {
61                addconfigint("at1_running", 1);
62                search1 = (struct searchoption*)calloc(1, sizeof(struct searchoption));
63                if(search1 == NULL)
64                {
65                        err("no memory");
66                        return;
67                }
68                search1->search = getconfig("at1_search", NULL);
69                if(search1->search == NULL)
70                {
71                        autotimerthread->aktion = STOP;
72                }
73                search1->channelname = getconfig("at1_channelname", NULL);
74                if(search1->channelname == NULL)
75                {
76                        autotimerthread->aktion = STOP;
77                }
78                tmpstr = ostrcat(tmpstr, getconfig("at1_starttime", NULL), 1, 0);
79                if(tmpstr == NULL)
80                {
81                        autotimerthread->aktion = STOP;
82                }
83                else
84                {
85                        tmpstr[2] = '\0';
86                        tmpstr[5] = '\0';
87                        search1->starttime = (atoi(tmpstr) * 100) + atoi(tmpstr+3);
88                }
89                free(tmpstr); tmpstr = NULL;
90                tmpstr = ostrcat(tmpstr, getconfig("at1_endtime", NULL), 1, 0);
91                if(tmpstr == NULL)
92                {
93                        autotimerthread->aktion = STOP;
94                }
95                else
96                {
97                        tmpstr[2] = '\0';
98                        tmpstr[5] = '\0';
99                        search1->endtime = (atoi(tmpstr) * 100) + atoi(tmpstr+3);
100                }
101                free(tmpstr); tmpstr = NULL;
102                if( search1->starttime > search1->endtime) {
103                        search1->endtime = search1->endtime + 2400;
104                        tageswechsel = 1;
105                }
106                channel1 = channel;
107                channelfind = 0;
108                while(channel1 != NULL)
109                {
110                        if(ostrcmp(channel1->name, search1->channelname) == 0)
111                                break;
112                        channel1 = channel1->next;
113                }
114                if(channel1 == NULL)
115                {
116                        search1->channelname = NULL;
117                        search1->search = NULL;
118                        free(search1); search1=NULL;
119                }
120        }
121
122        if(getconfigint("at2_akt", NULL) == 1)
123        {
124                addconfigint("at1_running", 1);
125                search2 = (struct searchoption*)calloc(1, sizeof(struct searchoption));
126                if(search2 == NULL)
127                {
128                        err("no memory");
129                        return;
130                }
131                search2->search = getconfig("at2_search", NULL);
132                if(search2->search == NULL)
133                {
134                        autotimerthread->aktion = STOP;
135                }
136                search2->channelname = getconfig("at2_channelname", NULL);
137                if(search2->channelname == NULL)
138                {
139                        autotimerthread->aktion = STOP;
140                }
141                tmpstr = ostrcat(tmpstr, getconfig("at2_starttime", NULL), 1, 0);
142                if(tmpstr == NULL)
143                {
144                        autotimerthread->aktion = STOP;
145                }
146                else
147                {
148                        tmpstr[2] = '\0';
149                        tmpstr[5] = '\0';
150                        search2->starttime = (atoi(tmpstr) * 100) + atoi(tmpstr+3);
151                }
152                free(tmpstr); tmpstr = NULL;
153                tmpstr = ostrcat(tmpstr, getconfig("at2_endtime", NULL), 1, 0);
154                if(tmpstr == NULL)
155                {
156                        autotimerthread->aktion = STOP;
157                }
158                else
159                {
160                        tmpstr[2] = '\0';
161                        tmpstr[5] = '\0';
162                        search2->endtime = (atoi(tmpstr) * 100) + atoi(tmpstr+3);
163                }
164                free(tmpstr); tmpstr = NULL;
165                if( search2->starttime > search2->endtime) {
166                        search2->endtime = search2->endtime + 2400;
167                        tageswechsel = 1;
168                }
169                channel2 = channel;
170                channelfind = 0;
171                while(channel2 != NULL)
172                {
173                        if(ostrcmp(channel2->name, search2->channelname) == 0)
174                                break;
175                        channel2 = channel2->next;
176                }
177                if(channel2 == NULL)
178                {
179                        search2->channelname = NULL;
180                        search2->search = NULL;
181                        free(search2); search1=NULL;
182                }
183        }
184       
185        if(search1 == NULL && search2 == NULL)
186                autotimerthread->aktion = STOP;
187               
188        while (autotimerthread->aktion != STOP && autostart > 0)
189        {
190                sleep(1);
191                autostart = autostart - 1;
192        }
193               
194        while (autotimerthread->aktion != STOP)
195        {
196                searchall = NULL;
197                channelall = NULL;
198                searchpos = 0;
199                if(search1 != NULL)
200                {
201                        searchall = search1;
202                        channelall = channel1;
203                        searchpos = 1;
204                }
205                else if(search2 != NULL)
206                {
207                        searchall = search2;
208                        channelall = channel2;
209                        searchpos = 2;
210                }
211               
212                while (searchall != NULL)
213                {
214                        i = 0;
215                        node = NULL;
216                        write_note = 0;
217                        epgnode = getepgakt(channelall);
218                        while(epgnode != NULL)
219                        {                                       
220                                result = NULL;
221                                loctime = olocaltime(&epgnode->starttime);
222                                strftime(buf, MINMALLOC, "%H%M", loctime);
223                                epg_starttime = atoi(buf);
224                                free(loctime); loctime = NULL;
225                                if(tageswechsel == 1 && epg_starttime < searchall->starttime)
226                                        epg_starttime = epg_starttime + 2400;
227                                if(epg_starttime >= searchall->starttime &&  epg_starttime <= searchall->endtime)
228                                {
229                                        if(epgnode->title != NULL)
230                                                        result = ostrstrcase(epgnode->title, searchall->search);
231                                        if(result != NULL)
232                                        {
233                                                if(node == NULL)
234                                                {
235                                                        node = addrectimernode(NULL, NULL);
236                                                        node->disabled = 1;
237                                                }
238                                                else if(node->disabled == 0)
239                                                {
240                                                        node = addrectimernode(NULL, NULL);
241                                                        node->disabled = 1;
242                                                }
243       
244                                                node->begin = epgnode->starttime;
245                                                node->begin -= getconfigint("recforerun" , NULL) * 60;
246                                                node->begin -= (node->begin % 60);
247                                                node->end = epgnode->endtime;
248                                                node->end += getconfigint("recoverrun" , NULL) * 60;
249                                                node->end -= (node->end % 60);
250                                                node->transponderid = channelall->transponderid;
251                                                recnode = rectimer;
252                                                konflikt = 0;
253                                               
254                                                //gleiche Sendung, gleicher Sender?
255                                                while(recnode != NULL)
256                                                {
257                                                        if(recnode != node && recnode->status < 2)
258                                                        {
259                                                                if((node->begin >= recnode->begin && node->begin < recnode->end) || (node->end >= recnode->begin && node->end < recnode->end))
260                                                                {
261                                                                        if(ostrcmp(epgnode->title, recnode->name) == 0 && channelall->transponderid == recnode->transponderid && channelall->servicetype == recnode->servicetype)
262                                                                        {       
263                                                                                konflikt = 1;
264                                                                                break;
265                                                                        }
266                                                                }
267                                                        }
268                                                        recnode = recnode->next;
269                                                }
270                                               
271                                                //allgemeiner Konflikt?
272                                                recnode = rectimer;
273                                                if(konflikt == 0)
274                                                {
275                                                        if(checkrectimerconflict(recnode) != 0)
276                                                                konflikt = 1;
277                                                }
278                                               
279                                                if(konflikt == 0)
280                                                {
281                                                        node->pincode = ostrcat("0000", NULL, 0, 0);
282                                                        node->recpath = ostrcat(NULL, getconfig("rec_path", NULL), 0, 0);
283                                                        node->afterevent = 0;
284                                                        node->repeate = 0;
285                                                        if(searchpos == 1)
286                                                        {
287                                                                node->justplay = getconfigint("at1_event", NULL);
288                                                                node->afterevent = getconfigint("at1_afterevent", NULL);
289                                                        }
290                                                        else if(searchpos == 2)
291                                                        {
292                                                                node->justplay = getconfigint("at2_event", NULL);
293                                                                node->afterevent = getconfigint("at2_afterevent", NULL);
294                                                        }
295                                                        node->serviceid = channelall->serviceid;
296                                                        node->servicetype = channelall->servicetype;
297                                                        node->disabled = 0;
298                                                        node->name = ostrcat(epgnode->title, NULL, 0, 0);
299                                                        write_note = 1;
300                                                }
301                                        }
302                                }
303                                epgnode = epgnode->next;
304                        }
305                        if(node != NULL)
306                        {
307                                if(write_note == 1)
308                                {
309                                        if(node->disabled == 1)
310                                                delrectimer(node, 1, 0);
311                                        else
312                                        {
313                                                status.writerectimer = 1;
314                                                writerectimer(getconfig("rectimerfile", NULL), 0);
315                                        }
316                                }
317                                else
318                                        delrectimer(node, 0, 0);
319                        }
320                        if(searchpos == 1)
321                        {
322                                if(search2 != NULL)
323                                {
324                                        searchall = search2;
325                                        channelall = channel2;
326                                        searchpos = 2;
327                                }
328                                else
329                                {
330                                        searchall = NULL;
331                                        channelall = NULL;
332                                  searchpos = 0;
333                                }
334                        }
335                        else if(searchpos == 2)
336                        {
337                                searchall = NULL;
338                                channelall = NULL;
339                                searchpos = 0;
340                        }
341                }
342                while (autotimerthread->aktion != STOP && i < 43200)
343                {
344                        sleep(2);
345                        i = i + 1;
346                }
347        }
348        free(buf); buf = NULL;
349        if(search1 != NULL) {
350                search1->channelname = NULL;
351                search1->search = NULL;
352                free(search1); search1 = NULL;
353        }
354        if(search2 != NULL) {
355                search2->channelname = NULL;
356                search2->search = NULL;
357                free(search2); search2 = NULL;
358        }
359        autotimerthread = NULL;
360        addconfigint("at1_running", 0);
361  return;
362}
363
364
365void autotimer_main()
366{
367        autostart = 10;
368        if(getconfigint("at1_running", NULL) == 1)
369        {
370                if(autotimerthread == NULL)
371                        autotimerthread = addtimer(&autotimer_thread, START, 10000, 1, NULL, NULL, NULL);
372        }
373        else
374        {
375                if(autotimerthread != NULL)
376                        autotimerthread->aktion = STOP;
377        }
378}       
379                       
380//wird beim laden ausgefuehrt
381void init(void)
382{
383        char* tmpstr = NULL;
384        pluginaktiv = 1;
385        tmpstr = createpluginpath("/autotimer/skin.xml", 0);
386        readscreen(tmpstr, 120, 1);
387        free(tmpstr); tmpstr = NULL;
388        debug(10, "Autot Timer Plugin loadet !!!");
389        if(getconfig("at1_akt", NULL) != NULL)
390        {
391                if(getconfigint("at1_running", NULL) == 1)
392                {
393                        if(autotimerthread == NULL)
394                        {
395                                autostart = 120;
396                                autotimerthread = addtimer(&autotimer_thread, START, 10000, 1, NULL, NULL, NULL);
397                        }
398                }
399        }
400        else
401                addconfigint("at1_running", 0);
402}
403
404//wird beim entladen ausgefuehrt
405void deinit(void)
406{
407        if(autotimerthread != NULL)
408        {
409                autotimerthread->aktion = STOP;
410                sleep(2);
411        }
412        delmarkedscreen(120);
413        pluginaktiv = 0;
414        debug(10, "Autot Timer removed !!!");
415}
416
417//wird in der Pluginverwaltung bzw Menue ausfeguehrt
418void start(void)
419{
420        char* tmpstr = NULL;
421        int rcret;
422 
423        struct skin* autotimer = getscreen("autotimer");
424        struct skin* autotimer_listbox = getscreennode(autotimer, "listbox");
425       
426        struct skin* autotimer_akt = getscreennode(autotimer, "akt");
427        struct skin* autotimer_channel = getscreennode(autotimer, "channel");
428        struct skin* autotimer_search = getscreennode(autotimer, "search");
429        struct skin* autotimer_begin = getscreennode(autotimer, "begin");
430        struct skin* autotimer_end = getscreennode(autotimer, "end");
431        struct skin* autotimer_event = getscreennode(autotimer, "event");
432        struct skin* autotimer_afterevent = getscreennode(autotimer, "afterevent");
433       
434        struct skin* autotimer_akt2 = getscreennode(autotimer, "akt2");
435        struct skin* autotimer_channel2 = getscreennode(autotimer, "channel2");
436        struct skin* autotimer_search2 = getscreennode(autotimer, "search2");
437        struct skin* autotimer_begin2 = getscreennode(autotimer, "begin2");
438        struct skin* autotimer_end2 = getscreennode(autotimer, "end2");
439        struct skin* autotimer_event2 = getscreennode(autotimer, "event2");
440        struct skin* autotimer_afterevent2 = getscreennode(autotimer, "afterevent2");
441       
442        struct skin* autotimer_blue = getscreennode(autotimer, "blue");
443        struct skin* tmp = NULL;
444       
445        autostart = 10;
446       
447        if(autotimerthread == NULL)
448                changetext(autotimer_blue, "start");
449        else
450                changetext(autotimer_blue, "stop");
451
452        //**** Block1
453        addchoicebox(autotimer_akt, "0", _("no"));
454        addchoicebox(autotimer_akt, "1", _("yes"));
455        setchoiceboxselection(autotimer_akt, getconfig("at1_akt", NULL));
456       
457        changemask(autotimer_channel, "abcdefghijklmnopqrstuvwxyz");
458        changeinput(autotimer_channel, getconfig("at1_channelname", NULL));
459       
460        changemask(autotimer_search, "abcdefghijklmnopqrstuvwxyz");
461        changeinput(autotimer_search, getconfig("at1_search", NULL));
462
463        changemask(autotimer_begin, "00:00");
464        if(getconfig("at1_starttime", NULL)== NULL)
465                changeinput(autotimer_begin, "00:00");
466        else
467                changeinput(autotimer_begin, getconfig("at1_starttime", NULL));
468       
469        changemask(autotimer_end, "00:00");
470        if(getconfig("at1_endtime", NULL) == NULL)
471                changeinput(autotimer_end, "23:59");
472        else
473                changeinput(autotimer_end, getconfig("at1_endtime", NULL));
474       
475        addchoicebox(autotimer_event, "0", _("record"));
476        addchoicebox(autotimer_event, "1", _("switch channel"));
477        setchoiceboxselection(autotimer_event, getconfig("at1_event", NULL));
478       
479        addchoicebox(autotimer_afterevent, "0", _("auto"));
480        addchoicebox(autotimer_afterevent, "1", _("nothing"));
481        addchoicebox(autotimer_afterevent, "2", _("standby"));
482        addchoicebox(autotimer_afterevent, "3", _("power off"));
483        setchoiceboxselection(autotimer_afterevent, getconfig("at1_afterevent", NULL));
484       
485        //**** Block2
486        addchoicebox(autotimer_akt2, "0", _("no"));
487        addchoicebox(autotimer_akt2, "1", _("yes"));
488        setchoiceboxselection(autotimer_akt2, getconfig("at2_akt", NULL));
489       
490        changemask(autotimer_channel2, "abcdefghijklmnopqrstuvwxyz");
491        changeinput(autotimer_channel2, getconfig("at2_channelname", NULL));
492       
493        changemask(autotimer_search2, "abcdefghijklmnopqrstuvwxyz");
494        changeinput(autotimer_search2, getconfig("at2_search", NULL));
495
496        changemask(autotimer_begin2, "00:00");
497        if(getconfig("at2_starttime", NULL)== NULL)
498                changeinput(autotimer_begin2, "00:00");
499        else
500                changeinput(autotimer_begin2, getconfig("at2_starttime", NULL));
501       
502        changemask(autotimer_end2, "00:00");
503        if(getconfig("at2_endtime", NULL) == NULL)
504                changeinput(autotimer_end2, "23:59");
505        else
506                changeinput(autotimer_end2, getconfig("at2_endtime", NULL));
507       
508        addchoicebox(autotimer_event2, "0", _("record"));
509        addchoicebox(autotimer_event2, "1", _("switch channel"));
510        setchoiceboxselection(autotimer_event2, getconfig("at2_event", NULL));
511       
512        addchoicebox(autotimer_afterevent2, "0", _("auto"));
513        addchoicebox(autotimer_afterevent2, "1", _("nothing"));
514        addchoicebox(autotimer_afterevent2, "2", _("standby"));
515        addchoicebox(autotimer_afterevent2, "3", _("power off"));
516        setchoiceboxselection(autotimer_afterevent2, getconfig("at2_afterevent", NULL));       
517       
518        drawscreen(autotimer, 0, 0);
519        addscreenrc(autotimer, autotimer_listbox);
520        tmp = autotimer_listbox->select;
521       
522        while(1)
523        {
524                addscreenrc(autotimer, tmp);
525                rcret = waitrc(autotimer, 0, 0);
526                tmp = autotimer_listbox->select;
527               
528                if (rcret == getrcconfigint("rcexit", NULL)) break;
529                if (rcret == getrcconfigint("rcblue", NULL))
530                {
531                        if(autotimerthread != NULL)
532                        {
533                                autotimerthread->aktion = STOP;
534                                sleep(3);
535                                if(autotimerthread == NULL)
536                                {
537                                        textbox(_("Stop AutoTimer"), _("AutoTimer succesfull stopped"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0);   
538                                        changetext(autotimer_blue, "start");
539                                        addconfigint("at1_running", 0);
540                                }
541                                else
542                                {
543                                        textbox(_("Stop AutoTimer"), _("ERROR... AutoTimer not stopped"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0);         
544                                        addconfigint("at1_running", 1);
545                                }
546                        }
547                        else
548                        {       
549                                addconfig("at1_akt", autotimer_akt->ret);
550                                addconfig("at1_channelname", autotimer_channel->ret);
551                                addconfig("at1_search", autotimer_search->ret);
552                                addconfig("at1_starttime", autotimer_begin->ret);
553                                addconfig("at1_endtime", autotimer_end->ret);
554                                addconfig("at1_event", autotimer_event->ret);
555                                addconfig("at1_afterevent", autotimer_afterevent->ret);
556                                addconfig("at2_akt", autotimer_akt2->ret);
557                                addconfig("at2_channelname", autotimer_channel2->ret);
558                                addconfig("at2_search", autotimer_search2->ret);
559                                addconfig("at2_starttime", autotimer_begin2->ret);
560                                addconfig("at2_endtime", autotimer_end2->ret);
561                                addconfig("at2_event", autotimer_event2->ret);
562                                addconfig("at2_afterevent", autotimer_afterevent2->ret);
563                               
564                                autotimerthread = addtimer(&autotimer_thread, START, 10000, 1, NULL, NULL, NULL);
565                                sleep(1);
566                                if(autotimerthread != NULL)
567                                {
568                                        textbox(_("Start AutoTimer"), _("AutoTimer succesfull started"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0);   
569                                        changetext(autotimer_blue, "stop");
570                                        addconfigint("at1_running", 1);
571                                }
572                                else
573                                {
574                                        addconfigint("at1_running", 0);
575                                        textbox(_("Start AutoTimer"), _("ERROR... AutoTimer not started"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0);         
576                                }
577                        }
578                }
579                if (rcret == getrcconfigint("rcgreen", NULL))
580                {
581                        addconfig("at1_akt", autotimer_akt->ret);
582                        addconfig("at1_channelname", autotimer_channel->ret);
583                        addconfig("at1_search", autotimer_search->ret);
584                        addconfig("at1_starttime", autotimer_begin->ret);
585                        addconfig("at1_endtime", autotimer_end->ret);
586                        addconfig("at1_event", autotimer_event->ret);
587                        addconfig("at1_afterevent", autotimer_afterevent->ret);
588                        addconfig("at2_akt", autotimer_akt2->ret);
589                        addconfig("at2_channelname", autotimer_channel2->ret);
590                        addconfig("at2_search", autotimer_search2->ret);
591                        addconfig("at2_starttime", autotimer_begin2->ret);
592                        addconfig("at2_endtime", autotimer_end2->ret);
593                        addconfig("at2_event", autotimer_event2->ret);
594                        addconfig("at2_afterevent", autotimer_afterevent2->ret);
595                       
596                        if(getconfigint("at1_running", NULL) == 1)
597                        {
598                                autotimerthread->aktion = STOP;
599                                sleep(2);
600                                if(autotimerthread == NULL)
601                                        autotimerthread = addtimer(&autotimer_thread, START, 10000, 1, NULL, NULL, NULL);
602                        }
603                        textbox(_("Save Settings"), _("Settings succesfull saved"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0);
604                }
605                if (rcret == getrcconfigint("rcepg", NULL))
606                {
607                        if(status.aktservice->channel != NULL)
608                        {
609                                tmpstr = ostrcat(tmpstr, status.aktservice->channel->name, 1, 0);
610                                if(autotimer_listbox->select != NULL)
611                                {
612                                        if(ostrcmp(autotimer_listbox->select->name, "channel") == 0)
613                                                changeinput(autotimer_channel, tmpstr);
614                                        else if(ostrcmp(autotimer_listbox->select->name, "channel2") == 0)
615                                                changeinput(autotimer_channel2, tmpstr);
616                                }
617                                tmpstr = NULL;
618                        }
619                }
620                       
621
622                drawscreen(autotimer, 0, 0);
623        }
624        delownerrc(autotimer);
625        clearscreen(autotimer);
626}
Note: See TracBrowser for help on using the repository browser.