-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlmccom.h
216 lines (166 loc) · 6.37 KB
/
lmccom.h
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
/*
* lmccom.h
*
* See the README file for copyright information
*
*/
// https://github.com/elParaguayo/LMS-CLI-Documentation/blob/master/LMS-CLI.md
/*
or (better) use JSON interface like:
- the first parameter is the player id or "" for 'global' commands
curl -X POST -d '{ "method": "slim.request", "params":["b8:27:eb:91:96:1b", ["mixer", "volume", "100"]]}' gate:9000/jsonrpc.js
curl -X POST -d '{ "method": "slim.request", "params":["b8:27:eb:91:96:1b", ["status"]]}' gate:9000/jsonrpc.js | json_pp
curl -X POST -d '{ "method": "slim.request", "params":["", ["players", "0", "100"]]}' gate:9000/jsonrpc.js | json_pp
curl -X POST -d '{ "method": "slim.request", "id":1, "params":[ "", ["albums", "0","100" ]]}' gate:9000/jsonrpc.js | json_pp
curl -X POST -d '{ "method": "slim.request", "id":1, "params":[ "", ["serverstatus", "0","100" ]]}' gate:9000/jsonrpc.js | json_pp
// query tracks with genre_id 2:
curl -X POST -d '{ "method": "slim.request", "id":4711, "params":[ 0, ["tracks", "0","100", "genre_id:2"]]}' gate:9000/jsonrpc.js | json_pp | less
-> https://lyrion.org/reference/cli/using-the-cli/#jsonrpcjs
-> https://forums.slimdevices.com/showthread.php?82985-JSON-help
*/
#pragma once
#include <curl/curl.h>
#include <vector>
#include <list>
#include <string>
#include "lib/tcpchannel.h"
#include "lib/curl.h"
#include "lib/json.h"
#include "lmcservice.h"
// #define LmcLock cMutexLock lock(&comMutex)
// #define LmcDoLock comMutex.Lock()
// #define LmcUnLock comMutex.Unlock()
#define LmcLock
#define LmcDoLock
#define LmcUnLock
//***************************************************************************
// LMC Communication
//***************************************************************************
class LmcCom : public TcpChannel
{
public:
enum RangeQueryType
{
rqtUnknown = na,
rqtGenres, // 0
rqtArtists, // 1
rqtAlbums, // 2
rqtNewMusic, // 3
rqtTracks, // 4
rqtYears, // 5
rqtPlaylists, // 6
rqtRadios, // 7
rqtRadioApps, // 8
rqtFavorites, // 9
rqtPlayers, // 10
rqtMusic, // 11
rqtCount
};
static const char* rangeQueryTypes [];
static const char* toName(LmcCom::RangeQueryType rqt);
struct ListItem
{
ListItem() {};
void clear() { id = ""; content = ""; command = ""; hasItems = false; isAudio = false, isConnected = false; }
int isEmpty() { return content == ""; }
std::string id;
int tracknum {na};
std::string content;
std::string command;
std::string commandSpecial;
std::string icon;
bool contextMenu {false};
bool hasItems {false};
bool isAudio {false};
bool isConnected {false};
};
typedef std::list<ListItem> RangeList;
typedef std::list<std::string> Parameters;
enum Misc
{
sizeMaxCommand = 100
};
enum Results
{
wrnNoEventPending = -1000,
ifoTrack,
ifoPlayer
};
LmcCom(const char* aMac = nullptr);
~LmcCom();
void setMac(const char* aMac)
{
free(mac);
mac = strdup(aMac);
free(escId);
escId = escape(mac);
}
int open(const char* host = "localhost", unsigned short port = 9090);
int restQuery(std::string what);
int restQuery(std::string what, Parameters pars);
int restQueryRange(std::string what, int from, int count, const char* special,
RangeList* list, int& total, std::string& title, Parameters* pars = nullptr);
json_t* getRestResult();
int queryPlayers(RangeList* list);
// cover
int getCurrentCover(MemoryStruct* cover, TrackInfo* track = nullptr, bool big = false);
int getCurrentCoverUrl(TrackInfo* track, std::string& coverUrl, bool big = false);
int getCover(MemoryStruct* cover, TrackInfo* track);
int getCoverUrl(TrackInfo* track, std::string& coverUrl);
// notification channel
int startNotify();
int stopNotify();
int checkNotify(uint64_t timeout = 0);
int execute(const char* command);
// const char* getLastQueryTitle() { return queryTitle ? queryTitle : ""; }
// infos
int update(int stateOnly = no);
TrackInfo* getCurrentTrack()
{
if (playerState.plIndex >= 0 && playerState.plIndex < (int)tracks.size())
return &tracks.at(playerState.plIndex);
return &dummyTrack;
}
bool hasMetadataChanged() { return metaDataChanged; }
PlayerState* getPlayerState() { return &playerState; }
int getTrackCount() { return tracks.size(); }
TrackInfo* getTrack(int idx) { return &tracks[idx]; }
char* unescape(char* buf); // url like encoding
char* escape(const char* buf);
int request(const char* command);
int response(char* response = nullptr, int max = 0);
private:
int parseLocalRadio(json_t* jObj, ListItem& item);
int parseArtists(json_t* jObj, ListItem& item);
int parseAlbums(json_t* jObj, ListItem& item);
int parseNewmusic(json_t* jObj, ListItem& item);
int parseTracks(json_t* jObj, ListItem& item);
int parseYears(json_t* jObj, ListItem& item);
int parsePlaylists(json_t* jObj, ListItem& item);
int parseRadios(json_t* jObj, ListItem& item);
int parseRadioapps(json_t* jObj, ListItem& item);
int parseFavorites(json_t* jObj, ListItem& item);
int parsePlayers(json_t* jObj, ListItem& item);
int parseMusic(json_t* jObj, ListItem& item);
int parseGenres(json_t* jObj, ListItem& item);
// void setQueryTitle(const char* title) { free(queryTitle); queryTitle = strdup(title); }
// char* queryTitle {};
char* host {};
unsigned short port {9090};
char* mac {};
char* escId {}; // escaped player id (build from mac)
cCurl* curl {};
char* curlUrl {};
char lastCommand[sizeMaxCommand+TB] {};
std::string lastPar;
TrackInfo dummyTrack;
TrackInfo currentTrack;
PlayerState playerState;
LmcCom* notify {};
std::vector<TrackInfo> tracks;
bool metaDataChanged {false};
std::string lastRestResult;
#ifdef VDR_PLUGIN
cMutex comMutex;
#endif
};