Skip to content

Commit

Permalink
JSON logic error handling in player status update. Printing LMS statu…
Browse files Browse the repository at this point in the history
…s response in verbose mode. Fixes #13
  • Loading branch information
Terényi, Balázs committed Oct 14, 2016
1 parent 1bc205a commit 7397166
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
6 changes: 3 additions & 3 deletions Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Controller::run()
}
catch (const jsonrpc::JsonRpcException& e)
{
cerr << "Startup volume can not be set: " << e.what() << endl;
cerr << "[ERROR] Startup volume can not be set: " << e.what() << endl;
}
ev::default_loop loop;
loop.run(0);
Expand Down Expand Up @@ -315,7 +315,7 @@ void Controller::handleEvent(const Event event)
}
catch (const jsonrpc::JsonRpcException& e)
{
cerr << e.what() << endl;
cerr << "[ERROR] " << e.what() << endl;
}
}

Expand Down Expand Up @@ -357,7 +357,7 @@ void Controller::readInput(ev::io& w, int revents)
}
if (readSize == -1 && errno != EAGAIN && errno != EINTR)
{
cerr << "Input device read error" << endl;
cerr << "[ERROR] Input device read error" << endl;
}
}

Expand Down
81 changes: 46 additions & 35 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,52 @@ void Player::update(const bool fullPlaylist)
{
Json::Value response = mServer.playerStatus(mId, fullPlaylist);

string s = response["mode"].asString();
if (s == "play")
mMode = Play;
else if (s == "stop")
mMode = Stop;
else if (s == "pause")
mMode = Pause;
else
mMode = Unknown;

mPowered = response["power"].asInt() == 1;

mPlaylistSize = response["playlist_tracks"].asInt();
s = response["playlist_cur_index"].asString();
if (s.length() > 0)
mPlaylistIndex = stoi(s);
else
mPlaylistIndex = 0;

int loopIndex = fullPlaylist ? mPlaylistIndex : 0;

mRemote = response["playlist_loop"][loopIndex]["remote"].asString() == "1";
mRemoteTitle = response["playlist_loop"][loopIndex]["remote_title"].asString();
mArtist = response["playlist_loop"][loopIndex]["artist"].asString();
mAlbum = response["playlist_loop"][loopIndex]["album"].asString();
mTitle = response["playlist_loop"][loopIndex]["title"].asString();
mDuration = response["duration"].asFloat();
mTime = response["time"].asFloat();

mVolume = response["mixer volume"].asInt();
mRepeat = response["playlist repeat"].asInt();
mShuffle = response["playlist shuffle"].asInt();

if (fullPlaylist)
mPlaylist = mPlaylistSize == 0 ? Json::arrayValue : response["playlist_loop"];
if (Config::verbose())
cout << "Status: " << response.toStyledString() << endl;

try
{
string s = response["mode"].asString();
if (s == "play")
mMode = Play;
else if (s == "stop")
mMode = Stop;
else if (s == "pause")
mMode = Pause;
else
mMode = Unknown;

mPowered = response["power"].asInt() == 1;

mPlaylistSize = response["playlist_tracks"].asInt();
s = response["playlist_cur_index"].asString();
if (s.length() > 0)
mPlaylistIndex = stoi(s);
else
mPlaylistIndex = 0;

int loopIndex = fullPlaylist ? mPlaylistIndex : 0;

mRemote = response["playlist_loop"][loopIndex]["remote"].asString() == "1";
mRemoteTitle = response["playlist_loop"][loopIndex]["remote_title"].asString();
mArtist = response["playlist_loop"][loopIndex]["artist"].asString();
mAlbum = response["playlist_loop"][loopIndex]["album"].asString();
mTitle = response["playlist_loop"][loopIndex]["title"].asString();
mDuration = response["duration"].asFloat();
mTime = response["time"].asFloat();

mVolume = response["mixer volume"].asInt();
mRepeat = response["playlist repeat"].asInt();
mShuffle = response["playlist shuffle"].asInt();

if (fullPlaylist)
mPlaylist = mPlaylistSize == 0 ? Json::arrayValue : response["playlist_loop"];
}
catch (const Json::LogicError& e)
{
cerr << "[ERROR] Status: " << response.toStyledString() << endl;
cerr << "[ERROR] Invalid JSON response from server: " << e.what() << endl;
}
}

void Player::setVolume(const int volume)
Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char* argv[])
}
catch (const TCLAP::ArgException& e)
{
cerr << "Error: " << e.error() << " " << e.argId() << endl;
cerr << "[ERROR] " << e.error() << " " << e.argId() << endl;
return EXIT_FAILURE;
}

Expand All @@ -57,7 +57,7 @@ int main(int argc, char* argv[])
}
catch (const lcdapi::LCDException& e)
{
cerr << "LCDd communication error: " << e.what() << endl;
cerr << "[ERROR] LCDd communication problem: " << e.what() << endl;
}

// Sleeping for some time and retrying initialization
Expand Down

0 comments on commit 7397166

Please sign in to comment.