SendDevice.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 SendDevice
  9. {
  10. private:
  11. public:
  12. /* data */
  13. SendDevice(){};
  14. ~SendDevice(){};
  15. int deviceId;
  16. string commandCode;
  17. int status;
  18. int workStatus;
  19. int powerStatus;
  20. int batteryStatus;
  21. int num;
  22. int oxyPressure;
  23. int gasPressure;
  24. string remark;
  25. string attr1;
  26. string attr2;
  27. string attr3;
  28. string msgTime;
  29. void objectToJson(std::string& str){
  30. rapidjson::StringBuffer strBuf;
  31. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  32. this->objectToJson(writer);
  33. str = strBuf.GetString();
  34. }
  35. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  36. writer.StartObject();
  37. writer.Key("deviceId");
  38. writer.Int(deviceId);
  39. writer.Key("commandCode");
  40. writer.String(commandCode.c_str());
  41. writer.Key("status");
  42. writer.Int(status);
  43. writer.Key("workStatus");
  44. writer.Int(workStatus);
  45. writer.Key("powerStatus");
  46. writer.Int(powerStatus);
  47. writer.Key("batteryStatus");
  48. writer.Int(batteryStatus);
  49. writer.Key("num");
  50. writer.Int(num);
  51. writer.Key("oxyPressure");
  52. writer.Int(oxyPressure);
  53. writer.Key("gasPressure");
  54. writer.Int(gasPressure);
  55. writer.Key("gasPressure");
  56. writer.Int(gasPressure);
  57. writer.Key("remark");
  58. writer.String(remark.c_str());
  59. writer.Key("attr1");
  60. writer.String(attr1.c_str());
  61. writer.Key("attr2");
  62. writer.String(attr2.c_str());
  63. writer.Key("attr3");
  64. writer.String(attr3.c_str());
  65. writer.Key("msgTime");
  66. writer.String(msgTime.c_str());
  67. writer.EndObject();
  68. }
  69. bool jsonToObject(const std::string& json){
  70. rapidjson::Document doc;
  71. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  72. return false;
  73. }
  74. // get members
  75. const auto end = doc.MemberEnd();
  76. // json_type
  77. if(end == doc.FindMember("deviceId") || !doc["deviceId"].IsInt()) {
  78. return false;
  79. }else{
  80. deviceId = doc["deviceId"].GetInt();
  81. }
  82. if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
  83. return false;
  84. }else{
  85. commandCode = doc["commandCode"].GetString();
  86. }
  87. if(end == doc.FindMember("status") || !doc["status"].IsInt()) {
  88. return false;
  89. }else{
  90. status = doc["status"].GetInt();
  91. }
  92. if(end == doc.FindMember("workStatus") || !doc["workStatus"].IsInt()) {
  93. return false;
  94. }else{
  95. workStatus = doc["workStatus"].GetInt();
  96. }
  97. if(end == doc.FindMember("powerStatus") || !doc["powerStatus"].IsInt()) {
  98. return false;
  99. }else{
  100. powerStatus = doc["powerStatus"].GetInt();
  101. }
  102. if(end == doc.FindMember("batteryStatus") || !doc["batteryStatus"].IsInt()) {
  103. return false;
  104. }else{
  105. batteryStatus = doc["batteryStatus"].GetInt();
  106. }
  107. if(end == doc.FindMember("num") || !doc["num"].IsInt()) {
  108. return false;
  109. }else{
  110. num = doc["num"].GetInt();
  111. }
  112. if(end == doc.FindMember("oxyPressure") || !doc["oxyPressure"].IsInt()) {
  113. return false;
  114. }else{
  115. oxyPressure = doc["oxyPressure"].GetInt();
  116. }
  117. if(end == doc.FindMember("gasPressure") || !doc["gasPressure"].IsInt()) {
  118. return false;
  119. }else{
  120. gasPressure = doc["gasPressure"].GetInt();
  121. }
  122. if(end == doc.FindMember("remark") || !doc["remark"].IsString()) {
  123. return false;
  124. }else{
  125. remark = doc["remark"].GetString();
  126. }
  127. if(end == doc.FindMember("attr1") || !doc["attr1"].IsString()) {
  128. return false;
  129. }else{
  130. attr1 = doc["attr1"].GetString();
  131. }
  132. if(end == doc.FindMember("attr2") || !doc["attr2"].IsString()) {
  133. return false;
  134. }else{
  135. attr2 = doc["attr2"].GetString();
  136. }
  137. if(end == doc.FindMember("attr3") || !doc["attr3"].IsString()) {
  138. return false;
  139. }else{
  140. attr3 = doc["attr3"].GetString();
  141. }
  142. if(end == doc.FindMember("msgTime") || !doc["msgTime"].IsString()) {
  143. return false;
  144. }else{
  145. msgTime = doc["msgTime"].GetString();
  146. }
  147. return true;
  148. }
  149. };