summaryrefslogtreecommitdiff
path: root/infosatepg.cpp
blob: 7fc3cff1ea78928174fd060f27d6bef5b47548bf (plain)
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
/*
 * infosatepg.cpp: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/plugin.h>
#include <vdr/device.h>
#include <vdr/channels.h>
#include <vdr/transfer.h>
#include <vdr/shutdown.h>
#include <vdr/eitscan.h>
#include <getopt.h>

#include "infosatepg.h"
#include "setup.h"
#include "process.h"

// --- cPluginInfosatepg
cPluginInfosatepg::cPluginInfosatepg(void)
{
    // Initialize any member variables here.
    // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
    // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
    statusMonitor=NULL;
    global=new cGlobalInfosatepg;
    numprocessed=0;
    mac=EPG_FIRST_DAY_MAC;
}

cPluginInfosatepg::~cPluginInfosatepg()
{
    // Clean up after yourself!
    if (statusMonitor) delete statusMonitor;
    delete global;
}

const char *cPluginInfosatepg::CommandLineHelp(void)
{
    // Return a string that describes all known command line options.
    return "  -d DIR,   --dir=DIR      use DIR as infosat data directory\n"
           "                           (default: /tmp)\n";
}

bool cPluginInfosatepg::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.
    static struct option long_options[] =
    {
        { "dir",      required_argument, NULL, 'd'
        },
        { NULL }
    };

    int c;
    while ((c = getopt_long(argc, argv, "d:", long_options, NULL)) != -1)
    {
        switch (c)
        {
        case 'd':
            if (global->SetDirectory(optarg))
            {
                isyslog("infosatepg: using directory '%s' for data",optarg);
            }
            else
            {
                fprintf(stderr,"infosatepg: can't access data directory: %s\n",
                        optarg);
                return false;
            }
            break;
        default:
            return false;
        }
    }
    return true;
}

bool cPluginInfosatepg::Initialize(void)
{
    // Initialize plugin - check channellist
    tChannelID ChannelID;
    for (int i=0; i<global->InfosatChannels(); i++)
    {
        ChannelID=global->GetChannelID(i);
        if (!Channels.GetByChannelID(ChannelID))
        {
            dsyslog("infosatepg: remove %s",*ChannelID.ToString());
            global->RemoveChannel(i);
            // Removing entries from setup.conf is not possible!
        }
    }
    if (!global->FindReceiverChannel())
        esyslog("infosatepg: found no channel to receive, check setup");
    return true;
}

bool cPluginInfosatepg::Start(void)
{
    // Start any background activities the plugin shall perform
    if (global->Load()==-1)
    {
        isyslog("infosatepg: failed to load plugin status");
    }
    statusMonitor = new cStatusInfosatepg(global);

    return true;
}

void cPluginInfosatepg::Stop(void)
{
    // Stop any background activities the plugin is performing.
    if (global->Save()==-1)
    {
        isyslog("infosatepg: failed to save plugin status");
    }
}

void cPluginInfosatepg::Housekeeping(void)
{
    // Perform any cleanup or other regular tasks.
}

void cPluginInfosatepg::MainThreadHook(void)
{
    // Perform actions in the context of the main program thread.
    if (!global->WaitOk()) return;
    if (global->ReceivedAll() && !global->ProcessedAll)
    {
        if (!global->Infosatdata[mac].Processed)
        {
            isyslog ("infosatepg: found data to be processed: day=%i month=%i",
                     global->Infosatdata[mac].Day(),global->Infosatdata[mac].Month());
            cProcessInfosatepg process(mac,global);
            global->SetWaitTimer();
        }
        if (global->Infosatdata[mac].Processed)
        {
            numprocessed++;
            mac++;
        }
        if (numprocessed==EPG_DAYS)
        {
            global->ProcessedAll=true;
        }
    }
    else
    {
        numprocessed=0;
        mac=EPG_FIRST_DAY_MAC;
    }

    if ((global->Switched()) || (global->ReceivedAll()) || (global->Channel()==-1)) return;

    cChannel *chan=Channels.GetByNumber(global->Channel());
    if (!chan) return;

    if (ShutdownHandler.IsUserInactive())
    {
        // we are idle -> use live device if we can
        cDevice *dev;
        dev=cDevice::ActualDevice();
        if (dev->ProvidesTransponder(chan) && !dev->Receiving())
        {
            // ok -> use this device
            dsyslog("infosatepg: found free device %i (live)",dev->DeviceNumber()+1);
            if (global->LastCurrentChannel==-1) global->LastCurrentChannel=
                    cDevice::PrimaryDevice()->CurrentChannel();
            cDevice::PrimaryDevice()->SwitchChannel(chan,true);
            global->SetWaitTimer();
            return;
        }
    }

    // Cannot use live device try another (if possible)

    for (int i=0; i<cDevice::NumDevices(); i++)
    {
        cDevice *dev=cDevice::GetDevice(i);
        if (dev)
        {
            if (!dev->ProvidesTransponder(chan)) continue; // device cannot provide transponder -> skip
            if (EITScanner.UsesDevice(dev)) continue; // EITScanner is updating EPG -> skip
            if (dev->Receiving()) continue; // device is recording -> skip
            if (dev->IsPrimaryDevice()) continue; // device is primary -> skip
            if (cDevice::ActualDevice()->CardIndex()==i) continue; // device is live viewing -> skip
            if (dev->IsTunedToTransponder(chan))
            {
                // we already have a device which is tuned (maybe switched manually?)
                // the filter will be added in status.cpp
                global->SetWaitTimer();
                return;
            }

            // ok -> use this device
            dsyslog("infosatepg: found free device %i",dev->DeviceNumber()+1);
            dev->SwitchChannel(chan,false);
            global->SetWaitTimer();
            return;
        }
    }

}

cString cPluginInfosatepg::Active(void)
{
    // Returns a message string if we are not ready

    if (global->NoDeferredShutdown) return NULL;

    // if we cannot receive, we shouldn't wait
    if (global->Channel()==-1) return NULL;

    if (!global->ProcessedAll)
        return tr("Infosat plugin still working");

    // we are done
    if (global->LastCurrentChannel!=-1)
    {
        // we switched from users last channel
        if (cDevice::PrimaryDevice()->CurrentChannel()==global->Channel())
        {
            // we are still on infosatepg channel
            cChannel *chan=Channels.GetByNumber(global->LastCurrentChannel);
            if (chan)
            {
                // switch back to users last viewed channel
                cDevice::PrimaryDevice()->SwitchChannel(chan,true);
                global->LastCurrentChannel=-1;
            }
        }
    }
    return NULL;
}

time_t cPluginInfosatepg::WakeupTime(void)
{
    // Returns custom wakeup time for shutdown script

    if (global->NoWakeup) return 0; // user option set -> don't wake up
    if (global->Channel()==-1) return 0; // we cannot receive, so we don't need to wake up
    if (global->WakeupTime()==-1) global->SetWakeupTime(300); // just to be safe
    time_t Now = time(NULL);
    time_t Time = cTimer::SetTime(Now,cTimer::TimeToInt(global->WakeupTime()));
    if (Time <= Now)
        Time = cTimer::IncDay(Time,1);
    return Time;
}

cOsdObject *cPluginInfosatepg::MainMenuAction(void)
{
    // Perform the action when selected from the main VDR menu.
    return NULL;
}

cMenuSetupPage *cPluginInfosatepg::SetupMenu(void)
{
    // Return the setup menu.
    return new cMenuSetupInfosatepg(global);
}

bool cPluginInfosatepg::SetupParse(const char *Name, const char *Value)
{
    // Parse your own setup parameters and store their values.
    if      (!strcasecmp(Name,"Pid")) global->Pid=atoi(Value);
    else if (!strcasecmp(Name,"Frequency")) global->Frequency=atoi(Value);
    else if (!strcasecmp(Name,"Polarization")) global->Polarization=atoi(Value);
    else if (!strcasecmp(Name,"Srate")) global->Srate=atoi(Value);
    else if (!strcasecmp(Name,"NoWakeup")) global->NoWakeup=atoi(Value);
    else if (!strcasecmp(Name,"NoDeferredShutdown")) global->NoDeferredShutdown=atoi(Value);
    else if (!strcasecmp(Name,"WaitTime")) global->WaitTime=atoi(Value);
    else if (!strcasecmp(Name,"EventTimeDiff")) global->EventTimeDiff=60*atoi(Value);
    else if (!strncasecmp(Name,"Channel",7))
    {
        if (strlen(Name)<10) return false;
        tChannelID ChannelID=tChannelID::FromString(&Name[8]);
        if (ChannelID==tChannelID::InvalidID) return false;
        int val=atoi(Value);
        global->AddChannel(ChannelID,(val & 0xFFFF),(val & 0xF0000)>>16);
    }
    else return false;
    return true;
}

bool cPluginInfosatepg::Service(const char *Id, void *Data)
{
    // Handle custom service requests from other plugins
    return false;
}

const char **cPluginInfosatepg::SVDRPHelpPages(void)
{
    // Returns help text for SVDRP
    static const char *HelpPages[] =
    {
        "STAT\n"
        "    Return actual state of the plugin",
        "RESR\n"
        "    Reset received all",
        "REPR\n"
        "    Reprocess again",
        NULL
    };
    return HelpPages;
}

cString cPluginInfosatepg::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
    // Process SVDRP commands
    char *output=NULL;
    if (!strcasecmp(Command,"RESR"))
    {
        global->ResetReceivedAll();
        asprintf(&output,"OK\n");
    }
    if (!strcasecmp(Command,"REPR"))
    {
        global->ResetProcessed();
        asprintf(&output,"OK\n");
    }
    if (!strcasecmp(Command,"STAT"))
    {
        int day,month;
        asprintf(&output,"InfosatEPG state:\n");
        if (global->ReceivedAll(&day,&month))
            asprintf(&output,"%s Received all: yes (%02i.%02i.)",output,day,month);
        else
            asprintf(&output,"%s Received all: no",output);
        asprintf(&output,"%s Processed all: %s",output,global->ProcessedAll ? "yes" : "no");
        asprintf(&output,"%s Switched: %s\n",output,global->Switched() ? "yes" : "no");
        if (global->WakeupTime()!=-1)
        {
            asprintf(&output,"%s WakeupTime: %04i ", output,global->WakeupTime());
            if (global->NoWakeup) asprintf(&output,"%s (blocked) ",output);
        }
        else
        {
            asprintf(&output,"%s WakeupTime: unset ", output);
        }
        if (global->LastCurrentChannel!=-1)
        {
            asprintf(&output,"%s Switchback to: %i\n", output, global->LastCurrentChannel);
        }
        else
        {
            asprintf(&output,"%s Switchback to: unset\n",output);
        }
        for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
        {
            asprintf(&output,"%s Day %i (%02i.%02i.): %3i%% %s\n",
                     output,mac,global->Infosatdata[mac].Day(),global->Infosatdata[mac].Month(),
                     global->Infosatdata[mac].ReceivedPercent(),
                     global->Infosatdata[mac].Processed ? "processed" : "");
        }
    }
    return output;
}

VDRPLUGINCREATOR(cPluginInfosatepg); // Don't touch this!