RecDeviceCommand.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. doc.Parse(json.c_str());
  45. if(!doc.IsObject()){
  46. return false;
  47. }
  48. // get members
  49. const auto end = doc.MemberEnd();
  50. // json_type
  51. if (end == doc.FindMember("deviceId")) {
  52. return false;
  53. } else {
  54. if(doc["deviceId"].IsInt())
  55. deviceId = doc["deviceId"].GetInt();
  56. else if(doc["deviceId"].IsString())
  57. deviceId = std::stoi(doc["deviceId"].GetString());
  58. }
  59. if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
  60. return false;
  61. }else{
  62. commandCode = doc["commandCode"].GetString();
  63. }
  64. return true;
  65. }
  66. bool jsonToObject(const rapidjson::Value& object){
  67. const auto end = object.MemberEnd();
  68. if(end == object.FindMember("deviceId")){
  69. return false;
  70. }
  71. else{
  72. if(object["deviceId"].IsInt()) deviceId = object["deviceId"].GetInt();
  73. else if(object["deviceId"].IsString()) deviceId = std::atoi(object["deviceId"].GetString());
  74. }
  75. if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
  76. return false;
  77. }
  78. else{
  79. commandCode = object["commandCode"].GetString();
  80. }
  81. return true;
  82. }
  83. };