update_assistant.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * update_assistant.cpp
  3. *
  4. * Created on: 2022年5月5日
  5. * Author: pengzc
  6. */
  7. #include "update_assistant.h"
  8. #include "base/http_client.h"
  9. #include "base/json_object.h"
  10. #include "utils/Log.h"
  11. #include "restclient-cpp/restclient.h"
  12. #include "storage/StoragePreferences.h"
  13. #include "service/BusinessConfig.h"
  14. namespace base {
  15. UpdateAssistant::UpdateAssistant() {
  16. // TODO 自动生成的构造函数存根
  17. }
  18. int UpdateAssistant::GetLatest(const std::string& app_name, VersionInfo* info) {
  19. if (info == NULL) {
  20. return -1;
  21. }
  22. // using namespace base;
  23. // HttpClient client;
  24. // client.SetTimeout(6000);
  25. // client.SetConnectionTimeout(6000);
  26. // client.SetCAInfoFilePath(CONFIGMANAGER->getResFilePath("cacert.pem"));
  27. // HttpRequest req("GET", getHttpGateway() + "/fly/" + app_name, "");
  28. // std::string url = getHttpGateway() + "/deviceBed/getAppVersion?device_type=304&part_id=" + StoragePreferences::getString(STORE_PARTID, "");
  29. // HttpRequest req("POST", url, "");
  30. // HttpResponse response = client.Do(req);
  31. //
  32. // if (response.StatusCode() != 200) {
  33. // return response.ErrorCode();
  34. // }
  35. //
  36. // base::JSONObject json;
  37. // if (!json.ParseObject(response.Body())) {
  38. // LOGD("Invalid response %s", response.Body().c_str());
  39. // return -1;
  40. // }
  41. // info->version = json.GetString("version_code", "");
  42. // info->versionNo = json.GetInt("version_no",1);
  43. // info->url = json.GetString("app_path", "");
  44. // info->file_size = json.GetInt("fileSize", 0);
  45. std::string url = getHttpGateway() + "/deviceBed/getAppVersion?device_type=304&part_id=" + StoragePreferences::getString(STORE_PARTID, "");
  46. std::string content_type = std::string("application/json");
  47. LOGD("请求版本信息. url = %s", url.c_str());
  48. //发起HTTP POST请求
  49. RestClient::Response r = RestClient::post(url, content_type, "");
  50. if (r.code != 200) {
  51. LOGD("请求版本信息-> 错误代码: %d", r.code);
  52. return r.code;
  53. }
  54. LOGD("获得版本信息. result = %s", r.body.c_str());
  55. //解析json
  56. Json::Reader reader;
  57. Json::Value root;
  58. if(reader.parse(r.body, root, false)) {
  59. info->version = root["version_code"].asString();
  60. info->versionNo = root["version_no"].asInt();
  61. info->url = root["app_path"].asString();
  62. info->file_size = root["fileSize"].asInt();
  63. LOGD("versionNo: %d", info->versionNo);
  64. }
  65. return 0;
  66. }
  67. UpdateAssistant::~UpdateAssistant() {
  68. // TODO 自动生成的析构函数存根
  69. }
  70. } /* namespace base */