Feat: finished mpd idle usage

This commit is contained in:
jcorporation 2018-08-15 15:06:45 +02:00
parent f69ec1820c
commit f0b577378e
4 changed files with 32 additions and 46 deletions

View File

@ -646,6 +646,7 @@ function webSocketConnect() {
showNotification('Connected to myMPD', '', '', 'success'); showNotification('Connected to myMPD', '', '', 'success');
modalConnectionError.hide(); modalConnectionError.hide();
appRoute(); appRoute();
sendAPI({"cmd":"MPD_API_GET_STATE"}, parseState);
} }
socket.onmessage = function got_packet(msg) { socket.onmessage = function got_packet(msg) {

View File

@ -37,9 +37,6 @@
#include "config.h" #include "config.h"
#include "../dist/src/frozen/frozen.h" #include "../dist/src/frozen/frozen.h"
/* forward declaration */
void mympd_notify(struct mg_mgr *s);
const char * mpd_cmd_strs[] = { const char * mpd_cmd_strs[] = {
MPD_CMDS(GEN_STR) MPD_CMDS(GEN_STR)
}; };
@ -462,7 +459,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
break; break;
case MPD_API_GET_STATS: case MPD_API_GET_STATS:
n = mympd_put_stats(mpd.buf); n = mympd_put_stats(mpd.buf);
break; break;
} }
if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
@ -497,11 +494,15 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
free(cmd); free(cmd);
} }
int mympd_close_handler(struct mg_connection *c) { void mympd_notify(struct mg_mgr *s) {
/* Cleanup session data */ for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
if (c->user_data) if (!is_websocket(c))
free(c->user_data); continue;
return 0; mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
}
#ifdef DEBUG
fprintf(stderr,"NOTIFY: %s\n", mpd.buf);
#endif
} }
void mympd_poll(struct mg_mgr *s, int timeout) { void mympd_poll(struct mg_mgr *s, int timeout) {
@ -547,7 +548,6 @@ void mympd_poll(struct mg_mgr *s, int timeout) {
fprintf(stderr, "MPD connection failed.\n"); fprintf(stderr, "MPD connection failed.\n");
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}"); snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}");
mympd_notify(s); mympd_notify(s);
case MPD_DISCONNECT: case MPD_DISCONNECT:
case MPD_RECONNECT: case MPD_RECONNECT:
if (mpd.conn != NULL) if (mpd.conn != NULL)
@ -569,21 +569,8 @@ void mympd_poll(struct mg_mgr *s, int timeout) {
} }
} }
void mympd_notify(struct mg_mgr *s) {
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
if (!is_websocket(c))
continue;
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
}
#ifdef DEBUG
fprintf(stderr,"NOTIFY: %s\n", mpd.buf);
#endif
}
void mympd_parse_idle(struct mg_mgr *s) { void mympd_parse_idle(struct mg_mgr *s) {
// mpd_connection_set_timeout(mpd.conn, 60);
enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false); enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false);
// mpd_connection_set_timeout(mpd.conn, mpd.timeout);
int len; int len;
for (unsigned j = 0;; ++j) { for (unsigned j = 0;; ++j) {
@ -659,13 +646,13 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsig
mpd.conn_state = MPD_FAILURE; mpd.conn_state = MPD_FAILURE;
return 0; return 0;
} }
if (status) { else {
audioformat = mpd_status_get_audio_format(status); audioformat = mpd_status_get_audio_format(status);
} }
len = json_printf(&out,"{type: update_state, data:{" len = json_printf(&out,"{type: update_state, data: {"
"state:%d, volume:%d, songPos: %d, elapsedTime: %d, " "state:%d, volume:%d, songPos: %d, elapsedTime: %d, "
"totalTime:%d, currentSongId: %d, kbitrate: %d, " "totalTime: %d, currentSongId: %d, kbitrate: %d, "
"audioFormat: { sampleRate: %d, bits: %d, channels: %d}, " "audioFormat: { sampleRate: %d, bits: %d, channels: %d}, "
"queueLength: %d, nextSongPos: %d, nextSongId: %d, " "queueLength: %d, nextSongPos: %d, nextSongId: %d, "
"queueVersion: %d", "queueVersion: %d",
@ -742,8 +729,8 @@ int mympd_put_settings(char *buffer) {
} }
len = json_printf(&out, len = json_printf(&out,
"{type:settings, data:{" "{type: settings, data: {"
"repeat:%d, single:%d, crossfade:%d, consume:%d, random:%d, " "repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, "
"mixrampdb: %f, mixrampdelay: %f, mpdhost : %Q, mpdport: %d, passwort_set: %B, " "mixrampdb: %f, mixrampdelay: %f, mpdhost : %Q, mpdport: %d, passwort_set: %B, "
"streamport: %d, coverimage: %Q, max_elements_per_page: %d, replaygain: %Q," "streamport: %d, coverimage: %Q, max_elements_per_page: %d, replaygain: %Q,"
"notificationWeb: %d, notificationPage: %d" "notificationWeb: %d, notificationPage: %d"

View File

@ -122,30 +122,30 @@ struct t_mpd {
} mpd; } mpd;
typedef struct { typedef struct {
int mpdport; int mpdport;
const char* mpdhost; const char* mpdhost;
const char* mpdpass; const char* mpdpass;
const char* webport; const char* webport;
bool ssl; bool ssl;
const char* sslport; const char* sslport;
const char* sslcert; const char* sslcert;
const char* sslkey; const char* sslkey;
const char* user; const char* user;
int streamport; int streamport;
const char* coverimage; const char* coverimage;
const char* statefile; const char* statefile;
} configuration; } configuration;
configuration config; configuration config;
static int is_websocket(const struct mg_connection *nc) { static int is_websocket(const struct mg_connection *nc) {
return nc->flags & MG_F_IS_WEBSOCKET; return nc->flags & MG_F_IS_WEBSOCKET;
} }
void mympd_poll(struct mg_mgr *sm, int timeout); void mympd_poll(struct mg_mgr *sm, int timeout);
void mympd_parse_idle(struct mg_mgr *s); void mympd_parse_idle(struct mg_mgr *s);
void callback_mympd(struct mg_connection *nc, const struct mg_str msg); void callback_mympd(struct mg_connection *nc, const struct mg_str msg);
int mympd_close_handler(struct mg_connection *c); void mympd_notify(struct mg_mgr *s);
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version);
int mympd_put_outputs(char *buffer); int mympd_put_outputs(char *buffer);
int mympd_put_current_song(char *buffer); int mympd_put_current_song(char *buffer);
@ -166,4 +166,3 @@ int mympd_put_songdetails(char *buffer, char *uri);
int mympd_queue_crop(char *buffer); int mympd_queue_crop(char *buffer);
void mympd_disconnect(); void mympd_disconnect();
#endif #endif

View File

@ -84,7 +84,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
#ifdef DEBUG #ifdef DEBUG
printf("Websocket connection closed\n"); printf("Websocket connection closed\n");
#endif #endif
mympd_close_handler(nc); // mympd_close_handler(nc);
} }
else { else {
#ifdef DEBUG #ifdef DEBUG
@ -255,10 +255,9 @@ int main(int argc, char **argv) {
if (config.ssl == true) if (config.ssl == true)
printf("myMPD started on ssl port %s\n", config.sslport); printf("myMPD started on ssl port %s\n", config.sslport);
//Main loop for http handling
while (s_signal_received == 0) { while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 100); mg_mgr_poll(&mgr, 100);
mympd_poll(&mgr, 1); mympd_poll(&mgr, 0);
} }
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
mympd_disconnect(); mympd_disconnect();