PlayerStatus.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-02-15 15:26:55
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-02-16 10:22:27
  8. */
  9. #ifndef __PALYERSTATUSE_H_
  10. #define __PALYERSTATUSE_H_
  11. #include <iostream>
  12. #include <rapidjson/document.h>
  13. #include <rapidjson/rapidjson.h>
  14. #include <rapidjson/stringbuffer.h>
  15. #include <rapidjson/writer.h>
  16. #include <vector>
  17. #include "PlayerDeviceList.h"
  18. class PlayerInquire
  19. {
  20. private:
  21. std::string URL = "/qxsbr360xs.do";
  22. public:
  23. std::string cid;
  24. std::string ctype;
  25. std::string cname;
  26. std::string dtype;
  27. std::string did;
  28. public:
  29. PlayerInquire(){};
  30. ~PlayerInquire(){};
  31. void setURL(std::string url){
  32. this->URL = url;
  33. }
  34. std::string getUrl(){
  35. return this->URL;
  36. }
  37. void objectToJson(std::string& str){
  38. rapidjson::StringBuffer strBuf;
  39. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  40. this->objectToJson(writer);
  41. str = strBuf.GetString();
  42. }
  43. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  44. writer.StartObject();
  45. writer.Key("cid");
  46. writer.String(cid.c_str());
  47. writer.Key("ctype");
  48. writer.String(ctype.c_str());
  49. writer.Key("cname");
  50. writer.String(cname.c_str());
  51. writer.Key("dtype");
  52. writer.String(dtype.c_str());
  53. writer.Key("did");
  54. writer.String(did.c_str());
  55. writer.EndObject();
  56. }
  57. };
  58. class PlayerStatus
  59. {
  60. private:
  61. std::string cid;
  62. std::string cmsg;
  63. std::string code;
  64. vector<PlayerDeviceList> deviceList;
  65. public:
  66. PlayerStatus() {};
  67. ~PlayerStatus() {};
  68. /**
  69. * @description: json转换为对象
  70. * @param {string&} json
  71. * @return {*}
  72. */
  73. bool jsonToObject(const std::string& json){
  74. rapidjson::Document doc;
  75. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  76. return false;
  77. }
  78. // get members
  79. const auto end = doc.MemberEnd();
  80. if(end == doc.FindMember("cid") || !doc["cid"].IsString()){
  81. return false;
  82. }
  83. else{
  84. cid = doc["cid"].GetString();
  85. }
  86. if(end == doc.FindMember("cmsg") || !doc["cmsg"].IsString()){
  87. return false;
  88. }
  89. else{
  90. cmsg = doc["cmsg"].GetString();
  91. }
  92. if(end == doc.FindMember("code") || !doc["code"].IsString()){
  93. return false;
  94. }
  95. else{
  96. code = doc["code"].GetString();
  97. }
  98. if(end == doc.FindMember("deviceList") || !doc["deviceList"].IsArray()){
  99. return false;
  100. }
  101. else{
  102. const rapidjson::Value& objs = doc["deviceList"];
  103. for(size_t i = 0; i < objs.Size(); i++){
  104. const rapidjson::Value& obj = objs[i];
  105. PlayerDeviceList playerDevice;
  106. if(!playerDevice.jsonToObject(obj)){
  107. return false;
  108. }
  109. deviceList.push_back(playerDevice);
  110. }
  111. }
  112. return true;
  113. }
  114. };
  115. #endif