RecDeviceCommand.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4. class RecDeviceCommand
  5. {
  6. private:
  7. /* data */
  8. int deviceId;
  9. /**
  10. * 操作驱鸟设备的指令类型
  11. */
  12. std::string commandCode;
  13. public:
  14. RecDeviceCommand(){};
  15. ~RecDeviceCommand(){};
  16. void setDeviceId(int DeviceId){
  17. deviceId = DeviceId;
  18. }
  19. void setCommandCode(std::string CommandCode) {
  20. commandCode = CommandCode;
  21. }
  22. int getDeviceId(){
  23. return deviceId;
  24. }
  25. std::string getCommandCode(){
  26. return commandCode;
  27. }
  28. void objectToJson(std::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("deviceId");
  37. writer.Int(deviceId);
  38. writer.Key("deviceId");
  39. writer.String(commandCode.c_str());
  40. writer.EndObject();
  41. }
  42. bool jsonToObject(const std::string& json){
  43. rapidjson::Document doc;
  44. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  45. return false;
  46. }
  47. // get members
  48. const auto end = doc.MemberEnd();
  49. // json_type
  50. if (end == doc.FindMember("deviceId")) {
  51. return false;
  52. } else {
  53. if(doc["deviceId"].IsInt())
  54. deviceId = doc["deviceId"].GetInt();
  55. else if(doc["deviceId"].IsString())
  56. deviceId = std::stoi(doc["deviceId"].GetString());
  57. }
  58. if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
  59. return false;
  60. }else{
  61. commandCode = doc["commandCode"].GetString();
  62. }
  63. return true;
  64. }
  65. bool jsonToObject(const rapidjson::Value& object){
  66. const auto end = object.MemberEnd();
  67. if(end == object.FindMember("deviceId")){
  68. return false;
  69. }
  70. else{
  71. if(object["deviceId"].IsInt()) deviceId = object["deviceId"].GetInt();
  72. else if(object["deviceId"].IsString()) deviceId = std::atoi(object["deviceId"].GetString());
  73. }
  74. if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
  75. return false;
  76. }
  77. else{
  78. commandCode = object["commandCode"].GetString();
  79. }
  80. return true;
  81. }
  82. };