4 #include <unordered_map> 58 auto res = std::find_if(_server_versions_vec.begin(), _server_versions_vec.end(),
59 [&, device_name, up_str, comp_str, platform](std::unordered_map<std::string, std::string> ver)
61 return (is_device_name_equal(ver[
"device_name"],device_name,
true) && up_str == ver[
"policy_type"] && comp_str == ver[
"component"] && (platform == ver[
"platform"] || ver[
"platform"] ==
"*"));
64 if (
res != _server_versions_vec.end())
66 auto version_str = (*res)[
"version"];
67 out_version =
version(version_str);
78 if (!_server_versions_loaded)
return false;
84 if (component_str.empty())
return false;
87 auto res = std::find_if(_server_versions_vec.begin(), _server_versions_vec.end(),
88 [version, component_str, platform](std::unordered_map<std::string, std::string> version_map)
90 return (
sw_update::version(version_map[
"version"]) == version && component_str == version_map[
"component"] && (platform == version_map[
"platform"] || version_map[
"platform"] ==
"*"));
93 if (
res != _server_versions_vec.end())
95 out = (*res)[req_field];
108 case VIEWER:
return "VIEWER";
113 LOG_ERROR(
"Unknown component type: " + component);
129 LOG_ERROR(
"Unknown policy type: " + policy);
137 static std::unordered_map<std::string, component_part_type>
map =
143 auto val = map.find(component_str);
144 if (
val != map.end())
146 component_val =
val->second;
150 LOG_ERROR(
"Unknown component type: " + component_str);
155 static std::unordered_map<std::string, update_policy_type>
map =
160 auto val = map.find(policy_str);
161 if (
val != map.end())
163 policy_val =
val->second;
167 LOG_ERROR(
"Unknown policy type: " + policy_str);
173 bool server_data_retrieved(
false);
175 if (
false == _local_source_file)
181 server_data_retrieved =
true;
184 #ifdef CHECK_FOR_UPDATES 188 std::ifstream file(_dev_info_url);
191 server_data_retrieved =
true;
192 ver_data << file.rdbuf();
196 LOG_ERROR(
"Cannot open file: " + _dev_info_url);
200 return server_data_retrieved;
208 std::unordered_map<std::string, std::function<bool(const std::string&)>> schema;
209 build_schema(schema);
215 for (
auto &ver : j[
"versions"])
217 std::unordered_map<std::string, std::string> curr_version;
218 for (
auto it = ver.begin();
it != ver.end(); ++
it)
222 auto schema_field(schema.find(element_key));
223 if (schema_field != schema.end()) {
224 if (
it.value().is_string())
233 std::string error_str(
"Server versions file parsing error - validation fail on key: " + element_key +
" value: " +
it.value().get<
std::string>() +
" \n");
235 throw std::runtime_error(error_str);
240 std::string error_str(
"Server versions file parsing error - " + element_key +
" should be represented as a string");
242 throw std::runtime_error(error_str);
247 std::string error_str(
"Server versions file parsing error - " + element_key +
" - unknown field");
249 throw std::runtime_error(error_str);
255 if (std::all_of(schema.cbegin(), schema.cend(), [curr_version](
const std::pair<std::string, std::function<bool(const std::string&)>> &schema_item)
257 return curr_version.end() != std::find_if(curr_version.cbegin(), curr_version.cend(), [schema_item](
const std::pair<std::string, std::string> &ver_item) {
return schema_item.first == ver_item.first; });
260 _server_versions_vec.emplace_back(curr_version);
264 std::string error_str(
"Server versions json file corrupted - not matching schema requirements");
266 throw std::runtime_error(error_str);
272 std::string error_str(
"Server versions json file corrupted - Expect versions field of type array \n)" 273 "Parsed key: " + j.
begin().
key() +
", value type array: " + (j.
begin().
value().is_array() ?
"TRUE" :
"FALSE"));
275 throw std::runtime_error(error_str);
283 if (!_server_versions_loaded)
285 std::stringstream server_versions_data;
287 if (!get_server_data(server_versions_data))
294 parse_versions_data(server_versions_data);
295 _server_versions_loaded =
true;
304 verifier.emplace(
"device_name", [](
const std::string&
val) ->
bool {
return true; });
305 verifier.emplace(
"policy_type", [](
const std::string& val) ->
bool {
return (val ==
"EXPERIMENTAL") || (val ==
"RECOMMENDED") || (val ==
"ESSENTIAL"); });
306 verifier.emplace(
"component", [](
const std::string& val) ->
bool {
return (val ==
"LIBREALSENSE") || (val ==
"VIEWER") || (val ==
"DEPTH_QUALITY_TOOL") || (val ==
"FIRMWARE"); });
308 verifier.emplace(
"platform", [&](
const std::string& val) ->
bool {
return (val ==
"*") || (val ==
"Windows amd64") || (val ==
"Windows x86") || (val ==
"Linux amd64") || (val ==
"Linux arm") || (val ==
"Mac OS"); });
309 verifier.emplace(
"link", [](
const std::string& val) ->
bool {
return true; });
310 verifier.emplace(
"release_notes_link", [](
const std::string& val) ->
bool {
return true; });
311 verifier.emplace(
"description", [](
const std::string& val) ->
bool {
return true; });
318 return (0 == str_from_db.compare(0, str_from_db.find(
'*'), str_compared, 0, str_from_db.find(
'*')));
320 return (str_from_db == str_compared);
object_t::key_type key() const
return the key of an object iterator
constexpr const char * PLATFORM
static basic_json parse(T(&array)[N], const parser_callback_t cb=nullptr)
deserialize from an array
a class to store JSON values
void parse_versions_data(const std::stringstream &ver_data)
GLsizei const GLchar *const * string
void build_schema(std::unordered_map< std::string, std::function< bool(const std::string &)>> &verifier)
bool get_server_data(std::stringstream &ver_data)
std::string to_string(const component_part_type &component)
bool get_version_data_common(const component_part_type component, const version &version, const std::string &req_field, std::string &out)
bool is_device_name_equal(const std::string &str_from_db, const std::string &str_compared, bool allow_wildcard)
query_status_type query_versions(const std::string &device_name, component_part_type component, const update_policy_type policy, version &out_version)
bool download_to_stream(const std::string &url, std::stringstream &output, user_callback_func_type user_callback_func=user_callback_func_type())
reference value() const
return the value of an iterator
basic_json<> json
default JSON class
bool from_string(const std::string &component_str, component_part_type &component_val)
iterator begin() noexcept
returns an iterator to the first element
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)