HepuTarget.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include <iostream>
  3. #include <rapidjson/document.h>
  4. #include <rapidjson/rapidjson.h>
  5. #include <rapidjson/stringbuffer.h>
  6. #include <rapidjson/writer.h>
  7. using namespace std;
  8. class HepuTarget
  9. {
  10. private:
  11. public:
  12. using Ptr = std::shared_ptr<HepuTarget>;
  13. public:
  14. const string cmd = "ivpTrackingCtrl";
  15. string token;
  16. bool bTracking;
  17. int channelid;
  18. int trackingTime;
  19. struct TrackingRect{
  20. int x;
  21. int y;
  22. int w;
  23. int h;
  24. };
  25. TrackingRect trackingRect;
  26. HepuTarget(){};
  27. ~HepuTarget(){};
  28. void objectToJson(string& str){
  29. rapidjson::StringBuffer strBuf;
  30. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  31. this->objectToJson(writer);
  32. str = strBuf.GetString();
  33. }
  34. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  35. writer.StartObject();
  36. writer.Key("cmd");
  37. writer.String(cmd.c_str());
  38. writer.Key("param");
  39. writer.StartObject();
  40. writer.Key("token");
  41. writer.String(token.c_str());
  42. writer.Key("bTracking");
  43. writer.Bool(bTracking);
  44. writer.Key("channelid");
  45. writer.Int(channelid);
  46. writer.Key("trackingTime");
  47. writer.Int(trackingTime);
  48. writer.Key("trackingRect");
  49. writer.StartObject();
  50. writer.Key("x");
  51. writer.Int(trackingRect.x);
  52. writer.Key("y");
  53. writer.Int(trackingRect.y);
  54. writer.Key("w");
  55. writer.Int(trackingRect.w);
  56. writer.Key("h");
  57. writer.Int(trackingRect.h);
  58. writer.EndObject();
  59. writer.EndObject();
  60. writer.EndObject();
  61. }
  62. bool jsonToObject(std::string& json){
  63. rapidjson::Document doc;
  64. doc.Parse(json.c_str());
  65. if(!doc.IsObject()){
  66. return false;
  67. }
  68. const auto end = doc.MemberEnd();
  69. if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
  70. return false;
  71. }else{
  72. if(cmd != doc["cmd"].GetString())
  73. return false;
  74. }
  75. if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
  76. return false;
  77. }else{
  78. if(end == doc.FindMember("token") || !doc["token"].IsString()) {
  79. return false;
  80. }else{
  81. token = doc["token"].GetString();
  82. }
  83. if(end == doc.FindMember("bTracking") || !doc["bTracking"].IsBool()) {
  84. return false;
  85. }else{
  86. bTracking = doc["bTracking"].GetBool();
  87. }
  88. if(end == doc.FindMember("channelid") || !doc["channelid"].IsInt()) {
  89. return false;
  90. }else{
  91. channelid = doc["channelid"].GetInt();
  92. }
  93. if(end == doc.FindMember("trackingTime") || !doc["trackingTime"].IsInt()) {
  94. return false;
  95. }else{
  96. trackingTime = doc["trackingTime"].GetInt();
  97. }
  98. if(end == doc.FindMember("trackingRect") || !doc["trackingRect"].IsObject()) {
  99. return false;
  100. }else{
  101. if(end == doc.FindMember("x") || !doc["x"].IsInt()) {
  102. return false;
  103. }else{
  104. trackingRect.x = doc["x"].GetInt();
  105. }
  106. if(end == doc.FindMember("y") || !doc["y"].IsInt()) {
  107. return false;
  108. }else{
  109. trackingRect.y = doc["y"].GetInt();
  110. }
  111. if(end == doc.FindMember("w") || !doc["w"].IsInt()) {
  112. return false;
  113. }else{
  114. trackingRect.w = doc["w"].GetInt();
  115. }
  116. if(end == doc.FindMember("h") || !doc["h"].IsInt()) {
  117. return false;
  118. }else{
  119. trackingRect.h = doc["h"].GetInt();
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. };