summaryrefslogtreecommitdiff
path: root/service.c
blob: 6c939c96a7a1462cf8fa168a2cb95f4bcb52d006 (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
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
/**
 *  GraphLCD plugin for the Video Disk Recorder
 *
 *  service.h  -  class for events from external services
 *
 *  (c) 2010-2011 Wolfgang Astleitner <mrwastl AT users sourceforge net>
 *
 *  mailbox-contribution: user 'Keine_Ahnung'
 **/

#include "strfct.h"
#include <limits>

#include "service.h"

#include <vdr/plugin.h>


cGraphLCDService::cGraphLCDService(cGraphLCDState * state)
{
    mState = state;

    /* initialise flags for services */
    radioActive    = false;    radioChanged   = false;    radioUse     = false;
    lcrActive      = false;    lcrChanged     = false;    lcrUse       = false;
    femonActive    = false;    femonChanged   = false;    femonUse     = false;
    mailboxActive  = false;    mailboxChanged = false;    mailboxUse   = false;

    radioLastChange = lcrLastChange = femonLastChange = mailboxLastChange = 0;

    femonVersionChecked = femonVersionValid = false;

    // default min. change delays
    radioUpdateDelay   = 100;   // 100 ms
    lcrUpdateDelay     = 60000; // 60 sec
    femonUpdateDelay   = 2000;  // 2 sec
    mailboxUpdateDelay = 10000; // 10 sec
}

cGraphLCDService::~cGraphLCDService()
{
}


bool cGraphLCDService::ServiceIsAvailable(const std::string & Name) {
    if (Name == "RadioTextService-v1.0" || Name == "radio") {
        radioUse = true;
        return (mState->GetChannelInfo().isRadio) ? radioActive : false;
    } else if (Name == "LcrService-v1.0" || Name == "lcr") {
        lcrUse = true;
        return lcrActive;
    } else if (Name == "FemonService-v1.0" || Name == "femon") {
        femonUse = true;
        return femonActive;
    } else if (Name == "MailBox-1.0" || Name == "mailbox") {
        mailboxUse = true;
        return mailboxActive;
    }
    return false;
}


void cGraphLCDService::SetServiceUpdateDelay(const std::string & Name, int delay) {
    if (delay < 100)
        return;

    if (Name == "RadioTextService-v1.0" || Name == "radio") {
        radioUpdateDelay = delay;
    } else if (Name == "LcrService-v1.0" || Name == "lcr") {
        lcrUpdateDelay = delay;
    } else if (Name == "FemonService-v1.0" || Name == "femon") {
        femonUpdateDelay = delay;
    } else if (Name == "MailBox-1.0" || Name == "mailbox") {
        mailboxUpdateDelay = delay;
    }
}


GLCD::cType cGraphLCDService::GetItem(const std::string & ServiceName, const std::string & Item = "")
{
    size_t found = 0;
    std::string ItemName = Item;
    std::string ItemFormat = "";
    std::string ItemExtra = "";

    if (ItemName != "") {
        found = Item.find(",");
        if (found != std::string::npos) {
            ItemName = Item.substr(0, found);
            ItemFormat = Item.substr(found+1);
            if ( (found = ItemFormat.find(",")) != std::string::npos) {
                ItemExtra = ItemFormat.substr(found+1);
                ItemFormat = ItemFormat.substr(0, found);
            }
        } else {
            ItemName = Item;
        }
    }

    //NeedsUpdate();
    if (ServiceName == "RadioTextService-v1.0" || ServiceName == "radio") {
        radioUse = true;
        if (radioActive) {
            if (ItemName == "" || ItemName == "default") {
                if (currRTSData.rds_info == 2 && strstr(currRTSData.rds_title, "---") == NULL) {
                    char rtpinfo[2][65], rtstr[140];
                    strcpy(rtpinfo[0], currRTSData.rds_title);
                    strcpy(rtpinfo[1], currRTSData.rds_artist);
                    sprintf(rtstr, "%02d:%02d  %s | %s", currRTSData.title_start->tm_hour, currRTSData.title_start->tm_min, trim(((std::string)(rtpinfo[0]))).c_str(), trim(((std::string)(rtpinfo[1]))).c_str());
                    return rtstr;
                } else if (currRTSData.rds_info > 0) {
                    return trim(currRTSData.rds_text);
                }
            } else if (ItemName == "title") {
                    return trim(currRTSData.rds_title);
            } else if (ItemName == "artist") {
                    return trim(currRTSData.rds_artist);
            } else if (ItemName == "text") {
                    return trim(currRTSData.rds_text);
            } else if (ItemName == "rds_info" || ItemName == "info") {
                    return (int)(currRTSData.rds_info);
            } else if (ItemName == "rds_pty" || ItemName == "pty") {
                    return (int)(currRTSData.rds_pty);
            }
        }

    } else if (ServiceName == "LcrService-v1.0" || ServiceName == "lcr") {
        lcrUse = true;
        if (lcrActive) {
            if ( strstr( currLcrData.destination, "---" ) == NULL ) {
                char lcrStringParts[3][25], lcrString[100];
                strcpy( lcrStringParts[0], (const char *)currLcrData.destination );
                strcpy( lcrStringParts[1], (const char *)currLcrData.price );
                strcpy( lcrStringParts[2], (const char *)currLcrData.pulse );
                sprintf(lcrString, "%s | %s", trim((std::string)(lcrStringParts[1])).c_str(), trim((std::string)(lcrStringParts[2])).c_str());
                if (ItemName == "" || ItemName == "default") {
                    return trim(lcrString);
                } else if (ItemName == "destination") {
                    return trim(lcrStringParts[0]);
                } else if (ItemName == "price") {
                    return trim(lcrStringParts[1]);
                } else if (ItemName == "pulse") {
                    return trim(lcrStringParts[2]);
                }
            }
        }
    } else if (ServiceName == "FemonService-v1.0" || ServiceName == "femon") {
        femonUse = true;
        if (femonActive) {
            if (ItemName == "" || ItemName == "default" || ItemName == "status") {
                return (const char*)currFemonData.fe_status;
            } else if (ItemName == "name") {
                return (const char*)currFemonData.fe_name;
            } else if (ItemName == "snr") {
                return (int)currFemonData.fe_snr;
            } else if (ItemName == "signal") {
                return (int)currFemonData.fe_signal;
            } else if (ItemName == "percent_snr") {
                return (int)((currFemonData.fe_snr * 100) / 65535);
            } else if (ItemName == "percent_signal") {
                return (int)((currFemonData.fe_signal * 100) / 65535);
            } else if (ItemName == "ber") {
                return (int)currFemonData.fe_ber;
            } else if (ItemName == "unc") {
                return (int)currFemonData.fe_unc;
            } else if (ItemName == "video_bitrate" || ItemName == "vbr") {
                if (ItemFormat == "" && (ItemExtra == "" || ItemExtra == "0")) {
                    return (int)currFemonData.video_bitrate;
                } else {
                    double divisor = (ItemExtra == "") ? 1.0 : atof(ItemExtra.c_str());
                    double value = currFemonData.video_bitrate;
                    if (ItemFormat == "") {
                       return (int)(value / divisor);
                    } else {
                       char buf[20];
                       snprintf(buf, 19, ItemFormat.c_str(), (value / divisor));
                       return buf;
                    }
                }
            } else if (ItemName == "audio_bitrate" || ItemName == "abr") {
                if (ItemFormat == "" && (ItemExtra == "" || ItemExtra == "0")) {
                    return (int)currFemonData.audio_bitrate;
                } else {
                    double divisor = (ItemExtra == "") ? 1 : atof(ItemExtra.c_str());
                    double value = currFemonData.audio_bitrate;
                    if (ItemFormat == "") {
                       return (int)(value / divisor);
                    } else {
                       char buf[20];
                       snprintf(buf, 19, ItemFormat.c_str(), (value / divisor));
                       return buf;
                    }
                }
            } else if (ItemName == "dolby_bitrate" || ItemName == "dbr") {
                if (ItemFormat == "" && (ItemExtra == "" || ItemExtra == "0")) {
                    return (int)currFemonData.dolby_bitrate;
                } else {
                    double divisor = (ItemExtra == "") ? 1 : atof(ItemExtra.c_str());
                    double value = currFemonData.dolby_bitrate;
                    if (ItemFormat == "") {
                       return (int)(value / divisor);
                    } else {
                       char buf[20];
                       snprintf(buf, 19, ItemFormat.c_str(), (value / divisor));
                       return buf;
                    }
                }
            }
        }
    } else if (ServiceName == "MailBox-1.0" || ServiceName == "mailbox") {
        mailboxUse = true;
        if (mailboxActive) {
            if (ItemName == "" || ItemName == "default"|| ItemName == "hasnew") {
                return (bool)currMailboxNewData;
            } else if (ItemName == "newcount") {
                if (currMailboxUnseenData > std::numeric_limits<int>::max()) {
                    return (int)-1;
                } else {
                    return (int)currMailboxUnseenData;
                }
            }
        }
    }

    return "";
}



/* async. check event updates for services from other plugins */
/* only sets flags but does NOT update display output */
bool cGraphLCDService::NeedsUpdate(uint64_t CurrentTime)
{
    //mutex.Lock();

    /*radioActive  = false;*/    radioChanged   = false;
    /*lcrActive    = false;*/    lcrChanged     = false;
    /*femonActive  = false;*/    femonChanged   = false;
                                 mailboxChanged = false;

    cPlugin *p = NULL;

    // Radiotext
    // only ask if radio-services are defined in the skin and min. request delay exceeded
    if (radioUse && ((CurrentTime-radioLastChange) >= (uint64_t)radioUpdateDelay)) {
        radioLastChange = CurrentTime;
        p = cPluginManager::CallFirstService("RadioTextService-v1.0", NULL);
        if (p) {
            if (cPluginManager::CallFirstService("RadioTextService-v1.0", &checkRTSData)) {
                radioActive = true;
                if (
                        (currRTSData.rds_info != checkRTSData.rds_info) ||
                        (currRTSData.rds_pty != checkRTSData.rds_pty) ||
                        (strcmp(currRTSData.rds_text, checkRTSData.rds_text) != 0) ||
                        (strcmp(currRTSData.rds_title, checkRTSData.rds_title) != 0) ||
                        (strcmp(currRTSData.rds_artist, checkRTSData.rds_artist) != 0)
                  )
                {
                    currRTSData.rds_info   = checkRTSData.rds_info;
                    currRTSData.rds_pty    = checkRTSData.rds_pty;
                    currRTSData.rds_text   = checkRTSData.rds_text;
                    currRTSData.rds_title  = checkRTSData.rds_title;
                    currRTSData.rds_artist = checkRTSData.rds_artist;
                    currRTSData.title_start= checkRTSData.title_start;

                    radioChanged = true;
                }
            }
        }
    }

    // Lcr
    // only ask if lcr-services are defined in the skin and min. request delay exceeded
    if (lcrUse && ((CurrentTime-lcrLastChange) >= (uint64_t)lcrUpdateDelay)) {
        lcrLastChange = CurrentTime;
        p = cPluginManager::CallFirstService("LcrService-v1.0", NULL);
        if (p) {
            if (cPluginManager::CallFirstService("LcrService-v1.0", &checkLcrData)) {
                lcrActive = true;
                if (
                        (strcmp(currLcrData.destination, checkLcrData.destination) != 0) ||
                        (strcmp(currLcrData.price, checkLcrData.price) != 0) ||
                        (strcmp(currLcrData.pulse, checkLcrData.pulse) != 0)
                  )
                {
                    currLcrData.destination = checkLcrData.destination;
                    currLcrData.price       = checkLcrData.price;
                    currLcrData.pulse       = checkLcrData.pulse;

                    lcrChanged = true;
                }
            }
        }
    }


    // femon
    // only ask if femon-services are defined in the skin and min. request delay exceeded
    if (femonUse && ((CurrentTime-femonLastChange) >= (uint64_t)femonUpdateDelay)) {
        femonLastChange = CurrentTime;
        p = cPluginManager::CallFirstService("FemonService-v1.0", NULL);
        if (p) {
#ifdef GRAPHLCD_SERVICE_FEMON_VALID
            femonVersionChecked = femonVersionValid = true;
#else
            // nota bene: femon version <= 1.2.x will not make it until here because of a 2nd bug in femon in these versions
            if (!femonVersionChecked) { // only execute this check once
                std::string version = p->Version();
                size_t mstart = 0;
                size_t mend = std::string::npos;
                int numval = 0;

                if ((mend = version.find(".", mstart)) != std::string::npos) {
                    numval = atoi(version.substr(mstart, mend-mstart).c_str());
                    if (numval > 1) {
                        femonVersionValid = true;
                    } else if (numval == 1) { // version 1.x.x
                        mstart = mend + 1;
                        if ((mend = version.find(".", mstart)) != std::string::npos) {
                            numval = atoi(version.substr(mstart, mend-mstart).c_str());
                            if (numval <= 6) { // version <= 1.6.x
                                isyslog("graphlcd plugin: femon <= 1.7.7 requires a patch prior to be usable with graphlcd.\n");
                                isyslog("graphlcd plugin: see README for further instructions.\n");
                            } else if (numval == 7) {
                                mstart = mend + 1;
                                numval = atoi(version.substr(mstart).c_str());  // ignore trailing characters
                                if (numval <= 7) {
                                    isyslog("graphlcd plugin: femon <= 1.7.7 requires a patch prior to be usable with graphlcd.\n");
                                    isyslog("graphlcd plugin: see README for further instructions.\n");
                                } else { // version >= 1.7.8: ok
                                    femonVersionValid = true;
                                }
                            } else { // version >= 1.8.x: ok
                                femonVersionValid = true;
                            }
                        } else {
                            isyslog("graphlcd plugin: unable to decode version information of femon.\n");
                        }
                    } else {
                        isyslog("graphlcd plugin: unable to decode version information of femon.\n");
                    }
                }
                femonVersionChecked = true;
            }
#endif  /* GRAPHLCD_SERVICE_FEMON_VALID */

            if (femonVersionValid) {
                if (cPluginManager::CallFirstService("FemonService-v1.0", &checkFemonData)) {
                    femonActive = true;
                    if (
#if 0
                            (strcmp(currFemonData.fe_name, checkFemonData.fe_name) != 0)  ||
                            (strcmp(currFemonData.fe_status, checkFemonData.fe_status) != 0)  ||
#endif
                            (currFemonData.fe_signal     != checkFemonData.fe_signal)     ||
                            (currFemonData.fe_ber        != checkFemonData.fe_ber)        ||
                            (currFemonData.fe_unc        != checkFemonData.fe_unc)        ||
                            (currFemonData.video_bitrate != checkFemonData.video_bitrate) ||
                            (currFemonData.audio_bitrate != checkFemonData.audio_bitrate) ||
                            (currFemonData.dolby_bitrate != checkFemonData.dolby_bitrate)
                      )
                    {
                        currFemonData.fe_name       = checkFemonData.fe_name;
                        currFemonData.fe_status     = checkFemonData.fe_status;
                        currFemonData.fe_snr        = checkFemonData.fe_snr;
                        currFemonData.fe_signal     = checkFemonData.fe_signal;
                        currFemonData.fe_ber        = checkFemonData.fe_ber;
                        currFemonData.fe_unc        = checkFemonData.fe_unc;
                        currFemonData.video_bitrate = checkFemonData.video_bitrate;
                        currFemonData.audio_bitrate = checkFemonData.audio_bitrate;
                        currFemonData.dolby_bitrate = checkFemonData.dolby_bitrate;

                        femonChanged = true;
                    }
                }
            }
        }
    }

    // Mailbox
    // only ask if mailbox-services are defined in the skin and min. request delay exceeded
    if (mailboxUse && ((CurrentTime-mailboxLastChange) >= (uint64_t)mailboxUpdateDelay)) {
        mailboxLastChange = CurrentTime;
        if (cPluginManager::CallFirstService("MailBox-HasNewMail-1.0", NULL) &&
            cPluginManager::CallFirstService("MailBox-GetTotalUnseen-1.0", NULL)) {
            if (cPluginManager::CallFirstService("MailBox-HasNewMail-1.0", &checkMailboxNewData) &&
                cPluginManager::CallFirstService("MailBox-GetTotalUnseen-1.0", &checkMailboxUnseenData)) {
                mailboxActive = true;
                if (
                        (currMailboxNewData != checkMailboxNewData) ||
                        (currMailboxUnseenData != checkMailboxUnseenData)
                  )
                {
                    currMailboxNewData = checkMailboxNewData;
                    currMailboxUnseenData = checkMailboxUnseenData;

                    mailboxChanged = true;
                }
            }
        }
    }

    //mutex.Unlock();
    return (radioChanged || lcrChanged || femonChanged || mailboxChanged);
}