Commit e0225fa0 authored by Martin Cífka's avatar Martin Cífka
Browse files

Changed ISO and speed to enum, added json serialization when sending

parent 80f80918
Loading
Loading
Loading
Loading
+39 −87
Original line number Diff line number Diff line
@@ -10,6 +10,31 @@ using namespace std;

Commander::Commander(){}

string Commander::generateJSON(MSG msg)
{
    DynamicJsonDocument json(JSON_OBJECT_SIZE(2));
    json["msg_id"] = (int)msg;
    json["token"] = _token;
    
    string str;
    serializeJson(json, str);
    
    return str;
}

string Commander::generateJSON(MSG msg, int param)
{
    DynamicJsonDocument json(JSON_OBJECT_SIZE(3));
    json["msg_id"] = (int)MSG::setSpeed;
    json["param"] = (int)param;
    json["token"] = _token;
    
    string str;
    serializeJson(json, str);
    
    return str;
}

bool Commander::getJSON(DynamicJsonDocument& json, int expected_rval)
{
    // Get response
@@ -17,6 +42,8 @@ bool Commander::getJSON(DynamicJsonDocument& json, int expected_rval)
    if (!_sock.recv(response))
        return false;
    
    cout << response << endl;
    
    DeserializationError err = deserializeJson(json, response);
    
    if (err)
@@ -49,6 +76,7 @@ bool Commander::str2int (const char* s, int& i, int base)
    return true;
}


bool Commander::connect()
{
    return _sock.connect("127.0.0.1", 7878);
@@ -57,7 +85,8 @@ bool Commander::connect()

bool Commander::getToken()
{
    if (!_sock.send("{\"msg_id\":257,\"token\":0}"))
    
    if (!_sock.send(generateJSON(MSG::getToken))) 
        return false;
    
    // Get response
@@ -77,33 +106,9 @@ bool Commander::getToken()
}


bool Commander::setISO(const string& iso_str)
bool Commander::setISO(Commander::ISO iso)
{
    int iso;
    if (iso_str == "Auto")
        iso = 0;
    else
    {
        if (!str2int(iso_str.c_str(), iso))
            return false;
                    
        switch(iso)
        {
            //case 0: // Auto
            case 50:
            case 100:
            case 200:
            case 400:
            case 800:
            case 1600:
                break;

            default:
                return false;
                break;
        }
    }
    if (!_sock.send("{\"param\":\"" + to_string(iso) + "\",\"msg_id\":5172,\"token\":" + to_string(_token) + "}"))
    if (!_sock.send(generateJSON(MSG::setISO, (int)iso))) 
        return false;
    
    // Get response
@@ -115,67 +120,13 @@ bool Commander::setISO(const string& iso_str)
}


bool Commander::setSpeed(const string& speed_str)
bool Commander::setSpeed(Commander::Speed speed)
{    
    int speed;
    if (speed_str == "Auto")
        speed = 0;
    else if (speed_str.size() >= 2 && speed_str[0] == '1' && speed_str[1] == '/')
    {
        
        string denominator = speed_str.substr(2, speed_str.size()-2);
        if (!str2int(denominator.c_str(), speed))
            return false;
        
        switch (speed)
        {
            case 6400:
            case 3200:
            case 2000:
            case 1000:
            case 500:
            case 240:
            case 120:
            case 60:
            case 30:
            case 15:
            case 8:
            case 4:
                break;
            default:
                return false;
        }
        speed += 32768;
    }
    else
    {
        if (!str2int(speed_str.c_str(), speed))
            return false;
        
        switch (speed)
        {
            //case 0:
            case 1:
            case 2:
            case 4:
            case 8:
            case 12:
            case 16:
            case 20:
            case 24:
            case 28:
            case 32:
                break;
            default:
                return false;
        }
    }
    
    if (!_sock.send("{\"param\":\"" + to_string(speed) + "\",\"msg_id\":5171,\"token\":" + to_string(_token) + "}"))
    if (!_sock.send(generateJSON(MSG::setSpeed, (int)speed))) 
        return false;
    
    // Get response
    DynamicJsonDocument json(JSON_OBJECT_SIZE(2) + 20);
    DynamicJsonDocument json(JSON_OBJECT_SIZE(3) + 20);
    if (!getJSON(json))
        return false;
    
@@ -185,9 +136,10 @@ bool Commander::setSpeed(const string& speed_str)

bool Commander::shoot()
{
    if (!_sock.send("{\"msg_id\":4864,\"token\":" + to_string(_token) + "}"))
    if (!_sock.send(generateJSON(MSG::shoot)))
        return false;
    
    
    // await "capture was enqueued"
    DynamicJsonDocument json(JSON_OBJECT_SIZE(2) + 20);
    if (!getJSON(json))
@@ -199,7 +151,7 @@ bool Commander::shoot()
        return false;
    
    string key = "msg_id";
    if (!json.containsKey(key) || !json[key].is<int>() || json[key] != 8193)
    if (!json.containsKey(key) || !json[key].is<int>() || json[key] != (int)MSG::imgCaptured)
    {
            cerr << "Error: unexpected JSON response: \"" << key << "\" = " << json[key] << endl;
            return false;
+63 −2
Original line number Diff line number Diff line
@@ -11,20 +11,81 @@

class Commander {
public:
    
    enum class ISO
    {
        Auto = 0,
        I_50 = 50,
        I_100 = 100,
        I_200 = 2,
        I_400 = 400,
        ISO_800 = 800,
        ISO_1600 = 1600
    };
    
    enum class Speed
    {
        Auto = 0,
        
        S_1_6400 = 39168,
        S_1_3200 = 35968,
        S_1_2000 = 34768,
        S_1_1000 = 33768,
        S_1_500 = 33268,
        S_1_240 = 33008,
        S_1_120 = 32888,
        S_1_60 = 32828,
        S_1_30 = 32798,
        S_1_15 = 32783,
        S_1_8 = 32776,
        S_1_4 = 32772,
        
        S_1 = 1,
        S_2 = 2,
        S_4 = 4,
        S_8 = 8,
        S_12 = 12,
        S_16 = 16,
        S_20 = 20,
        S_24 = 24,
        S_28 = 28,
        S_32 = 32
    };
    
    
    Commander();
    bool connect();
    bool getToken();
    bool setISO(const std::string& iso);
    bool setSpeed(const std::string& speed);
    bool setISO(ISO iso);
    bool setSpeed(Speed speed);
    bool shoot();
    void close();
    
    
    
private:
    enum class MSG
    {
        getToken = 257,
        shoot = 4864,
        setSpeed = 5171,
        setISO = 5172,
        
        imgCaptured = 8193
    };
    
    TcpSocket _sock;
    int _token;

    bool getJSON(DynamicJsonDocument& json, int expected_rval = 0);
    static bool str2int (const char* s, int& i, int base = 0);
    
    std::string generateJSON(MSG msg);
    std::string generateJSON(MSG msg, int param);
    
    
    
    
};

#endif /* COMMANDER_H */
+15 −5
Original line number Diff line number Diff line
@@ -9,11 +9,21 @@ int main(int argc , char *argv[])
{
    Commander comm;
    
    comm.connect()         && cout << "===== Connected =====\n\n" &&
    comm.getToken()        && cout << "====== Token OK =====\n\n" &&
    comm.setISO("50")      && cout << "====== ISO set ======\n\n" &&
    comm.setSpeed("1/120") && cout << "===== Speed set =====\n\n" &&
    comm.shoot()           && cout << "=== Picture taken ===\n\n";
    comm.connect()
            && cout << "===== Connected =====\n\n" &&
    comm.getToken()
            && cout << "====== Token OK =====\n\n" &&
    comm.setISO(Commander::ISO::I_50)
            && cout << "====== ISO set ======\n\n" &&
            
    comm.setSpeed(Commander::Speed::S_1_240)
            && cout << "===== Speed set =====\n\n" &&
            
    comm.setSpeed(Commander::Speed::S_1_500)
            && cout << "===== Speed set =====\n\n" &&
            
    comm.shoot()
            && cout << "=== Picture taken ===\n\n";
                          
    comm.close();
    //cout << "=== Socket closed ===\n";
+0 −1
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ bool TcpSocket::send(const string& msg)
		cerr << "send failed\n";
		return false;
	}
	cout << "Sent: " << msg << endl;
        return true;
}