20bool findJsonValue(
const std::string &
json,
const std::string &key, std::size_t &valueStart)
22 const std::string token =
"\"" + key +
"\"";
23 const auto keyStart =
json.
find(token);
24 if (keyStart == std::string::npos)
27 const auto colon =
json.
find(
':', keyStart + token.size());
28 if (colon == std::string::npos)
31 valueStart =
json.find_first_not_of(
" \t\r\n", colon + 1);
32 return valueStart != std::string::npos;
42 const auto positionMarker = response.find(
"P ");
43 const auto movingMarker = response.find(
";M ");
44 if (positionMarker == std::string::npos || movingMarker == std::string::npos || movingMarker <= positionMarker + 2)
47 const std::string positionText = response.substr(positionMarker + 2, movingMarker - (positionMarker + 2));
50 const long long parsedPosition = std::strtoll(positionText.c_str(), &end, 10);
51 if (
errno != 0 || end == positionText.c_str() || *end !=
'\0' || parsedPosition < 0)
54 const auto movingStart = movingMarker + 3;
55 bool parsedMoving =
false;
56 if (response.compare(movingStart, 4,
"true") == 0)
58 else if (response.compare(movingStart, 5,
"false") != 0)
62 status.
moving = parsedMoving;
68 const auto marker = response.find(
"V ");
69 if (marker == std::string::npos)
72 const char *start = response.c_str() + marker + 2;
75 const long parsed = std::strtol(start, &end, 10);
76 if (
errno != 0 || end == start || parsed <= 0)
79 version =
static_cast<int>(parsed);
85 std::size_t start = 0;
86 if (!findJsonValue(
json, key, start))
91 const long long parsed = std::strtoll(
json.c_str() + start, &end, 10);
92 if (
errno != 0 || end ==
json.c_str() + start)
101 std::size_t start = 0;
102 if (!findJsonValue(
json, key, start))
107 const double parsed = std::strtod(
json.c_str() + start, &end);
108 if (
errno != 0 || end ==
json.c_str() + start)
117 std::size_t start = 0;
118 if (!findJsonValue(
json, key, start))
121 if (
json.compare(start, 4,
"true") == 0)
127 if (
json.compare(start, 5,
"false") == 0)
138 return response.find(
"ERR:") != std::string::npos;
143 return response.find(
"EFucoser") != std::string::npos;
148 if (version >= 1200 && version < 1300)
149 return "Arduino Nano ULN2003";
150 if (version >= 1100 && version < 1200)
151 return "ESP8266 ULN2003";
152 if (version >= 1000 && version < 1100)
153 return "ESP8266 STEP/DIR";
159 return (version >= 1201 && version < 1300) ||
160 (version >= 1103 && version < 1200) ||
161 (version >= 1005 && version < 1100);
a class to store JSON values
iterator find(const typename object_t::key_type &key)
find an element in a JSON object
bool parseJsonBoolean(const std::string &json, const std::string &key, bool &value)
const char * modelForVersion(int version)
bool isSupportedIdentity(const std::string &response)
bool isErrorResponse(const std::string &response)
bool isSupportedVersion(int version)
bool parseJsonNumber(const std::string &json, const std::string &key, double &value)
bool parseMotionStatus(const std::string &response, MotionStatus &status)
bool parseVersion(const std::string &response, int &version)
bool parseJsonInteger(const std::string &json, const std::string &key, int64_t &value)