-
Notifications
You must be signed in to change notification settings - Fork 22
/
lmccom.h
270 lines (209 loc) · 8 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
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
/*
* 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:
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
-> 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 "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,
rqtArtists,
rqtAlbums,
rqtNewMusic,
rqtTracks,
rqtYears,
rqtPlaylists,
rqtRadios,
rqtRadioApps,
rqtFavorites,
rqtPlayers
};
struct ListItem
{
ListItem() { }
void clear() { id = ""; content = ""; command = ""; hasItems = false; isAudio = false, isConnected = false; }
int isEmpty() { return content == ""; }
std::string id;
std::string content;
std::string command;
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 execute(const char* command, Parameters* pars = nullptr);
int execute(const char* command, int par);
int execute(const char* command, const char* par);
int query(const char* command, char* response, int max);
int queryInt(const char* command, int& value);
int queryRange(RangeQueryType queryType, int from, int count,
RangeList* list, int& total, const char* special = "", Parameters* pars = nullptr);
// cover
int getCurrentCover(MemoryStruct* cover, TrackInfo* track = nullptr);
int getCurrentCoverUrl(TrackInfo* track, std::string& coverUrl);
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);
// player steering
int play() { return execute("play"); }
int pause() { return execute("pause", "1"); }
int pausePlay() { return execute("pause"); } // toggle pause/play
int stop() { return execute("stop"); }
int volumeUp() { return execute("mixer volume", "+5"); }
int volumeDown() { return execute("mixer volume", "-5"); }
int mute() { return execute("mixer muting", "1"); }
int unmute() { return execute("mixer muting", "0"); }
int muteToggle() { return execute("mixer muting toggle"); }
int clear() { return execute("playlist clear"); }
int save() { return execute("playlist save", escId); }
int resume() { return execute("playlist resume", escId); }
int randomTracks() { return execute("randomplay tracks"); }
int shuffle() { return execute("playlist shuffle"); }
int repeat() { return execute("playlist repeat"); }
int scroll(short step)
{
char par[50] {};
sprintf(par, "%c%d", step < 0 ? '-' : '+', (int)abs(step));
return execute("time", par);
}
const char* getLastQueryTitle() { return queryTitle ? queryTitle : ""; }
int nextTrack() { return execute("playlist index", "+1"); }
int prevTrack() { return execute("playlist index", "-1"); }
int track(unsigned short index)
{
char par[50] {};
sprintf(par, "%d", index);
return execute("playlist index", par);
}
int loadAlbum(const char* genre = "*", const char* artist = "*", const char* album = "*")
{
return ctrlAlbum("loadalbum", genre, artist, album);
}
int appendAlbum(const char* genre = "*", const char* artist = "*", const char* album = "*")
{
return ctrlAlbum("addalbum", genre, artist, album);
}
int ctrlAlbum(const char* command, const char* genre = "*", const char* artist = "*", const char* album = "*")
{
int status;
char *a {}, *g {}, *l {};
char* cmd {};
asprintf(&cmd, "playlist %s %s %s %s", command,
*genre == '*' ? genre : g = escape(genre),
*artist == '*' ? artist : a = escape(artist),
*album == '*' ? album : l = escape(album));
status = execute(cmd);
free(cmd); free(a); free(g); free(l);
return status;
}
int loadPlaylist(const char* playlist)
{
char par[500] {};
Parameters pars;
pars.push_back("cmd:load");
sprintf(par, "playlist_name:%s", playlist);
pars.push_back(par);
return execute("playlistcontrol", &pars);
}
int appendPlaylist(const char* playlist)
{
char par[500] {};
Parameters pars;
pars.push_back("cmd:add");
sprintf(par, "playlist_name:%s", playlist);
pars.push_back(par);
return execute("playlistcontrol", &pars);
}
// 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, Parameters* par = nullptr);
int request(const char* command, const char* par);
int responseP(char*& result);
int response(char* response = nullptr, int max = 0);
private:
void setQueryTitle(const char* title) { free(queryTitle); queryTitle = strdup(title); }
// data
char* host {};
unsigned short port {9090};
char* mac {};
char* escId {}; // escaped player id (build from mac)
CURL* curl {};
char lastCommand[sizeMaxCommand+TB] {};
std::string lastPar;
TrackInfo dummyTrack;
TrackInfo currentTrack;
PlayerState playerState;
LmcCom* notify {};
std::vector<TrackInfo> tracks;
char* queryTitle {};
bool metaDataChanged {false};
#ifdef VDR_PLUGIN
cMutex comMutex;
#endif
};