filmout_display/src/state.cpp

92 lines
2.1 KiB
C++

#include "state.hpp"
State::State () {
//
}
void State::error () {
ERROR = true;
image.clear();
}
bool State::imageExists (string& image) {
struct stat buffer;
return (stat (image.c_str(), &buffer) == 0);
}
void State::setAction (json& msgData) {
action = static_cast<Action>(msgData["action"]);
cout << "Action = " << action << endl;
}
void State::setImage (json& msgData) {
image = msgData["image"];
if (!imageExists(image)) {
error();
cerr << "Image " << msgData["image"] << " does not exist" << endl;
return;
}
cout << "Image = " << image << endl;
}
void State::setMode (json& msgData) {
mode = static_cast<Mode>(msgData["mode"]);
cout << "Mode = " << mode << endl;
}
void State::setPosition (json& msgData) {
x = msgData["position"]["x"];
y = msgData["position"]["y"];
width = msgData["position"]["w"];
height = msgData["position"]["h"];
cout << "Position[x] = " << x << " [y] = " << y << " [width] = " << width << " [height] = " << height << endl;
}
void State::setExposure (json& msgData) {
if (msgData["exposure"].size() == 1) {
exposure = { msgData["exposure"][0] };
} else if (msgData["exposure"].size() == 3) {
exposure = { msgData["exposure"][0], msgData["exposure"][1], msgData["exposure"][2] };
} else {
error();
cerr << "Exposure array is incorrect length, " << msgData["exposure"].size() << endl;
return;
}
}
void State::receiveMessage (string msgString) {
ERROR = false;
json msgData = json::parse(msgString);
if (msgData.contains("action")) {
setAction(msgData);
} else {
action = NONE;
error();
cerr << "Received invalid message: " << msgString << endl;
return;
}
if (msgData.contains("image")) {
setImage(msgData);
}
if (action == LOAD && msgData.contains("mode")) {
setMode(msgData);
}
if (action == LOAD && msgData.contains("position")) {
setPosition(msgData);
}
if (action == DISPLAY && msgData.contains("exposure")) {
setExposure(msgData);
}
}
string State::createMessage (bool success) {
json msgData = {
{ "action", action },
{ "success", success }
};
return msgData.dump();
}