RecDeviceCommand.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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") || !doc["deviceId"].IsInt()) {
  51. return false;
  52. } else {
  53. deviceId = doc["deviceId"].GetInt();
  54. }
  55. if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
  56. return false;
  57. }else{
  58. commandCode = doc["commandCode"].GetString();
  59. }
  60. return true;
  61. }
  62. bool jsonToObject(const rapidjson::Value& object){
  63. const auto end = object.MemberEnd();
  64. if(end == object.FindMember("deviceId") || !object["deviceId"].IsInt()){
  65. return false;
  66. }
  67. else{
  68. deviceId = object["deviceId"].GetInt();
  69. }
  70. if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
  71. return false;
  72. }
  73. else{
  74. commandCode = object["commandCode"].GetString();
  75. }
  76. return true;
  77. }
  78. };