123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #pragma once
- #include <iostream>
- #include <rapidjson/document.h>
- #include <rapidjson/rapidjson.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/writer.h>
- using namespace std;
- class HepuTarget
- {
- private:
- public:
- const string cmd = "ivpTrackingCtrl";
- string token;
- bool bTracking;
- int channelid;
- int trackingTime;
- struct TrackingRect{
- int x;
- int y;
- int w;
- int h;
- };
- TrackingRect trackingRect;
-
- HepuTarget(){};
- ~HepuTarget(){};
- void objectToJson(string& str){
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
- this->objectToJson(writer);
- str = strBuf.GetString();
- }
- void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
- writer.StartObject();
- writer.Key("cmd");
- writer.String(cmd.c_str());
- writer.Key("param");
- writer.StartObject();
- writer.Key("token");
- writer.String(token.c_str());
- writer.Key("bTracking");
- writer.Bool(bTracking);
- writer.Key("channelid");
- writer.Int(channelid);
- writer.Key("trackingTime");
- writer.Int(trackingTime);
- writer.Key("trackingRect");
-
- writer.StartObject();
- writer.Key("x");
- writer.Int(trackingRect.x);
- writer.Key("y");
- writer.Int(trackingRect.y);
- writer.Key("w");
- writer.Int(trackingRect.w);
- writer.Key("h");
- writer.Int(trackingRect.h);
- writer.EndObject();
- writer.EndObject();
- writer.EndObject();
- }
- bool jsonToObject(std::string& json){
- rapidjson::Document doc;
- doc.Parse(json.c_str());
- if(!doc.IsObject()){
- return false;
- }
- const auto end = doc.MemberEnd();
- if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
- return false;
- }else{
- if(cmd != doc["cmd"].GetString())
- return false;
- }
- if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
- return false;
- }else{
- if(end == doc.FindMember("token") || !doc["token"].IsString()) {
- return false;
- }else{
- token = doc["token"].GetString();
- }
- if(end == doc.FindMember("bTracking") || !doc["bTracking"].IsBool()) {
- return false;
- }else{
- bTracking = doc["bTracking"].GetBool();
- }
- if(end == doc.FindMember("channelid") || !doc["channelid"].IsInt()) {
- return false;
- }else{
- channelid = doc["channelid"].GetInt();
- }
- if(end == doc.FindMember("trackingTime") || !doc["trackingTime"].IsInt()) {
- return false;
- }else{
- trackingTime = doc["trackingTime"].GetInt();
- }
- if(end == doc.FindMember("trackingRect") || !doc["trackingRect"].IsObject()) {
- return false;
- }else{
- if(end == doc.FindMember("x") || !doc["x"].IsInt()) {
- return false;
- }else{
- trackingRect.x = doc["x"].GetInt();
- }
- if(end == doc.FindMember("y") || !doc["y"].IsInt()) {
- return false;
- }else{
- trackingRect.y = doc["y"].GetInt();
- }
- if(end == doc.FindMember("w") || !doc["w"].IsInt()) {
- return false;
- }else{
- trackingRect.w = doc["w"].GetInt();
- }
- if(end == doc.FindMember("h") || !doc["h"].IsInt()) {
- return false;
- }else{
- trackingRect.h = doc["h"].GetInt();
- }
- }
- }
- return true;
- }
- };
|