-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlmc.c
307 lines (233 loc) · 8.65 KB
/
lmc.c
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
//***************************************************************************
// Automation Control
// File lmc.c
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 2010-2023 Jörg Wendel
//***************************************************************************
#include "lib/json.h"
#include "daemon.h"
//***************************************************************************
// Init / Exit
//***************************************************************************
int Daemon::lmcInit()
{
if (isEmpty(lmcHost) || isEmpty(lmcPlayerMac))
return done;
static time_t lastTryAt {0};
if (lastTryAt > time(0) - 10)
return done;
lastTryAt = time(0);
if (!lmc)
lmc = new LmcCom(lmcPlayerMac);
if (lmc->open(lmcHost, lmcPort) != success)
{
tell(eloAlways, "[LMC] Opening connection to server at '%s:%d' failed", lmcHost, lmcPort);
return fail;
}
tell(eloAlways, "[LMC] Opening connection to server at '%s:%d' succeeded", lmcHost, lmcPort);
lmc->update();
// lmc->startNotify();
lmcUpdates();
return success;
}
int Daemon::lmcExit()
{
if (lmc)
{
lmc->stopNotify();
lmc->close();
}
delete lmc;
lmc = nullptr;
return success;
}
//***************************************************************************
// Perform LMC Updates
//***************************************************************************
int Daemon::performLmcUpdates()
{
if (!lmc)
return done;
if (!lmc->isOpen())
{
if (lmcInit() != success)
return fail;
}
if (lmc->checkNotify(0) == success)
{
tell(eloAlways, "[LMC] Changes pending");
lmcUpdates();
}
return success;
}
int Daemon::lmcUpdates(long client)
{
if (!lmc)
return done;
json_t* oJson = json_object();
lmcTrack2Json(oJson, lmc->getCurrentTrack());
lmcPlayerState2Json(oJson);
lmcPlaylist2Json(oJson);
lmcMainMenu2Json(oJson);
return pushOutMessage(oJson, "lmcdata", client);
}
//***************************************************************************
// LMC Track To Json
//***************************************************************************
int Daemon::lmcTrack2Json(json_t* obj, TrackInfo* track)
{
tell(eloDebug, "[LMC] Track: %s / %s / %s / %s ", track->title, track->artist, track->genre, track->album);
// current track
json_t* current = json_object();
json_object_set_new(obj, "current", current);
json_object_set_new(current, "title", json_string(track->title));
json_object_set_new(current, "artist", json_string(track->artist));
json_object_set_new(current, "genre", json_string(track->genre));
json_object_set_new(current, "album", json_string(track->album));
json_object_set_new(current, "duration", json_integer(track->duration));
json_object_set_new(current, "bitrate", json_integer(track->bitrate));
json_object_set_new(current, "contentType", json_string(track->contentType));
json_object_set_new(current, "year", json_integer(track->year));
json_object_set_new(current, "lyrics", json_string(track->lyrics));
// json_object_set_new(current, "id", json_integer(track->id));
std::string url;
lmc->getCurrentCoverUrl(track, url);
tell(eloAlways, "[LMC] Artworkurl: %s", track->artworkurl);
json_object_set_new(current, "cover", json_string(url.c_str()));
return success;
}
int Daemon::lmcPlayerState2Json(json_t* obj)
{
// player state
json_t* state = json_object();
json_object_set_new(obj, "state", state);
PlayerState* currentState = lmc->getPlayerState();
// json_object_set_new(state, "updatedAtMs", json_integer(currentState->updatedAt));
json_object_set_new(state, "plIndex", json_integer(currentState->plIndex));
json_object_set_new(state, "plName", json_string(currentState->plName));
json_object_set_new(state, "plCount", json_integer(currentState->plCount));
json_object_set_new(state, "plShuffle", json_integer(currentState->plShuffle));
json_object_set_new(state, "plRepeat", json_integer(currentState->plRepeat));
json_object_set_new(state, "muted", json_boolean(currentState->muted));
json_object_set_new(state, "mode", json_string(currentState->mode));
json_object_set_new(state, "trackTime", json_integer(currentState->trackTime));
json_object_set_new(state, "volume", json_integer(currentState->volume / (100 / 100.0)));
static int lastTime {0};
tell(eloDebug, "[LMC] Mmode: %s", currentState->mode);
if (strcmp(currentState->mode, "play") == 0)
lastTime = currentState->trackTime + (cTimeMs::Now() - currentState->updatedAt) / 1000;
json_object_set_new(state, "trackTime", json_integer(lastTime));
return success;
}
int Daemon::lmcPlaylist2Json(json_t* obj)
{
// playlist
json_t* oPlaylist = json_array();
json_object_set_new(obj, "playlist", oPlaylist);
for (int i = 0; i < lmc->getTrackCount(); i++)
{
TrackInfo* track = lmc->getTrack(i);
json_t* oTrack = json_object();
json_array_append_new(oPlaylist, oTrack);
json_object_set_new(oTrack, "title", json_string(track->title));
json_object_set_new(oTrack, "artist", json_string(track->artist));
json_object_set_new(oTrack, "album", json_string(track->album));
std::string url;
if (lmc->getCoverUrl(track, url) == success)
json_object_set_new(oTrack, "cover", json_string(url.c_str()));
}
return success;
}
struct MenuItem
{
const char* name;
LmcCom::RangeQueryType queryType;
};
MenuItem menuItems[] =
{
{ "Artists", LmcCom::rqtArtists },
{ "Albums", LmcCom::rqtAlbums },
{ "Genres", LmcCom::rqtGenres },
{ "Years", LmcCom::rqtYears },
{ "Play random tracks", LmcCom::rqtUnknown },
{ "Playlists", LmcCom::rqtPlaylists },
{ "Radios", LmcCom::rqtRadios },
{ "Favorites", LmcCom::rqtFavorites },
{ "Favorites", LmcCom::rqtFavorites },
// { "", LmcCom::rqtUnknown }
};
int Daemon::lmcMainMenu2Json(json_t* obj)
{
json_t* oMenu = json_array();
json_object_set_new(obj, "menu", oMenu);
for (const auto& item : menuItems)
{
json_t* oItem = json_object();
json_array_append_new(oMenu, oItem);
json_object_set_new(oItem, "name", json_string(item.name));
json_object_set_new(oItem, "id", json_integer(item.queryType));
}
return success;
}
//***************************************************************************
// LMC Action
//***************************************************************************
int Daemon::performLmcAction(json_t* oObject, long client)
{
if (!lmc)
return done;
std::string action = getStringFromJson(oObject, "action", "");
int index = getIntFromJson(oObject, "index", na);
LmcCom::RangeQueryType queryType = (LmcCom::RangeQueryType)getIntFromJson(oObject, "query", na);
tell(eloAlways, "[LMC] Action '%s' (%d)", action.c_str(), queryType);
if (action == "menu" && queryType != LmcCom::rqtUnknown)
{
static int maxElements {50000};
LmcCom::Parameters filters;
LmcCom::RangeList list;
int total {0};
if (queryType == LmcCom::rqtNewMusic)
filters.push_back("sort:new");
json_t* oMenu = json_array();
tell(eloAlways, "[LMC] Query (%d)", queryType);
if (lmc->queryRange(queryType, 0, maxElements, &list, total, "", &filters) == success)
{
LmcCom::RangeList::iterator it;
for (it = list.begin(); it != list.end(); ++it)
{
json_t* oItem = json_object();
json_array_append_new(oMenu, oItem);
json_object_set_new(oItem, "name", json_string((*it).content.c_str()));
json_object_set_new(oItem, "id", json_string((*it).id.c_str()));
}
pushOutMessage(oMenu, "lmcmenu", client);
if (total > maxElements)
tell(eloAlways, "[LMC] Warning: %d more, only maxElements supported", total-maxElements);
}
}
else if (action == "play" && index != na)
lmc->track(index);
else if (action == "play")
lmc->play();
else if (action == "pause")
lmc->pause();
else if (action == "stop")
lmc->stop();
else if (action == "prevTrack")
lmc->prevTrack();
else if (action == "nextTrack")
lmc->nextTrack();
else if (action == "repeat")
lmc->repeat();
else if (action == "shuffle")
lmc->randomTracks();
// lmc->shuffle(); // lmc->randomTracks();
else if (action == "volumeDown")
lmc->volumeDown();
else if (action == "volumeUp")
lmc->volumeUp();
else if (action == "muteToggle")
lmc->muteToggle();
return done;
}