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-09-28 14:22:04
  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. doc.Parse(json.c_str());
  76. if(!doc.IsObject()){
  77. return false;
  78. }
  79. // get members
  80. const auto end = doc.MemberEnd();
  81. if(end == doc.FindMember("cid") || !doc["cid"].IsString()){
  82. return false;
  83. }
  84. else{
  85. cid = doc["cid"].GetString();
  86. }
  87. if(end == doc.FindMember("cmsg") || !doc["cmsg"].IsString()){
  88. return false;
  89. }
  90. else{
  91. cmsg = doc["cmsg"].GetString();
  92. }
  93. if(end == doc.FindMember("code") || !doc["code"].IsString()){
  94. return false;
  95. }
  96. else{
  97. code = doc["code"].GetString();
  98. }
  99. if(end == doc.FindMember("deviceList") || !doc["deviceList"].IsArray()){
  100. return false;
  101. }
  102. else{
  103. const rapidjson::Value& objs = doc["deviceList"];
  104. for(size_t i = 0; i < objs.Size(); i++){
  105. const rapidjson::Value& obj = objs[i];
  106. PlayerDeviceList playerDevice;
  107. if(!playerDevice.jsonToObject(obj)){
  108. return false;
  109. }
  110. deviceList.push_back(playerDevice);
  111. }
  112. }
  113. return true;
  114. }
  115. };
  116. #endif