Switch to unified view

a b/upmpd/ohtime.cxx
1
/* Copyright (C) 2014 J.F.Dockes
2
 *     This program is free software; you can redistribute it and/or modify
3
 *     it under the terms of the GNU General Public License as published by
4
 *     the Free Software Foundation; either version 2 of the License, or
5
 *     (at your option) any later version.
6
 *
7
 *     This program is distributed in the hope that it will be useful,
8
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *     GNU General Public License for more details.
11
 *
12
 *     You should have received a copy of the GNU General Public License
13
 *     along with this program; if not, write to the
14
 *     Free Software Foundation, Inc.,
15
 *     59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <unistd.h>
21
#include <sys/types.h>
22
#include <pwd.h>
23
24
#include <string>
25
#include <iostream>
26
#include <sstream>
27
#include <vector>
28
#include <functional>
29
#include <set>
30
using namespace std;
31
using namespace std::placeholders;
32
33
#include "libupnpp/upnpplib.hxx"
34
#include "libupnpp/soaphelp.hxx"
35
#include "libupnpp/device.hxx"
36
#include "libupnpp/log.hxx"
37
38
#include "upmpd.hxx"
39
#include "ohtime.hxx"
40
#include "mpdcli.hxx"
41
#include "upmpdutils.hxx"
42
43
static const string sTpProduct("urn:av-openhome-org:service:Time:1");
44
static const string sIdProduct("urn:av-openhome-org:serviceId:Time");
45
46
OHTime::OHTime(UpMpd *dev)
47
    : UpnpService(sTpProduct, sIdProduct, dev), m_dev(dev)
48
{
49
    dev->addActionMapping("Time", bind(&OHTime::ohtime, this, _1, _2));
50
}
51
52
void OHTime::getdata(string& trackcount, string &duration, 
53
                     string& seconds)
54
{
55
    const MpdStatus &mpds =  m_dev->getMpdStatus();
56
57
    char cbuf[30];
58
    sprintf(cbuf, "%d", mpds.trackcounter);
59
    trackcount = cbuf;
60
    bool is_song = (mpds.state == MpdStatus::MPDS_PLAY) || 
61
        (mpds.state == MpdStatus::MPDS_PAUSE);
62
    if (is_song) {
63
        sprintf(cbuf, "%u", mpds.songlenms / 1000);
64
        duration = cbuf;
65
        sprintf(cbuf, "%u", mpds.songelapsedms / 1000);
66
        seconds = cbuf;
67
    }
68
}
69
70
bool OHTime::makestate(unordered_map<string, string> &st)
71
{
72
    st.clear();
73
    string trackcount("0"), duration("0"), seconds("0");
74
    getdata(trackcount, duration, seconds);
75
    st["TrackCount"] = trackcount;
76
    st["Duration"] = duration;
77
    st["Seconds"] = seconds;
78
    return true;
79
}
80
81
bool OHTime::getEventData(bool all, std::vector<std::string>& names, 
82
                          std::vector<std::string>& values)
83
{
84
    LOGDEB("OHTime::getEventData" << endl);
85
86
    unordered_map<string, string> state;
87
    makestate(state);
88
89
    unordered_map<string, string> changed;
90
    if (all) {
91
        changed = state;
92
    } else {
93
        changed = diffmaps(m_state, state);
94
    }
95
    m_state = state;
96
97
    for (unordered_map<string, string>::iterator it = changed.begin();
98
         it != changed.end(); it++) {
99
        names.push_back(it->first);
100
        values.push_back(it->second);
101
    }
102
103
    return true;
104
}
105
106
int OHTime::ohtime(const SoapArgs& sc, SoapData& data)
107
{
108
    LOGDEB("OHTime::ohtime" << endl);
109
    string trackcount("0"), duration("0"), seconds("0");
110
    getdata(trackcount, duration, seconds);
111
    data.addarg("TrackCount", trackcount);
112
    data.addarg("Duration", duration);
113
    data.addarg("Seconds", seconds);
114
    return UPNP_E_SUCCESS;
115
}