HepuTarget.h 4.3 KB

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