#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(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(msgData["mode"]); cout << "Mode = " << mode << endl; } void State::setPosition (json& msgData) { x = msgData["position"]["x"]; y = msgData["position"]["y"]; w = msgData["position"]["w"]; h = msgData["position"]["h"]; cout << "Position[x] = " << x << " [y] = " << y << " [w] = " << w << " [h] = " << h << 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 (action == LOAD && 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(); }