SendDevice.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. doc.Parse(json.c_str());
  72. if(!doc.IsObject()){
  73. return false;
  74. }
  75. // get members
  76. const auto end = doc.MemberEnd();
  77. // json_type
  78. if(end == doc.FindMember("deviceId") || !doc["deviceId"].IsInt()) {
  79. return false;
  80. }else{
  81. deviceId = doc["deviceId"].GetInt();
  82. }
  83. if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
  84. return false;
  85. }else{
  86. commandCode = doc["commandCode"].GetString();
  87. }
  88. if(end == doc.FindMember("status") || !doc["status"].IsInt()) {
  89. return false;
  90. }else{
  91. status = doc["status"].GetInt();
  92. }
  93. if(end == doc.FindMember("workStatus") || !doc["workStatus"].IsInt()) {
  94. return false;
  95. }else{
  96. workStatus = doc["workStatus"].GetInt();
  97. }
  98. if(end == doc.FindMember("powerStatus") || !doc["powerStatus"].IsInt()) {
  99. return false;
  100. }else{
  101. powerStatus = doc["powerStatus"].GetInt();
  102. }
  103. if(end == doc.FindMember("batteryStatus") || !doc["batteryStatus"].IsInt()) {
  104. return false;
  105. }else{
  106. batteryStatus = doc["batteryStatus"].GetInt();
  107. }
  108. if(end == doc.FindMember("num") || !doc["num"].IsInt()) {
  109. return false;
  110. }else{
  111. num = doc["num"].GetInt();
  112. }
  113. if(end == doc.FindMember("oxyPressure") || !doc["oxyPressure"].IsInt()) {
  114. return false;
  115. }else{
  116. oxyPressure = doc["oxyPressure"].GetInt();
  117. }
  118. if(end == doc.FindMember("gasPressure") || !doc["gasPressure"].IsInt()) {
  119. return false;
  120. }else{
  121. gasPressure = doc["gasPressure"].GetInt();
  122. }
  123. if(end == doc.FindMember("remark") || !doc["remark"].IsString()) {
  124. return false;
  125. }else{
  126. remark = doc["remark"].GetString();
  127. }
  128. if(end == doc.FindMember("attr1") || !doc["attr1"].IsString()) {
  129. return false;
  130. }else{
  131. attr1 = doc["attr1"].GetString();
  132. }
  133. if(end == doc.FindMember("attr2") || !doc["attr2"].IsString()) {
  134. return false;
  135. }else{
  136. attr2 = doc["attr2"].GetString();
  137. }
  138. if(end == doc.FindMember("attr3") || !doc["attr3"].IsString()) {
  139. return false;
  140. }else{
  141. attr3 = doc["attr3"].GetString();
  142. }
  143. if(end == doc.FindMember("msgTime") || !doc["msgTime"].IsString()) {
  144. return false;
  145. }else{
  146. msgTime = doc["msgTime"].GetString();
  147. }
  148. return true;
  149. }
  150. };