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

Changed deserializeJson to overload with CustomReader

parent 616fc076
Loading
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -7,8 +7,8 @@

using namespace std;


Commander::Commander(){}
Commander::Commander() :
    _token(0), _sock(TcpSocket()), _cr(CustomReader(&_sock)) {};

string Commander::generateJSON(MSG msg)
{
@@ -39,10 +39,8 @@ bool Commander::getJSON(DynamicJsonDocument& json, int expected_rval)
{
    // Get response
    string response;
    if (!_sock.recv(response))
        return false;
    
    DeserializationError err = deserializeJson(json, response);
    DeserializationError err = deserializeJson(json, _cr);
    
    if (err)
    {
+8 −4
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include <ArduinoJson.h>

#include "tcpSocket.h"
#include "customReader.h"


class Commander {
@@ -74,8 +75,10 @@ private:
        imgCaptured = 8193
    };
    
    TcpSocket _sock;
    int _token;
    TcpSocket _sock;
    CustomReader _cr;
    

    bool getJSON(DynamicJsonDocument& json, int expected_rval = 0);
    static bool str2int (const char* s, int& i, int base = 0);
@@ -83,10 +86,11 @@ private:
    std::string generateJSON(MSG msg);
    std::string generateJSON(MSG msg, int param);
    
};




};

#endif /* COMMANDER_H */

+52 −0
Original line number Diff line number Diff line
#include "customReader.h"
#include <string>

using namespace std;

CustomReader::CustomReader(TcpSocket* sock) : _sock(sock) {};

// Reads one byte, or returns -1
int CustomReader::read()
{
    if (q.empty())
    {
        string str;
        if (!_sock->recv(str))
            return -1;

        for (size_t i = 0; i < str.size(); ++i)
            q.push(str[i]);
    }

    if (q.empty())
        return -1;

    char c = q.front();
    q.pop();
    return c;
}
    
// Reads several bytes, returns the number of bytes read.
size_t CustomReader::readBytes(char* buffer, size_t length)
{
    size_t read = 0;
    while (q.size() < length)
    {
        std::string str;
        if (!_sock->recv(str))
            break;

        for (size_t i = 0; i < str.size(); ++i)
            q.push(str[i]);

        read += str.size();
    }

    for (size_t i = 0; i < std::min(read, length); ++i)
    {
        buffer[i] = q.front();
        q.pop();
    }

    return read;
};
 No newline at end of file

sdk/src/customReader.h

0 → 100644
+21 −0
Original line number Diff line number Diff line
#ifndef CUSTOM_READER_H
#define CUSTOM_READER_H

#include <queue>
#include "tcpSocket.h"

struct CustomReader
{    
    CustomReader(TcpSocket* sock);

    // Reads one byte, or returns -1
    int read();
    
    // Reads several bytes, returns the number of bytes read.
    size_t readBytes(char* buffer, size_t length);
    
    std::queue<char> q;
    TcpSocket* _sock;
};

#endif /* CUSTOM_READER_H */
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
#include <iostream>
#include <string>

#include "commander.h"

using namespace std;